UNPKG

5.61 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 if (typeof network !== 'object') {
127 network = get(network);
128 }
129 for (var i = 0; i < networks.length; i++) {
130 if (networks[i] === network) {
131 networks.splice(i, 1);
132 }
133 }
134 for (var key in networkMaps) {
135 if (networkMaps[key].length) {
136 const index = networkMaps[key].indexOf(network);
137 if (index >= 0) {
138 networkMaps[key].splice(index, 1);
139 }
140 if (networkMaps[key].length === 0) {
141 delete networkMaps[key];
142 }
143 } else if (networkMaps[key] === network) {
144 delete networkMaps[key];
145 }
146 }
147}
148
149addNetwork({
150 name: 'livenet',
151 alias: 'mainnet',
152 pubkeyhash: 0x00,
153 privatekey: 0x80,
154 scripthash: 0x05,
155 bech32prefix: 'bc',
156 xpubkey: 0x0488b21e,
157 xprivkey: 0x0488ade4,
158 networkMagic: 0xf9beb4d9,
159 port: 8333,
160 dnsSeeds: [
161 'seed.bitcoin.sipa.be',
162 'dnsseed.bluematt.me',
163 'dnsseed.bitcoin.dashjr.org',
164 'seed.bitcoinstats.com',
165 'seed.bitnodes.io',
166 'bitseed.xf2.org'
167 ]
168});
169
170/**
171 * @instance
172 * @member Networks#livenet
173 */
174var livenet = get('livenet');
175
176addNetwork({
177 name: 'testnet',
178 alias: 'test',
179 pubkeyhash: 0x6f,
180 privatekey: 0xef,
181 scripthash: 0xc4,
182 bech32prefix: 'tb',
183 xpubkey: 0x043587cf,
184 xprivkey: 0x04358394,
185 networkMagic: 0x0b110907,
186 port: 18333,
187 dnsSeeds: [
188 'testnet-seed.bitcoin.petertodd.org',
189 'testnet-seed.bluematt.me',
190 'testnet-seed.alexykot.me',
191 'testnet-seed.bitcoin.schildbach.de'
192 ]
193});
194
195/**
196 * @instance
197 * @member Networks#testnet
198 */
199var testnet = get('testnet');
200
201addNetwork({
202 name: 'regtest',
203 alias: 'dev',
204 pubkeyhash: 0x6f,
205 privatekey: 0xef,
206 scripthash: 0xc4,
207 bech32prefix: 'bcrt',
208 xpubkey: 0x043587cf,
209 xprivkey: 0x04358394,
210 networkMagic: 0xfabfb5da,
211 port: 18444,
212 dnsSeeds: []
213});
214
215/**
216 * @instance
217 * @member Networks#testnet
218 */
219var regtest = get('regtest');
220
221/**
222 * @function
223 * @deprecated
224 * @member Networks#enableRegtest
225 * Will enable regtest features for testnet
226 */
227function enableRegtest() {
228 testnet.regtestEnabled = true;
229}
230
231/**
232 * @function
233 * @deprecated
234 * @member Networks#disableRegtest
235 * Will disable regtest features for testnet
236 */
237function disableRegtest() {
238 testnet.regtestEnabled = false;
239}
240
241/**
242 * @namespace Networks
243 */
244module.exports = {
245 add: addNetwork,
246 remove: removeNetwork,
247 defaultNetwork: livenet,
248 livenet: livenet,
249 mainnet: livenet,
250 testnet: testnet,
251 regtest: regtest,
252 get: get,
253 enableRegtest: enableRegtest,
254 disableRegtest: disableRegtest
255};