forked from you21979/node-electrum-client
-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.js
169 lines (158 loc) · 5.49 KB
/
index.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
'use strict';
const Client = require('./lib/client');
class ElectrumClient extends Client {
constructor(net, tls, port, host, protocol, options) {
super(net, tls, port, host, protocol, options);
this.timeLastCall = 0;
}
initElectrum(electrumConfig, persistencePolicy = { maxRetry: 1000, callback: null }) {
this.persistencePolicy = persistencePolicy;
this.electrumConfig = electrumConfig;
this.timeLastCall = 0;
return this.connect().then(() => this.server_version(this.electrumConfig.client, this.electrumConfig.version));
}
// Override parent
request(method, params) {
this.timeLastCall = new Date().getTime();
const parentPromise = super.request(method, params);
return parentPromise.then(response => {
this.keepAlive();
return response;
});
}
requestBatch(method, params, secondParam) {
this.timeLastCall = new Date().getTime();
const parentPromise = super.requestBatch(method, params, secondParam);
return parentPromise.then(response => {
this.keepAlive();
return response;
});
}
onClose() {
super.onClose();
const list = [
'blockchain.headers.subscribe',
'blockchain.scripthash.subscribe',
];
list.forEach(event => this.subscribe.removeAllListeners(event));
setTimeout(() => {
if (this.persistencePolicy != null && this.persistencePolicy.maxRetry > 0) {
this.reconnect();
this.persistencePolicy.maxRetry -= 1;
} else if (this.persistencePolicy != null && this.persistencePolicy.callback != null) {
this.persistencePolicy.callback();
} else if (this.persistencePolicy == null) {
this.reconnect();
}
}, 1000);
}
// ElectrumX persistancy
keepAlive() {
if (this.timeout != null) {
clearTimeout(this.timeout);
}
this.timeout = setTimeout(() => {
if (this.timeLastCall !== 0 && new Date().getTime() > this.timeLastCall + 5000) {
const pingTimer = setTimeout(() => {
this.onError(new Error('keepalive ping timeout'));
}, 9000);
this.server_ping().catch((reason) => {
console.log('keepalive ping failed because of', reason);
clearTimeout(pingTimer);
}).then(() => clearTimeout(pingTimer));
}
}, 5000);
}
close() {
super.close();
if (this.timeout != null) {
clearTimeout(this.timeout);
}
this.reconnect = this.reconnect = this.onClose = this.keepAlive = () => {}; // dirty hack to make it stop reconnecting
}
reconnect() {
console.log('electrum reconnect');
this.initSocket();
return this.initElectrum(this.electrumConfig);
}
// ElectrumX API
server_version(client_name, protocol_version) {
return this.request('server.version', [client_name, protocol_version]);
}
server_banner() {
return this.request('server.banner', []);
}
server_features() {
return this.request('server.features', []);
}
server_ping() {
return this.request('server.ping', []);
}
server_addPeer(features) {
return this.request('server.add_peer', [features]);
}
serverDonation_address() {
return this.request('server.donation_address', []);
}
serverPeers_subscribe() {
return this.request('server.peers.subscribe', []);
}
blockchainAddress_getProof(address) {
return this.request('blockchain.address.get_proof', [address]);
}
blockchainScripthash_getBalance(scripthash) {
return this.request('blockchain.scripthash.get_balance', [scripthash]);
}
blockchainScripthash_getBalanceBatch(scripthash) {
return this.requestBatch('blockchain.scripthash.get_balance', scripthash);
}
blockchainScripthash_listunspentBatch(scripthash) {
return this.requestBatch('blockchain.scripthash.listunspent', scripthash);
}
blockchainScripthash_getHistory(scripthash) {
return this.request('blockchain.scripthash.get_history', [scripthash]);
}
blockchainScripthash_getHistoryBatch(scripthash) {
return this.requestBatch('blockchain.scripthash.get_history', scripthash);
}
blockchainScripthash_getMempool(scripthash) {
return this.request('blockchain.scripthash.get_mempool', [scripthash]);
}
blockchainScripthash_listunspent(scripthash) {
return this.request('blockchain.scripthash.listunspent', [scripthash]);
}
blockchainScripthash_subscribe(scripthash) {
return this.request('blockchain.scripthash.subscribe', [scripthash]);
}
blockchainBlock_header(height) {
return this.request('blockchain.block.header', [height]);
}
blockchainBlock_headers(start_height, count) {
return this.request('blockchain.block.headeres', [start_height, count]);
}
blockchainEstimatefee(number) {
return this.request('blockchain.estimatefee', [number]);
}
blockchainHeaders_subscribe() {
return this.request('blockchain.headers.subscribe', []);
}
blockchain_relayfee() {
return this.request('blockchain.relayfee', []);
}
blockchainTransaction_broadcast(rawtx) {
return this.request('blockchain.transaction.broadcast', [rawtx]);
}
blockchainTransaction_get(tx_hash, verbose) {
return this.request('blockchain.transaction.get', [tx_hash, verbose || false]);
}
blockchainTransaction_getBatch(tx_hash, verbose) {
return this.requestBatch('blockchain.transaction.get', tx_hash, verbose);
}
blockchainTransaction_getMerkle(tx_hash, height) {
return this.request('blockchain.transaction.get_merkle', [tx_hash, height]);
}
mempool_getFeeHistogram() {
return this.request('mempool.get_fee_histogram', []);
}
}
module.exports = ElectrumClient;