Skip to content

Instantly share code, notes, and snippets.

@alexanderattar
Last active October 4, 2023 15:11
Show Gist options
  • Star 16 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save alexanderattar/29bef134239d5760b8d1fcc310b632be to your computer and use it in GitHub Desktop.
Save alexanderattar/29bef134239d5760b8d1fcc310b632be to your computer and use it in GitHub Desktop.
Example of how to sign a message with web3 and recover the address that signed it
var ethUtil = require('ethereumjs-util');
var data = 'Login';
var message = ethUtil.toBuffer(data);
var msgHash = ethUtil.hashPersonalMessage(message);
var privateKey = new Buffer('62debf78d596673bce224a85a90da5aecf6e781d9aadcaedd4f65586cfe670d2', "hex")
var sig = ethUtil.ecsign(msgHash, privateKey);
var signature = ethUtil.toBuffer(sig)
var sigParams = ethUtil.fromRpcSig(signature)
var publicKey = ethUtil.ecrecover(msgHash, sigParams.v, sigParams.r, sigParams.s)
var sender = ethUtil.publicToAddress(publicKey)
var addr = ethUtil.bufferToHex(sender)
// Get hashed message in string form
ethUtil.bufferToHex(msgHash)
var Web3 = require('web3');
var web3 = new Web3();
var ethUtil = require('ethereumjs-util');
// Private Key
var pKey = "62debf78d596673bce224a85a90da5aecf6e781d9aadcaedd4f65586cfe670d2"
var pKeyx = new Buffer(pKey, "hex")
// Shared Message
var message = 'Login'
var messageHash = web3.sha3(message)
var messageHashx = new Buffer(messageHash.replace("0x", ""), "hex")
// Signed Hash
var signedMessage = ethUtil.ecsign(messageHashx, pKeyx)
var signedHash = ethUtil.toRpcSig(signedMessage.v, signedMessage.r, signedMessage.s).toString("hex")
// Recover Address
var sigDecoded = ethUtil.fromRpcSig(signedHash)
var recoveredPub = ethUtil.ecrecover(messageHashx, sigDecoded.v, sigDecoded.r, sigDecoded.s)
var recoveredAddress = ethUtil.pubToAddress(recoveredPub).toString("hex")
@daveodwyer
Copy link

Any idea what could be causing this error?

Error: Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: Login

@daveodwyer
Copy link

daveodwyer commented Jun 15, 2021

Anyone else who sees this, looks like the ethUtil library has updated. So far I have this:

var data = "Login";
data = ethUtil.fromUtf8(data);
var message = ethUtil.toBuffer(data);
var msgHash = ethUtil.hashPersonalMessage(message);

var privateKey = new Buffer.from('62debf78d596673bce224a85a90da5aecf6e781d9aadcaedd4f65586cfe670d2', 'hex');

I will update here if I get this working.

Currently when trying to use ecsign, my node environment is just hanging
var sig = ethUtil.ecsign(msgHash, privateKey);

@paulwongx
Copy link

paulwongx commented Apr 6, 2022

Any idea what could be causing this error?

Error: Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: Login

This works

const data = "this is a message";

const msgHex = ethUtil.bufferToHex(Buffer.from(data));
console.log(`msgHex ${msgHex}`);

const msgBuffer = ethUtil.toBuffer(msgHex);
console.log(`msgBuffer ${msgBuffer}`);

const msgHash = ethUtil.hashPersonalMessage(msgBuffer);
console.log(`msgHash ${msgHash}`);

@shineli1984
Copy link

2023 examples in both golang and js: https://www.ethsigpublicaddress.com/

@alexanderattar
Copy link
Author

Thanks for sharing @shineli1984

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