Skip to content
This repository has been archived by the owner on Jun 17, 2021. It is now read-only.

Commit

Permalink
Merge pull request #58 from ethereumjs/rpcsig-padding
Browse files Browse the repository at this point in the history
Pad rpcSig output
  • Loading branch information
axic committed Nov 9, 2016
2 parents cadd456 + e555b3b commit c86d92b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
12 changes: 11 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,9 +391,18 @@ exports.ecrecover = function (msgHash, v, r, s) {
* @return {String} sig
*/
exports.toRpcSig = function (v, r, s) {
// NOTE: with potential introduction of chainId this might need to be updated
if (v !== 27 && v !== 28) {
throw new Error('Invalid recovery id')
}

// geth (and the RPC eth_sign method) uses the 65 byte format used by Bitcoin
// FIXME: this might change in the future - https://github.com/ethereum/go-ethereum/issues/2053
return exports.bufferToHex(Buffer.concat([ r, s, exports.toBuffer(v - 27) ]))
return exports.bufferToHex(Buffer.concat([
exports.setLengthLeft(r, 32),
exports.setLengthLeft(s, 32),
exports.toBuffer(v - 27)
]))
}

/**
Expand All @@ -405,6 +414,7 @@ exports.toRpcSig = function (v, r, s) {
exports.fromRpcSig = function (sig) {
sig = exports.toBuffer(sig)

// NOTE: with potential introduction of chainId this might need to be updated
if (sig.length !== 65) {
throw new Error('Invalid signature length')
}
Expand Down
10 changes: 10 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -578,4 +578,14 @@ describe('message sig', function () {
ethUtils.fromRpcSig('0x99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca660042')
})
})

it('pad short r and s values', function () {
assert.equal(ethUtils.toRpcSig(27, r.slice(20), s.slice(20)), '0x00000000000000000000000000000000000000004a1579cf389ef88b20a1abe90000000000000000000000000000000000000000326fa689f228040429e3ca6600')
})

it('should throw on invalid v value', function () {
assert.throws(function () {
ethUtils.toRpcSig(1, r, s)
})
})
})

0 comments on commit c86d92b

Please sign in to comment.