Merge pull request #550 from bitcoinjs/testnet

Add network list tests for ECPair/HDNode
This commit is contained in:
Daniel Cousens 2016-02-25 18:20:53 +11:00
commit 7d2b2dee45
6 changed files with 20 additions and 9 deletions

View file

@ -58,7 +58,6 @@ ECPair.fromPublicKeyBuffer = function (buffer, network) {
} }
ECPair.fromWIF = function (string, network) { ECPair.fromWIF = function (string, network) {
network = network || NETWORKS.bitcoin
var buffer = bs58check.decode(string) var buffer = bs58check.decode(string)
if (types.Array(network)) { if (types.Array(network)) {
@ -67,9 +66,11 @@ ECPair.fromWIF = function (string, network) {
network = network.filter(function (network) { network = network.filter(function (network) {
return version === network.wif return version === network.wif
}).pop() }).pop()
if (!network) throw new Error('Invalid network version')
if (!network) throw new Error('Unknown network version')
} }
network = network || NETWORKS.bitcoin
var decoded = wif.decodeRaw(buffer, network.wif) var decoded = wif.decodeRaw(buffer, network.wif)
var d = BigInteger.fromBuffer(decoded.privateKey) var d = BigInteger.fromBuffer(decoded.privateKey)

View file

@ -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('Unknown 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]

View file

@ -107,7 +107,9 @@ describe('ECPair', function () {
fixtures.invalid.fromWIF.forEach(function (f) { fixtures.invalid.fromWIF.forEach(function (f) {
it('throws on ' + f.WIF, function () { it('throws on ' + f.WIF, function () {
assert.throws(function () { assert.throws(function () {
ECPair.fromWIF(f.WIF) var networks = f.network ? NETWORKS[f.network] : NETWORKS_LIST
ECPair.fromWIF(f.WIF, networks)
}, new RegExp(f.exception)) }, new RegExp(f.exception))
}) })
}) })

View file

@ -107,8 +107,13 @@
"fromWIF": [ "fromWIF": [
{ {
"exception": "Invalid network version", "exception": "Invalid network version",
"network": "bitcoin",
"WIF": "92Qba5hnyWSn5Ffcka56yMQauaWY6ZLd91Vzxbi4a9CCetaHtYj" "WIF": "92Qba5hnyWSn5Ffcka56yMQauaWY6ZLd91Vzxbi4a9CCetaHtYj"
}, },
{
"exception": "Unknown network version",
"WIF": "brQnSed3Fia1w9VcbbS6ZGDgJ6ENkgwuQY2LS7pEC5bKHD1fMF"
},
{ {
"exception": "Invalid compression flag", "exception": "Invalid compression flag",
"WIF": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sfZr2ym" "WIF": "KwDiBf89QgGbjEhKnhXJuH7LrciVrZi3qYjgd9M7rFU73sfZr2ym"

View file

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

View file

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