diff --git a/lib/address.js b/lib/address.js index b017bc7..629b142 100644 --- a/lib/address.js +++ b/lib/address.js @@ -437,6 +437,29 @@ Address.isValid = function(data, network, type) { return !Address.getValidationError(data, network, type); }; + +/** + * Translates an address to other network + * + * @param {Address} address object - Bitcore address + * @param {string} network - name of the network to translate the address to + * + * @returns {Address} Bitcore address object + */ +Address.translate = function(address, toNetwork) { + if (!Networks.get(toNetwork)) { + throw new TypeError('Unknown network ' + toNetwork); + } + + var orig = new Address(address); + var origObj = orig.toObject(); + + origObj.network = toNetwork; + return new Address.fromObject(origObj); +}; + + + /** * Returns true if an address is of pay to public key hash type * @return boolean diff --git a/lib/networks.js b/lib/networks.js index 30376c7..7902424 100644 --- a/lib/networks.js +++ b/lib/networks.js @@ -252,6 +252,25 @@ function disableRegtest() { testnet.regtestEnabled = false; } +addNetwork({ + name: 'bitcoincore', + alias: 'core', + pubkeyhash: 0, + privatekey: 0xef, + scripthash: 5, + xpubkey: 0x043587cf, + xprivkey: 0x04358394, + dnsSeeds: [ + 'seed.bitcoin.sipa.be', + 'dnsseed.bluematt.me', + 'dnsseed.bitcoin.dashjr.org', + 'seed.bitcoinstats.com', + 'seed.bitnodes.io', + 'bitseed.xf2.org' + ] +}); + + /** * @namespace Networks */ diff --git a/test/address.js b/test/address.js index 6cec39c..c206102 100644 --- a/test/address.js +++ b/test/address.js @@ -309,6 +309,18 @@ describe('Address', function() { new Address(hash, 'livenet').toString().should.equal(str); }); + + it('should translate an address to other network', function() { + var hash = pubkeyhash; //use the same hash + var a = Address.fromPublicKeyHash(hash, 'livenet'); + a.toString().should.equal('CMxTMLHVazpmroYhw2bfXNJFyxLuX4srpv'); + a.toObject().network.should.equal('livenet'); + + var a2 = Address.translate(a,"bitcoincore"); + a2.toString().should.equal('16VZnHwRhwrExfeHFHGjwrgEMq8VcYPs9r'); + a2.toObject().network.should.equal('bitcoincore'); + }); + it('should make an address using the default network', function() { var hash = pubkeyhash; //use the same hash var network = Networks.defaultNetwork;