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

Fix isValidSignature method #171

Merged
merged 3 commits into from
Feb 4, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ exports.addHexPrefix = function (str) {
/**
* Validate ECDSA signature
* @method isValidSignature
* @param {Buffer} v
* @param {Number} v
* @param {Buffer} r
* @param {Buffer} s
* @param {Boolean} [homestead=true]
Expand All @@ -598,7 +598,7 @@ exports.isValidSignature = function (v, r, s, homestead, chainId) {
return false
}

if ((homestead === false) && (new BN(s).cmp(SECP256K1_N_DIV_2) === 1)) {
if (homestead && (new BN(s).cmp(SECP256K1_N_DIV_2) === 1)) {
danjm marked this conversation as resolved.
Show resolved Hide resolved
return false
}

Expand Down
18 changes: 18 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,24 @@ describe('isValidSignature', function () {
const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
assert.equal(ethUtils.isValidSignature(29, r, s), false)
})
it('should fail when on homestead and s > secp256k1n/2', function () {
const SECP256K1_N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)

const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
const s = Buffer.from(SECP256K1_N_DIV_2.add(new BN('1', 16)).toString(16), 'hex')

const v = 27
assert.equal(ethUtils.isValidSignature(v, r, s), true)
})
it('should not fail when not on homestead but s > secp256k1n/2', function () {
const SECP256K1_N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16)

const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
const s = Buffer.from(SECP256K1_N_DIV_2.add(new BN('1', 16)).toString(16), 'hex')

const v = 27
assert.equal(ethUtils.isValidSignature(v, r, s), true)
})
it('should work otherwise', function () {
const r = Buffer.from('99e71a99cb2270b8cac5254f9e99b6210c6c10224a1579cf389ef88b20a1abe9', 'hex')
const s = Buffer.from('129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66', 'hex')
Expand Down