2

When you use dApps that have read/write functions that require auth, sometimes the dApp will have you sign a message to prove you are the EOA you claim to be. OpenSea does this when you want to modify your settings, for instance.

I am not entirely sure how to properly phrase this, but what I am looking for is an example of how I can capture a signature from an EOA that I can then validate with Ethers.js on a backend to authenticate a user.

The idea is upon wallet connect, I want to be able to send a message to the backend login server, validate the message with ethers, then sign a JWT cookie as a response to the frontend.

How does one use Ethers.js to sign such a message and then later validate it?

1 Answer 1

3
const message = 'Hello';

// In frontend
const provider = new ethers.providers.Web3Provider(window.ethereum);
await provider.send('eth_requestAccounts', []) // connects MetaMask
const signer = provider.getSigner()
const signature = await signer.signMessage(message);

// On server
const recoveredAddress = ethers.utils.verifyMessage(message, signature);

assert(recoveredAddress === await signer.getAddress());
1
  • 1
    That is exactly what I want. Thanks. You save my day. May 9 at 13:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.