Skip to content

Commit 058fa24

Browse files
authored
Merge pull request bitcoinjs#1023 from bitcoinjs/nosinon
rm sinon, sinon-test
2 parents 5b0ccb6 + 04c628e commit 058fa24

File tree

4 files changed

+91
-78
lines changed

4 files changed

+91
-78
lines changed

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,11 @@
5151
"bip65": "^1.0.1",
5252
"bs58": "^4.0.0",
5353
"dhttp": "^2.4.2",
54+
"hoodwink": "^1.0.0",
5455
"minimaldata": "^1.0.2",
5556
"mocha": "^5.0.1",
5657
"nyc": "^11.4.1",
5758
"proxyquire": "^1.4.0",
58-
"sinon": "^4.3.0",
59-
"sinon-test": "^2.1.3",
6059
"standard": "^9.0.2"
6160
},
6261
"license": "MIT"

test/ecdsa.js

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
var assert = require('assert')
44
var bcrypto = require('../src/crypto')
55
var ecdsa = require('../src/ecdsa')
6-
var sinon = require('sinon')
7-
var sinonTest = require('sinon-test')
8-
var setupTest = sinonTest(sinon)
6+
var hoodwink = require('hoodwink')
97

108
var BigInteger = require('bigi')
119
var ECSignature = require('../src/ecsignature')
@@ -30,12 +28,13 @@ describe('ecdsa', function () {
3028
})
3129
})
3230

