'Invalid signature error from ethers when trying to verify a signature

I am working on a dapp that needs to verify users' signatures. When testing with Metamask, everything is okay.

When trying to verify a signature from Ambire wallet, I get "Error: invalid signature string" from ethers.utils.verifyMessage For example, here's the signature I got from Ambire: 0xf1b5f561c1914d513f4020edd397f729836e26eeae8f1b9e4070d134492aa38b0a84b9d7cefe06f180ade91595fd74c4e845c2b8c2d053fa57785fdf5ec5dca31c01

When I try to verify it this happens

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

Any idea if this can be solved?



Solution 1:[1]

In order to support smart wallets like Ambire, Gnosis Safe, Argent, and others, you need to implement EIP 1271.

Ambire universal signature verification, supporting:

  • Standard message verification (eth_sign)
  • 712 Typed data verification (eth_signTypedData_v*)
  • 1271 Smart contract on-chain verification (isValidSignature) An optional smart contract signature off-chain verification (eg if the smart wallet is counterfactual and not deployed yet)

Simple eth_sign verification using @ambire/signature-validator

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))

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 shafa.yeat