Skip to content
This repository was archived by the owner on Jun 3, 2020. It is now read-only.

Commit b6017b0

Browse files
committed
HDNode: move isPrivate logic
1 parent 1ac79b8 commit b6017b0

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

src/hdnode.js

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,18 @@ var ECPubKey = require('./ecpubkey')
1111
var ecurve = require('ecurve')
1212
var curve = ecurve.getCurveByName('secp256k1')
1313

14-
function findBIP32ParamsByVersion(version) {
14+
function findBIP32NetworkByVersion(version) {
1515
for (var name in networks) {
1616
var network = networks[name]
1717

1818
if (version === network.bip32.private ||
1919
version === network.bip32.public) {
2020

21-
return {
22-
isPrivate: (version === network.bip32.private),
23-
network: network
24-
}
21+
return network
2522
}
2623
}
2724

28-
assert(false, 'Could not find version ' + version.toString(16))
25+
assert(false, 'Could not find network for ' + version.toString(16))
2926
}
3027

3128
function HDNode(K, chainCode, network) {
@@ -88,18 +85,13 @@ HDNode.fromBuffer = function(buffer, network, __ignoreDeprecation) {
8885

8986
// 4 byte: version bytes
9087
var version = buffer.readUInt32BE(0)
91-
var isPrivate
9288

9389
if (network) {
9490
assert(version === network.bip32.private || version === network.bip32.public, 'Network doesn\'t match')
95-
isPrivate = (version === network.bip32.private)
9691

97-
// auto-detection
92+
// auto-detect
9893
} else {
99-
var params = findBIP32ParamsByVersion(version)
100-
101-
isPrivate = params.isPrivate
102-
network = params.network
94+
network = findBIP32NetworkByVersion(version)
10395
}
10496

10597
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ...
@@ -121,7 +113,7 @@ HDNode.fromBuffer = function(buffer, network, __ignoreDeprecation) {
121113
var data, hd
122114

123115
// 33 bytes: private key data (0x00 + k)
124-
if (isPrivate) {
116+
if (version === network.bip32.private) {
125117
assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key')
126118
data = buffer.slice(46, 78)
127119
var d = BigInteger.fromBuffer(data)

test/fixtures/hdnode.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@
199199
"string": "xprvQQQQQQQQQQQQQQQQCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334"
200200
},
201201
{
202-
"exception": "Could not find version 0",
202+
"exception": "Could not find network for 0",
203203
"string": "1111111111111adADjFaSNPxwXqLjHLj4mBfYxuewDPbw9hEj1uaXCzMxRPXDFF3cUoezTFYom4sEmEVSQmENPPR315cFk9YUFVek73wE9"
204204
},
205205
{
@@ -226,7 +226,7 @@
226226
"hex": "0488b21e0000000000ffffffff7ffc03d4a1f2fb41ef93374c69e4d19e42e27c9a87ec8b799a205eecd3b43b5f02948d03e260a571e21bcf5bfd8e3b6602800df154906e06b2bc88eee410aee355"
227227
},
228228
{
229-
"exception": "Could not find version 22222222",
229+
"exception": "Could not find network for 22222222",
230230
"hex": "222222220000000000000000007ffc03d4a1f2fb41ef93374c69e4d19e42e27c9a87ec8b799a205eecd3b43b5f02948d03e260a571e21bcf5bfd8e3b6602800df154906e06b2bc88eee410aee355"
231231
},
232232
{

0 commit comments

Comments
 (0)