HDNode: use typeforce.tuple for arguments

This commit is contained in:
Daniel Cousens 2015-08-18 14:21:38 +10:00
parent eb752f8316
commit 5d2abb523e
2 changed files with 6 additions and 16 deletions

View file

@ -1,7 +1,8 @@
var base58check = require('bs58check') var base58check = require('bs58check')
var bcrypto = require('./crypto') var bcrypto = require('./crypto')
var createHmac = require('create-hmac') var createHmac = require('create-hmac')
var typeForce = require('typeforce') var typeforce = require('typeforce')
var types = require('./types')
var NETWORKS = require('./networks') var NETWORKS = require('./networks')
var BigInteger = require('bigi') var BigInteger = require('bigi')
@ -11,11 +12,8 @@ var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1') var curve = ecurve.getCurveByName('secp256k1')
function HDNode (keyPair, chainCode) { function HDNode (keyPair, chainCode) {
typeForce('ECPair', keyPair) typeforce(types.tuple('ECPair', types.Buffer256bit), arguments)
typeForce('Buffer', chainCode)
if (chainCode.length !== 32) throw new TypeError('Expected chainCode length of 32, got ' + chainCode.length)
if (!keyPair.network.bip32) throw new TypeError('Unknown BIP32 constants for network')
if (!keyPair.compressed) throw new TypeError('BIP32 only allows compressed keyPairs') if (!keyPair.compressed) throw new TypeError('BIP32 only allows compressed keyPairs')
this.keyPair = keyPair this.keyPair = keyPair
@ -30,7 +28,7 @@ HDNode.HIGHEST_BIT = 0x80000000
HDNode.LENGTH = 78 HDNode.LENGTH = 78
HDNode.fromSeedBuffer = function (seed, network) { HDNode.fromSeedBuffer = function (seed, network) {
typeForce('Buffer', seed) typeforce(types.tuple(types.Buffer, types.maybe(types.Network)), arguments)
if (seed.length < 16) throw new TypeError('Seed should be at least 128 bits') if (seed.length < 16) throw new TypeError('Seed should be at least 128 bits')
if (seed.length > 64) throw new TypeError('Seed should be at most 512 bits') if (seed.length > 64) throw new TypeError('Seed should be at most 512 bits')

View file

@ -52,16 +52,8 @@ describe('HDNode', function () {
it('throws when an invalid length chain code is given', function () { it('throws when an invalid length chain code is given', function () {
assert.throws(function () { assert.throws(function () {
new HDNode(keyPair, chainCode.slice(0, 20)) new HDNode(keyPair, new Buffer(20))
}, /Expected chainCode length of 32, got 20/) }, /Expected 256-bit Buffer, got 160-bit/)
})
it('throws when an unknown network is given', function () {
keyPair.network = {}
assert.throws(function () {
new HDNode(keyPair, chainCode)
}, /Unknown BIP32 constants for network/)
}) })
}) })