UNPKG

5.33 kBJavaScriptView Raw
1'use strict';
2var _ = require('lodash');
3
4var BufferUtil = require('./util/buffer');
5var JSUtil = require('./util/js');
6var networks = [];
7var networkMaps = {};
8
9/**
10 * A network is merely a map containing values that correspond to version
11 * numbers for each bitcoin network. Currently only supporting "livenet"
12 * (a.k.a. "mainnet") and "testnet".
13 * @constructor
14 */
15function Network() {}
16
17Network.prototype.toString = function toString() {
18 return this.name;
19};
20
21/**
22 * @function
23 * @member Networks#get
24 * Retrieves the network associated with a magic number or string.
25 * @param {string|number|Network} arg
26 * @param {string|Array} keys - if set, only check if the magic number associated with this name matches
27 * @return Network
28 */
29function get(arg, keys) {
30 if (~networks.indexOf(arg)) {
31 return arg;
32 }
33 if (keys) {
34 if (!_.isArray(keys)) {
35 keys = [keys];
36 }
37 var containsArg = function(key) {
38 return networks[index][key] === arg;
39 };
40 for (var index in networks) {
41 if (_.some(keys, containsArg)) {
42 return networks[index];
43 }
44 }
45 return undefined;
46 }
47 if(networkMaps[arg] && networkMaps[arg].length >= 1) {
48 return networkMaps[arg][0];
49 } else {
50 return networkMaps[arg];
51 }
52}
53
54/**
55 * @function
56 * @member Networks#add
57 * Will add a custom Network
58 * @param {Object} data
59 * @param {string} data.name - The name of the network
60 * @param {string} data.alias - The aliased name of the network
61 * @param {Number} data.pubkeyhash - The publickey hash prefix
62 * @param {Number} data.privatekey - The privatekey prefix
63 * @param {Number} data.scripthash - The scripthash prefix
64 * @param {string} data.bech32prefix - The native segwit prefix
65 * @param {Number} data.xpubkey - The extended public key magic
66 * @param {Number} data.xprivkey - The extended private key magic
67 * @param {Number} data.networkMagic - The network magic number
68 * @param {Number} data.port - The network port
69 * @param {Array} data.dnsSeeds - An array of dns seeds
70 * @return Network
71 */
72function addNetwork(data) {
73
74 var network = new Network();
75
76 JSUtil.defineImmutable(network, {
77 name: data.name,
78 alias: data.alias,
79 pubkeyhash: data.pubkeyhash,
80 privatekey: data.privatekey,
81 scripthash: data.scripthash,
82 bech32prefix: data.bech32prefix,
83 xpubkey: data.xpubkey,
84 xprivkey: data.xprivkey
85 });
86
87 if (data.networkMagic) {
88 JSUtil.defineImmutable(network, {
89 networkMagic: BufferUtil.integerAsBuffer(data.networkMagic)
90 });
91 }
92
93 if (data.port) {
94 JSUtil.defineImmutable(network, {
95 port: data.port
96 });
97 }
98
99 if (data.dnsSeeds) {
100 JSUtil.defineImmutable(network, {
101 dnsSeeds: data.dnsSeeds
102 });
103 }
104 _.each(network, function(value) {
105 if (!_.isUndefined(value) && !_.isObject(value)) {
106 if(!networkMaps[value]) {
107 networkMaps[value] = [];
108 }
109 networkMaps[value].push(network);
110 }
111 });
112
113 networks.push(network);
114
115 return network;
116
117}
118
119/**
120 * @function
121 * @member Networks#remove
122 * Will remove a custom network
123 * @param {Network} network
124 */
125function removeNetwork(network) {
126 for (var i = 0; i < networks.length; i++) {
127 if (networks[i] === network) {
128 networks.splice(i, 1);
129 }
130 }
131 for (var key in networkMaps) {
132 const index = networkMaps[key].indexOf(network);
133 if (index >= 0) {
134 delete networkMaps[key][index];
135 }
136 }
137}
138
139addNetwork({
140 name: 'livenet',
141 alias: 'mainnet',
142 pubkeyhash: 0x00,
143 privatekey: 0x80,
144 scripthash: 0x05,
145 bech32prefix: 'bc',
146 xpubkey: 0x0488b21e,
147 xprivkey: 0x0488ade4,
148 networkMagic: 0xf9beb4d9,
149 port: 8333,
150 dnsSeeds: [
151 'seed.bitcoin.sipa.be',
152 'dnsseed.bluematt.me',
153 'dnsseed.bitcoin.dashjr.org',
154 'seed.bitcoinstats.com',
155 'seed.bitnodes.io',
156 'bitseed.xf2.org'
157 ]
158});
159
160/**
161 * @instance
162 * @member Networks#livenet
163 */
164var livenet = get('livenet');
165
166addNetwork({
167 name: 'testnet',
168 alias: 'test',
169 pubkeyhash: 0x6f,
170 privatekey: 0xef,
171 scripthash: 0xc4,
172 bech32prefix: 'tb',
173 xpubkey: 0x043587cf,
174 xprivkey: 0x04358394,
175 networkMagic: 0x0b110907,
176 port: 18333,
177 dnsSeeds: [
178 'testnet-seed.bitcoin.petertodd.org',
179 'testnet-seed.bluematt.me',
180 'testnet-seed.alexykot.me',
181 'testnet-seed.bitcoin.schildbach.de'
182 ]
183});
184
185/**
186 * @instance
187 * @member Networks#testnet
188 */
189var testnet = get('testnet');
190
191addNetwork({
192 name: 'regtest',
193 alias: 'dev',
194 pubkeyhash: 0x6f,
195 privatekey: 0xef,
196 scripthash: 0xc4,
197 bech32prefix: 'bcrt',
198 xpubkey: 0x043587cf,
199 xprivkey: 0x04358394,
200 networkMagic: 0xfabfb5da,
201 port: 18444,
202 dnsSeeds: []
203});
204
205/**
206 * @instance
207 * @member Networks#testnet
208 */
209var regtest = get('regtest');
210
211/**
212 * @function
213 * @deprecated
214 * @member Networks#enableRegtest
215 * Will enable regtest features for testnet
216 */
217function enableRegtest() {
218 testnet.regtestEnabled = true;
219}
220
221/**
222 * @function
223 * @deprecated
224 * @member Networks#disableRegtest
225 * Will disable regtest features for testnet
226 */
227function disableRegtest() {
228 testnet.regtestEnabled = false;
229}
230
231/**
232 * @namespace Networks
233 */
234module.exports = {
235 add: addNetwork,
236 remove: removeNetwork,
237 defaultNetwork: livenet,
238 livenet: livenet,
239 mainnet: livenet,
240 testnet: testnet,
241 regtest: regtest,
242 get: get,
243 enableRegtest: enableRegtest,
244 disableRegtest: disableRegtest
245};