@electrum-cash/network 4.0.0-development.6655313659 → 4.0.0-development.8017810652
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +47 -68
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +101 -484
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -3
package/dist/index.mjs
CHANGED
|
@@ -1,33 +1,14 @@
|
|
|
1
|
+
import $dvphU$electrumcashdebuglogs from "@electrum-cash/debug-logs";
|
|
1
2
|
import {EventEmitter as $dvphU$EventEmitter} from "events";
|
|
2
3
|
import {Mutex as $dvphU$Mutex} from "async-mutex";
|
|
3
|
-
import $dvphU$
|
|
4
|
+
import {ElectrumWebSocket as $dvphU$ElectrumWebSocket} from "@electrum-cash/sockets";
|
|
4
5
|
import {parse as $dvphU$parse, parseNumberAndBigInt as $dvphU$parseNumberAndBigInt} from "lossless-json";
|
|
5
|
-
import {connect as $dvphU$connect, TLSSocket as $dvphU$TLSSocket} from "tls";
|
|
6
|
-
import {isIP as $dvphU$isIP, connect as $dvphU$connect1} from "net";
|
|
7
|
-
import {WebSocket as $dvphU$WebSocket} from "@monsterbitar/isomorphic-ws";
|
|
8
6
|
|
|
9
7
|
|
|
10
8
|
function $parcel$export(e, n, v, s) {
|
|
11
9
|
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
12
10
|
}
|
|
13
11
|
|
|
14
|
-
// Create the debug logs.
|
|
15
|
-
const $623a31cc1663a627$var$debug = {
|
|
16
|
-
client: (0, $dvphU$debug)("electrum-cash:client "),
|
|
17
|
-
errors: (0, $dvphU$debug)("electrum-cash:error "),
|
|
18
|
-
warning: (0, $dvphU$debug)("electrum-cash:warning"),
|
|
19
|
-
network: (0, $dvphU$debug)("electrum-cash:network"),
|
|
20
|
-
ping: (0, $dvphU$debug)("electrum-cash:pulses ")
|
|
21
|
-
};
|
|
22
|
-
// Set log colors.
|
|
23
|
-
$623a31cc1663a627$var$debug.client.color = "2";
|
|
24
|
-
$623a31cc1663a627$var$debug.errors.color = "9";
|
|
25
|
-
$623a31cc1663a627$var$debug.warning.color = "13";
|
|
26
|
-
$623a31cc1663a627$var$debug.network.color = "4";
|
|
27
|
-
$623a31cc1663a627$var$debug.ping.color = "8";
|
|
28
|
-
var // Export the logs.
|
|
29
|
-
$623a31cc1663a627$export$2e2bcd8739ae039 = $623a31cc1663a627$var$debug;
|
|
30
|
-
|
|
31
12
|
|
|
32
13
|
|
|
33
14
|
/**
|
|
@@ -38,9 +19,9 @@ $623a31cc1663a627$export$2e2bcd8739ae039 = $623a31cc1663a627$var$debug;
|
|
|
38
19
|
/**
|
|
39
20
|
* Helper function that builds an Electrum request object.
|
|
40
21
|
*
|
|
41
|
-
* @param
|
|
42
|
-
* @param
|
|
43
|
-
* @param
|
|
22
|
+
* @param method - method to call.
|
|
23
|
+
* @param parameters - method parameters for the call.
|
|
24
|
+
* @param requestId - unique string or number referencing this request.
|
|
44
25
|
*
|
|
45
26
|
* @returns a properly formatted Electrum request string.
|
|
46
27
|
*/ static buildRequestObject(method, parameters, requestId) {
|
|
@@ -87,354 +68,17 @@ const $e83d2e7688025acd$export$9598f0c76aa41d73 = function(object) {
|
|
|
87
68
|
|
|
88
69
|
var $d801b1f9b7fc3074$exports = {};
|
|
89
70
|
|
|
90
|
-
$parcel$export($d801b1f9b7fc3074$exports, "
|
|
91
|
-
$parcel$export($d801b1f9b7fc3074$exports, "
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
Port: 50002,
|
|
99
|
-
Scheme: "tcp_tls"
|
|
100
|
-
},
|
|
101
|
-
WS: {
|
|
102
|
-
Port: 50003,
|
|
103
|
-
Scheme: "ws"
|
|
104
|
-
},
|
|
105
|
-
WSS: {
|
|
106
|
-
Port: 50004,
|
|
107
|
-
Scheme: "wss"
|
|
108
|
-
}
|
|
109
|
-
};
|
|
110
|
-
const $d801b1f9b7fc3074$export$f019be48b3aacb1a = {
|
|
111
|
-
// Port number for TCP TLS connections
|
|
112
|
-
PORT: $d801b1f9b7fc3074$export$d048df559e6d3842.TCP_TLS.Port,
|
|
113
|
-
// Transport to connect to the Electrum server
|
|
114
|
-
TRANSPORT_SCHEME: $d801b1f9b7fc3074$export$d048df559e6d3842.TCP_TLS.Scheme,
|
|
115
|
-
// How long to wait before attempting to reconnect, in milliseconds.
|
|
116
|
-
RECONNECT: 5000,
|
|
117
|
-
// How long to wait for network operations before following up, in milliseconds.
|
|
118
|
-
TIMEOUT: 30000,
|
|
119
|
-
// Time between ping messages, in milliseconds. Pinging keeps the connection alive.
|
|
120
|
-
// The reason for pinging this frequently is to detect connection problems early.
|
|
121
|
-
PING_INTERVAL: 1000,
|
|
122
|
-
// If we use BigInt for numbers in json when parsing and returning json response from the server.
|
|
123
|
-
USE_BIG_INT: false
|
|
124
|
-
};
|
|
125
|
-
|
|
71
|
+
$parcel$export($d801b1f9b7fc3074$exports, "DEFAULT_PING_INTERVAL", () => $d801b1f9b7fc3074$export$a3a6b8a974871191);
|
|
72
|
+
$parcel$export($d801b1f9b7fc3074$exports, "DEFAULT_RECONNECT_INTERVAL", () => $d801b1f9b7fc3074$export$b2115084b892a79d);
|
|
73
|
+
$parcel$export($d801b1f9b7fc3074$exports, "DEFAULT_BIG_INTEGERS", () => $d801b1f9b7fc3074$export$788e8376894ebb88);
|
|
74
|
+
// Time between ping messages, in milliseconds. Pinging keeps the connection alive.
|
|
75
|
+
// The reason for pinging this frequently is to detect connection problems early.
|
|
76
|
+
const $d801b1f9b7fc3074$export$a3a6b8a974871191 = 1000;
|
|
77
|
+
const $d801b1f9b7fc3074$export$b2115084b892a79d = 5000;
|
|
78
|
+
const $d801b1f9b7fc3074$export$788e8376894ebb88 = false;
|
|
126
79
|
|
|
127
80
|
|
|
128
81
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
class $0a4cf22b9d6c493c$export$22c0ca2c816c3e08 extends (0, $dvphU$EventEmitter) {
|
|
135
|
-
/**
|
|
136
|
-
* Connect to host:port using the specified transport
|
|
137
|
-
*
|
|
138
|
-
* @param {string} host Fully qualified domain name or IP address of the host
|
|
139
|
-
* @param {number} port Network port for the host to connect to
|
|
140
|
-
* @param {TransportScheme} scheme Transport scheme to use
|
|
141
|
-
* @param {number} timeout If no connection is established after `timeout` ms, the connection is terminated
|
|
142
|
-
*
|
|
143
|
-
* @throws {Error} if an incorrect transport scheme is specified
|
|
144
|
-
*/ connect(host, port, scheme, timeout) {
|
|
145
|
-
// Check that no existing socket exists before initiating a new connection.
|
|
146
|
-
if (this.tcpSocket) throw new Error("Cannot initiate a new socket connection when an existing connection exists");
|
|
147
|
-
// Set a timer to force disconnect after `timeout` seconds
|
|
148
|
-
this.timers.disconnect = setTimeout(()=>this.disconnectOnTimeout(host, port, timeout), timeout);
|
|
149
|
-
// Remove the timer if a connection is successfully established
|
|
150
|
-
this.once("connect", this.clearDisconnectTimerOnTimeout);
|
|
151
|
-
// Define how to refer to the connection scheme in debug output.
|
|
152
|
-
const socketTypes = {
|
|
153
|
-
[(0, $d801b1f9b7fc3074$export$d048df559e6d3842).TCP.Scheme]: "a TCP Socket",
|
|
154
|
-
[(0, $d801b1f9b7fc3074$export$d048df559e6d3842).TCP_TLS.Scheme]: "an encrypted TCP socket"
|
|
155
|
-
};
|
|
156
|
-
// Log that we are trying to establish a connection.
|
|
157
|
-
(0, $623a31cc1663a627$export$2e2bcd8739ae039).network(`Initiating ${socketTypes[scheme]} connection to '${host}:${port}'.`);
|
|
158
|
-
if (scheme !== (0, $d801b1f9b7fc3074$export$d048df559e6d3842).TCP.Scheme && scheme !== (0, $d801b1f9b7fc3074$export$d048df559e6d3842).TCP_TLS.Scheme) // Throw an error if an incorrect transport is specified
|
|
159
|
-
throw new Error("Incorrect transport specified");
|
|
160
|
-
if (scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).TCP_TLS.Scheme) {
|
|
161
|
-
// Initialize connection options.
|
|
162
|
-
const connectionOptions = {
|
|
163
|
-
rejectUnauthorized: false
|
|
164
|
-
};
|
|
165
|
-
// If the hostname is not an IP address..
|
|
166
|
-
if (!$dvphU$isIP(host)) // Set the servername option which enables support for SNI.
|
|
167
|
-
// NOTE: SNI enables a server that hosts multiple domains to provide the appropriate TLS certificate.
|
|
168
|
-
connectionOptions.serverName = host;
|
|
169
|
-
// Initialize the socket (allowing self-signed certificates).
|
|
170
|
-
this.tcpSocket = $dvphU$connect(port, host, connectionOptions);
|
|
171
|
-
// Add a 'secureConnect' listener that checks the authorization status of
|
|
172
|
-
// the socket, and logs a warning when it uses a self signed certificate.
|
|
173
|
-
this.tcpSocket.once("secureConnect", ()=>{
|
|
174
|
-
// Cannot happen, since this event callback *only* exists on TLSSocket
|
|
175
|
-
if (!(this.tcpSocket instanceof $dvphU$TLSSocket)) return;
|
|
176
|
-
// Force cast authorizationError from Error to string (through unknown)
|
|
177
|
-
// because it is incorrectly typed as an Error
|
|
178
|
-
const authorizationError = this.tcpSocket.authorizationError;
|
|
179
|
-
if (authorizationError === "DEPTH_ZERO_SELF_SIGNED_CERT") (0, $623a31cc1663a627$export$2e2bcd8739ae039).warning(`Connection to ${host}:${port} uses a self-signed certificate`);
|
|
180
|
-
});
|
|
181
|
-
// Trigger successful connection events.
|
|
182
|
-
this.tcpSocket.on("secureConnect", this.onConnect.bind(this, socketTypes[scheme], host, port));
|
|
183
|
-
} else {
|
|
184
|
-
// Initialize the socket.
|
|
185
|
-
this.tcpSocket = $dvphU$connect1({
|
|
186
|
-
host: host,
|
|
187
|
-
port: port
|
|
188
|
-
});
|
|
189
|
-
// Trigger successful connection events.
|
|
190
|
-
this.tcpSocket.on("connect", this.onConnect.bind(this, socketTypes[scheme], host, port));
|
|
191
|
-
}
|
|
192
|
-
// Configure encoding.
|
|
193
|
-
this.tcpSocket.setEncoding("utf8");
|
|
194
|
-
// Enable persistent connections.
|
|
195
|
-
// NOTE: This will send a non-data message 0.25 second after last activity.
|
|
196
|
-
// After 10 consecutive such messages with no response, the connection will be cut.
|
|
197
|
-
this.tcpSocket.setKeepAlive(true, 250);
|
|
198
|
-
// Disable buffering of outgoing data.
|
|
199
|
-
this.tcpSocket.setNoDelay(true);
|
|
200
|
-
// Forward the encountered errors.
|
|
201
|
-
this.tcpSocket.on("error", this.eventForwarders.tcpError);
|
|
202
|
-
}
|
|
203
|
-
/**
|
|
204
|
-
* Sets up forwarding of events related to the connection.
|
|
205
|
-
*
|
|
206
|
-
* @param {string} connectionType Name of the connection/transport type, used for logging.
|
|
207
|
-
* @param {string} host Fully qualified domain name or IP address of the host
|
|
208
|
-
* @param {number} port Network port for the host to connect to
|
|
209
|
-
*/ onConnect(connectionType, host, port) {
|
|
210
|
-
// If the onConnect function has already run, do not execute it again.
|
|
211
|
-
if (this.onConnectHasRun) return;
|
|
212
|
-
// Log that the connection has been established.
|
|
213
|
-
(0, $623a31cc1663a627$export$2e2bcd8739ae039).network(`Established ${connectionType} connection with '${host}:${port}'.`);
|
|
214
|
-
// Forward the socket events
|
|
215
|
-
this.tcpSocket.addListener("close", this.eventForwarders.disconnect);
|
|
216
|
-
this.tcpSocket.addListener("data", this.eventForwarders.tcpData);
|
|
217
|
-
// Indicate that the onConnect function has run.
|
|
218
|
-
this.onConnectHasRun = true;
|
|
219
|
-
// Emit the connect event.
|
|
220
|
-
this.emit("connect");
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Clears the disconnect timer if it is still active.
|
|
224
|
-
*/ clearDisconnectTimerOnTimeout() {
|
|
225
|
-
// Clear the retry timer if it is still active.
|
|
226
|
-
if (this.timers.disconnect) clearTimeout(this.timers.disconnect);
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Forcibly terminate the connection.
|
|
230
|
-
*
|
|
231
|
-
* @throws {Error} if no connection was found
|
|
232
|
-
*/ disconnect() {
|
|
233
|
-
// Clear the disconnect timer so that the socket does not try to disconnect again later.
|
|
234
|
-
this.clearDisconnectTimerOnTimeout();
|
|
235
|
-
if (this.tcpSocket) {
|
|
236
|
-
// Remove all event forwarders.
|
|
237
|
-
this.tcpSocket.removeListener("close", this.eventForwarders.disconnect);
|
|
238
|
-
this.tcpSocket.removeListener("data", this.eventForwarders.tcpData);
|
|
239
|
-
this.tcpSocket.removeListener("error", this.eventForwarders.tcpError);
|
|
240
|
-
// Terminate the connection.
|
|
241
|
-
this.tcpSocket.destroy();
|
|
242
|
-
// Remove the stored socket.
|
|
243
|
-
this.tcpSocket = undefined;
|
|
244
|
-
}
|
|
245
|
-
// Indicate that the onConnect function has not run and it has to be run again.
|
|
246
|
-
this.onConnectHasRun = false;
|
|
247
|
-
// Emit a disconnect event
|
|
248
|
-
this.emit("disconnect");
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Write data to the socket
|
|
252
|
-
*
|
|
253
|
-
* @param {Uint8Array | string} data Data to be written to the socket
|
|
254
|
-
* @param {function} callback Callback function to be called when the write has completed
|
|
255
|
-
*
|
|
256
|
-
* @throws {Error} if no connection was found
|
|
257
|
-
* @returns true if the message was fully flushed to the socket, false if part of the message
|
|
258
|
-
* is queued in the user memory
|
|
259
|
-
*/ write(data, callback) {
|
|
260
|
-
// Throw an error if no active connection is found
|
|
261
|
-
if (!this.tcpSocket) throw new Error("Cannot write to socket when there is no active connection");
|
|
262
|
-
// Write data to the TLS Socket and return the status indicating
|
|
263
|
-
// whether the full message was flushed to the socket
|
|
264
|
-
return this.tcpSocket.write(data, callback);
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Force a disconnection if no connection is established after `timeout` milliseconds.
|
|
268
|
-
*
|
|
269
|
-
* @param {string} host Host of the connection that timed out
|
|
270
|
-
* @param {number} port Port of the connection that timed out
|
|
271
|
-
* @param {number} timeout Elapsed milliseconds
|
|
272
|
-
*/ disconnectOnTimeout(host, port, timeout) {
|
|
273
|
-
// Remove the connect listener.
|
|
274
|
-
this.removeListener("connect", this.clearDisconnectTimerOnTimeout);
|
|
275
|
-
// Create a new timeout error.
|
|
276
|
-
const timeoutError = {
|
|
277
|
-
code: "ETIMEDOUT",
|
|
278
|
-
message: `Connection to '${host}:${port}' timed out after ${timeout} milliseconds`
|
|
279
|
-
};
|
|
280
|
-
// Emit an error event so that connect is rejected upstream.
|
|
281
|
-
this.emit("error", timeoutError);
|
|
282
|
-
// Forcibly disconnect to clean up the connection on timeout
|
|
283
|
-
this.disconnect();
|
|
284
|
-
}
|
|
285
|
-
constructor(...args){
|
|
286
|
-
super(...args);
|
|
287
|
-
// Declare timers for keep-alive pings and reconnection
|
|
288
|
-
this.timers = {};
|
|
289
|
-
// Initialize boolean that indicates whether the onConnect function has run (initialize to false).
|
|
290
|
-
this.onConnectHasRun = false;
|
|
291
|
-
// Initialize event forwarding functions.
|
|
292
|
-
this.eventForwarders = {
|
|
293
|
-
disconnect: ()=>this.emit("disconnect"),
|
|
294
|
-
tcpData: (data)=>this.emit("data", data),
|
|
295
|
-
tcpError: (err)=>this.emit("error", err)
|
|
296
|
-
};
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
class $0a4cf22b9d6c493c$export$25b4633f61498e1 extends (0, $dvphU$EventEmitter) {
|
|
300
|
-
/**
|
|
301
|
-
* Connect to host:port using the specified transport
|
|
302
|
-
*
|
|
303
|
-
* @param {string} host Fully qualified domain name or IP address of the host
|
|
304
|
-
* @param {number} port Network port for the host to connect to
|
|
305
|
-
* @param {TransportScheme} scheme Transport scheme to use
|
|
306
|
-
* @param {number} timeout If no connection is established after `timeout` ms, the connection is terminated
|
|
307
|
-
*
|
|
308
|
-
* @throws {Error} if an incorrect transport scheme is specified
|
|
309
|
-
*/ connect(host, port, scheme, timeout) {
|
|
310
|
-
// Check that no existing socket exists before initiating a new connection.
|
|
311
|
-
if (this.webSocket) throw new Error("Cannot initiate a new socket connection when an existing connection exists");
|
|
312
|
-
// Set a timer to force disconnect after `timeout` seconds
|
|
313
|
-
this.timers.disconnect = setTimeout(()=>this.disconnectOnTimeout(host, port, timeout), timeout);
|
|
314
|
-
// Remove the timer if a connection is successfully established
|
|
315
|
-
this.once("connect", this.clearDisconnectTimerOnTimeout);
|
|
316
|
-
// Define how to refer to the connection scheme in debug output.
|
|
317
|
-
const socketTypes = {
|
|
318
|
-
[(0, $d801b1f9b7fc3074$export$d048df559e6d3842).WS.Scheme]: "a WebSocket",
|
|
319
|
-
[(0, $d801b1f9b7fc3074$export$d048df559e6d3842).WSS.Scheme]: "an encrypted WebSocket"
|
|
320
|
-
};
|
|
321
|
-
// Log that we are trying to establish a connection.
|
|
322
|
-
(0, $623a31cc1663a627$export$2e2bcd8739ae039).network(`Initiating ${socketTypes[scheme]} connection to '${host}:${port}'.`);
|
|
323
|
-
if (scheme !== (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WS.Scheme && scheme !== (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WSS.Scheme) // Throw an error if an incorrect transport is specified
|
|
324
|
-
throw new Error("Incorrect transport specified");
|
|
325
|
-
if (scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WSS.Scheme) // Initialize this.webSocket (rejecting self-signed certificates).
|
|
326
|
-
// We reject self-signed certificates to match functionality of browsers.
|
|
327
|
-
this.webSocket = new (0, $dvphU$WebSocket)(`wss://${host}:${port}`);
|
|
328
|
-
else // Initialize this.webSocket.
|
|
329
|
-
this.webSocket = new (0, $dvphU$WebSocket)(`ws://${host}:${port}`);
|
|
330
|
-
// Trigger successful connection events.
|
|
331
|
-
this.webSocket.addEventListener("open", this.onConnect.bind(this, socketTypes[scheme], host, port));
|
|
332
|
-
// Forward the encountered errors.
|
|
333
|
-
this.webSocket.addEventListener("error", this.eventForwarders.wsError);
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Sets up forwarding of events related to the connection.
|
|
337
|
-
*
|
|
338
|
-
* @param {string} connectionType Name of the connection/transport type, used for logging.
|
|
339
|
-
* @param {string} host Fully qualified domain name or IP address of the host
|
|
340
|
-
* @param {number} port Network port for the host to connect to
|
|
341
|
-
*/ onConnect(connectionType, host, port) {
|
|
342
|
-
// If the onConnect function has already run, do not execute it again.
|
|
343
|
-
if (this.onConnectHasRun) return;
|
|
344
|
-
// Log that the connection has been established.
|
|
345
|
-
(0, $623a31cc1663a627$export$2e2bcd8739ae039).network(`Established ${connectionType} connection with '${host}:${port}'.`);
|
|
346
|
-
// Forward the socket events
|
|
347
|
-
this.webSocket.addEventListener("close", this.eventForwarders.disconnect);
|
|
348
|
-
this.webSocket.addEventListener("message", this.eventForwarders.wsData);
|
|
349
|
-
// Indicate that the onConnect function has run.
|
|
350
|
-
this.onConnectHasRun = true;
|
|
351
|
-
// Emit the connect event.
|
|
352
|
-
this.emit("connect");
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* Clears the disconnect timer if it is still active.
|
|
356
|
-
*/ clearDisconnectTimerOnTimeout() {
|
|
357
|
-
// Clear the retry timer if it is still active.
|
|
358
|
-
if (this.timers.disconnect) clearTimeout(this.timers.disconnect);
|
|
359
|
-
}
|
|
360
|
-
/**
|
|
361
|
-
* Forcibly terminate the connection.
|
|
362
|
-
*
|
|
363
|
-
* @throws {Error} if no connection was found
|
|
364
|
-
*/ disconnect() {
|
|
365
|
-
// Clear the disconnect timer so that the socket does not try to disconnect again later.
|
|
366
|
-
this.clearDisconnectTimerOnTimeout();
|
|
367
|
-
try {
|
|
368
|
-
// Remove all event forwarders.
|
|
369
|
-
this.webSocket.removeEventListener("close", this.eventForwarders.disconnect);
|
|
370
|
-
this.webSocket.removeEventListener("message", this.eventForwarders.wsData);
|
|
371
|
-
this.webSocket.removeEventListener("error", this.eventForwarders.wsError);
|
|
372
|
-
// Gracefully terminate the connection.
|
|
373
|
-
this.webSocket.close();
|
|
374
|
-
} catch (ignored) {
|
|
375
|
-
// close() will throw an error if the connection has not been established yet.
|
|
376
|
-
// We ignore this error, since no similar error gets thrown in the TLS Socket.
|
|
377
|
-
} finally{
|
|
378
|
-
// Remove the stored socket regardless of any thrown errors.
|
|
379
|
-
this.webSocket = undefined;
|
|
380
|
-
}
|
|
381
|
-
// Indicate that the onConnect function has not run and it has to be run again.
|
|
382
|
-
this.onConnectHasRun = false;
|
|
383
|
-
// Emit a disconnect event
|
|
384
|
-
this.emit("disconnect");
|
|
385
|
-
}
|
|
386
|
-
/**
|
|
387
|
-
* Write data to the socket
|
|
388
|
-
*
|
|
389
|
-
* @param {Uint8Array | string} data Data to be written to the socket
|
|
390
|
-
* @param {function} callback Callback function to be called when the write has completed
|
|
391
|
-
*
|
|
392
|
-
* @throws {Error} if no connection was found
|
|
393
|
-
* @returns true if the message was fully flushed to the socket, false if part of the message
|
|
394
|
-
* is queued in the user memory
|
|
395
|
-
*/ write(data, callback) {
|
|
396
|
-
// Throw an error if no active connection is found
|
|
397
|
-
if (!this.webSocket) throw new Error("Cannot write to socket when there is no active connection");
|
|
398
|
-
// Write data to the WebSocket
|
|
399
|
-
this.webSocket.send(data, callback);
|
|
400
|
-
// WebSockets always fit everything in a single request, so we return true
|
|
401
|
-
return true;
|
|
402
|
-
}
|
|
403
|
-
/**
|
|
404
|
-
* Force a disconnection if no connection is established after `timeout` milliseconds.
|
|
405
|
-
*
|
|
406
|
-
* @param {string} host Host of the connection that timed out
|
|
407
|
-
* @param {number} port Port of the connection that timed out
|
|
408
|
-
* @param {number} timeout Elapsed milliseconds
|
|
409
|
-
*/ disconnectOnTimeout(host, port, timeout) {
|
|
410
|
-
// Remove the connect listener.
|
|
411
|
-
this.removeListener("connect", this.clearDisconnectTimerOnTimeout);
|
|
412
|
-
// Create a new timeout error.
|
|
413
|
-
const timeoutError = {
|
|
414
|
-
code: "ETIMEDOUT",
|
|
415
|
-
message: `Connection to '${host}:${port}' timed out after ${timeout} milliseconds`
|
|
416
|
-
};
|
|
417
|
-
// Emit an error event so that connect is rejected upstream.
|
|
418
|
-
this.emit("error", timeoutError);
|
|
419
|
-
// Forcibly disconnect to clean up the connection on timeout
|
|
420
|
-
this.disconnect();
|
|
421
|
-
}
|
|
422
|
-
constructor(...args){
|
|
423
|
-
super(...args);
|
|
424
|
-
// Declare timers for keep-alive pings and reconnection
|
|
425
|
-
this.timers = {};
|
|
426
|
-
// Initialize boolean that indicates whether the onConnect function has run (initialize to false).
|
|
427
|
-
this.onConnectHasRun = false;
|
|
428
|
-
// Initialize event forwarding functions.
|
|
429
|
-
this.eventForwarders = {
|
|
430
|
-
disconnect: ()=>this.emit("disconnect"),
|
|
431
|
-
wsData: (event)=>this.emit("data", `${event.data}\n`),
|
|
432
|
-
wsError: (event)=>this.emit("error", event.error)
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
|
|
438
82
|
var $db7c797e63383364$exports = {};
|
|
439
83
|
|
|
440
84
|
$parcel$export($db7c797e63383364$exports, "ClientState", () => $db7c797e63383364$export$c4f81c6d30ca200f);
|
|
@@ -467,38 +111,39 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
467
111
|
/**
|
|
468
112
|
* Sets up network configuration for an Electrum client connection.
|
|
469
113
|
*
|
|
470
|
-
* @param
|
|
471
|
-
* @param
|
|
472
|
-
* @param
|
|
473
|
-
* @param
|
|
474
|
-
* @param
|
|
475
|
-
* @param
|
|
476
|
-
* @param {number} pingInterval the time between sending pings to the electrum host, in milliseconds.
|
|
477
|
-
* @param {number} reconnectInterval the time between reconnection attempts to the electrum host, in milliseconds.
|
|
478
|
-
* @param {boolean} useBigInt whether to use bigint for numbers in json response.
|
|
114
|
+
* @param application - your application name, used to identify to the electrum host.
|
|
115
|
+
* @param version - protocol version to use with the host.
|
|
116
|
+
* @param socketOrHostname - pre-configured electrum socket or fully qualified domain name or IP number of the host
|
|
117
|
+
* @param pingInterval - the time between sending pings to the electrum host, in milliseconds.
|
|
118
|
+
* @param reconnectInterval - the time between reconnection attempts to the electrum host, in milliseconds.
|
|
119
|
+
* @param useBigInt - whether to use bigint for numbers in json response.
|
|
479
120
|
*
|
|
480
121
|
* @throws {Error} if `version` is not a valid version string.
|
|
481
|
-
*/ constructor(application, version,
|
|
122
|
+
*/ constructor(application, version, socketOrHostname, pingInterval = (0, $d801b1f9b7fc3074$export$a3a6b8a974871191), reconnectInterval = (0, $d801b1f9b7fc3074$export$b2115084b892a79d), useBigInt = (0, $d801b1f9b7fc3074$export$788e8376894ebb88)){
|
|
482
123
|
// Initialize the event emitter.
|
|
483
124
|
super();
|
|
484
125
|
this.application = application;
|
|
485
126
|
this.version = version;
|
|
486
|
-
this.
|
|
487
|
-
this.port = port;
|
|
488
|
-
this.scheme = scheme;
|
|
489
|
-
this.timeout = timeout;
|
|
127
|
+
this.socketOrHostname = socketOrHostname;
|
|
490
128
|
this.pingInterval = pingInterval;
|
|
491
129
|
this.reconnectInterval = reconnectInterval;
|
|
492
130
|
this.useBigInt = useBigInt;
|
|
493
131
|
this.status = (0, $db7c797e63383364$export$7516420eb880ab68).DISCONNECTED;
|
|
494
|
-
this.timers = {};
|
|
495
132
|
this.verifications = [];
|
|
496
133
|
this.messageBuffer = "";
|
|
497
134
|
// Check if the provided version is a valid version number.
|
|
498
135
|
if (!(0, $24139611f53a54b8$export$2e2bcd8739ae039).versionRegexp.test(version)) // Throw an error since the version number was not valid.
|
|
499
136
|
throw new Error(`Provided version string (${version}) is not a valid protocol version number.`);
|
|
500
|
-
//
|
|
501
|
-
|
|
137
|
+
// If a hostname was provided..
|
|
138
|
+
if (typeof socketOrHostname === "string") // Use a web socket with default parameters.
|
|
139
|
+
this.socket = new (0, $dvphU$ElectrumWebSocket)(socketOrHostname);
|
|
140
|
+
else // Use the provided socket.
|
|
141
|
+
this.socket = socketOrHostname;
|
|
142
|
+
// Set up handlers for connection and disconnection.
|
|
143
|
+
this.socket.on("connect", this.onSocketConnect.bind(this));
|
|
144
|
+
this.socket.on("disconnect", this.onSocketDisconnect.bind(this));
|
|
145
|
+
// Set up handler for incoming data.
|
|
146
|
+
this.socket.on("data", this.parseMessageChunk.bind(this));
|
|
502
147
|
// Handle visibility changes when run in a browser environment.
|
|
503
148
|
if (typeof document !== "undefined") document.addEventListener("visibilitychange", this.handleVisibilityChange.bind(this));
|
|
504
149
|
// Handle network connection changes when run in a browser environment.
|
|
@@ -507,36 +152,18 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
507
152
|
window.addEventListener("offline", this.handleNetworkChange.bind(this));
|
|
508
153
|
}
|
|
509
154
|
}
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
return `${this.host}:${this.port}`;
|
|
155
|
+
// Expose hostIdentifier from the socket.
|
|
156
|
+
get hostIdentifier() {
|
|
157
|
+
return this.socket.hostIdentifier;
|
|
514
158
|
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
if (this.scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).TCP.Scheme || this.scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).TCP_TLS.Scheme) // Initialize a new ElectrumTcpSocket
|
|
519
|
-
this.socket = new (0, $0a4cf22b9d6c493c$export$22c0ca2c816c3e08)();
|
|
520
|
-
else if (this.scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WS.Scheme || this.scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WSS.Scheme) // Initialize a new ElectrumWebSocket
|
|
521
|
-
this.socket = new (0, $0a4cf22b9d6c493c$export$25b4633f61498e1)();
|
|
522
|
-
else // Throw an error if an incorrect transport is specified
|
|
523
|
-
throw new Error(`Provided transport (${this.scheme}) is not a valid ElectrumTransport`);
|
|
524
|
-
// Set up handlers for connection and disconnection.
|
|
525
|
-
this.socket.on("connect", this.onSocketConnect.bind(this));
|
|
526
|
-
this.socket.on("disconnect", this.onSocketDisconnect.bind(this));
|
|
527
|
-
// Set up handler for incoming data.
|
|
528
|
-
this.socket.on("data", this.parseMessageChunk.bind(this));
|
|
529
|
-
}
|
|
530
|
-
/**
|
|
531
|
-
* Shuts down and destroys the current socket.
|
|
532
|
-
*/ destroySocket() {
|
|
533
|
-
// Close the socket connection and destroy the socket.
|
|
534
|
-
this.socket.disconnect();
|
|
159
|
+
// Expose port from the socket.
|
|
160
|
+
get encrypted() {
|
|
161
|
+
return this.socket.encrypted;
|
|
535
162
|
}
|
|
536
163
|
/**
|
|
537
164
|
* Assembles incoming data into statements and hands them off to the message parser.
|
|
538
165
|
*
|
|
539
|
-
* @param
|
|
166
|
+
* @param data - data to append to the current message buffer, as a string.
|
|
540
167
|
*
|
|
541
168
|
* @throws {SyntaxError} if the passed statement parts are not valid JSON.
|
|
542
169
|
*/ parseMessageChunk(data) {
|
|
@@ -601,7 +228,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
601
228
|
* part of the message is queued in the user memory
|
|
602
229
|
*/ ping() {
|
|
603
230
|
// Write a log message.
|
|
604
|
-
(0, $
|
|
231
|
+
(0, $dvphU$electrumcashdebuglogs).ping(`Sending keep-alive ping to '${this.hostIdentifier}'`);
|
|
605
232
|
// Craft a keep-alive message.
|
|
606
233
|
const message = (0, $24139611f53a54b8$export$2e2bcd8739ae039).buildRequestObject("server.ping", [], "keepAlive");
|
|
607
234
|
// Send the keep-alive message.
|
|
@@ -637,7 +264,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
637
264
|
// Define a function to wrap version negotiation as a callback.
|
|
638
265
|
const versionNegotiator = ()=>{
|
|
639
266
|
// Write a log message to show that we have started version negotiation.
|
|
640
|
-
(0, $
|
|
267
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Requesting protocol version ${this.version} with '${this.hostIdentifier}'.`);
|
|
641
268
|
// remove the one-time error handler since no error was detected.
|
|
642
269
|
this.socket.removeListener("error", rejector);
|
|
643
270
|
// Build a version negotiation message.
|
|
@@ -654,7 +281,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
654
281
|
// Declare an error message.
|
|
655
282
|
const errorMessage = "unsupported protocol version.";
|
|
656
283
|
// Log the error.
|
|
657
|
-
(0, $
|
|
284
|
+
(0, $dvphU$electrumcashdebuglogs).errors(`Failed to connect with ${this.hostIdentifier} due to ${errorMessage}`);
|
|
658
285
|
// Reject the connection with false since version negotiation failed.
|
|
659
286
|
reject(errorMessage);
|
|
660
287
|
} else if (version.protocol !== this.version && `${version.protocol}.0` !== this.version && `${version.protocol}.0.0` !== this.version) {
|
|
@@ -663,12 +290,12 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
663
290
|
// Declare an error message.
|
|
664
291
|
const errorMessage = `incompatible protocol version negotiated (${version.protocol} !== ${this.version}).`;
|
|
665
292
|
// Log the error.
|
|
666
|
-
(0, $
|
|
293
|
+
(0, $dvphU$electrumcashdebuglogs).errors(`Failed to connect with ${this.hostIdentifier} due to ${errorMessage}`);
|
|
667
294
|
// Reject the connection with false since version negotiation failed.
|
|
668
295
|
reject(errorMessage);
|
|
669
296
|
} else {
|
|
670
297
|
// Write a log message.
|
|
671
|
-
(0, $
|
|
298
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Negotiated protocol version ${version.protocol} with '${this.hostIdentifier}', powered by ${version.software}.`);
|
|
672
299
|
// Set connection status to connected
|
|
673
300
|
this.status = (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED;
|
|
674
301
|
// Emit a connect event now that the connection is usable.
|
|
@@ -687,7 +314,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
687
314
|
// Set up handler for network errors.
|
|
688
315
|
this.socket.on("error", this.onSocketError.bind(this));
|
|
689
316
|
// Connect to the server.
|
|
690
|
-
this.socket.connect(
|
|
317
|
+
this.socket.connect();
|
|
691
318
|
};
|
|
692
319
|
// Wait until connection is established and version negotiation succeeds.
|
|
693
320
|
await new Promise(connectionResolver);
|
|
@@ -698,14 +325,13 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
698
325
|
// If a reconnect timer is set, remove it
|
|
699
326
|
await this.clearReconnectTimer();
|
|
700
327
|
// Write a log message.
|
|
701
|
-
(0, $
|
|
328
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Trying to reconnect to '${this.hostIdentifier}'..`);
|
|
702
329
|
// Set the status to reconnecting for more accurate log messages.
|
|
703
330
|
this.status = (0, $db7c797e63383364$export$7516420eb880ab68).RECONNECTING;
|
|
704
331
|
// Emit a connect event now that the connection is usable.
|
|
705
332
|
this.emit("reconnecting");
|
|
706
|
-
//
|
|
707
|
-
this.
|
|
708
|
-
this.createSocket();
|
|
333
|
+
// Disconnect the underlying socket.
|
|
334
|
+
this.socket.disconnect();
|
|
709
335
|
try {
|
|
710
336
|
// Try to connect again.
|
|
711
337
|
await this.connect();
|
|
@@ -717,30 +343,30 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
717
343
|
* Removes the current reconnect timer.
|
|
718
344
|
*/ clearReconnectTimer() {
|
|
719
345
|
// If a reconnect timer is set, remove it
|
|
720
|
-
if (this.
|
|
346
|
+
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
721
347
|
// Reset the timer reference.
|
|
722
|
-
this.
|
|
348
|
+
this.reconnectTimer = undefined;
|
|
723
349
|
}
|
|
724
350
|
/**
|
|
725
351
|
* Removes the current keep-alive timer.
|
|
726
352
|
*/ clearKeepAliveTimer() {
|
|
727
353
|
// If a keep-alive timer is set, remove it
|
|
728
|
-
if (this.
|
|
354
|
+
if (this.keepAliveTimer) clearTimeout(this.keepAliveTimer);
|
|
729
355
|
// Reset the timer reference.
|
|
730
|
-
this.
|
|
356
|
+
this.keepAliveTimer = undefined;
|
|
731
357
|
}
|
|
732
358
|
/**
|
|
733
359
|
* Initializes the keep alive timer loop.
|
|
734
360
|
*/ setupKeepAliveTimer() {
|
|
735
361
|
// If the keep-alive timer loop is not currently set up..
|
|
736
|
-
if (!this.
|
|
737
|
-
this.
|
|
362
|
+
if (!this.keepAliveTimer) // Set a new keep-alive timer.
|
|
363
|
+
this.keepAliveTimer = setTimeout(this.ping.bind(this), this.pingInterval);
|
|
738
364
|
}
|
|
739
365
|
/**
|
|
740
366
|
* Tears down the current connection and removes all event listeners on disconnect.
|
|
741
367
|
*
|
|
742
|
-
* @param
|
|
743
|
-
* @param
|
|
368
|
+
* @param force - disconnect even if the connection has not been fully established yet.
|
|
369
|
+
* @param intentional - update connection state if disconnect is intentional.
|
|
744
370
|
*
|
|
745
371
|
* @returns true if successfully disconnected, or false if there was no connection.
|
|
746
372
|
*/ async disconnect(force = false, intentional = true) {
|
|
@@ -761,8 +387,8 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
761
387
|
const disconnectResolver = (resolve)=>{
|
|
762
388
|
// Resolve to true after the connection emits a disconnect
|
|
763
389
|
this.once("disconnected", ()=>resolve(true));
|
|
764
|
-
// Close the connection
|
|
765
|
-
this.
|
|
390
|
+
// Close the connection on the socket level.
|
|
391
|
+
this.socket.disconnect();
|
|
766
392
|
};
|
|
767
393
|
// Return true to indicate that we disconnected.
|
|
768
394
|
return new Promise(disconnectResolver);
|
|
@@ -810,7 +436,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
810
436
|
/**
|
|
811
437
|
* Sends an arbitrary message to the server.
|
|
812
438
|
*
|
|
813
|
-
* @param
|
|
439
|
+
* @param message - json encoded request object to send to the server, as a string.
|
|
814
440
|
*
|
|
815
441
|
* @returns true if the message was fully flushed to the socket, false if part of the message
|
|
816
442
|
* is queued in the user memory
|
|
@@ -820,7 +446,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
820
446
|
// Get the current timestamp in milliseconds.
|
|
821
447
|
const currentTime = Date.now();
|
|
822
448
|
// Follow up and verify that the message got sent..
|
|
823
|
-
const verificationTimer = setTimeout(this.verifySend.bind(this, currentTime), this.timeout);
|
|
449
|
+
const verificationTimer = setTimeout(this.verifySend.bind(this, currentTime), this.socket.timeout);
|
|
824
450
|
// Store the verification timer locally so that it can be cleared when data has been received.
|
|
825
451
|
this.verifications.push(verificationTimer);
|
|
826
452
|
// Set a new keep-alive timer.
|
|
@@ -841,7 +467,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
841
467
|
// Remove the current keep-alive timer if it exists.
|
|
842
468
|
this.clearKeepAliveTimer();
|
|
843
469
|
// Write a notification to the logs.
|
|
844
|
-
(0, $
|
|
470
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Connection to '${this.hostIdentifier}' timed out.`);
|
|
845
471
|
// Close the connection to avoid re-use.
|
|
846
472
|
// NOTE: This initiates reconnection routines if the connection has not
|
|
847
473
|
// been marked as intentionally disconnected.
|
|
@@ -878,18 +504,18 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
878
504
|
// Remove all event listeners
|
|
879
505
|
this.removeAllListeners();
|
|
880
506
|
// Write a log message.
|
|
881
|
-
(0, $
|
|
507
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Disconnected from '${this.hostIdentifier}'.`);
|
|
882
508
|
} else {
|
|
883
509
|
// If this is for an established connection..
|
|
884
510
|
if (this.status === (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED) // Write a notification to the logs.
|
|
885
|
-
(0, $
|
|
511
|
+
(0, $dvphU$electrumcashdebuglogs).errors(`Connection with '${this.hostIdentifier}' was closed, trying to reconnect in ${this.reconnectInterval / 1000} seconds.`);
|
|
886
512
|
// Mark the connection as disconnected for now..
|
|
887
513
|
this.status = (0, $db7c797e63383364$export$7516420eb880ab68).DISCONNECTED;
|
|
888
514
|
// Send a disconnect signal higher up the stack.
|
|
889
515
|
this.emit("disconnected");
|
|
890
516
|
// If we don't have a pending reconnection timer..
|
|
891
|
-
if (!this.
|
|
892
|
-
this.
|
|
517
|
+
if (!this.reconnectTimer) // Attempt to reconnect after one keep-alive duration.
|
|
518
|
+
this.reconnectTimer = setTimeout(this.reconnect.bind(this), this.reconnectInterval);
|
|
893
519
|
}
|
|
894
520
|
}
|
|
895
521
|
/**
|
|
@@ -901,19 +527,8 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
901
527
|
// https://stackoverflow.com/a/18804298
|
|
902
528
|
if (typeof error === "undefined") // Do nothing, and instead rely on the socket disconnect event for further information.
|
|
903
529
|
return;
|
|
904
|
-
//
|
|
905
|
-
|
|
906
|
-
(0, $623a31cc1663a627$export$2e2bcd8739ae039).errors(`Failed to look up DNS records for '${this.host}'.`);
|
|
907
|
-
return;
|
|
908
|
-
}
|
|
909
|
-
// If the connection timed out..
|
|
910
|
-
if (error.code === "ETIMEDOUT") {
|
|
911
|
-
// Log the provided timeout message.
|
|
912
|
-
(0, $623a31cc1663a627$export$2e2bcd8739ae039).errors(error.message);
|
|
913
|
-
return;
|
|
914
|
-
}
|
|
915
|
-
// Log unknown error
|
|
916
|
-
(0, $623a31cc1663a627$export$2e2bcd8739ae039).errors(`Unknown network error ('${this.hostIdentifier}'): `, error);
|
|
530
|
+
// Log the error, as there is nothing we can do to actually handle it.
|
|
531
|
+
(0, $dvphU$electrumcashdebuglogs).errors(`Network error ('${this.hostIdentifier}'): `, error);
|
|
917
532
|
}
|
|
918
533
|
}
|
|
919
534
|
var // Export the connection.
|
|
@@ -958,26 +573,20 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
958
573
|
/**
|
|
959
574
|
* Initializes an Electrum client.
|
|
960
575
|
*
|
|
961
|
-
* @param
|
|
962
|
-
* @param
|
|
963
|
-
* @param
|
|
964
|
-
* @param
|
|
965
|
-
* @param
|
|
966
|
-
* @param
|
|
967
|
-
* @param {number} pingInterval the time between sending pings to the electrum host, in milliseconds.
|
|
968
|
-
* @param {number} reconnectInterval the time between reconnection attempts to the electrum host, in milliseconds.
|
|
969
|
-
* @param {boolean} useBigInt whether to use bigint for numbers in json response.
|
|
576
|
+
* @param application - your application name, used to identify to the electrum host.
|
|
577
|
+
* @param version - protocol version to use with the host.
|
|
578
|
+
* @param socketOrHostname - pre-configured electrum socket or fully qualified domain name or IP number of the host
|
|
579
|
+
* @param pingInterval - the time between sending pings to the electrum host, in milliseconds.
|
|
580
|
+
* @param reconnectInterval - the time between reconnection attempts to the electrum host, in milliseconds.
|
|
581
|
+
* @param useBigInt - whether to use bigint for numbers in json response.
|
|
970
582
|
*
|
|
971
583
|
* @throws {Error} if `version` is not a valid version string.
|
|
972
|
-
*/ constructor(application, version,
|
|
584
|
+
*/ constructor(application, version, socketOrHostname, pingInterval = (0, $d801b1f9b7fc3074$export$a3a6b8a974871191), reconnectInterval = (0, $d801b1f9b7fc3074$export$b2115084b892a79d), useBigInt = (0, $d801b1f9b7fc3074$export$788e8376894ebb88)){
|
|
973
585
|
// Initialize the event emitter.
|
|
974
586
|
super();
|
|
975
587
|
this.application = application;
|
|
976
588
|
this.version = version;
|
|
977
|
-
this.
|
|
978
|
-
this.port = port;
|
|
979
|
-
this.scheme = scheme;
|
|
980
|
-
this.timeout = timeout;
|
|
589
|
+
this.socketOrHostname = socketOrHostname;
|
|
981
590
|
this.pingInterval = pingInterval;
|
|
982
591
|
this.reconnectInterval = reconnectInterval;
|
|
983
592
|
this.useBigInt = useBigInt;
|
|
@@ -987,7 +596,15 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
987
596
|
this.requestResolvers = {};
|
|
988
597
|
this.connectionLock = new (0, $dvphU$Mutex)();
|
|
989
598
|
// Set up a connection to an electrum server.
|
|
990
|
-
this.connection = new (0, $ff134c9a9e1f7361$export$2e2bcd8739ae039)(application, version,
|
|
599
|
+
this.connection = new (0, $ff134c9a9e1f7361$export$2e2bcd8739ae039)(application, version, socketOrHostname, pingInterval, reconnectInterval, useBigInt);
|
|
600
|
+
}
|
|
601
|
+
// Expose hostIdentifier from the connection.
|
|
602
|
+
get hostIdentifier() {
|
|
603
|
+
return this.connection.hostIdentifier;
|
|
604
|
+
}
|
|
605
|
+
// Expose port from the connection.
|
|
606
|
+
get encrypted() {
|
|
607
|
+
return this.connection.encrypted;
|
|
991
608
|
}
|
|
992
609
|
/**
|
|
993
610
|
* Connects to the remote server.
|
|
@@ -1023,8 +640,8 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1023
640
|
/**
|
|
1024
641
|
* Disconnects from the remote server and removes all event listeners/subscriptions and open requests.
|
|
1025
642
|
*
|
|
1026
|
-
* @param
|
|
1027
|
-
* @param
|
|
643
|
+
* @param force - disconnect even if the connection has not been fully established yet.
|
|
644
|
+
* @param retainSubscriptions - retain subscription data so they will be restored on reconnection.
|
|
1028
645
|
*
|
|
1029
646
|
* @returns true if successfully disconnected, or false if there was no connection.
|
|
1030
647
|
*/ async disconnect(force = false, retainSubscriptions = false) {
|
|
@@ -1040,15 +657,15 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1040
657
|
/**
|
|
1041
658
|
* Calls a method on the remote server with the supplied parameters.
|
|
1042
659
|
*
|
|
1043
|
-
* @param
|
|
1044
|
-
* @param
|
|
660
|
+
* @param method - name of the method to call.
|
|
661
|
+
* @param parameters - one or more parameters for the method.
|
|
1045
662
|
*
|
|
1046
663
|
* @throws {Error} if the client is disconnected.
|
|
1047
664
|
* @returns a promise that resolves with the result of the method or an Error.
|
|
1048
665
|
*/ async request(method, ...parameters) {
|
|
1049
666
|
// If we are not connected to a server..
|
|
1050
667
|
if (this.connection.status !== (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED) // Reject the request with a disconnected error message.
|
|
1051
|
-
throw new Error(`Unable to send request to a disconnected server '${this.
|
|
668
|
+
throw new Error(`Unable to send request to a disconnected server '${this.hostIdentifier}'.`);
|
|
1052
669
|
// Increase the request ID by one.
|
|
1053
670
|
this.requestId += 1;
|
|
1054
671
|
// Store a copy of the request id.
|
|
@@ -1069,7 +686,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1069
686
|
this.connection.send(message);
|
|
1070
687
|
};
|
|
1071
688
|
// Write a log message.
|
|
1072
|
-
(0, $
|
|
689
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Sending request '${method}' to '${this.hostIdentifier}'`);
|
|
1073
690
|
// return a promise to deliver results later.
|
|
1074
691
|
return new Promise(requestResolver);
|
|
1075
692
|
}
|
|
@@ -1078,8 +695,8 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1078
695
|
*
|
|
1079
696
|
* @note the response for the subscription request is issued as a notification event.
|
|
1080
697
|
*
|
|
1081
|
-
* @param
|
|
1082
|
-
* @param
|
|
698
|
+
* @param method - one of the subscribable methods the server supports.
|
|
699
|
+
* @param parameters - one or more parameters for the method.
|
|
1083
700
|
*
|
|
1084
701
|
* @throws {Error} if the client is disconnected.
|
|
1085
702
|
* @returns a promise resolving when the subscription is established.
|
|
@@ -1108,15 +725,15 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1108
725
|
* Unsubscribes to the method at the server and removes any callback functions
|
|
1109
726
|
* when there are no more subscriptions for the method.
|
|
1110
727
|
*
|
|
1111
|
-
* @param
|
|
1112
|
-
* @param
|
|
728
|
+
* @param method - a previously subscribed to method.
|
|
729
|
+
* @param parameters - one or more parameters for the method.
|
|
1113
730
|
*
|
|
1114
731
|
* @throws {Error} if no subscriptions exist for the combination of the provided `method` and `parameters.
|
|
1115
732
|
* @throws {Error} if the client is disconnected.
|
|
1116
733
|
* @returns a promise resolving when the subscription is removed.
|
|
1117
734
|
*/ async unsubscribe(method, ...parameters) {
|
|
1118
735
|
// Throw an error if the client is disconnected.
|
|
1119
|
-
if (this.connection.status !== (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED) throw new Error(`Unable to send unsubscribe request to a disconnected server '${this.
|
|
736
|
+
if (this.connection.status !== (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED) throw new Error(`Unable to send unsubscribe request to a disconnected server '${this.hostIdentifier}'.`);
|
|
1120
737
|
// If this method has no subscriptions..
|
|
1121
738
|
if (!this.subscriptionMethods[method]) // Reject this promise with an explanation.
|
|
1122
739
|
throw new Error(`Cannot unsubscribe from '${method}' since the method has no subscriptions.`);
|
|
@@ -1131,7 +748,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1131
748
|
// NOTE: As a convenience we allow users to define the method as the subscribe or unsubscribe version.
|
|
1132
749
|
await this.request(method.replace(".subscribe", ".unsubscribe"), ...parameters);
|
|
1133
750
|
// Write a log message.
|
|
1134
|
-
(0, $
|
|
751
|
+
(0, $dvphU$electrumcashdebuglogs).client(`Unsubscribed from '${String(method)}' for the '${subscriptionParameters}' parameters.`);
|
|
1135
752
|
}
|
|
1136
753
|
/**
|
|
1137
754
|
* Restores existing subscriptions without updating status or triggering manual callbacks.
|
|
@@ -1143,7 +760,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1143
760
|
* @ignore
|
|
1144
761
|
*/ async resubscribeOnConnect() {
|
|
1145
762
|
// Write a log message.
|
|
1146
|
-
(0, $
|
|
763
|
+
(0, $dvphU$electrumcashdebuglogs).client(`Connected to '${this.hostIdentifier}'.`);
|
|
1147
764
|
// Synchronize with the underlying connection status.
|
|
1148
765
|
this.handleConnectionStatusChanges("connected");
|
|
1149
766
|
// Initialize an empty list of resubscription promises.
|
|
@@ -1161,12 +778,12 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1161
778
|
await Promise.all(resubscriptionPromises);
|
|
1162
779
|
}
|
|
1163
780
|
// Write a log message if there was any subscriptions to restore.
|
|
1164
|
-
if (resubscriptionPromises.length > 0) (0, $
|
|
781
|
+
if (resubscriptionPromises.length > 0) (0, $dvphU$electrumcashdebuglogs).client(`Restored ${resubscriptionPromises.length} previous subscriptions for '${this.hostIdentifier}'`);
|
|
1165
782
|
}
|
|
1166
783
|
/**
|
|
1167
784
|
* Parser messages from the remote server to resolve request promises and emit subscription events.
|
|
1168
785
|
*
|
|
1169
|
-
* @param
|
|
786
|
+
* @param message - the response message
|
|
1170
787
|
*
|
|
1171
788
|
* @throws {Error} if the message ID does not match an existing request.
|
|
1172
789
|
* @ignore
|
|
@@ -1174,7 +791,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1174
791
|
// If the received message is a notification, we forward it to all event listeners
|
|
1175
792
|
if ((0, $abcb763a48577a1e$export$280de919a0cf6928)(message)) {
|
|
1176
793
|
// Write a log message.
|
|
1177
|
-
(0, $
|
|
794
|
+
(0, $dvphU$electrumcashdebuglogs).client(`Received notification for '${message.method}' from '${this.hostIdentifier}'`);
|
|
1178
795
|
// Forward the message content to all event listeners.
|
|
1179
796
|
this.emit("notification", message);
|
|
1180
797
|
// Try to update the chain height.
|
|
@@ -1190,7 +807,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1190
807
|
// If we do not have a request resolver for this response message..
|
|
1191
808
|
if (!requestResolver) {
|
|
1192
809
|
// Log that a message was ignored since the request has already been rejected.
|
|
1193
|
-
(0, $
|
|
810
|
+
(0, $dvphU$electrumcashdebuglogs).warning(`Ignoring response #${message.id} as the request has already been rejected.`);
|
|
1194
811
|
// Return as this has now been fully handled.
|
|
1195
812
|
return;
|
|
1196
813
|
}
|
|
@@ -1286,5 +903,5 @@ $558b46d3f899ced5$export$2e2bcd8739ae039 = $558b46d3f899ced5$var$ElectrumClient;
|
|
|
1286
903
|
|
|
1287
904
|
|
|
1288
905
|
|
|
1289
|
-
export {$558b46d3f899ced5$export$2e2bcd8739ae039 as ElectrumClient, $e83d2e7688025acd$export$e1f38ab2b4ebdde6 as isVersionRejected, $e83d2e7688025acd$export$9598f0c76aa41d73 as isVersionNegotiated, $d801b1f9b7fc3074$export$
|
|
906
|
+
export {$558b46d3f899ced5$export$2e2bcd8739ae039 as ElectrumClient, $e83d2e7688025acd$export$e1f38ab2b4ebdde6 as isVersionRejected, $e83d2e7688025acd$export$9598f0c76aa41d73 as isVersionNegotiated, $d801b1f9b7fc3074$export$a3a6b8a974871191 as DEFAULT_PING_INTERVAL, $d801b1f9b7fc3074$export$b2115084b892a79d as DEFAULT_RECONNECT_INTERVAL, $d801b1f9b7fc3074$export$788e8376894ebb88 as DEFAULT_BIG_INTEGERS, $db7c797e63383364$export$c4f81c6d30ca200f as ClientState, $db7c797e63383364$export$7516420eb880ab68 as ConnectionStatus};
|
|
1290
907
|
//# sourceMappingURL=index.mjs.map
|