Browse Source

HDNode/tests: add testing for invalid network version from networks list

hk-custom-address
Daniel Cousens 9 years ago
parent
commit
bdb0dd4a17
  1. 6
      src/hdnode.js
  2. 5
      test/fixtures/hdnode.json
  3. 4
      test/hdnode.js

6
src/hdnode.js

@ -64,7 +64,9 @@ HDNode.fromBase58 = function (string, networks) {
network = networks.filter(function (network) { network = networks.filter(function (network) {
return version === network.bip32.private || return version === network.bip32.private ||
version === network.bip32.public version === network.bip32.public
}).pop() || {} }).pop()
if (!network) throw new Error('Invalid network version')
// otherwise, assume a network object (or default to bitcoin) // otherwise, assume a network object (or default to bitcoin)
} else { } else {
@ -72,7 +74,7 @@ HDNode.fromBase58 = function (string, networks) {
} }
if (version !== network.bip32.private && if (version !== network.bip32.private &&
version !== network.bip32.public) throw new Error('Invalid network') version !== network.bip32.public) throw new Error('Invalid network version')
// 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ... // 1 byte: depth: 0x00 for master nodes, 0x01 for level-1 descendants, ...
var depth = buffer[4] var depth = buffer[4]

5
test/fixtures/hdnode.json

@ -199,11 +199,12 @@
"string": "xprvQQQQQQQQQQQQQQQQCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334" "string": "xprvQQQQQQQQQQQQQQQQCviVfJSKyQ1mDYahRjijr5idH2WwLsEd4Hsb2Tyh8RfQMuPh7f7RtyzTtdrbdqqsunu5Mm3wDvUAKRHSC34sJ7in334"
}, },
{ {
"exception": "Invalid network", "exception": "Invalid network version",
"string": "1111111111111adADjFaSNPxwXqLjHLj4mBfYxuewDPbw9hEj1uaXCzMxRPXDFF3cUoezTFYom4sEmEVSQmENPPR315cFk9YUFVek73wE9" "string": "1111111111111adADjFaSNPxwXqLjHLj4mBfYxuewDPbw9hEj1uaXCzMxRPXDFF3cUoezTFYom4sEmEVSQmENPPR315cFk9YUFVek73wE9"
}, },
{ {
"exception": "Invalid network", "exception": "Invalid network version",
"network": "bitcoin",
"string": "Ltpv73XYpw28ZyVe2zEVyiFnxUZxoKLGQNdZ8NxUi1WcqjNmMBgtLbh3KimGSnPHCoLv1RmvxHs4dnKmo1oXQ8dXuDu8uroxrbVxZPA1gXboYvx" "string": "Ltpv73XYpw28ZyVe2zEVyiFnxUZxoKLGQNdZ8NxUi1WcqjNmMBgtLbh3KimGSnPHCoLv1RmvxHs4dnKmo1oXQ8dXuDu8uroxrbVxZPA1gXboYvx"
} }
], ],

4
test/hdnode.js

@ -210,9 +210,9 @@ describe('HDNode', function () {
fixtures.invalid.fromBase58.forEach(function (f) { fixtures.invalid.fromBase58.forEach(function (f) {
it('throws on ' + f.string, function () { it('throws on ' + f.string, function () {
assert.throws(function () { assert.throws(function () {
var network = NETWORKS[f.network] var networks = f.network ? NETWORKS[f.network] : NETWORKS_LIST
HDNode.fromBase58(f.string, network) HDNode.fromBase58(f.string, networks)
}, new RegExp(f.exception)) }, new RegExp(f.exception))
}) })
}) })

Loading…
Cancel
Save