1 | 'use strict';
|
2 | var _ = require('lodash');
|
3 |
|
4 | var BufferUtil = require('./util/buffer');
|
5 | var JSUtil = require('./util/js');
|
6 | var networks = [];
|
7 | var networkMaps = {};
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | function Network() {}
|
16 |
|
17 | Network.prototype.toString = function toString() {
|
18 | return this.name;
|
19 | };
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | function 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 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 | function 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 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | function 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 |
|
139 | addNetwork({
|
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 |
|
162 |
|
163 |
|
164 | var livenet = get('livenet');
|
165 |
|
166 | addNetwork({
|
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 |
|
187 |
|
188 |
|
189 | var testnet = get('testnet');
|
190 |
|
191 | addNetwork({
|
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 |
|
207 |
|
208 |
|
209 | var regtest = get('regtest');
|
210 |
|
211 | /**
|
212 | * @function
|
213 | * @deprecated
|
214 | * @member Networks#enableRegtest
|
215 | * Will enable regtest features for testnet
|
216 | */
|
217 | function enableRegtest() {
|
218 | testnet.regtestEnabled = true;
|
219 | }
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | function disableRegtest() {
|
228 | testnet.regtestEnabled = false;
|
229 | }
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | module.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 | };
|