1

Web3js as of version 1.0.0 beta supports an interface for verifying signed messages: https://github.com/ethereum/web3.js/issues/404

This is documented here: https://web3js.readthedocs.io/en/1.0/web3-eth-accounts.html#recover

Following the documentation, i try to sign and verify a message, and notice the address I get from ecrecover is not the same as the one that created the message.

> let message = "this is a message"
> let account = '0x6eE80F4D7494E61b90c91a7175804018332d6e17'
> let privateKey = '82c290abc9dc56631a62ca9523dcb51b7c2beb6c941bd47ad0332a124eb822ce'
> let signature = '0x21ef296f199e079cae27abffc63389ffd910e91bd7196f389ca2b0177f116d020945a0dbed2ea62d4be17c09947ce33a7a183722e1fc2a75cd9dc9c6bd97d1501b'
> let signatureObject = we3.eth.accounts.sign(message, privateKey)

{ message: 'this is a message',
  messageHash:
   '0x29d8e880f198acda69a1cd82dd2c8e37edc6bb7e84da26527fb8a0cf7d482cda',
  v: '0x1b',
  r:
   '0x21ef296f199e079cae27abffc63389ffd910e91bd7196f389ca2b0177f116d02',
  s:
   '0x0945a0dbed2ea62d4be17c09947ce33a7a183722e1fc2a75cd9dc9c6bd97d150',
  signature:
   '0x21ef296f199e079cae27abffc63389ffd910e91bd7196f389ca2b0177f116d020945a0dbed2ea62d4be17c09947ce33a7a183722e1fc2a75cd9dc9c6bd97d1501b' }

> we3.eth.accounts.recover(signatureObject)

'0x1aB8B2a2BE93b08879b3b3e7eB1EF2D59A544B91'

Notice the address returned is not the same as the address used to sign the message ( the account variable).

I also tried hashing the message before signing using web3.utils.sha3() and web3.eth.accounts.hashMessage(), then passing that hash into sign. They all yield the same address as above.

I have already verified the address and private key I am using are correct.

1 Answer 1

2

Prefix your private key with 0x. I'm not sure how it's interpreted without that, but with the 0x prefix you'll get the expected result.

1
  • This works. Interesting that without the 0x it doesn't fail or give some other invalid response, instead gives another address. Mar 21, 2019 at 0:21

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.