diff --git a/lib/address.js b/lib/address.js index 39cfcd66f..1b3fa95e3 100644 --- a/lib/address.js +++ b/lib/address.js @@ -162,24 +162,18 @@ Address._transformObject = function(data) { */ Address._classifyFromVersion = function(buffer) { var version = {}; - version.network = Networks.get(buffer[0]); - switch (buffer[0]) { // the version byte - case Networks.livenet.pubkeyhash: - version.type = Address.PayToPublicKeyHash; - break; - case Networks.livenet.scripthash: - version.type = Address.PayToScriptHash; - break; + var pubkeyhashNetwork = Networks.get(buffer[0], 'pubkeyhash'); + var scripthashNetwork = Networks.get(buffer[0], 'scripthash'); - case Networks.testnet.pubkeyhash: - version.type = Address.PayToPublicKeyHash; - break; - - case Networks.testnet.scripthash: - version.type = Address.PayToScriptHash; - break; + if (pubkeyhashNetwork) { + version.network = pubkeyhashNetwork; + version.type = Address.PayToPublicKeyHash; + } else if (scripthashNetwork) { + version.network = scripthashNetwork; + version.type = Address.PayToScriptHash; } + return version; }; diff --git a/lib/networks.js b/lib/networks.js index 96e6176f2..da90f2772 100644 --- a/lib/networks.js +++ b/lib/networks.js @@ -86,6 +86,25 @@ function addNetwork(data) { } +/** + * @function + * @member Networks#remove + * Will remove a custom network + * @param {Network} network + */ +function removeNetwork(network) { + for (var i = 0; i < networks.length; i++) { + if (networks[i] === network) { + networks.splice(i, 1); + } + } + for (var key in networkMaps) { + if (networkMaps[key] === network) { + delete networkMaps[key]; + } + } +} + addNetwork({ name: 'livenet', alias: 'mainnet', @@ -141,6 +160,7 @@ var testnet = getNetwork('testnet'); */ module.exports = { add: addNetwork, + remove: removeNetwork, defaultNetwork: livenet, livenet: livenet, mainnet: livenet, diff --git a/test/address.js b/test/address.js index b971e5f79..f611afb11 100644 --- a/test/address.js +++ b/test/address.js @@ -326,6 +326,12 @@ describe('Address', function() { address.toString().should.equal('19gH5uhqY6DKrtkU66PsZPUZdzTd11Y7ke'); }); + it('should use the default network for pubkey', function() { + var pubkey = new PublicKey('0285e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b004'); + var address = Address.fromPublicKey(pubkey); + address.network.should.equal(Networks.defaultNetwork); + }); + it('should make this address from an uncompressed pubkey', function() { var pubkey = new PublicKey('0485e9737a74c30a873f74df05124f2aa6f53042c2fc0a130d6cbd7d16b944b00' + '4833fef26c8be4c4823754869ff4e46755b85d851077771c220e2610496a29d98'); @@ -335,6 +341,26 @@ describe('Address', function() { b.toString().should.equal('16JXnhxjJUhxfyx4y6H4sFcxrgt8kQ8ewX'); }); + it('should classify from a custom network', function() { + var custom = { + name: 'customnetwork', + pubkeyhash: 0x1c, + privatekey: 0x1e, + scripthash: 0x28, + xpubkey: 0x02e8de8f, + xprivkey: 0x02e8da54, + networkMagic: 0x0c110907, + port: 7333 + }; + var addressString = 'CX4WePxBwq1Y6u7VyMJfmmitE7GiTgC9aE'; + Networks.add(custom); + var network = Networks.get('customnetwork'); + var address = Address.fromString(addressString); + address.type.should.equal(Address.PayToPublicKeyHash); + address.network.should.equal(network); + Networks.remove(network); + }); + describe('from a script', function() { it('should fail to build address from a non p2sh,p2pkh script', function() { var s = new Script('OP_CHECKMULTISIG'); @@ -482,6 +508,15 @@ describe('Address', function() { expect(new Address(address.toObject()).toString()).to.equal(P2SHLivenet[0]); }); + it('will use the default network for an object', function() { + var obj = { + hash: '19a7d869032368fd1f1e26e5e73a4ad0e474960e', + type: 'scripthash' + }; + var address = new Address(obj); + address.network.should.equal(Networks.defaultNetwork); + }); + describe('creating a P2SH address from public keys', function() { var public1 = '02da5798ed0c055e31339eb9b5cef0d3c0ccdec84a62e2e255eb5c006d4f3e7f5b'; diff --git a/test/networks.js b/test/networks.js index d019479ba..9e2d7dd05 100644 --- a/test/networks.js +++ b/test/networks.js @@ -7,6 +7,8 @@ var networks = bitcore.Networks; describe('Networks', function() { + var customnet; + it('should contain all Networks', function() { should.exist(networks.livenet); should.exist(networks.testnet); @@ -30,7 +32,7 @@ describe('Networks', function() { ] }; networks.add(custom); - var customnet = networks.get('customnet'); + customnet = networks.get('customnet'); for (var key in custom) { if (key !== 'networkMagic') { customnet[key].should.equal(custom[key]); @@ -40,6 +42,12 @@ describe('Networks', function() { } } }); + + it('can remove a custom network', function() { + networks.remove(customnet); + var net = networks.get('customnet'); + should.equal(net, undefined); + }); it('should not set a network map for an undefined value', function() { var custom = { @@ -58,6 +66,7 @@ describe('Networks', function() { networks.add(custom); var network = networks.get(undefined); should.not.exist(network); + networks.remove(custom); }); var constants = ['name', 'alias', 'pubkeyhash', 'scripthash', 'xpubkey', 'xprivkey'];