question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get valid signatures for signMessage for web3-provider

See original GitHub issue

I can’t get a verifiable signature when I use web3 provider like ethers, direct usage of connector works fine.

This works (with direct connector usage): https://codesandbox.io/s/unruffled-benz-9ubn6?file=/src/App.vue

This doesn’t work: https://codesandbox.io/s/gallant-banach-z7svg?file=/src/App.vue the same code works for direct MetaMask usage as a browser extension.

I was also testing with wrappers like:

const rawMessageLength = new Blob([rawMessage]).size
let message = ethers.utils.toUtf8Bytes("\x19Ethereum Signed Message:\n" + rawMessageLength + rawMessage)
message = ethers.utils.keccak256(message)

They don’t help as well.

This topic was also discussed here: https://stackoverflow.com/questions/63793873/sign-and-verifiy-message-on-ethereum-using-wallet-connect-not-working/63817084#63817084

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:19 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
Xaber20110202commented, Jul 15, 2021

The method below you can try, and you can see the full raw message (not hex) on the client side

import { Signer, utils, providers } from 'ethers'

const signMessageAsync = async (signer: Signer, address: string, message: string): Promise<string> => {
  const messageBytes = utils.toUtf8Bytes(message)
  if (signer instanceof providers.JsonRpcSigner) {
    try {
      const signature = await signer.provider.send('personal_sign', [
        utils.hexlify(messageBytes),
        address.toLowerCase()
      ])
      return signature
    } catch (e) {
      if (e.message.includes('personal_sign')) {
        return await signer.signMessage(messageBytes)
      }
      throw e
    }
  } else {
    return await signer.signMessage(messageBytes)
  }
}

reference from zkSync

  1. change message string to Utf8Bytes
    1. zkSync utils getSignedBytesFromMessage call
    2. zkSync utils getSignedBytesFromMessage
  2. use provider to send personal_sign call
    1. zkSync utils signMessagePersonalAPI call
    2. zkSync utils signMessagePersonalAPI
2reactions
pakokrewcommented, Jan 12, 2021

Here’s my workaround that works for window.ethereum and wallet connect, with ethers:

async function signMessage(provider: ethers.providers.Web3Provider, rawMessage: string) {
  if(provider.provider instanceof WalletConnectProvider) {
    const rawMessageLength = new Blob([rawMessage]).size
    const message = ethers.utils.toUtf8Bytes("\x19Ethereum Signed Message:\n" + rawMessageLength + rawMessage)
    const signer = provider.getSigner();
    const address = await signer.getAddress();
    const keccakMessage = ethers.utils.keccak256(message);

    const wc = provider.provider as WalletConnectProvider;
    const signature = await wc.connector.signMessage([
      address.toLowerCase(),
      keccakMessage,
    ]);
    return signature;
  } else {
    const signer = provider.getSigner();
    return await signer.signMessage(rawMessage);
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Trouble with signature verification using `signer.signMessage ...
I'm trying to do some simple signature verification using signer.signMessage with web3Provider but I think I'm missing something. I can't ...
Read more >
I need help with signatures - Ethereum Stack Exchange
I know how to sign messages in web3 and how to extract r, s and v so that ecrecover returns my address. But...
Read more >
Web3 signature verification is failing - ethers.js - Stack Overflow
Figured it out! Turns out, I was signing the string dataHash instead of bytes value of dataHash. I was able to get the...
Read more >
Ethereum : Using Web3.js for message signing - Medium
Object : The signed data RLP encoded signature, or if returnSignature is true the signature values as follows: message - String : The...
Read more >
How to Verify a Message Signature on Ethereum
Creating and verifying signatures does not require a connection to the Ethereum ... signMessage(message); // Using our wallet instance which holds our ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found