3

I am getting an error when trying to sign data with web3 and metamask

signed_data = myWeb3.eth.sign("hello", web3.eth.accounts[0])

web3.eth.accounts[0] is populated with my account number in string form

Results in error:

Uncaught Error: invalid address at c (inpage.js:1) at inpage.js:1 at Array.map (<anonymous>) at o.formatInput (inpage.js:1) at o.toPayload (inpage.js:1) at n.e [as sign] (inpage.js:1) at test.html:12

2

Take a look here at a complete working sample of signing and recovering messages with Web3.js and MetaMask:

https://github.com/shawntabrizi/web3js-sign-and-verify-ethereum

You can play around with a deployed version of this code here:

https://shawntabrizi.com/ethsign/

There could be a few problems here with your code, which is hard to decipher with the information you provided:

  • Using web3.eth.sign vs web3.eth.personal.sign. Starting with Web3.js 1.0, using personal where possible is much better, and this is what should be used for MetaMask accounts

  • web3.eth.accounts[0] not being populated. In Web3.js 1.0, this value is not populated by default. Instead you need to get accounts using something like:

    var accounts = await web3.eth.getAccounts()
    console.log(accounts[0])
    
  • Order of your parameters. Depending on the version of Web3.js you are using, the order of the parameters change:

    web3.eth.sign(address, dataToSign [, callback]) // web3.js 0.x.x
    
    web3.eth.sign(dataToSign, address [, callback]) // web3.js 1.0
    

Given the number of issues, it would be best you start with a working sample like the one I linked above.

0

Just update your web3.eth.sign call as:-web3.eth.sign(address, dataToSign, [, callback]); You just passing wrong order of argument to sign() function. May this will help you out.

  • i have tried it both ways and get the same error – bwbrowning May 24 '18 at 11:43

Your Answer

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

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