commit
058fa24e09
4 changed files with 91 additions and 78 deletions
|
@ -51,12 +51,11 @@
|
||||||
"bip65": "^1.0.1",
|
"bip65": "^1.0.1",
|
||||||
"bs58": "^4.0.0",
|
"bs58": "^4.0.0",
|
||||||
"dhttp": "^2.4.2",
|
"dhttp": "^2.4.2",
|
||||||
|
"hoodwink": "^1.0.0",
|
||||||
"minimaldata": "^1.0.2",
|
"minimaldata": "^1.0.2",
|
||||||
"mocha": "^5.0.1",
|
"mocha": "^5.0.1",
|
||||||
"nyc": "^11.4.1",
|
"nyc": "^11.4.1",
|
||||||
"proxyquire": "^1.4.0",
|
"proxyquire": "^1.4.0",
|
||||||
"sinon": "^4.3.0",
|
|
||||||
"sinon-test": "^2.1.3",
|
|
||||||
"standard": "^9.0.2"
|
"standard": "^9.0.2"
|
||||||
},
|
},
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
|
|
|
@ -3,9 +3,7 @@
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var bcrypto = require('../src/crypto')
|
var bcrypto = require('../src/crypto')
|
||||||
var ecdsa = require('../src/ecdsa')
|
var ecdsa = require('../src/ecdsa')
|
||||||
var sinon = require('sinon')
|
var hoodwink = require('hoodwink')
|
||||||
var sinonTest = require('sinon-test')
|
|
||||||
var setupTest = sinonTest(sinon)
|
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECSignature = require('../src/ecsignature')
|
var ECSignature = require('../src/ecsignature')
|
||||||
|
@ -30,12 +28,13 @@ describe('ecdsa', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('loops until an appropriate k value is found', setupTest(function () {
|
it('loops until an appropriate k value is found', hoodwink(function () {
|
||||||
this.mock(BigInteger).expects('fromBuffer')
|
this.mock(BigInteger, 'fromBuffer', function f (b) {
|
||||||
.exactly(3)
|
assert.strictEqual(b.length, 32)
|
||||||
.onCall(0).returns(new BigInteger('0')) // < 1
|
if (f.calls === 0) return BigInteger.ZERO // < 1
|
||||||
.onCall(1).returns(curve.n) // > n-1
|
if (f.calls === 1) return curve.n // > n - 1
|
||||||
.onCall(2).returns(new BigInteger('42')) // valid
|
if (f.calls === 2) return new BigInteger('42') // valid
|
||||||
|
}, 3)
|
||||||
|
|
||||||
var x = new BigInteger('1').toBuffer(32)
|
var x = new BigInteger('1').toBuffer(32)
|
||||||
var h1 = Buffer.alloc(32)
|
var h1 = Buffer.alloc(32)
|
||||||
|
@ -44,24 +43,17 @@ describe('ecdsa', function () {
|
||||||
assert.strictEqual(k.toString(), '42')
|
assert.strictEqual(k.toString(), '42')
|
||||||
}))
|
}))
|
||||||
|
|
||||||
it('loops until a suitable signature is found', setupTest(function () {
|
it('loops until a suitable signature is found', hoodwink(function () {
|
||||||
this.mock(BigInteger).expects('fromBuffer')
|
var checkSigStub = this.stub(function f () {
|
||||||
.exactly(4)
|
if (f.calls === 0) return false // bad signature
|
||||||
.onCall(0).returns(new BigInteger('0')) // < 1
|
if (f.calls === 1) return true // good signature
|
||||||
.onCall(1).returns(curve.n) // > n-1
|
}, 2)
|
||||||
.onCall(2).returns(new BigInteger('42')) // valid, but 'bad' signature
|
|
||||||
.onCall(3).returns(new BigInteger('53')) // valid, good signature
|
|
||||||
|
|
||||||
var mockCheckSig = this.mock()
|
var x = BigInteger.ONE.toBuffer(32)
|
||||||
mockCheckSig.exactly(2)
|
|
||||||
mockCheckSig.onCall(0).returns(false) // bad signature
|
|
||||||
mockCheckSig.onCall(1).returns(true) // good signature
|
|
||||||
|
|
||||||
var x = new BigInteger('1').toBuffer(32)
|
|
||||||
var h1 = Buffer.alloc(32)
|
var h1 = Buffer.alloc(32)
|
||||||
var k = ecdsa.deterministicGenerateK(h1, x, mockCheckSig)
|
var k = ecdsa.deterministicGenerateK(h1, x, checkSigStub)
|
||||||
|
|
||||||
assert.strictEqual(k.toString(), '53')
|
assert.strictEqual(k.toHex(), 'a9b1a1a84a4c2f96b6158ed7a81404c50cb74373c22e8d9e02d0411d719acae2')
|
||||||
}))
|
}))
|
||||||
|
|
||||||
fixtures.valid.rfc6979.forEach(function (f) {
|
fixtures.valid.rfc6979.forEach(function (f) {
|
||||||
|
|
|
@ -5,9 +5,7 @@ var assert = require('assert')
|
||||||
var ecdsa = require('../src/ecdsa')
|
var ecdsa = require('../src/ecdsa')
|
||||||
var ecurve = require('ecurve')
|
var ecurve = require('ecurve')
|
||||||
var proxyquire = require('proxyquire')
|
var proxyquire = require('proxyquire')
|
||||||
var sinon = require('sinon')
|
var hoodwink = require('hoodwink')
|
||||||
var sinonTest = require('sinon-test')
|
|
||||||
var setupTest = sinonTest(sinon)
|
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECPair = require('../src/ecpair')
|
var ECPair = require('../src/ecpair')
|
||||||
|
@ -76,9 +74,10 @@ describe('ECPair', function () {
|
||||||
keyPair = new ECPair(BigInteger.ONE)
|
keyPair = new ECPair(BigInteger.ONE)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('wraps Q.getEncoded', setupTest(function () {
|
it('wraps Q.getEncoded', hoodwink(function () {
|
||||||
this.mock(keyPair.Q).expects('getEncoded')
|
this.mock(keyPair.Q, 'getEncoded', function (compressed) {
|
||||||
.once().withArgs(keyPair.compressed)
|
assert.strictEqual(compressed, keyPair.compressed)
|
||||||
|
}, 1)
|
||||||
|
|
||||||
keyPair.getPublicKeyBuffer()
|
keyPair.getPublicKeyBuffer()
|
||||||
}))
|
}))
|
||||||
|
@ -167,21 +166,31 @@ describe('ECPair', function () {
|
||||||
assert.strictEqual(keyPair.network, NETWORKS.testnet)
|
assert.strictEqual(keyPair.network, NETWORKS.testnet)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('loops until d is within interval [1, n - 1] : 1', setupTest(function () {
|
it('throws if d is bad length', function () {
|
||||||
var rng = this.mock()
|
function rng () {
|
||||||
rng.exactly(2)
|
return BigInteger.ZERO.toBuffer(28)
|
||||||
rng.onCall(0).returns(BigInteger.ZERO.toBuffer(32)) // invalid length
|
}
|
||||||
rng.onCall(1).returns(BigInteger.ONE.toBuffer(32)) // === 1
|
|
||||||
|
assert.throws(function () {
|
||||||
|
ECPair.makeRandom({ rng: rng })
|
||||||
|
}, /Expected Buffer\(Length: 32\), got Buffer\(Length: 28\)/)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('loops until d is within interval [1, n - 1] : 1', hoodwink(function () {
|
||||||
|
var rng = this.stub(function f () {
|
||||||
|
if (f.calls === 0) return BigInteger.ZERO.toBuffer(32) // 0
|
||||||
|
return BigInteger.ONE.toBuffer(32) // >0
|
||||||
|
}, 2)
|
||||||
|
|
||||||
ECPair.makeRandom({ rng: rng })
|
ECPair.makeRandom({ rng: rng })
|
||||||
}))
|
}))
|
||||||
|
|
||||||
it('loops until d is within interval [1, n - 1] : n - 1', setupTest(function () {
|
it('loops until d is within interval [1, n - 1] : n - 1', hoodwink(function () {
|
||||||
var rng = this.mock()
|
var rng = this.stub(function f () {
|
||||||
rng.exactly(3)
|
if (f.calls === 0) return BigInteger.ZERO.toBuffer(32) // <1
|
||||||
rng.onCall(0).returns(BigInteger.ZERO.toBuffer(32)) // < 1
|
if (f.calls === 1) return curve.n.toBuffer(32) // >n-1
|
||||||
rng.onCall(1).returns(curve.n.toBuffer(32)) // > n-1
|
return curve.n.subtract(BigInteger.ONE).toBuffer(32) // n-1
|
||||||
rng.onCall(2).returns(curve.n.subtract(BigInteger.ONE).toBuffer(32)) // === n-1
|
}, 3)
|
||||||
|
|
||||||
ECPair.makeRandom({ rng: rng })
|
ECPair.makeRandom({ rng: rng })
|
||||||
}))
|
}))
|
||||||
|
@ -217,9 +226,11 @@ describe('ECPair', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('signing', function () {
|
describe('signing', function () {
|
||||||
it('wraps ecdsa.sign', setupTest(function () {
|
it('wraps ecdsa.sign', hoodwink(function () {
|
||||||
this.mock(ecdsa).expects('sign')
|
this.mock(ecdsa, 'sign', function (h, d) {
|
||||||
.once().withArgs(hash, keyPair.d)
|
assert.strictEqual(h, hash)
|
||||||
|
assert.strictEqual(d, keyPair.d)
|
||||||
|
}, 1)
|
||||||
|
|
||||||
keyPair.sign(hash)
|
keyPair.sign(hash)
|
||||||
}))
|
}))
|
||||||
|
@ -240,9 +251,12 @@ describe('ECPair', function () {
|
||||||
signature = keyPair.sign(hash)
|
signature = keyPair.sign(hash)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('wraps ecdsa.verify', setupTest(function () {
|
it('wraps ecdsa.verify', hoodwink(function () {
|
||||||
this.mock(ecdsa).expects('verify')
|
this.mock(ecdsa, 'verify', function (h, s, q) {
|
||||||
.once().withArgs(hash, signature, keyPair.Q)
|
assert.strictEqual(h, hash)
|
||||||
|
assert.strictEqual(s, signature)
|
||||||
|
assert.strictEqual(q, keyPair.Q)
|
||||||
|
}, 1)
|
||||||
|
|
||||||
keyPair.verify(hash, signature)
|
keyPair.verify(hash, signature)
|
||||||
}))
|
}))
|
||||||
|
|
|
@ -3,9 +3,7 @@
|
||||||
|
|
||||||
var assert = require('assert')
|
var assert = require('assert')
|
||||||
var ecdsa = require('../src/ecdsa')
|
var ecdsa = require('../src/ecdsa')
|
||||||
var sinon = require('sinon')
|
var hoodwink = require('hoodwink')
|
||||||
var sinonTest = require('sinon-test')
|
|
||||||
var setupTest = sinonTest(sinon)
|
|
||||||
|
|
||||||
var BigInteger = require('bigi')
|
var BigInteger = require('bigi')
|
||||||
var ECPair = require('../src/ecpair')
|
var ECPair = require('../src/ecpair')
|
||||||
|
@ -81,18 +79,20 @@ describe('HDNode', function () {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it('throws if IL is not within interval [1, n - 1] | IL === 0', setupTest(function () {
|
it('throws if IL is not within interval [1, n - 1] | IL === 0', hoodwink(function () {
|
||||||
this.mock(BigInteger).expects('fromBuffer')
|
this.mock(BigInteger, 'fromBuffer', function () {
|
||||||
.once().returns(BigInteger.ZERO)
|
return BigInteger.ZERO
|
||||||
|
}, 1)
|
||||||
|
|
||||||
assert.throws(function () {
|
assert.throws(function () {
|
||||||
HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff')
|
HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff')
|
||||||
}, /Private key must be greater than 0/)
|
}, /Private key must be greater than 0/)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
it('throws if IL is not within interval [1, n - 1] | IL === n', setupTest(function () {
|
it('throws if IL is not within interval [1, n - 1] | IL === n', hoodwink(function () {
|
||||||
this.mock(BigInteger).expects('fromBuffer')
|
this.mock(BigInteger, 'fromBuffer', function () {
|
||||||
.once().returns(curve.n)
|
return curve.n
|
||||||
|
}, 1)
|
||||||
|
|
||||||
assert.throws(function () {
|
assert.throws(function () {
|
||||||
HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff')
|
HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff')
|
||||||
|
@ -124,38 +124,43 @@ describe('HDNode', function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getAddress', function () {
|
describe('getAddress', function () {
|
||||||
it('wraps keyPair.getAddress', setupTest(function () {
|
it('wraps keyPair.getAddress', hoodwink(function () {
|
||||||
this.mock(keyPair).expects('getAddress')
|
this.mock(hd.keyPair, 'getAddress', function () {
|
||||||
.once().withArgs().returns('foobar')
|
return 'foo'
|
||||||
|
}, 1)
|
||||||
|
|
||||||
assert.strictEqual(hd.getAddress(), 'foobar')
|
assert.strictEqual(hd.getAddress(), 'foo')
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getNetwork', function () {
|
describe('getNetwork', function () {
|
||||||
it('wraps keyPair.getNetwork', setupTest(function () {
|
it('wraps keyPair.getNetwork', hoodwink(function () {
|
||||||
this.mock(keyPair).expects('getNetwork')
|
this.mock(hd.keyPair, 'getNetwork', function () {
|
||||||
.once().withArgs().returns('network')
|
return 'foo'
|
||||||
|
}, 1)
|
||||||
|
|
||||||
assert.strictEqual(hd.getNetwork(), 'network')
|
assert.strictEqual(hd.getNetwork(), 'foo')
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('getPublicKeyBuffer', function () {
|
describe('getPublicKeyBuffer', function () {
|
||||||
it('wraps keyPair.getPublicKeyBuffer', setupTest(function () {
|
it('wraps keyPair.getPublicKeyBuffer', hoodwink(function () {
|
||||||
this.mock(keyPair).expects('getPublicKeyBuffer')
|
this.mock(hd.keyPair, 'getPublicKeyBuffer', function () {
|
||||||
.once().withArgs().returns('pubKeyBuffer')
|
return 'foo'
|
||||||
|
}, 1)
|
||||||
|
|
||||||
assert.strictEqual(hd.getPublicKeyBuffer(), 'pubKeyBuffer')
|
assert.strictEqual(hd.getPublicKeyBuffer(), 'foo')
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('sign', function () {
|
describe('sign', function () {
|
||||||
it('wraps keyPair.sign', setupTest(function () {
|
it('wraps keyPair.sign', hoodwink(function () {
|
||||||
this.mock(keyPair).expects('sign')
|
this.mock(hd.keyPair, 'sign', function (h) {
|
||||||
.once().withArgs(hash).returns('signed')
|
assert.strictEqual(hash, h)
|
||||||
|
return 'foo'
|
||||||
|
}, 1)
|
||||||
|
|
||||||
assert.strictEqual(hd.sign(hash), 'signed')
|
assert.strictEqual(hd.sign(hash), 'foo')
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -166,11 +171,14 @@ describe('HDNode', function () {
|
||||||
signature = hd.sign(hash)
|
signature = hd.sign(hash)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('wraps keyPair.verify', setupTest(function () {
|
it('wraps keyPair.verify', hoodwink(function () {
|
||||||
this.mock(keyPair).expects('verify')
|
this.mock(hd.keyPair, 'verify', function (h, s) {
|
||||||
.once().withArgs(hash, signature).returns('verified')
|
assert.strictEqual(hash, h)
|
||||||
|
assert.strictEqual(signature, s)
|
||||||
|
return 'foo'
|
||||||
|
}, 1)
|
||||||
|
|
||||||
assert.strictEqual(hd.verify(hash, signature), 'verified')
|
assert.strictEqual(hd.verify(hash, signature), 'foo')
|
||||||
}))
|
}))
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue