HDNode: add sign/verify

This commit is contained in:
Daniel Cousens 2015-09-21 16:34:25 +10:00
parent d1abddb3de
commit 923266672b
2 changed files with 53 additions and 17 deletions

View file

@ -149,6 +149,14 @@ HDNode.prototype.neutered = function () {
return neutered
}
HDNode.prototype.sign = function (hash) {
return this.keyPair.sign(hash)
}
HDNode.prototype.verify = function (hash, signature) {
return this.keyPair.verify(hash, signature)
}
HDNode.prototype.toBase58 = function (__isPrivate) {
if (__isPrivate !== undefined) throw new TypeError('Unsupported argument in 2.0.0')

View file

@ -101,6 +101,51 @@ describe('HDNode', function () {
})
})
describe('ECPair wrappers', function () {
var keyPair, hd, hash
beforeEach(function () {
keyPair = ECPair.makeRandom()
hash = new Buffer(32)
var chainCode = new Buffer(32)
hd = new HDNode(keyPair, chainCode)
})
describe('getAddress', function () {
it('wraps keyPair.getAddress', sinon.test(function () {
this.mock(keyPair).expects('getAddress')
.once().withArgs().returns('foobar')
assert.strictEqual(hd.getAddress(), 'foobar')
}))
})
describe('sign', function () {
it('wraps keyPair.sign', sinon.test(function () {
this.mock(keyPair).expects('sign')
.once().withArgs(hash).returns('signed')
assert.strictEqual(hd.sign(hash), 'signed')
}))
})
describe('verify', function () {
var signature
beforeEach(function () {
signature = hd.sign(hash)
})
it('wraps keyPair.verify', sinon.test(function () {
this.mock(keyPair).expects('verify')
.once().withArgs(hash, signature).returns('verified')
assert.strictEqual(hd.verify(hash, signature), 'verified')
}))
})
})
describe('toBase58', function () {
fixtures.valid.forEach(function (f) {
it('exports ' + f.master.base58 + ' (public) correctly', function () {
@ -173,23 +218,6 @@ describe('HDNode', function () {
})
})
describe('getAddress', function () {
var hd
beforeEach(function () {
var f = fixtures.valid[0]
hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
})
it('wraps ECPair.getAddress', sinon.test(function () {
this.mock(hd.keyPair).expects('getAddress')
.once().returns('foobar')
assert.strictEqual(hd.getAddress(), 'foobar')
}))
})
describe('neutered', function () {
var f = fixtures.valid[0]