HDNode: add optional network flag to import functions

This commit is contained in:
Daniel Cousens 2014-11-29 12:04:02 +11:00
parent 4fca3ac196
commit 311df7e406
4 changed files with 75 additions and 34 deletions

View file

@ -74,12 +74,12 @@ HDNode.fromSeedHex = function(hex, network) {
return HDNode.fromSeedBuffer(new Buffer(hex, 'hex'), network)
}
HDNode.fromBase58 = function(string) {
return HDNode.fromBuffer(base58check.decode(string), true)
HDNode.fromBase58 = function(string, network) {
return HDNode.fromBuffer(base58check.decode(string), network, true)
}
// FIXME: remove in 2.x.y
HDNode.fromBuffer = function(buffer, __ignoreDeprecation) {
HDNode.fromBuffer = function(buffer, network, __ignoreDeprecation) {
if (!__ignoreDeprecation) {
console.warn('HDNode.fromBuffer() is deprecated for removal in 2.x.y, use fromBase58 instead')
}
@ -88,7 +88,19 @@ HDNode.fromBuffer = function(buffer, __ignoreDeprecation) {
// 4 byte: version bytes
var version = buffer.readUInt32BE(0)
var params = findBIP32ParamsByVersion(version)
var isPrivate
if (network) {
assert(version === network.bip32.private || version === network.bip32.public, 'Network doesn\'t match')
isPrivate = (version === network.bip32.private)
// auto-detection
} else {
var params = findBIP32ParamsByVersion(version)
isPrivate = params.isPrivate
network = params.network
}
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ...
var depth = buffer.readUInt8(4)
@ -109,11 +121,11 @@ HDNode.fromBuffer = function(buffer, __ignoreDeprecation) {
var data, hd
// 33 bytes: private key data (0x00 + k)
if (params.isPrivate) {
if (isPrivate) {
assert.strictEqual(buffer.readUInt8(45), 0x00, 'Invalid private key')
data = buffer.slice(46, 78)
var d = BigInteger.fromBuffer(data)
hd = new HDNode(d, chainCode, params.network)
hd = new HDNode(d, chainCode, network)
// 33 bytes: public key data (0x02 + X or 0x03 + X)
} else {
@ -125,7 +137,7 @@ HDNode.fromBuffer = function(buffer, __ignoreDeprecation) {
// If not, the extended public key is invalid.
curve.validate(Q)
hd = new HDNode(Q, chainCode, params.network)
hd = new HDNode(Q, chainCode, network)
}
hd.depth = depth
@ -136,8 +148,8 @@ HDNode.fromBuffer = function(buffer, __ignoreDeprecation) {
}
// FIXME: remove in 2.x.y
HDNode.fromHex = function(hex) {
return HDNode.fromBuffer(new Buffer(hex, 'hex'))
HDNode.fromHex = function(hex, network) {
return HDNode.fromBuffer(new Buffer(hex, 'hex'), network)
}
HDNode.prototype.getIdentifier = function() {

View file

@ -1,6 +1,7 @@
{
"valid": [
{
"network": "bitcoin",
"master": {
"seed": "000102030405060708090a0b0c0d0e0f",
"wif": "L52XzL2cMkHxqxBXRyEpnPQZGUs3uKiL3R11XbAdHigRzDozKZeW",
@ -8,7 +9,6 @@
"chainCode": "873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508",
"hex": "0488b21e000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d5080339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2",
"hexPriv": "0488ade4000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d50800e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35",
"network": "bitcoin",
"base58": "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8",
"base58Priv": "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi",
"identifier": "3442193e1bb70916e914552172cd4e2dbc9df811",
@ -96,12 +96,12 @@
]
},
{
"network": "bitcoin",
"master": {
"seed": "fffcf9f6f3f0edeae7e4e1dedbd8d5d2cfccc9c6c3c0bdbab7b4b1aeaba8a5a29f9c999693908d8a8784817e7b7875726f6c696663605d5a5754514e4b484542",
"wif": "KyjXhyHF9wTphBkfpxjL8hkDXDUSbE3tKANT94kXSyh6vn6nKaoy",
"pubKey": "03cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7",
"chainCode": "60499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd9689",
"network": "bitcoin",
"base58": "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB",
"base58Priv": "xprv9s21ZrQH143K31xYSDQpPDxsXRTUcvj2iNHm5NUtrGiGG5e2DtALGdso3pGz6ssrdK4PFmM8NSpSBHNqPqm55Qn3LqFtT2emdEXVYsCzC2U",
"hex": "0488b21e00000000000000000060499f801b896d83179a4374aeb7822aaeaceaa0db1f85ee3e904c4defbd968903cbcaa9c98c877a26977d00825c956a238e8dddfbd322cce4f74b0b5bd6ace4a7",
@ -189,6 +189,37 @@
"address": "14UKfRV9ZPUp6ZC9PLhqbRtxdihW9em3xt"
}
]
},
{
"network": "litecoin",
"master": {
"seed": "000102030405060708090a0b0c0d0e0f",
"wif": "TAroS5Knm8GZcnpPycBgzjwwDLWMyQjDrcuGPPoArgrbW7Ln22qp",
"pubKey": "0339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2",
"chainCode": "873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d508",
"hex": "0488b21e000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d5080339a36013301597daef41fbe593a02cc513d0b55527ec2df1050e2e8ff49c85c2",
"hexPriv": "019d9cfe000000000000000000873dff81c02f525623fd1fe5167eac3a55a049de3d314bb42ee227ffed37d50800e8f32e723decf4051aefac8e2c93c9c5b214313817cdb01a1494b917c8436b35",
"base58": "Ltub2SSUS19CirucWFod2ZsYA2J4v4U76YiCXHdcQttnoiy5aGanFHCPDBX7utfG6f95u1cUbZJNafmvzNCzZZJTw1EmyFoL8u1gJbGM8ipu491",
"base58Priv": "Ltpv71G8qDifUiNetP6nmxPA5STrUVmv2J9YSmXajv8VsYBUyuPhvN9xCaQrfX2wo5xxJNtEazYCFRUu5FmokYMM79pcqz8pcdo4rNXAFPgyB4k",
"identifier": "3442193e1bb70916e914552172cd4e2dbc9df811",
"fingerprint": "3442193e",
"address": "LPzGaoLUtXFkmNo3u1chDxGxDnSaBQTTxm"
},
"children": [
{
"description": "m/0'",
"m": 0,
"hardened": true,
"wif": "TB22qU2V9EJCVKJ8cdYaTfvDhnYcCzthcWgFm1k6hbvbKM1NLxoL",
"pubKey": "035a784662a4a20a65bf6aab9ae98a6c068a81c52e4b032c0fb5400c706cfccc56",
"chainCode": "47fdacbd0f1097043b78c63c20c34ef4ed9a111d980047ad16282c7ae6236141",
"base58": "Ltub2UhtRiSfp82berwLEKkB34QBEt2TUdCDCu4WNzGumvAMwYsxfWjULKsXhADxqy3cuDu3TnqoKJr1xmB8Wb2qzthWAtbb4CutpXPuSU1YMgG",
"base58Priv": "Ltpv73XYpw28ZyVe2zEVyiFnxUZxoKLGQNdZ8NxUi1WcqjNmMBgtLbh3KimGSnPHCoLv1RmvxHs4dnKmo1oXQ8dXuDu8uroxrbVxZPA1gXboYvx",
"identifier": "5c1bd648ed23aa5fd50ba52b2457c11e9e80a6a7",
"fingerprint": "5c1bd648",
"address": "LTcyn1jun6g9hvxtsT7cqMRSyix7AULC76"
}
]
}
],
"invalid": {

View file

@ -65,9 +65,10 @@ describe('HDNode', function() {
describe('fromSeed*', function() {
fixtures.valid.forEach(function(f) {
it('calculates privKey and chainCode for ' + f.master.fingerprint, function() {
var hd = HDNode.fromSeedHex(f.master.seed)
var network = networks[f.network]
var hd = HDNode.fromSeedHex(f.master.seed, network)
assert.equal(hd.privKey.toWIF(), f.master.wif)
assert.equal(hd.privKey.toWIF(network), f.master.wif)
assert.equal(hd.chainCode.toString('hex'), f.master.chainCode)
})
})
@ -88,7 +89,8 @@ describe('HDNode', function() {
describe('toBase58', function() {
fixtures.valid.forEach(function(f) {
it('exports ' + f.master.base58 + ' (public) correctly', function() {
var hd = HDNode.fromSeedHex(f.master.seed).neutered()
var network = networks[f.network]
var hd = HDNode.fromSeedHex(f.master.seed, network).neutered()
assert.equal(hd.toBase58(), f.master.base58)
})
@ -96,7 +98,8 @@ describe('HDNode', function() {
fixtures.valid.forEach(function(f) {
it('exports ' + f.master.base58Priv + ' (private) correctly', function() {
var hd = HDNode.fromSeedHex(f.master.seed)
var network = networks[f.network]
var hd = HDNode.fromSeedHex(f.master.seed, network)
assert.equal(hd.toBase58(), f.master.base58Priv)
})
@ -175,7 +178,8 @@ describe('HDNode', function() {
fixtures.valid.forEach(function(f) {
it('exports ' + f.master.hexPriv + ' (private) correctly', function() {
var hd = HDNode.fromSeedHex(f.master.seed)
var network = networks[f.network]
var hd = HDNode.fromSeedHex(f.master.seed, network)
assert.equal(hd.toHex(), f.master.hexPriv)
})
@ -212,19 +216,12 @@ describe('HDNode', function() {
})
describe('getAddress', function() {
var f = fixtures.valid[0]
fixtures.valid.forEach(function(f) {
it('returns ' + f.master.address + ' for ' + f.master.fingerprint, function() {
var hd = HDNode.fromBase58(f.master.base58)
it('returns the Address (pubHash) for ' + f.master.fingerprint, function() {
var hd = HDNode.fromBase58(f.master.base58)
assert.equal(hd.getAddress().toString(), f.master.address)
})
it('supports alternative networks', function() {
var hd = HDNode.fromBase58(f.master.base58)
hd.network = networks.testnet
assert.equal(hd.getAddress().version, networks.testnet.pubKeyHash)
assert.equal(hd.getAddress().toString(), f.master.address)
})
})
})
@ -244,8 +241,8 @@ describe('HDNode', function() {
})
describe('derive', function() {
function verifyVector(hd, v, depth) {
assert.equal(hd.privKey.toWIF(), v.wif)
function verifyVector(hd, network, v, depth) {
assert.equal(hd.privKey.toWIF(network), v.wif)
assert.equal(hd.pubKey.toHex(), v.pubKey)
assert.equal(hd.chainCode.toString('hex'), v.chainCode)
assert.equal(hd.depth, depth || 0)
@ -257,8 +254,9 @@ describe('HDNode', function() {
}
}
fixtures.valid.forEach(function(f, j) {
var hd = HDNode.fromSeedHex(f.master.seed)
fixtures.valid.forEach(function(f) {
var network = networks[f.network]
var hd = HDNode.fromSeedHex(f.master.seed, network)
// FIXME: test data is only testing Private -> private for now
f.children.forEach(function(c, i) {
@ -270,7 +268,7 @@ describe('HDNode', function() {
hd = hd.derive(c.m)
}
verifyVector(hd, c, i + 1)
verifyVector(hd, network, c, i + 1)
})
})
})

View file

@ -25,7 +25,7 @@ describe('networks', function() {
var extb58 = f.bip32[name]
it('resolves ' + extb58 + ' to ' + f.network, function() {
assert.equal(HDNode.fromBase58(extb58).network, network)
assert.equal(HDNode.fromBase58(extb58, network).network, network)
})
})
})