add hoodwink dependency

This commit is contained in:
Daniel Cousens 2018-03-20 14:49:10 +11:00
parent 7c9c6ed204
commit 04c628e9f1
5 changed files with 18 additions and 83 deletions

View file

@ -51,6 +51,7 @@
"bip65": "^1.0.1",
"bs58": "^4.0.0",
"dhttp": "^2.4.2",
"hoodwink": "^1.0.0",
"minimaldata": "^1.0.2",
"mocha": "^5.0.1",
"nyc": "^11.4.1",

View file

@ -3,7 +3,7 @@
var assert = require('assert')
var bcrypto = require('../src/crypto')
var ecdsa = require('../src/ecdsa')
var mockme = require('./mockme')
var hoodwink = require('hoodwink')
var BigInteger = require('bigi')
var ECSignature = require('../src/ecsignature')
@ -28,7 +28,7 @@ describe('ecdsa', function () {
})
})
it('loops until an appropriate k value is found', mockme(function () {
it('loops until an appropriate k value is found', hoodwink(function () {
this.mock(BigInteger, 'fromBuffer', function f (b) {
assert.strictEqual(b.length, 32)
if (f.calls === 0) return BigInteger.ZERO // < 1
@ -43,7 +43,7 @@ describe('ecdsa', function () {
assert.strictEqual(k.toString(), '42')
}))
it('loops until a suitable signature is found', mockme(function () {
it('loops until a suitable signature is found', hoodwink(function () {
var checkSigStub = this.stub(function f () {
if (f.calls === 0) return false // bad signature
if (f.calls === 1) return true // good signature

View file

@ -5,7 +5,7 @@ var assert = require('assert')
var ecdsa = require('../src/ecdsa')
var ecurve = require('ecurve')
var proxyquire = require('proxyquire')
var mockme = require('./mockme')
var hoodwink = require('hoodwink')
var BigInteger = require('bigi')
var ECPair = require('../src/ecpair')
@ -74,7 +74,7 @@ describe('ECPair', function () {
keyPair = new ECPair(BigInteger.ONE)
})
it('wraps Q.getEncoded', mockme(function () {
it('wraps Q.getEncoded', hoodwink(function () {
this.mock(keyPair.Q, 'getEncoded', function (compressed) {
assert.strictEqual(compressed, keyPair.compressed)
}, 1)
@ -176,7 +176,7 @@ describe('ECPair', function () {
}, /Expected Buffer\(Length: 32\), got Buffer\(Length: 28\)/)
})
it('loops until d is within interval [1, n - 1] : 1', mockme(function () {
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
@ -185,7 +185,7 @@ describe('ECPair', function () {
ECPair.makeRandom({ rng: rng })
}))
it('loops until d is within interval [1, n - 1] : n - 1', mockme(function () {
it('loops until d is within interval [1, n - 1] : n - 1', hoodwink(function () {
var rng = this.stub(function f () {
if (f.calls === 0) return BigInteger.ZERO.toBuffer(32) // <1
if (f.calls === 1) return curve.n.toBuffer(32) // >n-1
@ -226,7 +226,7 @@ describe('ECPair', function () {
})
describe('signing', function () {
it('wraps ecdsa.sign', mockme(function () {
it('wraps ecdsa.sign', hoodwink(function () {
this.mock(ecdsa, 'sign', function (h, d) {
assert.strictEqual(h, hash)
assert.strictEqual(d, keyPair.d)
@ -251,7 +251,7 @@ describe('ECPair', function () {
signature = keyPair.sign(hash)
})
it('wraps ecdsa.verify', mockme(function () {
it('wraps ecdsa.verify', hoodwink(function () {
this.mock(ecdsa, 'verify', function (h, s, q) {
assert.strictEqual(h, hash)
assert.strictEqual(s, signature)

View file

@ -3,7 +3,7 @@
var assert = require('assert')
var ecdsa = require('../src/ecdsa')
var mockme = require('./mockme')
var hoodwink = require('hoodwink')
var BigInteger = require('bigi')
var ECPair = require('../src/ecpair')
@ -79,7 +79,7 @@ describe('HDNode', function () {
})
})
it('throws if IL is not within interval [1, n - 1] | IL === 0', mockme(function () {
it('throws if IL is not within interval [1, n - 1] | IL === 0', hoodwink(function () {
this.mock(BigInteger, 'fromBuffer', function () {
return BigInteger.ZERO
}, 1)
@ -89,7 +89,7 @@ describe('HDNode', function () {
}, /Private key must be greater than 0/)
}))
it('throws if IL is not within interval [1, n - 1] | IL === n', mockme(function () {
it('throws if IL is not within interval [1, n - 1] | IL === n', hoodwink(function () {
this.mock(BigInteger, 'fromBuffer', function () {
return curve.n
}, 1)
@ -124,7 +124,7 @@ describe('HDNode', function () {
})
describe('getAddress', function () {
it('wraps keyPair.getAddress', mockme(function () {
it('wraps keyPair.getAddress', hoodwink(function () {
this.mock(hd.keyPair, 'getAddress', function () {
return 'foo'
}, 1)
@ -134,7 +134,7 @@ describe('HDNode', function () {
})
describe('getNetwork', function () {
it('wraps keyPair.getNetwork', mockme(function () {
it('wraps keyPair.getNetwork', hoodwink(function () {
this.mock(hd.keyPair, 'getNetwork', function () {
return 'foo'
}, 1)
@ -144,7 +144,7 @@ describe('HDNode', function () {
})
describe('getPublicKeyBuffer', function () {
it('wraps keyPair.getPublicKeyBuffer', mockme(function () {
it('wraps keyPair.getPublicKeyBuffer', hoodwink(function () {
this.mock(hd.keyPair, 'getPublicKeyBuffer', function () {
return 'foo'
}, 1)
@ -154,7 +154,7 @@ describe('HDNode', function () {
})
describe('sign', function () {
it('wraps keyPair.sign', mockme(function () {
it('wraps keyPair.sign', hoodwink(function () {
this.mock(hd.keyPair, 'sign', function (h) {
assert.strictEqual(hash, h)
return 'foo'
@ -171,7 +171,7 @@ describe('HDNode', function () {
signature = hd.sign(hash)
})
it('wraps keyPair.verify', mockme(function () {
it('wraps keyPair.verify', hoodwink(function () {
this.mock(hd.keyPair, 'verify', function (h, s) {
assert.strictEqual(hash, h)
assert.strictEqual(signature, s)

View file

@ -1,66 +0,0 @@
// TODO: move to own dependency
function mockme (f) {
var mocks = []
function mock (constructor, functionName, func, n) {
n = n || Infinity
var initial = constructor[functionName]
var context = constructor.constructor.name !== 'Function' ? constructor : null
function __mock () {
if (func.calls > n) throw new RangeError('Exceeded expected number of calls')
var r = func.apply(context, arguments)
++func.calls
return r
}
func.calls = 0
func.expected = n
func.reset = function reset () {
constructor[functionName] = initial
}
constructor[functionName] = __mock
mocks.push(func)
}
function stub (func, n) {
n = n || Infinity
function __stub () {
if (func.calls > n) throw new RangeError('Exceeded expected number of calls')
var r = func.apply(null, arguments)
++func.calls
return r
}
func.calls = 0
func.expected = n
mocks.push(func)
return __stub
}
return function run () {
var err
try {
f.apply({
mock: mock,
stub: stub
}, arguments)
} catch (e) {
err = e
}
mocks.forEach(function (x) {
if (!err) {
if (x.expected !== Infinity && x.calls !== x.expected) {
err = new RangeError('Too few calls')
}
}
if (x.reset) x.reset()
})
if (err) throw err
}
}
module.exports = mockme