HDNode: move isPrivate logic

This commit is contained in:
Daniel Cousens 2014-12-01 10:43:52 +11:00
parent 1ac79b8327
commit b6017b0fae
2 changed files with 8 additions and 16 deletions

View file

@ -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)

View file

@ -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"
},
{