Ethereum – How to Sign and Verify a Message in JavaScript to Prove Ownership of an Address

messagesignature

Is there a js library or even just pure js where I can sign and verify I own an Ethereum address?

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:

> web3.eth.sign(<your address>, web3.sha3("Some text"))

0x32f689696d855dd79c73acd94b2374461261b6f3d00e758fa23d35607c0be3175cbc7a7ea02c7f23cd7ef9334b4718a3363dfe12c1c1de24da5f94eb68a67b6000"

The returned 130byte string is the concatenated r,s,v ECDSA values with the mapping:

r = signature[0:64]
s = signature[64:128]
v = signature[128:130]

While web3.js has no verification functions, Solidity has ecrecover call in the form:

ecrecover([sha3 hash of data], v, r, s)

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)

Related Topic