From c79d60dfb16e43d495732ccbc50f632c4b9edecb Mon Sep 17 00:00:00 2001 From: Chris Priest Date: Tue, 20 Jun 2017 21:42:49 -0600 Subject: [PATCH 1/6] Address._transformObject returns array for networks, also Networks.get returns multiple networks if there are multiple matches --- lib/address.js | 14 +++++++------- lib/networks.js | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/address.js b/lib/address.js index b017bc7f4..4ef3a616e 100644 --- a/lib/address.js +++ b/lib/address.js @@ -162,14 +162,14 @@ Address._transformObject = function(data) { Address._classifyFromVersion = function(buffer) { var version = {}; - var pubkeyhashNetwork = Networks.get(buffer[0], 'pubkeyhash'); - var scripthashNetwork = Networks.get(buffer[0], 'scripthash'); + var pubkeyhashNetworks = Networks.get(buffer[0], 'pubkeyhash'); + var scripthashNetworks = Networks.get(buffer[0], 'scripthash'); - if (pubkeyhashNetwork) { - version.network = pubkeyhashNetwork; + if (pubkeyhashNetworks) { + version.networks = pubkeyhashNetworks; version.type = Address.PayToPublicKeyHash; - } else if (scripthashNetwork) { - version.network = scripthashNetwork; + } else if (scripthashNetworks) { + version.networks = scripthashNetworks; version.type = Address.PayToScriptHash; } @@ -198,7 +198,7 @@ Address._transformBuffer = function(buffer, network, type) { network = Networks.get(network); var bufferVersion = Address._classifyFromVersion(buffer); - if (!bufferVersion.network || (network && network !== bufferVersion.network)) { + if (!bufferVersion.networks || (network && !_.find(bufferVersion.networks, network))) { throw new TypeError('Address has mismatched network type.'); } diff --git a/lib/networks.js b/lib/networks.js index 3a9381c5d..b2fc5197b 100644 --- a/lib/networks.js +++ b/lib/networks.js @@ -37,12 +37,13 @@ function get(arg, keys) { var containsArg = function(key) { return networks[index][key] === arg; }; + var matched_networks = []; for (var index in networks) { if (_.any(keys, containsArg)) { - return networks[index]; + matched_networks.push(networks[index]); } } - return undefined; + return matched_networks; } return networkMaps[arg]; } From 4655602f0bf1f8126764155905cc5d460effe8b0 Mon Sep 17 00:00:00 2001 From: Chris Priest Date: Fri, 23 Jun 2017 23:33:59 -0600 Subject: [PATCH 2/6] now only 4 tests fail --- lib/address.js | 27 ++++++++++++++++++++++++--- lib/networks.js | 8 +++++++- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/address.js b/lib/address.js index 4ef3a616e..cab928654 100644 --- a/lib/address.js +++ b/lib/address.js @@ -154,6 +154,8 @@ Address._transformObject = function(data) { /** * Internal function to discover the network and type based on the first data byte + * If two or more networks match the criteria, a list of networks is returned. + * If one network is found, that one single network is returned (not wrapped in a list). * * @param {Buffer} buffer - An instance of a hex encoded address Buffer * @returns {Object} An object with keys: network and type @@ -165,10 +167,24 @@ Address._classifyFromVersion = function(buffer) { var pubkeyhashNetworks = Networks.get(buffer[0], 'pubkeyhash'); var scripthashNetworks = Networks.get(buffer[0], 'scripthash'); - if (pubkeyhashNetworks) { + if(!pubkeyhashNetworks && !scripthashNetworks) { + return version + } + + if (pubkeyhashNetworks && pubkeyhashNetworks.name) { + version.network = pubkeyhashNetworks; + version.type = Address.PayToPublicKeyHash; + return version +} else if (scripthashNetworks && scripthashNetworks.name) { + version.network = scripthashNetworks[0]; + version.type = Address.PayToScriptHash; + return version + } + + if (pubkeyhashNetworks.length > 1) { version.networks = pubkeyhashNetworks; version.type = Address.PayToPublicKeyHash; - } else if (scripthashNetworks) { +} else if (scripthashNetworks.length > 1) { version.networks = scripthashNetworks; version.type = Address.PayToScriptHash; } @@ -197,8 +213,13 @@ Address._transformBuffer = function(buffer, network, type) { network = Networks.get(network); var bufferVersion = Address._classifyFromVersion(buffer); + //console.log(bufferVersion); + + if (bufferVersion.networks && (network && !_.find(bufferVersion.networks, network))) { + throw new TypeError('Address has mismatched network type.'); + } - if (!bufferVersion.networks || (network && !_.find(bufferVersion.networks, network))) { + if (bufferVersion.network && (network && bufferVersion.network != network)) { throw new TypeError('Address has mismatched network type.'); } diff --git a/lib/networks.js b/lib/networks.js index b2fc5197b..f59ca2d40 100644 --- a/lib/networks.js +++ b/lib/networks.js @@ -43,7 +43,13 @@ function get(arg, keys) { matched_networks.push(networks[index]); } } - return matched_networks; + if(matched_networks.length == 1) { + return matched_networks[0] + } + if(matched_networks.length == 0) { + return undefined; + } + return matched_networks } return networkMaps[arg]; } From 924b5471b285ff5d8ee3e0924845e1022df2e8fa Mon Sep 17 00:00:00 2001 From: Chris Priest Date: Fri, 23 Jun 2017 23:42:01 -0600 Subject: [PATCH 3/6] simplification --- lib/address.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/address.js b/lib/address.js index cab928654..a453cf81f 100644 --- a/lib/address.js +++ b/lib/address.js @@ -167,10 +167,6 @@ Address._classifyFromVersion = function(buffer) { var pubkeyhashNetworks = Networks.get(buffer[0], 'pubkeyhash'); var scripthashNetworks = Networks.get(buffer[0], 'scripthash'); - if(!pubkeyhashNetworks && !scripthashNetworks) { - return version - } - if (pubkeyhashNetworks && pubkeyhashNetworks.name) { version.network = pubkeyhashNetworks; version.type = Address.PayToPublicKeyHash; From d14ffd28a6d9b99f2e97202e483d5f5b1e6e18f3 Mon Sep 17 00:00:00 2001 From: Chris Priest Date: Fri, 23 Jun 2017 23:57:13 -0600 Subject: [PATCH 4/6] all tests pass! --- lib/address.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/address.js b/lib/address.js index a453cf81f..d37dee42c 100644 --- a/lib/address.js +++ b/lib/address.js @@ -172,7 +172,7 @@ Address._classifyFromVersion = function(buffer) { version.type = Address.PayToPublicKeyHash; return version } else if (scripthashNetworks && scripthashNetworks.name) { - version.network = scripthashNetworks[0]; + version.network = scripthashNetworks; version.type = Address.PayToScriptHash; return version } @@ -209,7 +209,6 @@ Address._transformBuffer = function(buffer, network, type) { network = Networks.get(network); var bufferVersion = Address._classifyFromVersion(buffer); - //console.log(bufferVersion); if (bufferVersion.networks && (network && !_.find(bufferVersion.networks, network))) { throw new TypeError('Address has mismatched network type.'); From e87f4731764aa9395f61ccd6822d13128f478a1a Mon Sep 17 00:00:00 2001 From: Chris Priest Date: Wed, 5 Jul 2017 22:53:47 -0600 Subject: [PATCH 5/6] handle networks with overlapping private key versions --- lib/privatekey.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index f820cae09..0982f30b8 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -152,7 +152,7 @@ PrivateKey._transformBuffer = function(buf, network) { throw new Error('Invalid network'); } - if (network && info.network !== Networks.get(network)) { + if (network && (info.network !== Networks.get(network) || !_.find(info.network, network))) { throw new TypeError('Private key network mismatch'); } From 9393a5f6561eb497715c40ff641d0ea1841a3cbb Mon Sep 17 00:00:00 2001 From: Chris Priest Date: Wed, 5 Jul 2017 23:49:40 -0600 Subject: [PATCH 6/6] small fix --- lib/privatekey.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 0982f30b8..9bc5252cc 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -152,7 +152,7 @@ PrivateKey._transformBuffer = function(buf, network) { throw new Error('Invalid network'); } - if (network && (info.network !== Networks.get(network) || !_.find(info.network, network))) { + if (network && (info.network !== Networks.get(network) && !_.find(info.network, network))) { throw new TypeError('Private key network mismatch'); }