forked from BlueWallet/BlueWallet
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathBlueElectrum.js
575 lines (490 loc) · 15.1 KB
/
BlueElectrum.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* eslint-disable @typescript-eslint/no-empty-function */
import { difference, random } from 'lodash';
import { compose, map, mapValues, values, flatten, uniq, filter as fpFilter } from 'lodash/fp';
import { messages, AppErrors } from './error';
import logger from './logger';
import config from './src/config';
import { btcToSatoshi } from './utils/bitcoin';
import { wait } from './utils/time';
const BigNumber = require('bignumber.js');
const bitcoin = require('bitcoinjs-lib');
const reverse = require('buffer-reverse');
const ElectrumClient = require('electrum-client');
const hardcodedPeers = [
{ host: 'e1.electrumx.bitcoinvault.global', tcp: '50001' },
{ host: '157.245.20.66', tcp: '50001' },
];
let mainClient = false;
let mainConnected = false;
let currentHost = '';
const getHost = () => {
const hosts = config.hosts;
const hostsLength = hosts.length;
if (hostsLength === 1) {
return hosts[0];
}
while (true) {
const selectedHost = hosts[random(0, hostsLength - 1)];
if (currentHost !== selectedHost) {
currentHost = selectedHost;
return currentHost;
}
}
};
const setNewHost = () => {
mainClient.setHost(getHost());
};
const onClose = () => {
setNewHost();
logger.info({
message: 'closed connection to server',
category: 'BlueElectrum',
});
mainConnected = false;
};
const onConnect = () => {
logger.info({
message: 'connected to server',
category: 'BlueElectrum',
});
mainConnected = true;
};
async function connectMain() {
const usingPeer = { host: getHost(), port: config.port, protocol: config.protocol };
try {
logger.info({
message: `begin connection: ${JSON.stringify(usingPeer)}`,
category: 'BlueElectrum',
});
mainClient = new ElectrumClient(usingPeer.port, usingPeer.host, usingPeer.protocol);
mainClient.onConnectionClose = () => onClose();
mainClient.onConnect = () => onConnect();
mainClient.onError = function(e) {
logger.error({
message: e.message,
category: 'BlueElectrum',
});
};
const ver = await mainClient.initElectrum({
client: '2.7.11',
version: config.electrumXProtocolVersion,
});
if (ver && ver[0]) {
logger.info({
message: `connected to, ${ver}`,
category: 'BlueElectrum',
});
mainConnected = true;
}
} catch (e) {
mainConnected = false;
logger.error({
message: `bad connection: ${JSON.stringify(usingPeer)}, Error: ${e.message}`,
category: 'BlueElectrum',
});
}
if (!mainConnected) {
await wait(50);
await connectMain;
}
}
module.exports.getBlockchainHeaders = function() {
return mainClient.blockchainHeaders_subscribe();
};
module.exports.subscribe = function(event, handler) {
return mainClient.subscribe.on(event, handler);
};
module.exports.unsubscribe = function(event) {
return mainClient.subscribe.off(event);
};
module.exports.subscribeToOnConnect = function(handler) {
mainClient.onConnect = () => {
onConnect();
handler();
};
};
module.exports.subscribeToOnClose = function(handler) {
mainClient.onConnectionClose = () => {
onClose();
handler();
};
};
connectMain();
/**
*
* @param address {String}
* @returns {Promise<Object>}
*/
module.exports.getBalanceByAddress = async function(address) {
try {
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
const script = bitcoin.address.toOutputScript(address, config.network);
const hash = bitcoin.crypto.sha256(script);
const reversedHash = Buffer.from(reverse(hash));
const balance = await mainClient.blockchainScripthash_getBalance(reversedHash.toString('hex'));
balance.addr = address;
return balance;
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
module.exports.getConfig = async function() {
try {
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
return {
host: mainClient.host,
port: mainClient.port,
status: mainClient.status,
};
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
/**
*
* @param address {String}
* @returns {Promise<Array>}
*/
module.exports.getTransactionsByAddress = async function(address) {
try {
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
const script = bitcoin.address.toOutputScript(address, config.network);
const hash = bitcoin.crypto.sha256(script);
const reversedHash = Buffer.from(reverse(hash));
const history = await mainClient.blockchainScripthash_getHistory(reversedHash.toString('hex'));
return history;
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
module.exports.ping = async function() {
try {
await mainClient.server_ping();
} catch (_) {
mainConnected = false;
return false;
}
return true;
};
module.exports.multiGetTransactionsFullByTxid = async function(txIds) {
try {
const txs = await this.multiGetTransactionByTxid(uniq(txIds));
const inputsTxIds = difference(
compose(
uniq,
flatten,
map(tx => tx.vin.map(vin => vin.txid)),
)(txs),
txIds,
);
let inputsTxs = {};
if (inputsTxIds.length > 0) {
inputsTxs = await this.multiGetTransactionByTxid(inputsTxIds);
}
const allTxs = { ...txs, ...inputsTxs };
return compose(
// TODO: Add proper handling for coinbase transaction, this is quick fix with filtering them completly
fpFilter(tx => tx.inputs.length > 0),
values,
mapValues(tx => {
const inputs = tx.vin
.filter(input => input.txid)
.map(input => {
const prevOutputOfInput = allTxs[input.txid].vout[input.vout];
return { ...input, value: prevOutputOfInput.value, addresses: prevOutputOfInput.scriptPubKey.addresses };
});
const outputs = tx.vout
.filter(o => !!o.scriptPubKey.addresses)
.map(output => ({ ...output, addresses: output.scriptPubKey.addresses }));
return { ...tx, outputs, inputs };
}),
)(txs);
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
module.exports.subscribeToSriptHashes = scriptHashes => {
return Promise.all(scriptHashes.map(scriptHash => mainClient.blockchainScripthash_subscribe(scriptHash)));
};
module.exports.unsubscribeFromSriptHashes = scriptHashes => {
return Promise.all(scriptHashes.map(scriptHash => mainClient.blockchainScripthash_unsubscribe(scriptHash)));
};
/**
*
* @param addresses {Array}
* @param batchsize {Number}
* @returns {Promise<{balance: number, unconfirmed_balance: number, addresses: object}>}
*/
module.exports.multiGetBalanceByAddress = async function(addresses, batchsize) {
try {
batchsize = batchsize || 100;
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
const ret = { balance: 0, unconfirmed_balance: 0, incoming_balance: 0, outgoing_balance: 0, addresses: {} };
const chunks = splitIntoChunks(addresses, batchsize);
for (const chunk of chunks) {
const scripthashes = [];
const scripthash2addr = {};
for (const addr of chunk) {
const script = bitcoin.address.toOutputScript(addr, config.network);
const hash = bitcoin.crypto.sha256(script);
let reversedHash = Buffer.from(reverse(hash));
reversedHash = reversedHash.toString('hex');
scripthashes.push(reversedHash);
scripthash2addr[reversedHash] = addr;
}
const balances = await mainClient.blockchainScripthash_getBalanceBatch(scripthashes);
for (const bal of balances) {
ret.incoming_balance += +bal.result.alert_incoming;
ret.outgoing_balance += +bal.result.alert_outgoing;
ret.balance += +bal.result.confirmed;
ret.unconfirmed_balance += +bal.result.unconfirmed;
ret.addresses[scripthash2addr[bal.param]] = bal.result;
}
}
return ret;
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
module.exports.multiGetUtxoByAddress = async function(addresses, batchsize) {
try {
batchsize = batchsize || 100;
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
const ret = {};
const res = [];
const chunks = splitIntoChunks(addresses, batchsize);
for (const chunk of chunks) {
const scripthashes = [];
const scripthash2addr = {};
for (const addr of chunk) {
const script = bitcoin.address.toOutputScript(addr, config.network);
const hash = bitcoin.crypto.sha256(script);
let reversedHash = Buffer.from(reverse(hash));
reversedHash = reversedHash.toString('hex');
scripthashes.push(reversedHash);
scripthash2addr[reversedHash] = addr;
}
const results = await mainClient.blockchainScripthash_listunspentBatch(scripthashes);
for (const utxos of results) {
ret[scripthash2addr[utxos.param]] = utxos.result;
for (const utxo of ret[scripthash2addr[utxos.param]]) {
utxo.address = scripthash2addr[utxos.param];
utxo.txid = utxo.tx_hash;
utxo.vout = utxo.tx_pos;
utxo.spend_tx_num = utxo.spend_tx_num;
res.push(utxo);
}
}
}
return res;
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
module.exports.multiGetHistoryByAddress = async function(addresses, batchsize) {
try {
batchsize = batchsize || 100;
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
const ret = {};
const chunks = splitIntoChunks(addresses, batchsize);
for (const chunk of chunks) {
const scripthashes = [];
const scripthash2addr = {};
for (const addr of chunk) {
const script = bitcoin.address.toOutputScript(addr, config.network);
const hash = bitcoin.crypto.sha256(script);
let reversedHash = Buffer.from(reverse(hash));
reversedHash = reversedHash.toString('hex');
scripthashes.push(reversedHash);
scripthash2addr[reversedHash] = addr;
}
const results = await mainClient.blockchainScripthash_getHistoryBatch(scripthashes);
for (const history of results) {
ret[scripthash2addr[history.param]] = history.result;
for (const hist of ret[scripthash2addr[history.param]]) {
hist.address = scripthash2addr[history.param];
}
}
}
return ret;
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
module.exports.multiGetTransactionByTxid = async function(txids, batchsize, verbose) {
try {
batchsize = batchsize || 100;
verbose = verbose !== false;
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
const ret = {};
const chunks = splitIntoChunks(txids, batchsize);
for (const chunk of chunks) {
const results = await mainClient.blockchainTransaction_getBatch(chunk, verbose);
for (const txdata of results) {
ret[txdata.param] = txdata.result;
}
}
return ret;
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
/**
* Simple waiter till `mainConnected` becomes true (which means
* it Electrum was connected in other function), or timeout 30 sec.
*
*
* @returns {Promise<Promise<*> | Promise<*>>}
*/
module.exports.waitTillConnected = async function() {
try {
const retriesMax = 30;
for (let i = 0; i < retriesMax; i++) {
if (mainConnected) {
return true;
}
await wait(1000);
}
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError('Waiting for Electrum connection timeout');
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
module.exports.estimateFees = async function() {
try {
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
let fast = await mainClient.blockchainEstimatefee(1);
let medium = await mainClient.blockchainEstimatefee(5);
let slow = await mainClient.blockchainEstimatefee(10);
if (fast < 1) fast = 1;
if (medium < 1) medium = 1;
if (slow < 1) slow = 1;
return { fast, medium, slow };
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
/**
* Returns the estimated transaction fee to be confirmed within a certain number of blocks
*
* @param numberOfBlocks {number} The number of blocks to target for confirmation
* @returns {Promise<number>} Satoshis per byte
*/
module.exports.estimateFee = async function(numberOfBlocks) {
try {
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
numberOfBlocks = numberOfBlocks || 1;
const coinUnitsPerKilobyte = await mainClient.blockchainEstimatefee(numberOfBlocks);
if (coinUnitsPerKilobyte < 1) return 1;
return Math.round(
new BigNumber(coinUnitsPerKilobyte)
.dividedBy(1024)
.multipliedBy(100000000)
.toNumber(),
);
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
module.exports.getDustValue = async () => {
try {
const relayFee = await mainClient.blockchain_relayfee();
// magic numbers from electrum vault
return btcToSatoshi((183 * 3 * relayFee) / 1000, 0);
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
module.exports.broadcast = async function(hex) {
try {
if (!mainConnected) throw new AppErrors.ElectrumXConnectionError();
try {
return await mainClient.blockchainTransaction_broadcast(hex);
} catch (error) {
if (error?.code === 1) {
if (error.message.includes(messages.txnMempoolConflictCode18)) {
throw new AppErrors.DoubleSpentFundsError();
}
if (error.message.includes(messages.missingInputs)) {
throw new AppErrors.NotExistingFundsError();
}
if (error.message.includes(messages.dustCode64)) {
throw new AppErrors.DustError();
}
throw new AppErrors.BroadcastError(error.message);
}
throw error;
}
} catch (error) {
logger.error({
message: error.message,
category: error.name,
});
}
};
/**
*
* @param host
* @param tcpPort
* @returns {Promise<boolean>} Whether provided host:port is a valid electrum server
*/
module.exports.testConnection = async function(host, tcpPort) {
const client = new ElectrumClient(tcpPort, host, 'tcp');
try {
await client.connect();
await client.server_version('2.7.11', '1.4');
await client.server_ping();
client.close();
return true;
} catch (_) {
return false;
}
};
module.exports.forceDisconnect = () => {
mainClient.close();
};
module.exports.hardcodedPeers = hardcodedPeers;
const splitIntoChunks = function(arr, chunkSize) {
const groups = [];
let i;
for (i = 0; i < arr.length; i += chunkSize) {
groups.push(arr.slice(i, i + chunkSize));
}
return groups;
};