Search code examples
node.jsethereummetamask

Formatting of message in MetaMask Signature Request is off


I'm using MetaMask to sign a nonce in order to do server side authentication. Everything works as expected expect that the message shown in the MetaMask window are cryptic signs instead of the actual message.

This is how I trigger MetaMask to sign the message (n):

console.log(n); //0x123456789...123456789
const sig = await web3.eth.personal.sign(n, address, "password!");

The result:

MetaMask Signature Request

Anyone an idea why this is happening?

Thanks for your help!


Solution

  • You must convert the "n" variable to HEX like this:

    let message = web3.utils.utf8ToHex(n);
    const sig = await web3.eth.personal.sign(message, address, "password!");
    
    

    Link to documentation https://web3js.readthedocs.io/en/v1.2.11/web3-utils.html#utils-utf8tohex