Browse Source

adds "bitcoincore" network, and adds #translate to Address"

master
matiu 7 years ago
parent
commit
a3f2140c98
  1. 23
      lib/address.js
  2. 19
      lib/networks.js
  3. 12
      test/address.js

23
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

19
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
*/

12
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;

Loading…
Cancel
Save