1

I've figured out the mechanism to use Metamask and ethers for the user to sign an arbitrary message, but it involved computing a hash of the message, arrayifying it, and then calling verify message.

Something like this:

 function signMessage(msg : string) : string { 
    let msgHash= ethers.utils.solidityKeccak256(['string'],[msg]);      
    let sig = await this.mySigner.signMessage(ethers.utils.arrayify(hash));
    return sig;
 }

The problem I am trying to solve for is what the user sees when Metamask requests signature of the message, which looks like this:

enter image description here

The problem is that the end user is shown the hash, but no way of knowing what the actual message is they are being asked to sign. If I were the user, I'd like to be able to read the message I am being asked to sign within Metamask. Is there some way that, rather than signing a hash of the message, you can request signature of the clear text of the message? I've tried supplying a string to ethers signer.signMessage function but it produces errors.

2 Answers 2

0

You can take a look at this resource on how to use metamask to sign a plaintext message. It hinges on using eth.personal_sign(fromAddress, hexEncodedUtf8Message).

See: https://medium.com/metamask/the-new-secure-way-to-sign-data-in-your-browser-6af9dd2a1527#:~:text=MetaMask%20can%20now%20render%20plain,their%20signature%20challenges%20human%20readable.

1
  • That looks really nice, just what I'm looking for but unfortunately it looks like eth.js has been abandoned. (Archived in 2018). Can it be done with ethers.js?
    – GGizmos
    Commented Sep 25, 2022 at 22:08
0

You are using the correct method to sign message. But I don't know why you are trying to use python with the ether.js. And you don't need to hash the message when you signing it.

Here's the example:

const Message = async (message) => {
   const signer = ethers.getSigner();
   const signature = await signer.signMessage(message); 
} 

The above function works fine. Since the result singMessage returns is hash, we need to resolve the promise.

To take it little more furture, you can verify the signature using the another method from the ether.js.

const Message = async (message) => {
       const signer = ethers.getSigner();
       const signature = await signer.signMessage(message); 
       const verify = ethers.utils.verifyMessage(message, signature)
    } 

if you log the verify into console you will see the signer address. That is your ethereum address.

Tell me if it helps!

2
  • Thanks for that. I'm not actually using hardhat, so I can't use ethers.getSigners, but more importantly. (Not using python either, that's typescript). But more iimportantly, for reasons, I don't fully understand I've tried the code again, and this time I was able to sign the string directly. Previously when I tried it I kept getting an "arrayify" failure when I just passed a string to the signer.signMessage. Anyway, working now so thanks.
    – GGizmos
    Commented Sep 26, 2022 at 7:25
  • To able to show message in string, you should pass the string only. Anyways, you got it.
    – Ad-h0c
    Commented Sep 26, 2022 at 9:36

Your Answer

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

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