请问ERC20 permit方法的v, r, s参数是怎么获取的,比如说我要调用permit方法,我要怎么生成这三个参数

function permit(
    address owner,
    address spender,
    uint256 value,
    uint256 deadline,
    uint8 v,
    bytes32 r,
    bytes32 s
)
请先 登录 后评论

最佳答案 2021-11-23 10:29

先按照 EIP712 拼接数据,可以参考这个代码:

export function signPremit(tokenName, verifyAddr, owner, spender, value, deadline, chainid, nonce) {
      let types = {
        EIP712Domain: [{
            name: 'name',
            type: 'string'
          },
          {
            name: 'version',
            type: 'string'
          },
          {
            name: 'chainId',
            type: 'uint256'
          },
          {
            name: 'verifyingContract',
            type: 'address'
          },
        ],
        Permit: [{
            name: 'owner',
            type: 'address'
          },
          {
            name: 'spender',
            type: 'address'
          },
          {
            name: 'value',
            type: 'uint256'
          },
          {
            name: 'nonce',
            type: 'uint256'
          },
          {
            name: 'deadline',
            type: 'uint256'
          }
        ]
      };

      let primaryType = 'Permit';
      let domain = {
        name: tokenName,
        version: '1',
        chainId: chainid,
        verifyingContract: verifyAddr
      };
      let message = {
        owner,
        spender,
        value,
        nonce,
        deadline
      };

      return JSON.stringify({
        types,
        primaryType,
        domain,
        message
      });
    }

然后把拼接数据转给 metamask 的 sign-typed-data-v4 进行签名,可参考文档:

https://docs.metamask.io/guide/signing-data.html#sign-typed-data-v4

请先 登录 后评论

其它 0 个回答

  • 1 关注
  • 1 收藏,3195 浏览
  • PIGhaha 提出于 2021-11-22 23:54