Skip to content

Commit 5d2abb5

Browse files
committed
HDNode: use typeforce.tuple for arguments
1 parent eb752f8 commit 5d2abb5

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

src/hdnode.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var base58check = require('bs58check')
22
var bcrypto = require('./crypto')
33
var createHmac = require('create-hmac')
4-
var typeForce = require('typeforce')
4+
var typeforce = require('typeforce')
5+
var types = require('./types')
56
var NETWORKS = require('./networks')
67

78
var BigInteger = require('bigi')
@@ -11,11 +12,8 @@ var ecurve = require('ecurve')
1112
var curve = ecurve.getCurveByName('secp256k1')
1213

1314
function HDNode (keyPair, chainCode) {
14-
typeForce('ECPair', keyPair)
15-
typeForce('Buffer', chainCode)
15+
typeforce(types.tuple('ECPair', types.Buffer256bit), arguments)
1616

17-
if (chainCode.length !== 32) throw new TypeError('Expected chainCode length of 32, got ' + chainCode.length)
18-
if (!keyPair.network.bip32) throw new TypeError('Unknown BIP32 constants for network')
1917
if (!keyPair.compressed) throw new TypeError('BIP32 only allows compressed keyPairs')
2018

2119
this.keyPair = keyPair
@@ -30,7 +28,7 @@ HDNode.HIGHEST_BIT = 0x80000000
3028
HDNode.LENGTH = 78
3129

3230
HDNode.fromSeedBuffer = function (seed, network) {
33-
typeForce('Buffer', seed)
31+
typeforce(types.tuple(types.Buffer, types.maybe(types.Network)), arguments)
3432

3533
if (seed.length < 16) throw new TypeError('Seed should be at least 128 bits')
3634
if (seed.length > 64) throw new TypeError('Seed should be at most 512 bits')

test/hdnode.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,8 @@ describe('HDNode', function () {
5252

5353
it('throws when an invalid length chain code is given', function () {
5454
assert.throws(function () {
55-
new HDNode(keyPair, chainCode.slice(0, 20))
56-
}, /Expected chainCode length of 32, got 20/)
57-
})
58-
59-
it('throws when an unknown network is given', function () {
60-
keyPair.network = {}
61-
62-
assert.throws(function () {
63-
new HDNode(keyPair, chainCode)
64-
}, /Unknown BIP32 constants for network/)
55+
new HDNode(keyPair, new Buffer(20))
56+
}, /Expected 256-bit Buffer, got 160-bit/)
6557
})
6658
})
6759

0 commit comments

Comments
 (0)