I'm trying to sign message with private key offline to prove private key ownership.

The issue is that when validating signed object public key from private address does not match output from recover function.

var Web3 = require('web3');
var web3 = new Web3(Web3.givenProvider || "ws://localhost:8546");

var address = '0x2394d939C761cC9bD6137B82bB9e017657509Afb',
    privateKey = '6ac2663692506980c50e239c300f2fefdac6bc95b0e9bdf8a9bd3f615c1ca726';

var signedObject = web3.eth.accounts.sign('Some data', privateKey);
console.log('Public key matches:', address == web3.eth.accounts.recover(signedObject));

This returns false. What am I missing?

  • Public key in your question is actually an address. And web3.eth.accounts.recover returns the address used to sign the data. – Roman Frolov Jan 31 '18 at 10:29
  • 1
    Try adding 0x to your private key. – Roman Frolov Jan 31 '18 at 10:30
  • yes, the 0x did the trick. any reason behind this? – pankijs Jan 31 '18 at 11:18
  • Because the private key: " is 64 hexadecimal characters. Every single string of 64 hex are, hypothetically, an Ethereum private key" Source. This is just how you should use hex values, I guess. – Roman Frolov Jan 31 '18 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.