Skip to content

Commit b3b2397

Browse files
committed
Merge pull request bitcoinjs#539 from runn1ng/patch-3
Adding some checks on deriving indexes
2 parents 75bd833 + 4a72001 commit b3b2397

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

src/hdnode.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ HDNode.prototype.toBase58 = function (__isPrivate) {
206206

207207
// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#child-key-derivation-ckd-functions
208208
HDNode.prototype.derive = function (index) {
209+
typeforce(types.UInt32, index)
210+
209211
var isHardened = index >= HDNode.HIGHEST_BIT
210212
var data = new Buffer(37)
211213

@@ -277,6 +279,8 @@ HDNode.prototype.derive = function (index) {
277279
}
278280

279281
HDNode.prototype.deriveHardened = function (index) {
282+
typeforce(types.UInt31, index)
283+
280284
// Only derives hardened private keys by default
281285
return this.derive(index + HDNode.HIGHEST_BIT)
282286
}

src/types.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ function Hash256bit (value) { return nBuffer(value, 32) }
1212
function Buffer256bit (value) { return nBuffer(value, 32) }
1313

1414
var UINT53_MAX = Math.pow(2, 53) - 1
15+
var UINT31_MAX = Math.pow(2, 31) - 1
1516
function UInt2 (value) { return (value & 3) === value }
1617
function UInt8 (value) { return (value & 0xff) === value }
1718
function UInt32 (value) { return (value >>> 0) === value }
19+
function UInt31 (value) {
20+
return UInt32(value) && value <= UINT31_MAX
21+
}
1822
function UInt53 (value) {
1923
return typeforce.Number(value) &&
2024
value >= 0 &&
@@ -51,6 +55,7 @@ var types = {
5155
Network: Network,
5256
UInt2: UInt2,
5357
UInt8: UInt8,
58+
UInt31: UInt31,
5459
UInt32: UInt32,
5560
UInt53: UInt53
5661
}

test/hdnode.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,5 +327,47 @@ describe('HDNode', function () {
327327
master.deriveHardened(c.m)
328328
}, /Could not derive hardened child key/)
329329
})
330+
331+
it('throws on negative indexes', function () {
332+
var f = fixtures.valid[0]
333+
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
334+
335+
assert.throws(function () {
336+
master.deriveHardened(-1)
337+
}, /Expected UInt31/)
338+
assert.throws(function () {
339+
master.derive(-1)
340+
}, /Expected UInt32/)
341+
})
342+
343+
it('throws on high indexes', function () {
344+
var f = fixtures.valid[0]
345+
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
346+
347+
assert.throws(function () {
348+
master.deriveHardened(0x80000000)
349+
}, /Expected UInt31/)
350+
assert.throws(function () {
351+
master.derive(0x100000000)
352+
}, /Expected UInt32/)
353+
})
354+
355+
it('throws on non-numbers', function () {
356+
var f = fixtures.valid[0]
357+
var master = HDNode.fromBase58(f.master.base58, NETWORKS_LIST)
358+
359+
assert.throws(function () {
360+
master.deriveHardened()
361+
}, /Expected UInt31/)
362+
assert.throws(function () {
363+
master.derive()
364+
}, /Expected UInt32/)
365+
assert.throws(function () {
366+
master.deriveHardened('foo')
367+
}, /Expected UInt31/)
368+
assert.throws(function () {
369+
master.derive('foo')
370+
}, /Expected UInt32/)
371+
})
330372
})
331373
})

0 commit comments

Comments
 (0)