Skip to content

Commit c50d510

Browse files
committed
Merge pull request bitcoinjs#474 from bitcoinjs/hdkey
HDNode: add sign/verify
2 parents d1abddb + b048ccb commit c50d510

File tree

5 files changed

+107
-17
lines changed

5 files changed

+107
-17
lines changed

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
# 2.1.0
2+
3+
From this release users should use the HDNode directly (compared to accessing `.keyPair`) when performing ECDSA operations such as `sign` or `verify`.
4+
Ideally you shoud not have to directly access `HDNode` internals for general usage, as it can often be confusing and error prone.
5+
6+
__added__
7+
- `ECPair.prototype.getNetwork`
8+
- `HDNode.prototype.getNetwork`, wraps the underyling keyPair's `getNetwork` method
9+
- `HDNode.prototype.getPublicKeyBuffer`, wraps the underyling keyPair's `getPublicKeyBuffer` method
10+
- `HDNode.prototype.sign`, wraps the underlying keyPair's `sign` method
11+
- `HDNode.prototype.verify`, wraps the underlying keyPair's `verify` method
12+
13+
114
# 2.0.0
215

316
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.

src/ecpair.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ ECPair.prototype.getAddress = function () {
105105
return bs58check.encode(payload)
106106
}
107107

108+
ECPair.prototype.getNetwork = function () {
109+
return this.network
110+
}
111+
108112
ECPair.prototype.getPublicKeyBuffer = function () {
109113
return this.Q.getEncoded(this.compressed)
110114
}

src/hdnode.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,14 @@ HDNode.prototype.getFingerprint = function () {
136136
return this.getIdentifier().slice(0, 4)
137137
}
138138

139+
HDNode.prototype.getNetwork = function () {
140+
return this.keyPair.getNetwork()
141+
}
142+
143+
HDNode.prototype.getPublicKeyBuffer = function () {
144+
return this.keyPair.getPublicKeyBuffer()
145+
}
146+
139147
HDNode.prototype.neutered = function () {
140148
var neuteredKeyPair = new ECPair(null, this.keyPair.Q, {
141149
network: this.keyPair.network
@@ -149,6 +157,14 @@ HDNode.prototype.neutered = function () {
149157
return neutered
150158
}
151159

160+
HDNode.prototype.sign = function (hash) {
161+
return this.keyPair.sign(hash)
162+
}
163+
164+
HDNode.prototype.verify = function (hash, signature) {
165+
return this.keyPair.verify(hash, signature)
166+
}
167+
152168
HDNode.prototype.toBase58 = function (__isPrivate) {
153169
if (__isPrivate !== undefined) throw new TypeError('Unsupported argument in 2.0.0')
154170

test/ecpair.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ describe('ECPair', function () {
167167
})
168168
})
169169

170+
describe('getNetwork', function () {
171+
fixtures.valid.forEach(function (f) {
172+
it('returns ' + f.network + ' for ' + f.WIF, function () {
173+
var network = NETWORKS[f.network]
174+
var keyPair = ECPair.fromWIF(f.WIF, NETWORKS_LIST)
175+
176+
assert.strictEqual(keyPair.getNetwork(), network)
177+
})
178+
})
179+
})
180+
170181
describe('ecdsa wrappers', function () {
171182
var keyPair, hash
172183

test/hdnode.js

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,69 @@ describe('HDNode', function () {
101101
})
102102
})
103103

104+
describe('ECPair wrappers', function () {
105+
var keyPair, hd, hash
106+
107+
beforeEach(function () {
108+
keyPair = ECPair.makeRandom()
109+
hash = new Buffer(32)
110+
111+
var chainCode = new Buffer(32)
112+
hd = new HDNode(keyPair, chainCode)
113+
})
114+
115+
describe('getAddress', function () {
116+
it('wraps keyPair.getAddress', sinon.test(function () {
117+
this.mock(keyPair).expects('getAddress')
118+
.once().withArgs().returns('foobar')
119+
120+
assert.strictEqual(hd.getAddress(), 'foobar')
121+
}))
122+
})
123+
124+
describe('getNetwork', function () {
125+
it('wraps keyPair.getNetwork', sinon.test(function () {
126+
this.mock(keyPair).expects('getNetwork')
127+
.once().withArgs().returns('network')
128+
129+
assert.strictEqual(hd.getNetwork(), 'network')
130+
}))
131+
})
132+
133+
describe('getPublicKeyBuffer', function () {
134+
it('wraps keyPair.getPublicKeyBuffer', sinon.test(function () {
135+
this.mock(keyPair).expects('getPublicKeyBuffer')
136+
.once().withArgs().returns('pubKeyBuffer')
137+
138+
assert.strictEqual(hd.getPublicKeyBuffer(), 'pubKeyBuffer')
139+
}))
140+
})
141+
142+
describe('sign', function () {
143+
it('wraps keyPair.sign', sinon.test(function () {
144+
this.mock(keyPair).expects('sign')
145+
.once().withArgs(hash).returns('signed')
146+
147+
assert.strictEqual(hd.sign(hash), 'signed')
148+
}))
149+
})
150+
151+
describe('verify', function () {
152+
var signature
153+
154+
beforeEach(function () {
155+
signature = hd.sign(hash)
156+
})
157+
158+
it('wraps keyPair.verify', sinon.test(function () {
159+
this.mock(keyPair).expects('verify')
160+
.once().withArgs(hash, signature).returns('verified')
161+
162+
assert.strictEqual(hd.verify(hash, signature), 'verified')
163+
}))
164+
})
165+
})
166+
104167
describe('toBase58', function () {
105168
fixtures.valid.forEach(function (f) {
106169
it('exports ' + f.master.base58 + ' (public) correctly', function () {
@@ -173,23 +236,6 @@ describe('HDNode', function () {
173236
})
174237
})
175238

176-
describe('getAddress', function () {
177-
var hd
178-
179-
beforeEach(function () {
180-
var f = fixtures.valid[0]
181-
182-
hd = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
183-
})
184-
185-
it('wraps ECPair.getAddress', sinon.test(function () {
186-
this.mock(hd.keyPair).expects('getAddress')
187-
.once().returns('foobar')
188-
189-
assert.strictEqual(hd.getAddress(), 'foobar')
190-
}))
191-
})
192-
193239
describe('neutered', function () {
194240
var f = fixtures.valid[0]
195241

0 commit comments

Comments
 (0)