33-
it('loops until an appropriate k value is found', setupTest(function () {
34-
this.mock(BigInteger).expects('fromBuffer')
35-
.exactly(3)
36-
.onCall(0).returns(new BigInteger('0')) // < 1
37-
.onCall(1).returns(curve.n) // > n-1
38-
.onCall(2).returns(new BigInteger('42')) // valid
31+
it('loops until an appropriate k value is found', hoodwink(function () {
32+
this.mock(BigInteger, 'fromBuffer', function f (b) {
33+
assert.strictEqual(b.length, 32)
34+
if (f.calls === 0) return BigInteger.ZERO // < 1
35+
if (f.calls === 1) return curve.n // > n - 1
36+
if (f.calls === 2) return new BigInteger('42') // valid
37+
}, 3)
3938

4039
var x = new BigInteger('1').toBuffer(32)
4140
var h1 = Buffer.alloc(32)
@@ -44,24 +43,17 @@ describe('ecdsa', function () {
4443
assert.strictEqual(k.toString(), '42')
4544
}))
4645

47-
it('loops until a suitable signature is found', setupTest(function () {
48-
this.mock(BigInteger).expects('fromBuffer')
49-
.exactly(4)
50-
.onCall(0).returns(new BigInteger('0')) // < 1
51-
.onCall(1).returns(curve.n) // > n-1
52-
.onCall(2).returns(new BigInteger('42')) // valid, but 'bad' signature
53-
.onCall(3).returns(new BigInteger('53')) // valid, good signature
46+
it('loops until a suitable signature is found', hoodwink(function () {
47+
var checkSigStub = this.stub(function f () {
48+
if (f.calls === 0) return false // bad signature
49+
if (f.calls === 1) return true // good signature
50+
}, 2)
5451

55-
var mockCheckSig = this.mock()
56-
mockCheckSig.exactly(2)
57-
mockCheckSig.onCall(0).returns(false) // bad signature
58-
mockCheckSig.onCall(1).returns(true) // good signature
59-
60-
var x = new BigInteger('1').toBuffer(32)
52+
var x = BigInteger.ONE.toBuffer(32)
6153
var h1 = Buffer.alloc(32)
62-
var k = ecdsa.deterministicGenerateK(h1, x, mockCheckSig)
54+
var k = ecdsa.deterministicGenerateK(h1, x, checkSigStub)
6355

64-
assert.strictEqual(k.toString(), '53')
56+
assert.strictEqual(k.toHex(), 'a9b1a1a84a4c2f96b6158ed7a81404c50cb74373c22e8d9e02d0411d719acae2')
6557
}))
6658

6759
fixtures.valid.rfc6979.forEach(function (f) {

test/ecpair.js

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ var assert = require('assert')
55
var ecdsa = require('../src/ecdsa')
66
var ecurve = require('ecurve')
77
var proxyquire = require('proxyquire')
8-
var sinon = require('sinon')
9-
var sinonTest = require('sinon-test')
10-
var setupTest = sinonTest(sinon)
8+
var hoodwink = require('hoodwink')
119

1210
var BigInteger = require('bigi')
1311
var ECPair = require('../src/ecpair')
@@ -76,9 +74,10 @@ describe('ECPair', function () {
7674
keyPair = new ECPair(BigInteger.ONE)
7775
})
7876

79-
it('wraps Q.getEncoded', setupTest(function () {
80-
this.mock(keyPair.Q).expects('getEncoded')
81-
.once().withArgs(keyPair.compressed)
77+
it('wraps Q.getEncoded', hoodwink(function () {
78+
this.mock(keyPair.Q, 'getEncoded', function (compressed) {
79+
assert.strictEqual(compressed, keyPair.compressed)
80+
}, 1)
8281

8382
keyPair.getPublicKeyBuffer()
8483
}))
@@ -167,21 +166,31 @@ describe('ECPair', function () {
167166
assert.strictEqual(keyPair.network, NETWORKS.testnet)
168167
})
169168

170-
it('loops until d is within interval [1, n - 1] : 1', setupTest(function () {
171-
var rng = this.mock()
172-
rng.exactly(2)
173-
rng.onCall(0).returns(BigInteger.ZERO.toBuffer(32)) // invalid length
174-
rng.onCall(1).returns(BigInteger.ONE.toBuffer(32)) // === 1
169+
it('throws if d is bad length', function () {
170+
function rng () {
171+
return BigInteger.ZERO.toBuffer(28)
172+
}
173+
174+
assert.throws(function () {
175+
ECPair.makeRandom({ rng: rng })
176+
}, /Expected Buffer\(Length: 32\), got Buffer\(Length: 28\)/)
177+
})
178+
179+
it('loops until d is within interval [1, n - 1] : 1', hoodwink(function () {
180+
var rng = this.stub(function f () {
181+
if (f.calls === 0) return BigInteger.ZERO.toBuffer(32) // 0
182+
return BigInteger.ONE.toBuffer(32) // >0
183+
}, 2)
175184

176185
ECPair.makeRandom({ rng: rng })
177186
}))
178187

179-
it('loops until d is within interval [1, n - 1] : n - 1', setupTest(function () {
180-
var rng = this.mock()
181-
rng.exactly(3)
182-
rng.onCall(0).returns(BigInteger.ZERO.toBuffer(32)) // < 1
183-
rng.onCall(1).returns(curve.n.toBuffer(32)) // > n-1
184-
rng.onCall(2).returns(curve.n.subtract(BigInteger.ONE).toBuffer(32)) // === n-1
188+
it('loops until d is within interval [1, n - 1] : n - 1', hoodwink(function () {
189+
var rng = this.stub(function f () {
190+
if (f.calls === 0) return BigInteger.ZERO.toBuffer(32) // <1
191+
if (f.calls === 1) return curve.n.toBuffer(32) // >n-1
192+
return curve.n.subtract(BigInteger.ONE).toBuffer(32) // n-1
193+
}, 3)
185194

186195
ECPair.makeRandom({ rng: rng })
187196
}))
@@ -217,9 +226,11 @@ describe('ECPair', function () {
217226
})
218227

219228
describe('signing', function () {
220-
it('wraps ecdsa.sign', setupTest(function () {
221-
this.mock(ecdsa).expects('sign')
222-
.once().withArgs(hash, keyPair.d)
229+
it('wraps ecdsa.sign', hoodwink(function () {
230+
this.mock(ecdsa, 'sign', function (h, d) {
231+
assert.strictEqual(h, hash)
232+
assert.strictEqual(d, keyPair.d)
233+
}, 1)
223234

224235
keyPair.sign(hash)
225236
}))
@@ -240,9 +251,12 @@ describe('ECPair', function () {
240251
signature = keyPair.sign(hash)
241252
})
242253

243-
it('wraps ecdsa.verify', setupTest(function () {
244-
this.mock(ecdsa).expects('verify')
245-
.once().withArgs(hash, signature, keyPair.Q)
254+
it('wraps ecdsa.verify', hoodwink(function () {
255+
this.mock(ecdsa, 'verify', function (h, s, q) {
256+
assert.strictEqual(h, hash)
257+
assert.strictEqual(s, signature)
258+
assert.strictEqual(q, keyPair.Q)
259+
}, 1)
246260

247261
keyPair.verify(hash, signature)
248262
}))

test/hdnode.js

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33

44
var assert = require('assert')
55
var ecdsa = require('../src/ecdsa')
6-
var sinon = require('sinon')
7-
var sinonTest = require('sinon-test')
8-
var setupTest = sinonTest(sinon)
6+
var hoodwink = require('hoodwink')
97

108
var BigInteger = require('bigi')
119
var ECPair = require('../src/ecpair')
@@ -81,18 +79,20 @@ describe('HDNode', function () {
8179
})
8280
})
8381

84-
it('throws if IL is not within interval [1, n - 1] | IL === 0', setupTest(function () {
85-
this.mock(BigInteger).expects('fromBuffer')
86-
.once().returns(BigInteger.ZERO)
82+
it('throws if IL is not within interval [1, n - 1] | IL === 0', hoodwink(function () {
83+
this.mock(BigInteger, 'fromBuffer', function () {
84+
return BigInteger.ZERO
85+
}, 1)
8786

8887
assert.throws(function () {
8988
HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff')
9089
}, /Private key must be greater than 0/)
9190
}))
9291

93-
it('throws if IL is not within interval [1, n - 1] | IL === n', setupTest(function () {
94-
this.mock(BigInteger).expects('fromBuffer')
95-
.once().returns(curve.n)
92+
it('throws if IL is not within interval [1, n - 1] | IL === n', hoodwink(function () {
93+
this.mock(BigInteger, 'fromBuffer', function () {
94+
return curve.n
95+
}, 1)
9696

9797
assert.throws(function () {
9898
HDNode.fromSeedHex('ffffffffffffffffffffffffffffffff')
@@ -124,38 +124,43 @@ describe('HDNode', function () {
124124
})
125125

126126
describe('getAddress', function () {
127-
it('wraps keyPair.getAddress', setupTest(function () {
128-
this.mock(keyPair).expects('getAddress')
129-
.once().withArgs().returns('foobar')
127+
it('wraps keyPair.getAddress', hoodwink(function () {
128+
this.mock(hd.keyPair, 'getAddress', function () {
129+
return 'foo'
130+
}, 1)
130131

131-
assert.strictEqual(hd.getAddress(), 'foobar')
132+
assert.strictEqual(hd.getAddress(), 'foo')
132133
}))
133134
})
134135

135136
describe('getNetwork', function () {
136-
it('wraps keyPair.getNetwork', setupTest(function () {
137-
this.mock(keyPair).expects('getNetwork')
138-
.once().withArgs().returns('network')
137+
it('wraps keyPair.getNetwork', hoodwink(function () {
138+
this.mock(hd.keyPair, 'getNetwork', function () {
139+
return 'foo'
140+
}, 1)
139141

140-
assert.strictEqual(hd.getNetwork(), 'network')
142+
assert.strictEqual(hd.getNetwork(), 'foo')
141143
}))
142144
})
143145

144146
describe('getPublicKeyBuffer', function () {
145-
it('wraps keyPair.getPublicKeyBuffer', setupTest(function () {
146-
this.mock(keyPair).expects('getPublicKeyBuffer')
147-
.once().withArgs().returns('pubKeyBuffer')
147+
it('wraps keyPair.getPublicKeyBuffer', hoodwink(function () {
148+
this.mock(hd.keyPair, 'getPublicKeyBuffer', function () {
149+
return 'foo'
150+
}, 1)
148151

149-
assert.strictEqual(hd.getPublicKeyBuffer(), 'pubKeyBuffer')
152+
assert.strictEqual(hd.getPublicKeyBuffer(), 'foo')
150153
}))
151154
})
152155

153156
describe('sign', function () {
154-
it('wraps keyPair.sign', setupTest(function () {
155-
this.mock(keyPair).expects('sign')
156-
.once().withArgs(hash).returns('signed')
157+
it('wraps keyPair.sign', hoodwink(function () {
158+
this.mock(hd.keyPair, 'sign', function (h) {
159+
assert.strictEqual(hash, h)
160+
return 'foo'
161+
}, 1)
157162

158-
assert.strictEqual(hd.sign(hash), 'signed')
163+
assert.strictEqual(hd.sign(hash), 'foo')
159164
}))
160165
})
161166

@@ -166,11 +171,14 @@ describe('HDNode', function () {
166171
signature = hd.sign(hash)
167172
})
168173

169-
it('wraps keyPair.verify', setupTest(function () {
170-
this.mock(keyPair).expects('verify')
171-
.once().withArgs(hash, signature).returns('verified')
174+
it('wraps keyPair.verify', hoodwink(function () {
175+
this.mock(hd.keyPair, 'verify', function (h, s) {
176+
assert.strictEqual(hash, h)
177+
assert.strictEqual(signature, s)
178+
return 'foo'
179+
}, 1)
172180

173-
assert.strictEqual(hd.verify(hash, signature), 'verified')
181+
assert.strictEqual(hd.verify(hash, signature), 'foo')
174182
}))
175183
})
176184
})

0 commit comments

Comments
 (0)