diff --git a/lib/privatekey.js b/lib/privatekey.js index 8560f6852..dfd4638a5 100644 --- a/lib/privatekey.js +++ b/lib/privatekey.js @@ -157,11 +157,8 @@ 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 { + + if (!info.network) { throw new Error('Invalid network'); } diff --git a/test/networks.js b/test/networks.js index 989e06481..c3f9b8be1 100644 --- a/test/networks.js +++ b/test/networks.js @@ -48,7 +48,7 @@ describe('Networks', function() { var net = networks.get('customnet'); should.equal(net, undefined); }); - + 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 03aad24e8..006fb7301 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(); @@ -44,6 +45,31 @@ describe('PrivateKey', function() { should.exist(a.bn); }); + it('should create a private key from a custom network WIF string', function() { + 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(wifNamecoin, 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); @@ -114,6 +140,12 @@ describe('PrivateKey', function() { }).to.throw('Invalid network'); }); + it('should not be able to instantiate private key WIF because of network mismatch', function() { + expect(function(){ + var a = new PrivateKey(wifNamecoin, 'testnet'); + }).to.throw('Invalid network'); + }); + it('can be instantiated from a hex string', function() { var privhex = '906977a061af29276e40bf377042ffbde414e496ae2260bbf1fa9d085637bfff'; var pubhex = '02a1633cafcc01ebfb6d78e39f687a1f0995c62fc95f51ead10a02ee0be551b5dc';