我们正在尝试在我们的应用程序中使用 EIP-3009。第一步我们跟随#3010中的web3部分以下是我们代码的选定部分:
const nonce = web3.utils.randomHex(32);
const data = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
TransferWithAuthorization: [
{ name: "from", type: "address" },
{ name: "to", type: "address" },
{ name: "value", type: "uint256" },
{ name: "validAfter", type: "uint256" },
{ name: "validBefore", type: "uint256" },
{ name: "nonce", type: "bytes32" },
],
},
domain: {
name: "Coin Test",
version: "1",
chainId: "3",
verifyingContract: "0xd73ce105814e.......76",
},const nonce = web3.utils.randomHex(32);
const data = {
types: {
EIP712Domain: [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
TransferWithAuthorization: [
{ name: "from", type: "address" },
{ name: "to", type: "address" },
{ name: "value", type: "uint256" },
{ name: "validAfter", type: "uint256" },
{ name: "validBefore", type: "uint256" },
{ name: "nonce", type: "bytes32" },
],
},
domain: {
name: "Things Coin Test",
version: "1",
chainId: "3",
verifyingContract: "0xd73ce105.....Be76",
},
primaryType: "transferWithAuthorization",
message: {
from: this.state.senderAddress,
to: this.state.receiverAddress,
value: 100,
validAfter: 0,
validBefore: Math.floor(Date.now() / 1000) + 3600, // Valid for an hour
nonce: nonce,
},
};
const signedMessage = web3.eth.accounts.sign(data, this.state.privateKey)
primaryType: "transferWithAuthorization",
message: {
from: this.state.senderAddress,
to: this.state.receiverAddress,
value: 100,
validAfter: 0,
validBefore: Math.floor(Date.now() / 1000) + 3600, // Valid for an hour
nonce: nonce,
},
};
const signedMessage = web3.eth.accounts.sign(data, this.state.privateKey)
之后,我们使用交易对象并对其进行签名以发送到 Infura。walletAddress 是用以太币支付交易费用的第三人的地址。senderAddress 是发送 erc20 但没有任何以太币来支付费用的人的地址。
const rawTransaction = ({
from: walletAddress,
to: contractAddress,
value: web3.utils.toHex('0'),
gasprice: web3.utils.toHex('1000'),
gas: web3.utils.toHex("100000"),
data: TNGContract.methods.transferWithAuthorization(
this.state.senderAddress,
this.state.receiverAddress,
100,
0,
MAX_UINT256,
nonce,
signedMessage.v,
signedMessage.r,
signedMessage.s,
).encodeABI(),
});
console.log('-----------rawTransaction: ' + JSON.stringify(rawTransaction))
web3.eth.accounts.signTransaction(rawTransaction, privateKey)
.then((signedTransaction) => {
console.log('.-----signedTransaction: ' + JSON.stringify(signedTransaction))
web3.eth.sendSignedTransaction(signedTransaction.rawTransaction).then((receipt)=>{
console.log('.-----receipt: ' + JSON.stringify(receipt))
}).catch(err => {console.log("the error is----- " + err)})
}).catch((e) => console.log("the error is: " + e));
问题是我们每次发送交易时都会收到“失败并出现错误'EIP3009:无效签名'”。任何帮助都会得到帮助。