bitcore/lib/networks.js

148 lines
3.2 KiB
JavaScript
Raw Normal View History

2014-11-20 07:23:46 -08:00
'use strict';
2014-11-24 10:40:20 -08:00
var _ = require('lodash');
2014-11-20 07:23:46 -08:00
var BufferUtil = require('./util/buffer');
var networks = [];
var networkMaps = {};
2014-11-24 10:40:20 -08:00
/**
* A network is merely a map containing values that correspond to version
* numbers for each bitcoin network. Currently only supporting "livenet"
* (a.k.a. "mainnet") and "testnet".
2014-11-26 07:56:43 -08:00
* @constructor
2014-11-24 10:40:20 -08:00
*/
function Network() {}
Network.prototype.toString = function toString() {
return this.name;
};
2014-11-24 10:40:20 -08:00
/**
* @function
* @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
2014-11-24 10:40:20 -08:00
*/
function getNetwork(arg, key) {
if (~networks.indexOf(arg)) {
return arg;
}
if (key) {
for (var index in networks) {
if (networks[index][key] === arg) {
return networks[index];
}
}
return undefined;
}
return networkMaps[arg];
}
/**
* @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({
2014-11-24 10:40:20 -08:00
name: 'livenet',
alias: 'mainnet',
pubkeyhash: 0x00,
privatekey: 0x80,
scripthash: 0x05,
xpubkey: 0x0488b21e,
2014-12-05 08:10:09 -08:00
xprivkey: 0x0488ade4,
networkMagic: 0xf9beb4d9,
2014-12-05 08:10:09 -08:00
port: 8333,
dnsSeeds: [
'seed.bitcoin.sipa.be',
'dnsseed.bluematt.me',
'dnsseed.bitcoin.dashjr.org',
'seed.bitcoinstats.com',
'seed.bitnodes.io',
'bitseed.xf2.org'
]
2014-11-24 10:40:20 -08:00
});
addNetwork({
2014-11-24 10:40:20 -08:00
name: 'testnet',
2014-11-22 12:50:08 -08:00
alias: 'testnet',
2014-11-24 10:40:20 -08:00
pubkeyhash: 0x6f,
privatekey: 0xef,
scripthash: 0xc4,
xpubkey: 0x043587cf,
2014-12-05 08:10:09 -08:00
xprivkey: 0x04358394,
networkMagic: 0x0b110907,
2014-12-05 08:10:09 -08:00
port: 18333,
dnsSeeds: [
'testnet-seed.bitcoin.petertodd.org',
'testnet-seed.bluematt.me'
],
2014-11-24 10:40:20 -08:00
});
/**
* @instance
* @member Networks#livenet
*/
var livenet = getNetwork('livenet');
2014-11-24 10:40:20 -08:00
/**
* @instance
* @member Networks#testnet
*/
var testnet = getNetwork('testnet');
2014-11-24 10:40:20 -08:00
/**
* @namespace Networks
2014-11-24 10:40:20 -08:00
*/
module.exports = {
add: addNetwork,
2014-11-26 13:38:15 -08:00
defaultNetwork: livenet,
2014-11-24 10:40:20 -08:00
livenet: livenet,
mainnet: livenet,
2014-11-24 10:40:20 -08:00
testnet: testnet,
get: getNetwork
};