美文网首页
链下获取USDC中的Permit签名

链下获取USDC中的Permit签名

作者: 张亚伦 | 来源:发表于2023-05-18 16:44 被阅读0次

    文本用于示例,如果线下计算USDC中的Permit签名

    主要分为两步:首先,计算出permit摘要;然后,对摘要进行签名。
    代码如下:

    const { keccak256, defaultAbiCoder, toUtf8Bytes, solidityPack } = require("ethers/lib/utils");
    const { ethers } = require("ethers");
    
    // 注意参数
    var DOMAIN_SEPARATOR = "0xd8bcfd71283db806c953550a7728f6d9c566844f11de95a94f898d43860a4321";
    var PERMIT_TYPEHASH = "0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9";
    
    function getPermitDigest(
        owner,
        spender,
        value,
        nonce,
        deadline
    ) {
        return keccak256(
            solidityPack(
                ['bytes1', 'bytes1', 'bytes32', 'bytes32'],
                [
                    '0x19',
                    '0x01',
                    DOMAIN_SEPARATOR,
                    keccak256(
                        defaultAbiCoder.encode(
                            ['bytes32', 'address', 'address', 'uint256', 'uint256', 'uint256'],
                            [PERMIT_TYPEHASH, owner, spender, value, nonce, deadline]
                        )
                    ),
                ]
            )
        )
    }
    
    
    async function getPermitSign() {
        var usdc_owner_pk = "0x8d2e94f6b15ef64577e5adc58c6723ca50efcebdf9ae3373147c1c";
        var usdc_owner_addr = "0x349c666566fcD9eB2f1BCc68823b919641254d4e";
    
        // permit 参数
        var owner = usdc_owner_addr;
        var spender = "0xBC1F42383f9567B43C219200D83C71CF144C1146";
        var value = 90;
        var nonce = 0;
        var deadline = 1709360911; //2024-03-02 14:28:31
    
        // 1. 计算摘要
        var digest = getPermitDigest(owner, spender, value, nonce, deadline);
        console.log("digest:", digest);
    
        // 2. 签名
        var signer = new ethers.utils.SigningKey(usdc_owner_pk)
        var signature = signer.signDigest(digest);
        console.log("signature: ", signature);
    
    }
    
    if (require.main === module) {
        getPermitSign();
    }
    
    
    

    输出结果如下:

    digest: 0x67a9ee0d3bbe100aaba3e82a7e496f030cf754f5640ed0003c0d57613425b6bd
    signature:  {
      r: '0x8edd1b531b8f639f37eee808b866bc534035524e17baccfb9b85a8f683412baf',
      s: '0x743066a136fa8fb64cdfa249f821269a634b0f2e01ef38536b8fee39742c0157',
      _vs: '0x743066a136fa8fb64cdfa249f821269a634b0f2e01ef38536b8fee39742c0157',
      recoveryParam: 0,
      v: 27,
      yParityAndS: '0x743066a136fa8fb64cdfa249f821269a634b0f2e01ef38536b8fee39742c0157',
      compact: '0x8edd1b531b8f639f37eee808b866bc534035524e17baccfb9b85a8f683412baf743066a136fa8fb64cdfa249f821269a634b0f2e01ef38536b8fee39742c0157'
    }
    

    相关文章

      网友评论

          本文标题:链下获取USDC中的Permit签名

          本文链接:https://www.haomeiwen.com/subject/cozwsdtx.html