@electrum-cash/network 4.0.0-development.6655187551 → 4.0.0-development.8017676278
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 +26 -58
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +124 -474
- 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,356 +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
|
-
console.log("DEBUG: Added listeners:", this.tcpSocket.listeners("data"));
|
|
218
|
-
// Indicate that the onConnect function has run.
|
|
219
|
-
this.onConnectHasRun = true;
|
|
220
|
-
// Emit the connect event.
|
|
221
|
-
this.emit("connect");
|
|
222
|
-
}
|
|
223
|
-
/**
|
|
224
|
-
* Clears the disconnect timer if it is still active.
|
|
225
|
-
*/ clearDisconnectTimerOnTimeout() {
|
|
226
|
-
// Clear the retry timer if it is still active.
|
|
227
|
-
if (this.timers.disconnect) clearTimeout(this.timers.disconnect);
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* Forcibly terminate the connection.
|
|
231
|
-
*
|
|
232
|
-
* @throws {Error} if no connection was found
|
|
233
|
-
*/ disconnect() {
|
|
234
|
-
// Clear the disconnect timer so that the socket does not try to disconnect again later.
|
|
235
|
-
this.clearDisconnectTimerOnTimeout();
|
|
236
|
-
if (this.tcpSocket) {
|
|
237
|
-
// Remove all event forwarders.
|
|
238
|
-
this.tcpSocket.removeListener("close", this.eventForwarders.disconnect);
|
|
239
|
-
this.tcpSocket.removeListener("data", this.eventForwarders.tcpData);
|
|
240
|
-
this.tcpSocket.removeListener("error", this.eventForwarders.tcpError);
|
|
241
|
-
// Terminate the connection.
|
|
242
|
-
this.tcpSocket.destroy();
|
|
243
|
-
// Remove the stored socket.
|
|
244
|
-
this.tcpSocket = undefined;
|
|
245
|
-
}
|
|
246
|
-
// Indicate that the onConnect function has not run and it has to be run again.
|
|
247
|
-
this.onConnectHasRun = false;
|
|
248
|
-
// Emit a disconnect event
|
|
249
|
-
this.emit("disconnect");
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Write data to the socket
|
|
253
|
-
*
|
|
254
|
-
* @param {Uint8Array | string} data Data to be written to the socket
|
|
255
|
-
* @param {function} callback Callback function to be called when the write has completed
|
|
256
|
-
*
|
|
257
|
-
* @throws {Error} if no connection was found
|
|
258
|
-
* @returns true if the message was fully flushed to the socket, false if part of the message
|
|
259
|
-
* is queued in the user memory
|
|
260
|
-
*/ write(data, callback) {
|
|
261
|
-
// Throw an error if no active connection is found
|
|
262
|
-
if (!this.tcpSocket) throw new Error("Cannot write to socket when there is no active connection");
|
|
263
|
-
// Write data to the TLS Socket and return the status indicating
|
|
264
|
-
// whether the full message was flushed to the socket
|
|
265
|
-
return this.tcpSocket.write(data, callback);
|
|
266
|
-
}
|
|
267
|
-
/**
|
|
268
|
-
* Force a disconnection if no connection is established after `timeout` milliseconds.
|
|
269
|
-
*
|
|
270
|
-
* @param {string} host Host of the connection that timed out
|
|
271
|
-
* @param {number} port Port of the connection that timed out
|
|
272
|
-
* @param {number} timeout Elapsed milliseconds
|
|
273
|
-
*/ disconnectOnTimeout(host, port, timeout) {
|
|
274
|
-
// Remove the connect listener.
|
|
275
|
-
this.removeListener("connect", this.clearDisconnectTimerOnTimeout);
|
|
276
|
-
// Create a new timeout error.
|
|
277
|
-
const timeoutError = {
|
|
278
|
-
code: "ETIMEDOUT",
|
|
279
|
-
message: `Connection to '${host}:${port}' timed out after ${timeout} milliseconds`
|
|
280
|
-
};
|
|
281
|
-
// Emit an error event so that connect is rejected upstream.
|
|
282
|
-
this.emit("error", timeoutError);
|
|
283
|
-
// Forcibly disconnect to clean up the connection on timeout
|
|
284
|
-
this.disconnect();
|
|
285
|
-
}
|
|
286
|
-
constructor(...args){
|
|
287
|
-
super(...args);
|
|
288
|
-
// Declare timers for keep-alive pings and reconnection
|
|
289
|
-
this.timers = {};
|
|
290
|
-
// Initialize boolean that indicates whether the onConnect function has run (initialize to false).
|
|
291
|
-
this.onConnectHasRun = false;
|
|
292
|
-
// Initialize event forwarding functions.
|
|
293
|
-
this.eventForwarders = {
|
|
294
|
-
disconnect: ()=>this.emit("disconnect"),
|
|
295
|
-
tcpData: (data)=>this.emit("data", data),
|
|
296
|
-
tcpError: (err)=>this.emit("error", err)
|
|
297
|
-
};
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
class $0a4cf22b9d6c493c$export$25b4633f61498e1 extends (0, $dvphU$EventEmitter) {
|
|
301
|
-
/**
|
|
302
|
-
* Connect to host:port using the specified transport
|
|
303
|
-
*
|
|
304
|
-
* @param {string} host Fully qualified domain name or IP address of the host
|
|
305
|
-
* @param {number} port Network port for the host to connect to
|
|
306
|
-
* @param {TransportScheme} scheme Transport scheme to use
|
|
307
|
-
* @param {number} timeout If no connection is established after `timeout` ms, the connection is terminated
|
|
308
|
-
*
|
|
309
|
-
* @throws {Error} if an incorrect transport scheme is specified
|
|
310
|
-
*/ connect(host, port, scheme, timeout) {
|
|
311
|
-
// Check that no existing socket exists before initiating a new connection.
|
|
312
|
-
if (this.webSocket) throw new Error("Cannot initiate a new socket connection when an existing connection exists");
|
|
313
|
-
// Set a timer to force disconnect after `timeout` seconds
|
|
314
|
-
this.timers.disconnect = setTimeout(()=>this.disconnectOnTimeout(host, port, timeout), timeout);
|
|
315
|
-
// Remove the timer if a connection is successfully established
|
|
316
|
-
this.once("connect", this.clearDisconnectTimerOnTimeout);
|
|
317
|
-
// Define how to refer to the connection scheme in debug output.
|
|
318
|
-
const socketTypes = {
|
|
319
|
-
[(0, $d801b1f9b7fc3074$export$d048df559e6d3842).WS.Scheme]: "a WebSocket",
|
|
320
|
-
[(0, $d801b1f9b7fc3074$export$d048df559e6d3842).WSS.Scheme]: "an encrypted WebSocket"
|
|
321
|
-
};
|
|
322
|
-
// Log that we are trying to establish a connection.
|
|
323
|
-
(0, $623a31cc1663a627$export$2e2bcd8739ae039).network(`Initiating ${socketTypes[scheme]} connection to '${host}:${port}'.`);
|
|
324
|
-
if (scheme !== (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WS.Scheme && scheme !== (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WSS.Scheme) // Throw an error if an incorrect transport is specified
|
|
325
|
-
throw new Error("Incorrect transport specified");
|
|
326
|
-
if (scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WSS.Scheme) // Initialize this.webSocket (rejecting self-signed certificates).
|
|
327
|
-
// We reject self-signed certificates to match functionality of browsers.
|
|
328
|
-
this.webSocket = new (0, $dvphU$WebSocket)(`wss://${host}:${port}`);
|
|
329
|
-
else // Initialize this.webSocket.
|
|
330
|
-
this.webSocket = new (0, $dvphU$WebSocket)(`ws://${host}:${port}`);
|
|
331
|
-
// Trigger successful connection events.
|
|
332
|
-
this.webSocket.addEventListener("open", this.onConnect.bind(this, socketTypes[scheme], host, port));
|
|
333
|
-
// Forward the encountered errors.
|
|
334
|
-
this.webSocket.addEventListener("error", this.eventForwarders.wsError);
|
|
335
|
-
}
|
|
336
|
-
/**
|
|
337
|
-
* Sets up forwarding of events related to the connection.
|
|
338
|
-
*
|
|
339
|
-
* @param {string} connectionType Name of the connection/transport type, used for logging.
|
|
340
|
-
* @param {string} host Fully qualified domain name or IP address of the host
|
|
341
|
-
* @param {number} port Network port for the host to connect to
|
|
342
|
-
*/ onConnect(connectionType, host, port) {
|
|
343
|
-
// If the onConnect function has already run, do not execute it again.
|
|
344
|
-
if (this.onConnectHasRun) return;
|
|
345
|
-
// Log that the connection has been established.
|
|
346
|
-
(0, $623a31cc1663a627$export$2e2bcd8739ae039).network(`Established ${connectionType} connection with '${host}:${port}'.`);
|
|
347
|
-
// Forward the socket events
|
|
348
|
-
this.webSocket.addEventListener("close", this.eventForwarders.disconnect);
|
|
349
|
-
this.webSocket.addEventListener("message", this.eventForwarders.wsData);
|
|
350
|
-
console.log("DEBUG: Added listeners:", this.webSocket.listeners("data"));
|
|
351
|
-
// Indicate that the onConnect function has run.
|
|
352
|
-
this.onConnectHasRun = true;
|
|
353
|
-
// Emit the connect event.
|
|
354
|
-
this.emit("connect");
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Clears the disconnect timer if it is still active.
|
|
358
|
-
*/ clearDisconnectTimerOnTimeout() {
|
|
359
|
-
// Clear the retry timer if it is still active.
|
|
360
|
-
if (this.timers.disconnect) clearTimeout(this.timers.disconnect);
|
|
361
|
-
}
|
|
362
|
-
/**
|
|
363
|
-
* Forcibly terminate the connection.
|
|
364
|
-
*
|
|
365
|
-
* @throws {Error} if no connection was found
|
|
366
|
-
*/ disconnect() {
|
|
367
|
-
// Clear the disconnect timer so that the socket does not try to disconnect again later.
|
|
368
|
-
this.clearDisconnectTimerOnTimeout();
|
|
369
|
-
try {
|
|
370
|
-
// Remove all event forwarders.
|
|
371
|
-
this.webSocket.removeEventListener("close", this.eventForwarders.disconnect);
|
|
372
|
-
this.webSocket.removeEventListener("message", this.eventForwarders.wsData);
|
|
373
|
-
this.webSocket.removeEventListener("error", this.eventForwarders.wsError);
|
|
374
|
-
// Gracefully terminate the connection.
|
|
375
|
-
this.webSocket.close();
|
|
376
|
-
} catch (ignored) {
|
|
377
|
-
// close() will throw an error if the connection has not been established yet.
|
|
378
|
-
// We ignore this error, since no similar error gets thrown in the TLS Socket.
|
|
379
|
-
} finally{
|
|
380
|
-
// Remove the stored socket regardless of any thrown errors.
|
|
381
|
-
this.webSocket = undefined;
|
|
382
|
-
}
|
|
383
|
-
// Indicate that the onConnect function has not run and it has to be run again.
|
|
384
|
-
this.onConnectHasRun = false;
|
|
385
|
-
// Emit a disconnect event
|
|
386
|
-
this.emit("disconnect");
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Write data to the socket
|
|
390
|
-
*
|
|
391
|
-
* @param {Uint8Array | string} data Data to be written to the socket
|
|
392
|
-
* @param {function} callback Callback function to be called when the write has completed
|
|
393
|
-
*
|
|
394
|
-
* @throws {Error} if no connection was found
|
|
395
|
-
* @returns true if the message was fully flushed to the socket, false if part of the message
|
|
396
|
-
* is queued in the user memory
|
|
397
|
-
*/ write(data, callback) {
|
|
398
|
-
// Throw an error if no active connection is found
|
|
399
|
-
if (!this.webSocket) throw new Error("Cannot write to socket when there is no active connection");
|
|
400
|
-
// Write data to the WebSocket
|
|
401
|
-
this.webSocket.send(data, callback);
|
|
402
|
-
// WebSockets always fit everything in a single request, so we return true
|
|
403
|
-
return true;
|
|
404
|
-
}
|
|
405
|
-
/**
|
|
406
|
-
* Force a disconnection if no connection is established after `timeout` milliseconds.
|
|
407
|
-
*
|
|
408
|
-
* @param {string} host Host of the connection that timed out
|
|
409
|
-
* @param {number} port Port of the connection that timed out
|
|
410
|
-
* @param {number} timeout Elapsed milliseconds
|
|
411
|
-
*/ disconnectOnTimeout(host, port, timeout) {
|
|
412
|
-
// Remove the connect listener.
|
|
413
|
-
this.removeListener("connect", this.clearDisconnectTimerOnTimeout);
|
|
414
|
-
// Create a new timeout error.
|
|
415
|
-
const timeoutError = {
|
|
416
|
-
code: "ETIMEDOUT",
|
|
417
|
-
message: `Connection to '${host}:${port}' timed out after ${timeout} milliseconds`
|
|
418
|
-
};
|
|
419
|
-
// Emit an error event so that connect is rejected upstream.
|
|
420
|
-
this.emit("error", timeoutError);
|
|
421
|
-
// Forcibly disconnect to clean up the connection on timeout
|
|
422
|
-
this.disconnect();
|
|
423
|
-
}
|
|
424
|
-
constructor(...args){
|
|
425
|
-
super(...args);
|
|
426
|
-
// Declare timers for keep-alive pings and reconnection
|
|
427
|
-
this.timers = {};
|
|
428
|
-
// Initialize boolean that indicates whether the onConnect function has run (initialize to false).
|
|
429
|
-
this.onConnectHasRun = false;
|
|
430
|
-
// Initialize event forwarding functions.
|
|
431
|
-
this.eventForwarders = {
|
|
432
|
-
disconnect: ()=>this.emit("disconnect"),
|
|
433
|
-
wsData: (event)=>this.emit("data", `${event.data}\n`),
|
|
434
|
-
wsError: (event)=>this.emit("error", event.error)
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
}
|
|
438
|
-
|
|
439
|
-
|
|
440
82
|
var $db7c797e63383364$exports = {};
|
|
441
83
|
|
|
442
84
|
$parcel$export($db7c797e63383364$exports, "ClientState", () => $db7c797e63383364$export$c4f81c6d30ca200f);
|
|
@@ -469,38 +111,39 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
469
111
|
/**
|
|
470
112
|
* Sets up network configuration for an Electrum client connection.
|
|
471
113
|
*
|
|
472
|
-
* @param
|
|
473
|
-
* @param
|
|
474
|
-
* @param
|
|
475
|
-
* @param
|
|
476
|
-
* @param
|
|
477
|
-
* @param
|
|
478
|
-
* @param {number} pingInterval the time between sending pings to the electrum host, in milliseconds.
|
|
479
|
-
* @param {number} reconnectInterval the time between reconnection attempts to the electrum host, in milliseconds.
|
|
480
|
-
* @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 - socket 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.
|
|
481
120
|
*
|
|
482
121
|
* @throws {Error} if `version` is not a valid version string.
|
|
483
|
-
*/ constructor(application, version,
|
|
122
|
+
*/ constructor(application, version, socketOrHostname, pingInterval = (0, $d801b1f9b7fc3074$export$a3a6b8a974871191), reconnectInterval = (0, $d801b1f9b7fc3074$export$b2115084b892a79d), useBigInt = (0, $d801b1f9b7fc3074$export$788e8376894ebb88)){
|
|
484
123
|
// Initialize the event emitter.
|
|
485
124
|
super();
|
|
486
125
|
this.application = application;
|
|
487
126
|
this.version = version;
|
|
488
|
-
this.
|
|
489
|
-
this.port = port;
|
|
490
|
-
this.scheme = scheme;
|
|
491
|
-
this.timeout = timeout;
|
|
127
|
+
this.socketOrHostname = socketOrHostname;
|
|
492
128
|
this.pingInterval = pingInterval;
|
|
493
129
|
this.reconnectInterval = reconnectInterval;
|
|
494
130
|
this.useBigInt = useBigInt;
|
|
495
131
|
this.status = (0, $db7c797e63383364$export$7516420eb880ab68).DISCONNECTED;
|
|
496
|
-
this.timers = {};
|
|
497
132
|
this.verifications = [];
|
|
498
133
|
this.messageBuffer = "";
|
|
499
134
|
// Check if the provided version is a valid version number.
|
|
500
135
|
if (!(0, $24139611f53a54b8$export$2e2bcd8739ae039).versionRegexp.test(version)) // Throw an error since the version number was not valid.
|
|
501
136
|
throw new Error(`Provided version string (${version}) is not a valid protocol version number.`);
|
|
502
|
-
//
|
|
503
|
-
|
|
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));
|
|
504
147
|
// Handle visibility changes when run in a browser environment.
|
|
505
148
|
if (typeof document !== "undefined") document.addEventListener("visibilitychange", this.handleVisibilityChange.bind(this));
|
|
506
149
|
// Handle network connection changes when run in a browser environment.
|
|
@@ -509,36 +152,30 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
509
152
|
window.addEventListener("offline", this.handleNetworkChange.bind(this));
|
|
510
153
|
}
|
|
511
154
|
}
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
return `${this.host}:${this.port}`;
|
|
155
|
+
// Expose hostIdentifier from the socket.
|
|
156
|
+
get hostIdentifier() {
|
|
157
|
+
return this.socket.hostIdentifier;
|
|
516
158
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
if (this.scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).TCP.Scheme || this.scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).TCP_TLS.Scheme) // Initialize a new ElectrumTcpSocket
|
|
521
|
-
this.socket = new (0, $0a4cf22b9d6c493c$export$22c0ca2c816c3e08)();
|
|
522
|
-
else if (this.scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WS.Scheme || this.scheme === (0, $d801b1f9b7fc3074$export$d048df559e6d3842).WSS.Scheme) // Initialize a new ElectrumWebSocket
|
|
523
|
-
this.socket = new (0, $0a4cf22b9d6c493c$export$25b4633f61498e1)();
|
|
524
|
-
else // Throw an error if an incorrect transport is specified
|
|
525
|
-
throw new Error(`Provided transport (${this.scheme}) is not a valid ElectrumTransport`);
|
|
526
|
-
// Set up handlers for connection and disconnection.
|
|
527
|
-
this.socket.on("connect", this.onSocketConnect.bind(this));
|
|
528
|
-
this.socket.on("disconnect", this.onSocketDisconnect.bind(this));
|
|
529
|
-
// Set up handler for incoming data.
|
|
530
|
-
this.socket.on("data", this.parseMessageChunk.bind(this));
|
|
159
|
+
// Expose host from the socket.
|
|
160
|
+
get host() {
|
|
161
|
+
return this.socket.host;
|
|
531
162
|
}
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
163
|
+
// Expose port from the socket.
|
|
164
|
+
get port() {
|
|
165
|
+
return this.socket.port;
|
|
166
|
+
}
|
|
167
|
+
// Expose port from the socket.
|
|
168
|
+
get encrypted() {
|
|
169
|
+
return this.socket.encrypted;
|
|
170
|
+
}
|
|
171
|
+
// Expose timeout from the socket.
|
|
172
|
+
get timeout() {
|
|
173
|
+
return this.socket.timeout;
|
|
537
174
|
}
|
|
538
175
|
/**
|
|
539
176
|
* Assembles incoming data into statements and hands them off to the message parser.
|
|
540
177
|
*
|
|
541
|
-
* @param
|
|
178
|
+
* @param data - data to append to the current message buffer, as a string.
|
|
542
179
|
*
|
|
543
180
|
* @throws {SyntaxError} if the passed statement parts are not valid JSON.
|
|
544
181
|
*/ parseMessageChunk(data) {
|
|
@@ -603,7 +240,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
603
240
|
* part of the message is queued in the user memory
|
|
604
241
|
*/ ping() {
|
|
605
242
|
// Write a log message.
|
|
606
|
-
(0, $
|
|
243
|
+
(0, $dvphU$electrumcashdebuglogs).ping(`Sending keep-alive ping to '${this.hostIdentifier}'`);
|
|
607
244
|
// Craft a keep-alive message.
|
|
608
245
|
const message = (0, $24139611f53a54b8$export$2e2bcd8739ae039).buildRequestObject("server.ping", [], "keepAlive");
|
|
609
246
|
// Send the keep-alive message.
|
|
@@ -639,7 +276,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
639
276
|
// Define a function to wrap version negotiation as a callback.
|
|
640
277
|
const versionNegotiator = ()=>{
|
|
641
278
|
// Write a log message to show that we have started version negotiation.
|
|
642
|
-
(0, $
|
|
279
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Requesting protocol version ${this.version} with '${this.hostIdentifier}'.`);
|
|
643
280
|
// remove the one-time error handler since no error was detected.
|
|
644
281
|
this.socket.removeListener("error", rejector);
|
|
645
282
|
// Build a version negotiation message.
|
|
@@ -656,7 +293,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
656
293
|
// Declare an error message.
|
|
657
294
|
const errorMessage = "unsupported protocol version.";
|
|
658
295
|
// Log the error.
|
|
659
|
-
(0, $
|
|
296
|
+
(0, $dvphU$electrumcashdebuglogs).errors(`Failed to connect with ${this.hostIdentifier} due to ${errorMessage}`);
|
|
660
297
|
// Reject the connection with false since version negotiation failed.
|
|
661
298
|
reject(errorMessage);
|
|
662
299
|
} else if (version.protocol !== this.version && `${version.protocol}.0` !== this.version && `${version.protocol}.0.0` !== this.version) {
|
|
@@ -665,12 +302,12 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
665
302
|
// Declare an error message.
|
|
666
303
|
const errorMessage = `incompatible protocol version negotiated (${version.protocol} !== ${this.version}).`;
|
|
667
304
|
// Log the error.
|
|
668
|
-
(0, $
|
|
305
|
+
(0, $dvphU$electrumcashdebuglogs).errors(`Failed to connect with ${this.hostIdentifier} due to ${errorMessage}`);
|
|
669
306
|
// Reject the connection with false since version negotiation failed.
|
|
670
307
|
reject(errorMessage);
|
|
671
308
|
} else {
|
|
672
309
|
// Write a log message.
|
|
673
|
-
(0, $
|
|
310
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Negotiated protocol version ${version.protocol} with '${this.hostIdentifier}', powered by ${version.software}.`);
|
|
674
311
|
// Set connection status to connected
|
|
675
312
|
this.status = (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED;
|
|
676
313
|
// Emit a connect event now that the connection is usable.
|
|
@@ -689,7 +326,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
689
326
|
// Set up handler for network errors.
|
|
690
327
|
this.socket.on("error", this.onSocketError.bind(this));
|
|
691
328
|
// Connect to the server.
|
|
692
|
-
this.socket.connect(
|
|
329
|
+
this.socket.connect();
|
|
693
330
|
};
|
|
694
331
|
// Wait until connection is established and version negotiation succeeds.
|
|
695
332
|
await new Promise(connectionResolver);
|
|
@@ -700,14 +337,13 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
700
337
|
// If a reconnect timer is set, remove it
|
|
701
338
|
await this.clearReconnectTimer();
|
|
702
339
|
// Write a log message.
|
|
703
|
-
(0, $
|
|
340
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Trying to reconnect to '${this.hostIdentifier}'..`);
|
|
704
341
|
// Set the status to reconnecting for more accurate log messages.
|
|
705
342
|
this.status = (0, $db7c797e63383364$export$7516420eb880ab68).RECONNECTING;
|
|
706
343
|
// Emit a connect event now that the connection is usable.
|
|
707
344
|
this.emit("reconnecting");
|
|
708
|
-
//
|
|
709
|
-
this.
|
|
710
|
-
this.createSocket();
|
|
345
|
+
// Disconnect the underlying socket.
|
|
346
|
+
this.socket.disconnect();
|
|
711
347
|
try {
|
|
712
348
|
// Try to connect again.
|
|
713
349
|
await this.connect();
|
|
@@ -719,30 +355,30 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
719
355
|
* Removes the current reconnect timer.
|
|
720
356
|
*/ clearReconnectTimer() {
|
|
721
357
|
// If a reconnect timer is set, remove it
|
|
722
|
-
if (this.
|
|
358
|
+
if (this.reconnectTimer) clearTimeout(this.reconnectTimer);
|
|
723
359
|
// Reset the timer reference.
|
|
724
|
-
this.
|
|
360
|
+
this.reconnectTimer = undefined;
|
|
725
361
|
}
|
|
726
362
|
/**
|
|
727
363
|
* Removes the current keep-alive timer.
|
|
728
364
|
*/ clearKeepAliveTimer() {
|
|
729
365
|
// If a keep-alive timer is set, remove it
|
|
730
|
-
if (this.
|
|
366
|
+
if (this.keepAliveTimer) clearTimeout(this.keepAliveTimer);
|
|
731
367
|
// Reset the timer reference.
|
|
732
|
-
this.
|
|
368
|
+
this.keepAliveTimer = undefined;
|
|
733
369
|
}
|
|
734
370
|
/**
|
|
735
371
|
* Initializes the keep alive timer loop.
|
|
736
372
|
*/ setupKeepAliveTimer() {
|
|
737
373
|
// If the keep-alive timer loop is not currently set up..
|
|
738
|
-
if (!this.
|
|
739
|
-
this.
|
|
374
|
+
if (!this.keepAliveTimer) // Set a new keep-alive timer.
|
|
375
|
+
this.keepAliveTimer = setTimeout(this.ping.bind(this), this.pingInterval);
|
|
740
376
|
}
|
|
741
377
|
/**
|
|
742
378
|
* Tears down the current connection and removes all event listeners on disconnect.
|
|
743
379
|
*
|
|
744
|
-
* @param
|
|
745
|
-
* @param
|
|
380
|
+
* @param force - disconnect even if the connection has not been fully established yet.
|
|
381
|
+
* @param intentional - update connection state if disconnect is intentional.
|
|
746
382
|
*
|
|
747
383
|
* @returns true if successfully disconnected, or false if there was no connection.
|
|
748
384
|
*/ async disconnect(force = false, intentional = true) {
|
|
@@ -763,8 +399,8 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
763
399
|
const disconnectResolver = (resolve)=>{
|
|
764
400
|
// Resolve to true after the connection emits a disconnect
|
|
765
401
|
this.once("disconnected", ()=>resolve(true));
|
|
766
|
-
// Close the connection
|
|
767
|
-
this.
|
|
402
|
+
// Close the connection on the socket level.
|
|
403
|
+
this.socket.disconnect();
|
|
768
404
|
};
|
|
769
405
|
// Return true to indicate that we disconnected.
|
|
770
406
|
return new Promise(disconnectResolver);
|
|
@@ -812,7 +448,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
812
448
|
/**
|
|
813
449
|
* Sends an arbitrary message to the server.
|
|
814
450
|
*
|
|
815
|
-
* @param
|
|
451
|
+
* @param message - json encoded request object to send to the server, as a string.
|
|
816
452
|
*
|
|
817
453
|
* @returns true if the message was fully flushed to the socket, false if part of the message
|
|
818
454
|
* is queued in the user memory
|
|
@@ -843,7 +479,7 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
843
479
|
// Remove the current keep-alive timer if it exists.
|
|
844
480
|
this.clearKeepAliveTimer();
|
|
845
481
|
// Write a notification to the logs.
|
|
846
|
-
(0, $
|
|
482
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Connection to '${this.hostIdentifier}' timed out.`);
|
|
847
483
|
// Close the connection to avoid re-use.
|
|
848
484
|
// NOTE: This initiates reconnection routines if the connection has not
|
|
849
485
|
// been marked as intentionally disconnected.
|
|
@@ -880,18 +516,18 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
880
516
|
// Remove all event listeners
|
|
881
517
|
this.removeAllListeners();
|
|
882
518
|
// Write a log message.
|
|
883
|
-
(0, $
|
|
519
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Disconnected from '${this.hostIdentifier}'.`);
|
|
884
520
|
} else {
|
|
885
521
|
// If this is for an established connection..
|
|
886
522
|
if (this.status === (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED) // Write a notification to the logs.
|
|
887
|
-
(0, $
|
|
523
|
+
(0, $dvphU$electrumcashdebuglogs).errors(`Connection with '${this.hostIdentifier}' was closed, trying to reconnect in ${this.reconnectInterval / 1000} seconds.`);
|
|
888
524
|
// Mark the connection as disconnected for now..
|
|
889
525
|
this.status = (0, $db7c797e63383364$export$7516420eb880ab68).DISCONNECTED;
|
|
890
526
|
// Send a disconnect signal higher up the stack.
|
|
891
527
|
this.emit("disconnected");
|
|
892
528
|
// If we don't have a pending reconnection timer..
|
|
893
|
-
if (!this.
|
|
894
|
-
this.
|
|
529
|
+
if (!this.reconnectTimer) // Attempt to reconnect after one keep-alive duration.
|
|
530
|
+
this.reconnectTimer = setTimeout(this.reconnect.bind(this), this.reconnectInterval);
|
|
895
531
|
}
|
|
896
532
|
}
|
|
897
533
|
/**
|
|
@@ -905,17 +541,17 @@ var $db7c797e63383364$export$7516420eb880ab68;
|
|
|
905
541
|
return;
|
|
906
542
|
// If the DNS lookup failed.
|
|
907
543
|
if (error.code === "EAI_AGAIN") {
|
|
908
|
-
(0, $
|
|
544
|
+
(0, $dvphU$electrumcashdebuglogs).errors(`Failed to look up DNS records for '${this.host}'.`);
|
|
909
545
|
return;
|
|
910
546
|
}
|
|
911
547
|
// If the connection timed out..
|
|
912
548
|
if (error.code === "ETIMEDOUT") {
|
|
913
549
|
// Log the provided timeout message.
|
|
914
|
-
(0, $
|
|
550
|
+
(0, $dvphU$electrumcashdebuglogs).errors(error.message);
|
|
915
551
|
return;
|
|
916
552
|
}
|
|
917
553
|
// Log unknown error
|
|
918
|
-
(0, $
|
|
554
|
+
(0, $dvphU$electrumcashdebuglogs).errors(`Unknown network error ('${this.hostIdentifier}'): `, error);
|
|
919
555
|
}
|
|
920
556
|
}
|
|
921
557
|
var // Export the connection.
|
|
@@ -960,26 +596,20 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
960
596
|
/**
|
|
961
597
|
* Initializes an Electrum client.
|
|
962
598
|
*
|
|
963
|
-
* @param
|
|
964
|
-
* @param
|
|
965
|
-
* @param
|
|
966
|
-
* @param
|
|
967
|
-
* @param
|
|
968
|
-
* @param
|
|
969
|
-
* @param {number} pingInterval the time between sending pings to the electrum host, in milliseconds.
|
|
970
|
-
* @param {number} reconnectInterval the time between reconnection attempts to the electrum host, in milliseconds.
|
|
971
|
-
* @param {boolean} useBigInt whether to use bigint for numbers in json response.
|
|
599
|
+
* @param application - your application name, used to identify to the electrum host.
|
|
600
|
+
* @param version - protocol version to use with the host.
|
|
601
|
+
* @param socketOrHostname - socket fully qualified domain name or IP number of the host
|
|
602
|
+
* @param pingInterval - the time between sending pings to the electrum host, in milliseconds.
|
|
603
|
+
* @param reconnectInterval - the time between reconnection attempts to the electrum host, in milliseconds.
|
|
604
|
+
* @param useBigInt - whether to use bigint for numbers in json response.
|
|
972
605
|
*
|
|
973
606
|
* @throws {Error} if `version` is not a valid version string.
|
|
974
|
-
*/ constructor(application, version,
|
|
607
|
+
*/ constructor(application, version, socketOrHostname, pingInterval = (0, $d801b1f9b7fc3074$export$a3a6b8a974871191), reconnectInterval = (0, $d801b1f9b7fc3074$export$b2115084b892a79d), useBigInt = (0, $d801b1f9b7fc3074$export$788e8376894ebb88)){
|
|
975
608
|
// Initialize the event emitter.
|
|
976
609
|
super();
|
|
977
610
|
this.application = application;
|
|
978
611
|
this.version = version;
|
|
979
|
-
this.
|
|
980
|
-
this.port = port;
|
|
981
|
-
this.scheme = scheme;
|
|
982
|
-
this.timeout = timeout;
|
|
612
|
+
this.socketOrHostname = socketOrHostname;
|
|
983
613
|
this.pingInterval = pingInterval;
|
|
984
614
|
this.reconnectInterval = reconnectInterval;
|
|
985
615
|
this.useBigInt = useBigInt;
|
|
@@ -989,7 +619,27 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
989
619
|
this.requestResolvers = {};
|
|
990
620
|
this.connectionLock = new (0, $dvphU$Mutex)();
|
|
991
621
|
// Set up a connection to an electrum server.
|
|
992
|
-
this.connection = new (0, $ff134c9a9e1f7361$export$2e2bcd8739ae039)(application, version,
|
|
622
|
+
this.connection = new (0, $ff134c9a9e1f7361$export$2e2bcd8739ae039)(application, version, socketOrHostname, pingInterval, reconnectInterval, useBigInt);
|
|
623
|
+
}
|
|
624
|
+
// Expose hostIdentifier from the connection.
|
|
625
|
+
get hostIdentifier() {
|
|
626
|
+
return this.connection.hostIdentifier;
|
|
627
|
+
}
|
|
628
|
+
// Expose host from the connection.
|
|
629
|
+
get host() {
|
|
630
|
+
return this.connection.host;
|
|
631
|
+
}
|
|
632
|
+
// Expose port from the connection.
|
|
633
|
+
get port() {
|
|
634
|
+
return this.connection.port;
|
|
635
|
+
}
|
|
636
|
+
// Expose port from the connection.
|
|
637
|
+
get encrypted() {
|
|
638
|
+
return this.connection.encrypted;
|
|
639
|
+
}
|
|
640
|
+
// Expose timeout from the connection.
|
|
641
|
+
get timeout() {
|
|
642
|
+
return this.connection.timeout;
|
|
993
643
|
}
|
|
994
644
|
/**
|
|
995
645
|
* Connects to the remote server.
|
|
@@ -1025,8 +675,8 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1025
675
|
/**
|
|
1026
676
|
* Disconnects from the remote server and removes all event listeners/subscriptions and open requests.
|
|
1027
677
|
*
|
|
1028
|
-
* @param
|
|
1029
|
-
* @param
|
|
678
|
+
* @param force - disconnect even if the connection has not been fully established yet.
|
|
679
|
+
* @param retainSubscriptions - retain subscription data so they will be restored on reconnection.
|
|
1030
680
|
*
|
|
1031
681
|
* @returns true if successfully disconnected, or false if there was no connection.
|
|
1032
682
|
*/ async disconnect(force = false, retainSubscriptions = false) {
|
|
@@ -1042,15 +692,15 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1042
692
|
/**
|
|
1043
693
|
* Calls a method on the remote server with the supplied parameters.
|
|
1044
694
|
*
|
|
1045
|
-
* @param
|
|
1046
|
-
* @param
|
|
695
|
+
* @param method - name of the method to call.
|
|
696
|
+
* @param parameters - one or more parameters for the method.
|
|
1047
697
|
*
|
|
1048
698
|
* @throws {Error} if the client is disconnected.
|
|
1049
699
|
* @returns a promise that resolves with the result of the method or an Error.
|
|
1050
700
|
*/ async request(method, ...parameters) {
|
|
1051
701
|
// If we are not connected to a server..
|
|
1052
702
|
if (this.connection.status !== (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED) // Reject the request with a disconnected error message.
|
|
1053
|
-
throw new Error(`Unable to send request to a disconnected server '${this.
|
|
703
|
+
throw new Error(`Unable to send request to a disconnected server '${this.host}'.`);
|
|
1054
704
|
// Increase the request ID by one.
|
|
1055
705
|
this.requestId += 1;
|
|
1056
706
|
// Store a copy of the request id.
|
|
@@ -1071,7 +721,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1071
721
|
this.connection.send(message);
|
|
1072
722
|
};
|
|
1073
723
|
// Write a log message.
|
|
1074
|
-
(0, $
|
|
724
|
+
(0, $dvphU$electrumcashdebuglogs).network(`Sending request '${method}' to '${this.host}'`);
|
|
1075
725
|
// return a promise to deliver results later.
|
|
1076
726
|
return new Promise(requestResolver);
|
|
1077
727
|
}
|
|
@@ -1080,8 +730,8 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1080
730
|
*
|
|
1081
731
|
* @note the response for the subscription request is issued as a notification event.
|
|
1082
732
|
*
|
|
1083
|
-
* @param
|
|
1084
|
-
* @param
|
|
733
|
+
* @param method - one of the subscribable methods the server supports.
|
|
734
|
+
* @param parameters - one or more parameters for the method.
|
|
1085
735
|
*
|
|
1086
736
|
* @throws {Error} if the client is disconnected.
|
|
1087
737
|
* @returns a promise resolving when the subscription is established.
|
|
@@ -1110,15 +760,15 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1110
760
|
* Unsubscribes to the method at the server and removes any callback functions
|
|
1111
761
|
* when there are no more subscriptions for the method.
|
|
1112
762
|
*
|
|
1113
|
-
* @param
|
|
1114
|
-
* @param
|
|
763
|
+
* @param method - a previously subscribed to method.
|
|
764
|
+
* @param parameters - one or more parameters for the method.
|
|
1115
765
|
*
|
|
1116
766
|
* @throws {Error} if no subscriptions exist for the combination of the provided `method` and `parameters.
|
|
1117
767
|
* @throws {Error} if the client is disconnected.
|
|
1118
768
|
* @returns a promise resolving when the subscription is removed.
|
|
1119
769
|
*/ async unsubscribe(method, ...parameters) {
|
|
1120
770
|
// Throw an error if the client is disconnected.
|
|
1121
|
-
if (this.connection.status !== (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED) throw new Error(`Unable to send unsubscribe request to a disconnected server '${this.
|
|
771
|
+
if (this.connection.status !== (0, $db7c797e63383364$export$7516420eb880ab68).CONNECTED) throw new Error(`Unable to send unsubscribe request to a disconnected server '${this.host}'.`);
|
|
1122
772
|
// If this method has no subscriptions..
|
|
1123
773
|
if (!this.subscriptionMethods[method]) // Reject this promise with an explanation.
|
|
1124
774
|
throw new Error(`Cannot unsubscribe from '${method}' since the method has no subscriptions.`);
|
|
@@ -1133,7 +783,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1133
783
|
// NOTE: As a convenience we allow users to define the method as the subscribe or unsubscribe version.
|
|
1134
784
|
await this.request(method.replace(".subscribe", ".unsubscribe"), ...parameters);
|
|
1135
785
|
// Write a log message.
|
|
1136
|
-
(0, $
|
|
786
|
+
(0, $dvphU$electrumcashdebuglogs).client(`Unsubscribed from '${String(method)}' for the '${subscriptionParameters}' parameters.`);
|
|
1137
787
|
}
|
|
1138
788
|
/**
|
|
1139
789
|
* Restores existing subscriptions without updating status or triggering manual callbacks.
|
|
@@ -1145,7 +795,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1145
795
|
* @ignore
|
|
1146
796
|
*/ async resubscribeOnConnect() {
|
|
1147
797
|
// Write a log message.
|
|
1148
|
-
(0, $
|
|
798
|
+
(0, $dvphU$electrumcashdebuglogs).client(`Connected to '${this.hostIdentifier}'.`);
|
|
1149
799
|
// Synchronize with the underlying connection status.
|
|
1150
800
|
this.handleConnectionStatusChanges("connected");
|
|
1151
801
|
// Initialize an empty list of resubscription promises.
|
|
@@ -1163,12 +813,12 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1163
813
|
await Promise.all(resubscriptionPromises);
|
|
1164
814
|
}
|
|
1165
815
|
// Write a log message if there was any subscriptions to restore.
|
|
1166
|
-
if (resubscriptionPromises.length > 0) (0, $
|
|
816
|
+
if (resubscriptionPromises.length > 0) (0, $dvphU$electrumcashdebuglogs).client(`Restored ${resubscriptionPromises.length} previous subscriptions for '${this.hostIdentifier}'`);
|
|
1167
817
|
}
|
|
1168
818
|
/**
|
|
1169
819
|
* Parser messages from the remote server to resolve request promises and emit subscription events.
|
|
1170
820
|
*
|
|
1171
|
-
* @param
|
|
821
|
+
* @param message - the response message
|
|
1172
822
|
*
|
|
1173
823
|
* @throws {Error} if the message ID does not match an existing request.
|
|
1174
824
|
* @ignore
|
|
@@ -1176,7 +826,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1176
826
|
// If the received message is a notification, we forward it to all event listeners
|
|
1177
827
|
if ((0, $abcb763a48577a1e$export$280de919a0cf6928)(message)) {
|
|
1178
828
|
// Write a log message.
|
|
1179
|
-
(0, $
|
|
829
|
+
(0, $dvphU$electrumcashdebuglogs).client(`Received notification for '${message.method}' from '${this.host}'`);
|
|
1180
830
|
// Forward the message content to all event listeners.
|
|
1181
831
|
this.emit("notification", message);
|
|
1182
832
|
// Try to update the chain height.
|
|
@@ -1192,7 +842,7 @@ const $abcb763a48577a1e$export$94e3360fcddccc76 = function(message) {
|
|
|
1192
842
|
// If we do not have a request resolver for this response message..
|
|
1193
843
|
if (!requestResolver) {
|
|
1194
844
|
// Log that a message was ignored since the request has already been rejected.
|
|
1195
|
-
(0, $
|
|
845
|
+
(0, $dvphU$electrumcashdebuglogs).warning(`Ignoring response #${message.id} as the request has already been rejected.`);
|
|
1196
846
|
// Return as this has now been fully handled.
|
|
1197
847
|
return;
|
|
1198
848
|
}
|
|
@@ -1288,5 +938,5 @@ $558b46d3f899ced5$export$2e2bcd8739ae039 = $558b46d3f899ced5$var$ElectrumClient;
|
|
|
1288
938
|
|
|
1289
939
|
|
|
1290
940
|
|
|
1291
|
-
export {$558b46d3f899ced5$export$2e2bcd8739ae039 as ElectrumClient, $e83d2e7688025acd$export$e1f38ab2b4ebdde6 as isVersionRejected, $e83d2e7688025acd$export$9598f0c76aa41d73 as isVersionNegotiated, $d801b1f9b7fc3074$export$
|
|
941
|
+
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};
|
|
1292
942
|
//# sourceMappingURL=index.mjs.map
|