Merge pull request #998 from braydonf/feature/custom-networks
Networks: Added the ability to define a custom network. Closes #997
This commit is contained in:
commit
32b257d74c
163
lib/networks.js
163
lib/networks.js
|
@ -2,6 +2,8 @@
|
|||
var _ = require('lodash');
|
||||
|
||||
var BufferUtil = require('./util/buffer');
|
||||
var networks = [];
|
||||
var networkMaps = {};
|
||||
|
||||
/**
|
||||
* A network is merely a map containing values that correspond to version
|
||||
|
@ -15,79 +17,19 @@ Network.prototype.toString = function toString() {
|
|||
return this.name;
|
||||
};
|
||||
|
||||
/**
|
||||
* @instance
|
||||
* @member Network#livenet
|
||||
*/
|
||||
var livenet = new Network();
|
||||
_.extend(livenet, {
|
||||
name: 'livenet',
|
||||
alias: 'mainnet',
|
||||
pubkeyhash: 0x00,
|
||||
privatekey: 0x80,
|
||||
scripthash: 0x05,
|
||||
xpubkey: 0x0488b21e,
|
||||
xprivkey: 0x0488ade4,
|
||||
networkMagic: BufferUtil.integerAsBuffer(0xf9beb4d9),
|
||||
port: 8333,
|
||||
dnsSeeds: [
|
||||
'seed.bitcoin.sipa.be',
|
||||
'dnsseed.bluematt.me',
|
||||
'dnsseed.bitcoin.dashjr.org',
|
||||
'seed.bitcoinstats.com',
|
||||
'seed.bitnodes.io',
|
||||
'bitseed.xf2.org'
|
||||
]
|
||||
});
|
||||
|
||||
/**
|
||||
* @instance
|
||||
* @member Network#testnet
|
||||
*/
|
||||
var testnet = new Network();
|
||||
_.extend(testnet, {
|
||||
name: 'testnet',
|
||||
alias: 'testnet',
|
||||
pubkeyhash: 0x6f,
|
||||
privatekey: 0xef,
|
||||
scripthash: 0xc4,
|
||||
xpubkey: 0x043587cf,
|
||||
xprivkey: 0x04358394,
|
||||
networkMagic: BufferUtil.integerAsBuffer(0x0b110907),
|
||||
port: 18333,
|
||||
dnsSeeds: [
|
||||
'testnet-seed.bitcoin.petertodd.org',
|
||||
'testnet-seed.bluematt.me'
|
||||
],
|
||||
});
|
||||
|
||||
var networkMaps = {};
|
||||
|
||||
_.each(_.values(livenet), function(value) {
|
||||
if (!_.isObject(value)) {
|
||||
networkMaps[value] = livenet;
|
||||
}
|
||||
});
|
||||
_.each(_.values(testnet), function(value) {
|
||||
if (!_.isObject(value)) {
|
||||
networkMaps[value] = testnet;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* @function
|
||||
* @member Network#getNetwork
|
||||
* @member Networks#get
|
||||
* Retrieves the network associated with a magic number or string.
|
||||
* @param {string|number|Network} arg
|
||||
* @param {string} key - if set, only check if the magic number associated with this name matches
|
||||
* @return Network
|
||||
*/
|
||||
function getNetwork(arg, key) {
|
||||
if (arg === livenet || arg === testnet) {
|
||||
if (~networks.indexOf(arg)) {
|
||||
return arg;
|
||||
}
|
||||
if (key) {
|
||||
var networks = [livenet, testnet];
|
||||
for (var index in networks) {
|
||||
if (networks[index][key] === arg) {
|
||||
return networks[index];
|
||||
|
@ -99,9 +41,104 @@ function getNetwork(arg, key) {
|
|||
}
|
||||
|
||||
/**
|
||||
* @namespace Network
|
||||
* @function
|
||||
* @member Networks#add
|
||||
* Will add a custom Network
|
||||
* @param {Object} data
|
||||
* @param {String} data.name - The name of the network
|
||||
* @param {String} data.alias - The aliased name of the network
|
||||
* @param {Number} data.pubkeyhash - The publickey hash prefix
|
||||
* @param {Number} data.privatekey - The privatekey prefix
|
||||
* @param {Number} data.scripthash - The scripthash prefix
|
||||
* @param {Number} data.xpubkey - The extended public key magic
|
||||
* @param {Number} data.xprivkey - The extended private key magic
|
||||
* @param {Number} data.networkMagic - The network magic number
|
||||
* @param {Number} data.port - The network port
|
||||
* @param {Array} data.dnsSeeds - An array of dns seeds
|
||||
* @return Network
|
||||
*/
|
||||
function addNetwork(data) {
|
||||
|
||||
var network = new Network();
|
||||
|
||||
_.extend(network, {
|
||||
name: data.name,
|
||||
alias: data.alias,
|
||||
pubkeyhash: data.pubkeyhash,
|
||||
privatekey: data.privatekey,
|
||||
scripthash: data.scripthash,
|
||||
xpubkey: data.xpubkey,
|
||||
xprivkey: data.xprivkey,
|
||||
networkMagic: BufferUtil.integerAsBuffer(data.networkMagic),
|
||||
port: data.port,
|
||||
dnsSeeds: data.dnsSeeds
|
||||
});
|
||||
|
||||
_.each(_.values(network), function(value) {
|
||||
if (!_.isObject(value)) {
|
||||
networkMaps[value] = network;
|
||||
}
|
||||
});
|
||||
|
||||
networks.push(network);
|
||||
|
||||
return network;
|
||||
|
||||
}
|
||||
|
||||
addNetwork({
|
||||
name: 'livenet',
|
||||
alias: 'mainnet',
|
||||
pubkeyhash: 0x00,
|
||||
privatekey: 0x80,
|
||||
scripthash: 0x05,
|
||||
xpubkey: 0x0488b21e,
|
||||
xprivkey: 0x0488ade4,
|
||||
networkMagic: 0xf9beb4d9,
|
||||
port: 8333,
|
||||
dnsSeeds: [
|
||||
'seed.bitcoin.sipa.be',
|
||||
'dnsseed.bluematt.me',
|
||||
'dnsseed.bitcoin.dashjr.org',
|
||||
'seed.bitcoinstats.com',
|
||||
'seed.bitnodes.io',
|
||||
'bitseed.xf2.org'
|
||||
]
|
||||
});
|
||||
|
||||
addNetwork({
|
||||
name: 'testnet',
|
||||
alias: 'testnet',
|
||||
pubkeyhash: 0x6f,
|
||||
privatekey: 0xef,
|
||||
scripthash: 0xc4,
|
||||
xpubkey: 0x043587cf,
|
||||
xprivkey: 0x04358394,
|
||||
networkMagic: 0x0b110907,
|
||||
port: 18333,
|
||||
dnsSeeds: [
|
||||
'testnet-seed.bitcoin.petertodd.org',
|
||||
'testnet-seed.bluematt.me'
|
||||
],
|
||||
});
|
||||
|
||||
/**
|
||||
* @instance
|
||||
* @member Networks#livenet
|
||||
*/
|
||||
var livenet = getNetwork('livenet');
|
||||
|
||||
/**
|
||||
* @instance
|
||||
* @member Networks#testnet
|
||||
*/
|
||||
var testnet = getNetwork('testnet');
|
||||
|
||||
/**
|
||||
* @namespace Networks
|
||||
*/
|
||||
module.exports = {
|
||||
add: addNetwork,
|
||||
defaultNetwork: livenet,
|
||||
livenet: livenet,
|
||||
mainnet: livenet,
|
||||
|
|
|
@ -13,6 +13,34 @@ describe('Networks', function() {
|
|||
should.exist(networks.defaultNetwork);
|
||||
});
|
||||
|
||||
it('should be able to define a custom Network', 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);
|
||||
var customnet = networks.get('customnet');
|
||||
for (var key in custom) {
|
||||
if (key !== 'networkMagic') {
|
||||
customnet[key].should.equal(custom[key]);
|
||||
} else {
|
||||
var expected = new Buffer('e7beb4d4', 'hex');
|
||||
customnet[key].should.deep.equal(expected);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
var constants = ['name', 'alias', 'pubkeyhash', 'scripthash', 'xpubkey', 'xprivkey'];
|
||||
|
||||
constants.forEach(function(key){
|
||||
|
|
Loading…
Reference in New Issue