From a38c0c2d93a5743e15dc8e8cbb16cc3d733cd2ca Mon Sep 17 00:00:00 2001 From: Brandon Robertz Date: Mon, 8 Jun 2015 19:40:58 -0500 Subject: [PATCH 1/5] modularize network version check/tests issues/1265 --- lib/networks.js | 12 +++++++++++- lib/privatekey.js | 14 +++++++++----- test/networks.js | 26 +++++++++++++++++++++++++- test/privatekey.js | 29 +++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 7 deletions(-) diff --git a/lib/networks.js b/lib/networks.js index 3ea7c01..ba7246d 100644 --- a/lib/networks.js +++ b/lib/networks.js @@ -18,6 +18,15 @@ Network.prototype.toString = function toString() { return this.name; }; +/** + * @member Networks#all + * Retrieves all networks registered. + * @return Array + */ +function all() { + return networks; +} + /** * @function * @member Networks#get @@ -172,5 +181,6 @@ module.exports = { livenet: livenet, mainnet: livenet, testnet: testnet, - get: get + get: get, + all: all }; diff --git a/lib/privatekey.js b/lib/privatekey.js index 8560f68..2f56072 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -157,14 +157,18 @@ PrivateKey._transformBuffer = function(buf, network) { } info.network = Networks.get(buf[0], 'privatekey'); - if (buf[0] === Networks.livenet.privatekey) { - info.network = Networks.livenet; - } else if (buf[0] === Networks.testnet.privatekey) { - info.network = Networks.testnet; - } else { + + var allNetworks = Networks.all(); + var matches = _.filter( allNetworks, function( network) { + return buf[0] === network.privatekey; + }); + + if (matches.length !== 1) { throw new Error('Invalid network'); } + info.network = matches[0]; + if (network && info.network !== Networks.get(network)) { throw new TypeError('Private key network mismatch'); } diff --git a/test/networks.js b/test/networks.js index 989e064..bd2b7b5 100644 --- a/test/networks.js +++ b/test/networks.js @@ -48,7 +48,31 @@ describe('Networks', function() { var net = networks.get('customnet'); should.equal(net, undefined); }); - + + it('can return custom networks', function() { + var custom = { + name: 'customnet', + alias: 'mynet', + pubkeyhash: 0x10, + privatekey: 0x90, + scripthash: 0x08, + xpubkey: 0x0278b20e, + xprivkey: 0x0278ade4, + networkMagic: 0xe7beb4d4, + port: 20001, + dnsSeeds: [ + 'localhost', + 'mynet.localhost' + ] + }; + networks.add(custom); + customnet = networks.get('customnet'); + var allNetworks = networks.all(); + var customInOutput = allNetworks.indexOf(customnet) > -1; + should.equal(customInOutput, true); + networks.remove(customnet); + }); + it('should not set a network map for an undefined value', function() { var custom = { name: 'somenet', diff --git a/test/privatekey.js b/test/privatekey.js index 03aad24..cf0395f 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -44,6 +44,35 @@ describe('PrivateKey', function() { should.exist(a.bn); }); + it('should create a private key from a custom network WIF string', function() { + var wifNamecoin = '74pxNKNpByQ2kMow4d9kF6Z77BYeKztQNLq3dSyU4ES1K5KLNiz'; + var nmc = { + name: 'namecoin', + alias: 'namecoin', + pubkeyhash: 0x34, + privatekey: 0xB4, + // these below aren't the real NMC version numbers + scripthash: 0x08, + xpubkey: 0x0278b20e, + xprivkey: 0x0278ade4, + networkMagic: 0xf9beb4fe, + port: 20001, + dnsSeeds: [ + 'localhost', + 'mynet.localhost' + ] + }; + Networks.add(nmc); + var nmcNet = Networks.get('namecoin'); + var a = new PrivateKey( + '74pxNKNpByQ2kMow4d9kF6Z77BYeKztQNLq3dSyU4ES1K5KLNiz', + nmcNet + ); + should.exist(a); + should.exist(a.bn); + Networks.remove(nmcNet); + }); + it('should create a new random testnet private key with empty data', function() { var a = new PrivateKey(null, Networks.testnet); should.exist(a); From a53bd10f426431457fc4c2b4327001e12a08d299 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Thu, 11 Jun 2015 08:05:35 -0700 Subject: [PATCH 2/5] test explicitly passed invalid network --- test/privatekey.js | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/test/privatekey.js b/test/privatekey.js index cf0395f..98d50d6 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -22,6 +22,7 @@ describe('PrivateKey', function() { var wifTestnetUncompressed = '92jJzK4tbURm1C7udQXxeCBvXHoHJstDXRxAMouPG1k1XUaXdsu'; var wifLivenet = 'L2Gkw3kKJ6N24QcDuH4XDqt9cTqsKTVNDGz1CRZhk9cq4auDUbJy'; var wifLivenetUncompressed = '5JxgQaFM1FMd38cd14e3mbdxsdSa9iM2BV6DHBYsvGzxkTNQ7Un'; + var wifNamecoin = '74pxNKNpByQ2kMow4d9kF6Z77BYeKztQNLq3dSyU4ES1K5KLNiz'; it('should create a new random private key', function() { var a = new PrivateKey(); @@ -45,7 +46,6 @@ describe('PrivateKey', function() { }); it('should create a private key from a custom network WIF string', function() { - var wifNamecoin = '74pxNKNpByQ2kMow4d9kF6Z77BYeKztQNLq3dSyU4ES1K5KLNiz'; var nmc = { name: 'namecoin', alias: 'namecoin', @@ -64,10 +64,7 @@ describe('PrivateKey', function() { }; Networks.add(nmc); var nmcNet = Networks.get('namecoin'); - var a = new PrivateKey( - '74pxNKNpByQ2kMow4d9kF6Z77BYeKztQNLq3dSyU4ES1K5KLNiz', - nmcNet - ); + var a = new PrivateKey( wifNamecoin, nmcNet); should.exist(a); should.exist(a.bn); Networks.remove(nmcNet); @@ -143,6 +140,22 @@ describe('PrivateKey', function() { }).to.throw('Invalid network'); }); + it('should not be able to instantiate private key WIF because of network mismatch', function() { + var invalidNet = { + name: 'invalidnet', + alias: 'invalidnet', + pubkeyhash: 0xFF, + privatekey: 0xFF, + scripthash: 0xFF, + xpubkey: 0xFFFFFFFF, + xprivkey: 0xFFFFFFFF, + networkMagic: 0xFFFFFFFF + }; + expect(function(){ + var a = new PrivateKey( wifNamecoin, invalidNet ); + }).to.throw('Invalid network'); + }); + it('can be instantiated from a hex string', function() { var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc'; From e07186df0770efe0160292e37d0cb39efd4fb99b Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Sat, 20 Jun 2015 14:26:01 -0700 Subject: [PATCH 3/5] remove unnecessary filtering code --- lib/privatekey.js | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/lib/privatekey.js b/lib/privatekey.js index 2f56072..dfd4638 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -158,17 +158,10 @@ PrivateKey._transformBuffer = function(buf, network) { info.network = Networks.get(buf[0], 'privatekey'); - var allNetworks = Networks.all(); - var matches = _.filter( allNetworks, function( network) { - return buf[0] === network.privatekey; - }); - - if (matches.length !== 1) { + if (!info.network) { throw new Error('Invalid network'); } - info.network = matches[0]; - if (network && info.network !== Networks.get(network)) { throw new TypeError('Private key network mismatch'); } From 783c59d99ee594cae628446b3418d159c8dab231 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Mon, 22 Jun 2015 20:36:06 -0700 Subject: [PATCH 4/5] removed Networks.all and associated test --- lib/networks.js | 12 +----------- test/networks.js | 24 ------------------------ 2 files changed, 1 insertion(+), 35 deletions(-) diff --git a/lib/networks.js b/lib/networks.js index ba7246d..3ea7c01 100644 --- a/lib/networks.js +++ b/lib/networks.js @@ -18,15 +18,6 @@ Network.prototype.toString = function toString() { return this.name; }; -/** - * @member Networks#all - * Retrieves all networks registered. - * @return Array - */ -function all() { - return networks; -} - /** * @function * @member Networks#get @@ -181,6 +172,5 @@ module.exports = { livenet: livenet, mainnet: livenet, testnet: testnet, - get: get, - all: all + get: get }; diff --git a/test/networks.js b/test/networks.js index bd2b7b5..c3f9b8b 100644 --- a/test/networks.js +++ b/test/networks.js @@ -49,30 +49,6 @@ describe('Networks', function() { should.equal(net, undefined); }); - it('can return custom networks', function() { - var custom = { - name: 'customnet', - alias: 'mynet', - pubkeyhash: 0x10, - privatekey: 0x90, - scripthash: 0x08, - xpubkey: 0x0278b20e, - xprivkey: 0x0278ade4, - networkMagic: 0xe7beb4d4, - port: 20001, - dnsSeeds: [ - 'localhost', - 'mynet.localhost' - ] - }; - networks.add(custom); - customnet = networks.get('customnet'); - var allNetworks = networks.all(); - var customInOutput = allNetworks.indexOf(customnet) > -1; - should.equal(customInOutput, true); - networks.remove(customnet); - }); - it('should not set a network map for an undefined value', function() { var custom = { name: 'somenet', From 3518843f98370b3ffad27bc2e43a722583cc620d Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Tue, 23 Jun 2015 09:46:53 -0700 Subject: [PATCH 5/5] stylistic change and unnecessary test code removal --- test/privatekey.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/test/privatekey.js b/test/privatekey.js index 98d50d6..006fb73 100644 --- a/test/privatekey.js +++ b/test/privatekey.js @@ -64,7 +64,7 @@ describe('PrivateKey', function() { }; Networks.add(nmc); var nmcNet = Networks.get('namecoin'); - var a = new PrivateKey( wifNamecoin, nmcNet); + var a = new PrivateKey(wifNamecoin, nmcNet); should.exist(a); should.exist(a.bn); Networks.remove(nmcNet); @@ -141,18 +141,8 @@ describe('PrivateKey', function() { }); it('should not be able to instantiate private key WIF because of network mismatch', function() { - var invalidNet = { - name: 'invalidnet', - alias: 'invalidnet', - pubkeyhash: 0xFF, - privatekey: 0xFF, - scripthash: 0xFF, - xpubkey: 0xFFFFFFFF, - xprivkey: 0xFFFFFFFF, - networkMagic: 0xFFFFFFFF - }; expect(function(){ - var a = new PrivateKey( wifNamecoin, invalidNet ); + var a = new PrivateKey(wifNamecoin, 'testnet'); }).to.throw('Invalid network'); });