Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/ecpair.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ ECPair.makeRandom = function (options) {
typeforce(types.Buffer256bit, buffer)

d = BigInteger.fromBuffer(buffer)
} while (d.compareTo(secp256k1.n) >= 0)
} while (d.signum() <= 0 || d.compareTo(secp256k1.n) >= 0)

return new ECPair(d, null, options)
}
Expand Down
32 changes: 15 additions & 17 deletions test/ecpair.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var BigInteger = require('bigi')
var ECPair = require('../src/ecpair')

var fixtures = require('./fixtures/ecpair.json')
var secp256k1 = ecurve.getCurveByName('secp256k1')
var curve = ecdsa.__curve

var NETWORKS = require('../src/networks')
var NETWORKS_LIST = [] // Object.values(NETWORKS)
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('ECPair', function () {

it('throws if public and private key given', function () {
var qBuffer = new Buffer('0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798', 'hex')
var Q = ecurve.Point.decodeFrom(secp256k1, qBuffer)
var Q = ecurve.Point.decodeFrom(curve, qBuffer)

assert.throws(function () {
new ECPair(BigInteger.ONE, Q)
Expand Down Expand Up @@ -100,7 +100,7 @@ describe('ECPair', function () {

it('wraps Q.getEncoded', sinon.test(function () {
this.mock(keyPair.Q).expects('getEncoded')
.once().calledWith(keyPair.compressed)
.once().withArgs(keyPair.compressed)

keyPair.getPublicKeyBuffer()
}))
Expand Down Expand Up @@ -160,18 +160,6 @@ describe('ECPair', function () {
var keyPair = ProxiedECPair.makeRandom()
assert.strictEqual(keyPair.toWIF(), exWIF)
})

it('passes the options param', sinon.test(function () {
var options = {
compressed: true
}

// FIXME: waiting on https://github.com/cjohansen/Sinon.JS/issues/613
// this.mock(ECPair).expects('constructor')
// .once().calledWith(options)

ECPair.makeRandom(options)
}))
})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this test, as sinonjs/sinon#613 doesn't look like it'll be fixed any time soon.
We already check that the parameters themselves are being used, it would just have been an added precaution to verify the call behaviour.

it('allows a custom RNG to be used', function () {
Expand All @@ -181,6 +169,16 @@ describe('ECPair', function () {

assert.strictEqual(keyPair.toWIF(), exWIF)
})

it('loops until d is within interval [1, n - 1]', sinon.test(function () {
var rng = this.mock()
rng.exactly(3)
rng.onCall(0).returns(new BigInteger('0').toBuffer(32)) // < 1
rng.onCall(1).returns(curve.n.toBuffer(32)) // > n-1
rng.onCall(2).returns(new BigInteger('42').toBuffer(32)) // valid

ECPair.makeRandom({ rng: rng })
}))
})

describe('getAddress', function () {
Expand All @@ -204,7 +202,7 @@ describe('ECPair', function () {
describe('signing', function () {
it('wraps ecdsa.sign', sinon.test(function () {
this.mock(ecdsa).expects('sign')
.once().calledWith(secp256k1, hash, keyPair.d)
.once().withArgs(hash, keyPair.d)

keyPair.sign(hash)
}))
Expand All @@ -227,7 +225,7 @@ describe('ECPair', function () {

it('wraps ecdsa.verify', sinon.test(function () {
this.mock(ecdsa).expects('verify')
.once().calledWith(secp256k1, hash, signature, keyPair.Q)
.once().withArgs(hash, signature, keyPair.Q)

keyPair.verify(hash, signature)
}))
Expand Down