Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use solidity ecrecover with signature calculated with eth_sign
// Change accountToSignWith to the address of your account.
var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var accountToSignWith = '0xbedcf417ff2752d996d2ade98b97a6f0bef4beb9';
var message = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Tubulum fuisse, qua illum, cuius is condemnatus est rogatione, P. Eaedem res maneant alio modo.'
var contractSource = `
contract SignAndVerifyExample {
function RecoverAddress(bytes32 msgHash, uint8 v, bytes32 r, bytes32 s) constant returns (address) {
return ecrecover(msgHash, v, r, s);
}
}
`;
var contractABI = [{"constant":true,"inputs":[{"name":"msgHash","type":"bytes32"},{"name":"v","type":"uint8"},{"name":"r","type":"bytes32"},{"name":"s","type":"bytes32"}],"name":"RecoverAddress","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"}];
var contractAddress = '0x5481c0fe170641bd2e0ff7f04161871829c1902d'; // on Ropsten and Rinkeby
var signAndVerifyContract = web3.eth.contract(contractABI).at(contractAddress);
// eth_sign calculated the signature over keccak256("\x19Ethereum Signed Message:\n" + len(givenMessage) + givenMessage)))
// this gives context to a signature and prevents signing of transactions.
function messageHash(msg) {
return web3.sha3('\x19Ethereum Signed Message:\n' + msg.length + msg);
}
function verifyHandler(err, address) {
if (!err) {
console.log('Recovered address:', address);
console.log(' Address matched:', accountToSignWith === address);
} else {
console.err('Could not recover address:', err);
}
}
function signHandler(err, signature) {
if (!err) {
console.log('Signature:', signature);
signature = signature.substr(2);
r = '0x' + signature.substr(0, 64);
s = '0x' + signature.substr(64, 64);
v = '0x' + signature.substr(128, 2)
console.log(' r:', r)
console.log(' s:', s)
console.log(' v:', v)
console.log();
signAndVerifyContract.RecoverAddress(messageHash(message), v, r, s, verifyHandler);
} else {
console.error('Coult not sign message:', err);
}
}
console.log(' Message to sign:', message);
console.log('Sign with account:', accountToSignWith);
console.log();
var messageHex = '0x' + Buffer.from(message).toString('hex');
web3.eth.sign(accountToSignWith, messageHex, signHandler);
@sivachaitanya

This comment has been minimized.

sivachaitanya commented Aug 29, 2017

This is not working its returning me 0x0000000..

@Zwilla

This comment has been minimized.

Zwilla commented Aug 30, 2017

I’m working on this issue atm. This is just to set a reference.

@CTTeo

This comment has been minimized.

CTTeo commented May 7, 2018

I tried your code but it is returning me 0x0000...000. I am testing it with testrpc

@chitpot27

This comment has been minimized.

chitpot27 commented Jul 9, 2018

How to sign verified message or message signature hash what will i do thanx.

@PaulRBerg

This comment has been minimized.

PaulRBerg commented Sep 19, 2018

Be aware that this is not using web3.js ^1.0.0, rather the old versions starting with 0.

@beyondscl

This comment has been minimized.

beyondscl commented Oct 29, 2018

This is not working its returning me 0x0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment