我正在研究USDTexecuteMetaTransaction,以便用户可以执行无gas交易。
但是我收到签名者和签名不匹配错误。以下是我签署交易的方式:
const userWallet = new ethers.Wallet(PRIVATE_KEY, provider);
const nonce = await usdtContract.getNonce(userWallet.address);
const domain = {
name: '(PoS) Tether USD',
version: '1',
chainId: 137, // Polygon chain ID
verifyingContract: "0xc2132D05D31c914a87C6611C10748AEb04B58e8F" };
const types = {
MetaTransaction: [
{ name: "nonce", type: "uint256" },
{ name: "from", type: "address" },
{ name: "functionSignature", type: "bytes" }
] };
const message = {
nonce: nonce,
from: userWallet.address,
functionSignature: functionSignature };
const signature = await userWallet.signTypedData(domain, types, message);
const { r, s, v } = ethers.Signature.from(signature);
对于functionSignature我正在编码USDT的传输函数:
const recipientTransfer = usdtContract.interface.encodeFunctionData("transfer", [recipientAddress, amountAfterFee]);
知道有什么问题吗?
我建议使用 eth-sig-util 进行用户签名,并在域类型中使用 salt 。
const domainType = [
{ name: "name", type: "string" },
{ name: "version", type: "string" },
{ name: "verifyingContract", type: "address" },
{ name: "salt", type: "bytes32" }
];
const metaTransactionType = [
{ name: "nonce", type: "uint256" },
{ name: "from", type: "address" },
{ name: "functionSignature", type: "bytes" }
];
const domainData = {
name: "(PoS) Tether USD",
version: "1",
verifyingContract: "0xc2132D05D31c914a87C6611C10748AEb04B58e8F",
salt: "0x0000000000000000000000000000000000000000000000000000000000000089",
};
const transferFunctionAbi = ["function transfer(address to, uint amount)"];
const interfaceTransferFunction = new ethers.utils.Interface(transferFunctionAbi);
const functionSignature = interfaceTransferFunction.encodeFunctionData("transfer", [ to, amountUSDT ]);
const msgParams = {
types: {
EIP712Domain: domainType,
MetaTransaction: metaTransactionType
},
domain: domainData,
primaryType: "MetaTransaction",
message: {
nonce: nonce.toNumber(),
from: from,
functionSignature: functionSignature
}
};
const privateKey = Buffer.from(privateKeyUser, "hex")
const sig = sigUtil.signTypedData_v4(
privateKey,
{data: msgParams}
);