Browse Source

HDNode: move isPrivate logic

hk-custom-address
Daniel Cousens 10 years ago
parent
commit
b6017b0fae
  1. 20
      src/hdnode.js
  2. 4
      test/fixtures/hdnode.json

20
src/hdnode.js

@ -11,21 +11,18 @@ var ECPubKey = require('./ecpubkey')
var ecurve = require('ecurve') var ecurve = require('ecurve')
var curve = ecurve.getCurveByName('secp256k1') var curve = ecurve.getCurveByName('secp256k1')
function findBIP32ParamsByVersion(version) { function findBIP32NetworkByVersion(version) {
for (var name in networks) { for (var name in networks) {
var network = networks[name] var network = networks[name]
if (version === network.bip32.private || if (version === network.bip32.private ||
version === network.bip32.public) { version === network.bip32.public) {
return { return network
isPrivate: (version === network.bip32.private),
network: 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) { function HDNode(K, chainCode, network) {
@ -88,18 +85,13 @@ HDNode.fromBuffer = function(buffer, network, __ignoreDeprecation) {
// 4 byte: version bytes // 4 byte: version bytes
var version = buffer.readUInt32BE(0) var version = buffer.readUInt32BE(0)
var isPrivate
if (network) { if (network) {
assert(version === network.bip32.private || version === network.bip32.public, 'Network doesn\'t match') assert(version === network.bip32.private || version === network.bip32.public, 'Network doesn\'t match')
isPrivate = (version === network.bip32.private)
// auto-detection // auto-detect
} else { } else {
var params = findBIP32ParamsByVersion(version) network = findBIP32NetworkByVersion(version)
isPrivate = params.isPrivate
network = params.network
} }
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ... // 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 var data, hd
// 33 bytes: private key data (0x00 + k) // 33 bytes: private key data (0x00 + k)
if (isPrivate) { if (version === network.bip32.private) {
assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key') assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key')
data = buffer.slice(46, 78) data = buffer.slice(46, 78)
var d = BigInteger.fromBuffer(data) var d = BigInteger.fromBuffer(data)

4
test/fixtures/hdnode.json

@ -199,7 +199,7 @@
"string": "xprvQQQQQQQQQQQQQQQQCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334" "string": "xprvQQQQQQQQQQQQQQQQCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334"
}, },
{ {
"exception": "Could not find version 0", "exception": "Could not find network for 0",
"string": "1111111111111adADjFaSNPxwXqLjHLj4mBfYxuewDPbw9hEj1uaXCzMxRPXDFF3cUoezTFYom4sEmEVSQmENPPR315cFk9YUFVek73wE9" "string": "1111111111111adADjFaSNPxwXqLjHLj4mBfYxuewDPbw9hEj1uaXCzMxRPXDFF3cUoezTFYom4sEmEVSQmENPPR315cFk9YUFVek73wE9"
}, },
{ {
@ -226,7 +226,7 @@
"hex": "0488b21e0000000000ffffffff7ffc03d4a1f2fb41ef93374c69e4d19e42e27c9a87ec8b799a205eecd3b43b5f02948d03e260a571e21bcf5bfd8e3b6602800df154906e06b2bc88eee410aee355" "hex": "0488b21e0000000000ffffffff7ffc03d4a1f2fb41ef93374c69e4d19e42e27c9a87ec8b799a205eecd3b43b5f02948d03e260a571e21bcf5bfd8e3b6602800df154906e06b2bc88eee410aee355"
}, },
{ {
"exception": "Could not find version 22222222", "exception": "Could not find network for 22222222",
"hex": "222222220000000000000000007ffc03d4a1f2fb41ef93374c69e4d19e42e27c9a87ec8b799a205eecd3b43b5f02948d03e260a571e21bcf5bfd8e3b6602800df154906e06b2bc88eee410aee355" "hex": "222222220000000000000000007ffc03d4a1f2fb41ef93374c69e4d19e42e27c9a87ec8b799a205eecd3b43b5f02948d03e260a571e21bcf5bfd8e3b6602800df154906e06b2bc88eee410aee355"
}, },
{ {

Loading…
Cancel
Save