Skip to content

Instantly share code, notes, and snippets.

@AlwaysBCoding
Created March 3, 2017 03:28
Show Gist options
  • Save AlwaysBCoding/e28637188cfeeb3db4695ca1c17c18d6 to your computer and use it in GitHub Desktop.
Save AlwaysBCoding/e28637188cfeeb3db4695ca1c17c18d6 to your computer and use it in GitHub Desktop.
Ethereum Ðapp Development - Video 18 | Signing Arbitrary Messages
// Private Key
var pKey = "..."
var pKeyx = new Buffer(pKey, "hex")
// Shared Message
var message = "..."
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")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment