From b6017b0faee731c59909968093b7d58ddbff5cf4 Mon Sep 17 00:00:00 2001 From: Daniel Cousens Date: Mon, 1 Dec 2014 10:43:52 +1100 Subject: [PATCH] HDNode: move isPrivate logic --- src/hdnode.js | 20 ++++++-------------- test/fixtures/hdnode.json | 4 ++-- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/hdnode.js b/src/hdnode.js index 9e41d1b..8979a9e 100644 --- a/src/hdnode.js +++ b/src/hdnode.js @@ -11,21 +11,18 @@ var ECPubKey = require('./ecpubkey') var ecurve = require('ecurve') var curve = ecurve.getCurveByName('secp256k1') -function findBIP32ParamsByVersion(version) { +function findBIP32NetworkByVersion(version) { for (var name in networks) { var network = networks[name] if (version === network.bip32.private || version === network.bip32.public) { - return { - isPrivate: (version === network.bip32.private), - network: network - } + return network } } - assert(false, 'Could not find version ' + version.toString(16)) + assert(false, 'Could not find network for ' + version.toString(16)) } function HDNode(K, chainCode, network) { @@ -88,18 +85,13 @@ HDNode.fromBuffer = function(buffer, network, __ignoreDeprecation) { // 4 byte: version bytes var version = buffer.readUInt32BE(0) - var isPrivate if (network) { assert(version === network.bip32.private || version === network.bip32.public, 'Network doesn\'t match') - isPrivate = (version === network.bip32.private) - // auto-detection + // auto-detect } else { - var params = findBIP32ParamsByVersion(version) - - isPrivate = params.isPrivate - network = params.network + network = findBIP32NetworkByVersion(version) } // 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ... @@ -121,7 +113,7 @@ HDNode.fromBuffer = function(buffer, network, __ignoreDeprecation) { var data, hd // 33 bytes: private key data (0x00 + k) - if (isPrivate) { + if (version === network.bip32.private) { assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key') data = buffer.slice(46, 78) var d = BigInteger.fromBuffer(data) diff --git a/test/fixtures/hdnode.json b/test/fixtures/hdnode.json index 77342d0..797f940 100644 --- a/test/fixtures/hdnode.json +++ b/test/fixtures/hdnode.json @@ -199,7 +199,7 @@ "string": "xprvQQQQQQQQQQQQQQQQCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334" }, { - "exception": "Could not find version 0", + "exception": "Could not find network for 0", "string": "1111111111111adADjFaSNPxwXqLjHLj4mBfYxuewDPbw9hEj1uaXCzMxRPXDFF3cUoezTFYom4sEmEVSQmENPPR315cFk9YUFVek73wE9" }, { @@ -226,7 +226,7 @@ "hex": "0488b21e0000000000ffffffff7ffc03d4a1f2fb41ef93374c69e4d19e42e27c9a87ec8b799a205eecd3b43b5f02948d03e260a571e21bcf5bfd8e3b6602800df154906e06b2bc88eee410aee355" }, { - "exception": "Could not find version 22222222", + "exception": "Could not find network for 22222222", "hex": "222222220000000000000000007ffc03d4a1f2fb41ef93374c69e4d19e42e27c9a87ec8b799a205eecd3b43b5f02948d03e260a571e21bcf5bfd8e3b6602800df154906e06b2bc88eee410aee355" }, {