尝试验证签名时来自 ethers 的无效签名错误

Invalid signature error from ethers when trying to verify a signature

我正在开发一个需要验证用户签名的dapp。使用 Metamask 测试时,一切正常。

当尝试验证来自 Ambire 钱包的签名时,我从 ethers.utils.verifyMessage 收到“错误:无效的签名字符串” 例如,这是我从 Ambire 那里得到的签名: 0xf1b5f561c1914d513f4020edd397f729836e26eeae8f1b9e4070d134492aa38b0a84b9d7cefe06f180ade91595fd74c4e845c2b8c2d053fa57785fdf5ec5dca31c01

当我尝试验证时发生了这种情况

require('ethers').utils.verifyMessage('test', '0xf1b5f561c1914d513f4020edd397f729836e26eeae8f1b9e4070d134492aa38b0a84b9d7cefe06f180ade91595fd74c4e845c2b8c2d053fa57785fdf5ec5dca31c01')
Uncaught:
Error: invalid signature string (argument="signature", value="0xf1b5f561c1914d513f4020edd397f729836e26eeae8f1b9e4070d134492aa38b0a84b9d7cefe06f180ade91595fd74c4e845c2b8c2d053fa57785fdf5ec5dca31c01", code=INVALID_ARGUMENT, version=bytes/5.6.1)

知道这是否可以解决吗?

为了支持 Ambire、Gnosis Safe、Argent 等智能钱包,您需要 implement EIP 1271

A​​mbire通用签名验证,支持:

  • 标准消息验证(eth_sign)
  • 712 类型化数据验证 (eth_signTypedData_v*)
  • 1271 智能合约on-chain验证(isValidSignature) 可选的智能合约签名 off-chain 验证(例如,如果智能钱包是反事实的且尚未部署)

使用@ambire/signature-validator

的简单eth_sign验证
const ethers = require('ethers')
const { verifyMessage } = require('@ambire/signature-validator')

const provider = new ethers.providers.JsonRpcProvider('https://polygon-rpc.com')

async function run() {
    // Replace `ethers.verifyMessage(message, signature) === signer` with this:
    const isValidSig = await verifyMessage({
        signer: '0xaC39b311DCEb2A4b2f5d8461c1cdaF756F4F7Ae9',
        message: 'My funds are SAFU with Ambire Wallet',
        signature: '0x9863d84f3119ac01d9e3bf9294e6c0c3572a07780fc7c49e8dc913806f4b1dbd4cc075462dc84422a9b981b2556f9c9197d76da7ba3603e53e9300869c574d821c',
        // this is needed so that smart contract signatures can be verified
        provider,
    })
    console.log('is the sig valid: ', isValidSig)
}
run().catch(e => console.error(e))