You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Polygon Mumbai测试网USDC合约permit签名无效问题排查

解决Polygon Mumbai测试网USDC Permit签名无效问题

你的问题核心是EIP-712 Domain参数与合约实际配置不匹配,同时硬编码nonce值也可能导致校验失败,最终生成的签名无法通过合约验证。以下是具体排查和修复步骤:

1. 确认合约的EIP-712关键参数

你需要先获取目标USDC合约的真实name和version参数,这两个值是生成正确DOMAIN_SEPARATOR的核心:

  • 调用合约name()方法,返回值为USD Coin(而非你代码中的USD Coin (PoS))
  • 调用合约version()方法,返回值为2(而非你代码中的1)

2. 修正签名生成代码

将Domain参数替换为正确值,同时使用合约返回的真实nonce值(每个地址的permit nonce是递增的,硬编码0会直接导致校验失败)。修正后的代码如下:

import { ThirdwebSDK } from "@thirdweb-dev/sdk"
import { ethers } from "ethers"

const sdk = ThirdwebSDK.fromPrivateKey('你的私钥', "mumbai")
const usdcContractAddress = "0x0FA8781a83E46826621b3BC094Ea2A0212e71B23"

// 获取owner的实际permit nonce
const usdcContract = await sdk.getContract(usdcContractAddress)
const ownerNonce = await usdcContract.call("nonces", ["你的owner地址"])

const message = {
  owner: '你的owner地址',
  spender: '授权的spender地址',
  value: ethers.constants.MaxUint256,
  nonce: ownerNonce, // 使用合约返回的真实nonce
  deadline: ethers.constants.MaxUint256
}

const {signature} = await sdk?.wallet.signTypedData(
        {
          name: "USD Coin", // 修正为合约实际名称
          version: "2", // 修正为合约实际版本
          chainId: 80001,
          verifyingContract: usdcContractAddress
        },
        {
          Permit: [
            { name: "owner", type: "address" },
            { name: "spender", type: "address" },
            { name: "value", type: "uint256" },
            { name: "nonce", type: "uint256" },
            { name: "deadline", type: "uint256" },
          ],
        },
        message
      )
const split = ethers.utils.splitSignature(signature)
console.log(split)

3. 额外校验步骤

如果修正后仍有问题,可以手动对比DOMAIN_SEPARATOR确认参数正确性:

// 计算本地生成的domain separator
const localDomainSeparator = ethers.utils._TypedDataEncoder.hashDomain({
  name: "USD Coin",
  version: "2",
  chainId: 80001,
  verifyingContract: usdcContractAddress
})

// 获取合约返回的domain separator
const contractDomainSeparator = await usdcContract.call("DOMAIN_SEPARATOR")

// 两者必须完全一致
console.log(localDomainSeparator === contractDomainSeparator) // 应为true

内容的提问来源于stack exchange,提问作者larry rage

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 20:47:37