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
Best Answer
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
vsweb3.eth.personal.sign
. Starting with Web3.js 1.0, usingpersonal
where possible is much better, and this is what should be used for MetaMask accountsweb3.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:Order of your parameters. Depending on the version of Web3.js you are using, the order of the parameters change:
Given the number of issues, it would be best you start with a working sample like the one I linked above.