Skip to content

Commit

Permalink
Add helpers for new eth_sign behavior (#67)
Browse files Browse the repository at this point in the history
Add helpers for new eth_sign behaviour.
  • Loading branch information
yusefnapora authored and axic committed Jan 24, 2017
1 parent 0e246cd commit ac5d090
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,19 @@ exports.ecsign = function (msgHash, privateKey) {
return ret
}

/**
* Returns the keccak-256 hash of `message`, prefixed with the header used by the `eth_sign` RPC call.
* The output of this function can be fed into `ecsign` to produce the same signature as the `eth_sign`
* call for a given `message`, or fed to `ecrecover` along with a signature to recover the public key
* used to produce the signature.
* @param message
* @returns {Buffer} hash
*/
exports.hashPersonalMessage = function (message) {
var prefix = exports.toBuffer('\u0019Ethereum Signed Message:\n' + message.length.toString())
return exports.sha3(Buffer.concat([prefix, message]))
}

/**
* ECDSA public key recovery from signature
* @param {Buffer} msgHash
Expand Down
7 changes: 7 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,13 @@ describe('ecrecover', function () {
})
})

describe('hashPersonalMessage', function () {
it('should produce a deterministic hash', function () {
var h = ethUtils.hashPersonalMessage(Buffer.from('Hello world'))
assert.deepEqual(h, Buffer.from('8144a6fa26be252b86456491fbcd43c1de7e022241845ffea1c3df066f7cfede', 'hex'))
})
})

describe('isValidSignature', function () {
it('should fail on an invalid signature (shorter r))', function () {
var r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1ab', 'hex')
Expand Down

0 comments on commit ac5d090

Please sign in to comment.