Browse Source

Merge pull request #474 from bitcoinjs/hdkey

HDNode: add sign/verify
hk-custom-address
Daniel Cousens 9 years ago
parent
commit
c50d510aea
  1. 13
      CHANGELOG.md
  2. 4
      src/ecpair.js
  3. 16
      src/hdnode.js
  4. 11
      test/ecpair.js
  5. 80
      test/hdnode.js

13
CHANGELOG.md

@ -1,3 +1,16 @@
# 2.1.0
From this release users should use the HDNode directly (compared to accessing `.keyPair`) when performing ECDSA operations such as `sign` or `verify`.
Ideally you shoud not have to directly access `HDNode` internals for general usage, as it can often be confusing and error prone.
__added__
- `ECPair.prototype.getNetwork`
- `HDNode.prototype.getNetwork`, wraps the underyling keyPair's `getNetwork` method
- `HDNode.prototype.getPublicKeyBuffer`, wraps the underyling keyPair's `getPublicKeyBuffer` method
- `HDNode.prototype.sign`, wraps the underlying keyPair's `sign` method
- `HDNode.prototype.verify`, wraps the underlying keyPair's `verify` method
# 2.0.0
In this release we have strived to simplify the API, [using native types](https://github.com/bitcoinjs/bitcoinjs-lib/issues/407) wherevever possible to encourage cross-compatibility with other open source community modules.

4
src/ecpair.js

@ -105,6 +105,10 @@ ECPair.prototype.getAddress = function () {
return bs58check.encode(payload)
}
ECPair.prototype.getNetwork = function () {
return this.network
}
ECPair.prototype.getPublicKeyBuffer = function () {
return this.Q.getEncoded(this.compressed)
}

16
src/hdnode.js

@ -136,6 +136,14 @@ HDNode.prototype.getFingerprint = function () {
return this.getIdentifier().slice(0, 4)
}
HDNode.prototype.getNetwork = function () {
return this.keyPair.getNetwork()
}
HDNode.prototype.getPublicKeyBuffer = function () {
return this.keyPair.getPublicKeyBuffer()
}
HDNode.prototype.neutered = function () {
var neuteredKeyPair = new ECPair(null, this.keyPair.Q, {
network: this.keyPair.network
@ -149,6 +157,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')

11
test/ecpair.js

@ -167,6 +167,17 @@ describe('ECPair', function () {
})
})
describe('getNetwork', function () {
fixtures.valid.forEach(function (f) {
it('returns ' + f.network + ' for ' + f.WIF, function () {
var network = NETWORKS[f.network]
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
assert.strictEqual(keyPair.getNetwork(), network)
})
})
})
describe('ecdsa wrappers', function () {
var keyPair, hash

80
test/hdnode.js

@ -101,6 +101,69 @@ 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('getNetwork', function () {
it('wraps keyPair.getNetwork', sinon.test(function () {
this.mock(keyPair).expects('getNetwork')
.once().withArgs().returns('network')
assert.strictEqual(hd.getNetwork(), 'network')
}))
})
describe('getPublicKeyBuffer', function () {
it('wraps keyPair.getPublicKeyBuffer', sinon.test(function () {
this.mock(keyPair).expects('getPublicKeyBuffer')
.once().withArgs().returns('pubKeyBuffer')
assert.strictEqual(hd.getPublicKeyBuffer(), 'pubKeyBuffer')
}))
})
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 +236,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]

Loading…
Cancel
Save