Wednesday, 19 April 2023

Invalid signature when siging EIP-712 data

I am trying call transferWithAuthorization on the USDC contract (which implements EIP-3009). To do this I need to sign the transaction data and then execute the transaction, however I am getting the error: Fail with error 'FiatTokenV2: invalid signature'

Example transaction error: https://goerli.etherscan.io/tx/0x3d2cee8414682bcf55bc8adf86c2deaf14eb77448824c417629cca0d3f9bea94

Any ideas why?

const { ethers } = require('ethers');
const abi = require('./abi');

async function main() {
  /* Generate Signature */
  const sendingWallet = ethers.Wallet.createRandom();
  const sendingAddress = await sendingWallet.getAddress();

  const types = {
    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' },
    ],
  };

  const domain = {
    name: 'testapp',
    version: '1',
    chainId: '5',
    verifyingContract: '0x07865c6E87B9F70255377e024ace6630C1Eaa37F', // USDC Contract
  };

  const message = {
    from: sendingAddress,
    to: '0x1bC152F3E47CC7baDF5629bc77CBEf9DaE813843', // Receiver wallet
    value: 100000,
    validAfter: 0,
    validBefore: Math.floor(Date.now() / 1000) + 3600, // Valid for an hour
    nonce: ethers.utils.hexValue(ethers.utils.randomBytes(32)), // 32 byte hex string
  };

  const signature = await sendingWallet._signTypedData(domain, types, message);

  console.log(signature);

  const { v, r, s } = ethers.utils.splitSignature(signature);

  console.log(v, r, s);


  /* Execute transaction */
  // Throw away wallet :)
  const wallet = ethers.Wallet.fromMnemonic(
    'youth tool amount venue exact birth opinion derive lend charge roof food',
  );

  const connection = new ethers.providers.InfuraProvider(
    'goerli', // or 'ropsten', 'rinkeby', 'kovan', 'goerli'
    'x', // Infura API Key
  );

  const signer = wallet.connect(connection);

  const contract = new ethers.Contract(
    '0x07865c6E87B9F70255377e024ace6630C1Eaa37F',
    abi,
    signer,
  );

  const res = await contract.transferWithAuthorization(
    message.from,
    message.to,
    message.value,
    message.validAfter,
    message.validBefore,
    message.nonce,
    v,
    r,
    s,
    {
      gasLimit: 1000000,
    },
  );

  console.log(res);

  const receipt = await res.wait();

  console.log(receipt);
}

main();



from Invalid signature when siging EIP-712 data

No comments:

Post a Comment