Is there a js library or even just pure js where I can sign and verify I own an Ethereum address?
Ethereum – How to Sign and Verify a Message in JavaScript to Prove Ownership of an Address
messagesignature
messagesignature
Is there a js library or even just pure js where I can sign and verify I own an Ethereum address?
The problem is that eth.sign
returns a signature where v
is 0 or 1 and ecrecover
expect it to be 27 or 28.
A note in the documentation for web3 v0.20 is clear:
Note that if you are using
ecrecover
,v
will be either"00"
or"01"
. As a result, in order to use this value, you will have to parse it to an integer and then add27
. This will result in either a27
or a28
.
You have to do something like this
var signature = await web3.eth.sign(random, signer); // sign the random hash or the ethHash
signature = signature.substr(0, 130) + (signature.substr(130) == "00" ? "1b" : "1c");
var recovered = await sigs.recover(ethHash, signature); // recover from the random hash or the ethHash
Best Answer
You can sign a string of data using
web3.eth.sign
The address you use needs to be unlocked and you can sign text like this:
The returned 130byte string is the concatenated r,s,v ECDSA values with the mapping:
While web3.js has no verification functions, Solidity has
ecrecover
call in the form:This question explores usage of
ecrecover
.Further, there is another thread on the forum in which the OP uses the Python Bitcoin module to verify signed data.
Update: Since v1.0 web3.js has verification functions. See the other answers here (eg. web3.eth.accounts.recover)