@electrum-cash/network 4.1.4-development.11641455551 → 4.1.4-development.12704171077

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.
@@ -0,0 +1,438 @@
1
+ import { EventEmitter } from "eventemitter3";
2
+
3
+ //#region source/enums.d.ts
4
+
5
+ /**
6
+ * Enum that denotes the connection status of an ElectrumConnection.
7
+ * @enum {number}
8
+ * @property {0} DISCONNECTED The connection is disconnected.
9
+ * @property {1} AVAILABLE The connection is connected.
10
+ * @property {2} DISCONNECTING The connection is disconnecting.
11
+ * @property {3} CONNECTING The connection is connecting.
12
+ * @property {4} RECONNECTING The connection is restarting.
13
+ */
14
+ declare enum ConnectionStatus {
15
+ DISCONNECTED = 0,
16
+ CONNECTED = 1,
17
+ DISCONNECTING = 2,
18
+ CONNECTING = 3,
19
+ RECONNECTING = 4,
20
+ }
21
+ //#endregion
22
+ //#region source/rpc-interfaces.d.ts
23
+ type RPCParameter = string | number | boolean | object | null;
24
+ type RCPIdentifier = number | string | null;
25
+ interface RPCBase {
26
+ jsonrpc: string;
27
+ }
28
+ interface RPCNotification extends RPCBase {
29
+ method: string;
30
+ params?: RPCParameter[];
31
+ }
32
+ interface RPCStatement extends RPCBase {
33
+ id: RCPIdentifier;
34
+ result: string;
35
+ }
36
+ interface RPCError {
37
+ code: number;
38
+ message: string;
39
+ data?: unknown;
40
+ }
41
+ interface RPCErrorResponse extends RPCBase {
42
+ id: RCPIdentifier;
43
+ error: RPCError;
44
+ }
45
+ type RPCResponse = RPCErrorResponse | RPCStatement | RPCNotification;
46
+ //#endregion
47
+ //#region source/interfaces.d.ts
48
+ /**
49
+ * Optional settings that change the default behavior of the network connection.
50
+ */
51
+ interface ElectrumNetworkOptions {
52
+ /** If set to true, numbers that can safely be parsed as integers will be `BigInt` rather than `Number`. */
53
+ useBigInt?: boolean;
54
+ /** When connected, send a keep-alive Ping message this often. */
55
+ sendKeepAliveIntervalInMilliSeconds?: number;
56
+ /** When disconnected, attempt to reconnect after this amount of time. */
57
+ reconnectAfterMilliSeconds?: number;
58
+ /** After every send, verify that we have received data after this amount of time. */
59
+ verifyConnectionTimeoutInMilliSeconds?: number;
60
+ /** Turn off automatic handling of browser visibility, which disconnects when application is not visible to be consistent across browsers. */
61
+ disableBrowserVisibilityHandling?: boolean;
62
+ /** Turn off automatic handling of browser connectivity. */
63
+ disableBrowserConnectivityHandling?: boolean;
64
+ }
65
+ /**
66
+ * List of events emitted by the ElectrumSocket.
67
+ * @event
68
+ * @ignore
69
+ */
70
+ interface ElectrumSocketEvents {
71
+ /**
72
+ * Emitted when data has been received over the socket.
73
+ * @eventProperty
74
+ */
75
+ 'data': [string];
76
+ /**
77
+ * Emitted when a socket connects.
78
+ * @eventProperty
79
+ */
80
+ 'connected': [];
81
+ /**
82
+ * Emitted when a socket disconnects.
83
+ * @eventProperty
84
+ */
85
+ 'disconnected': [];
86
+ /**
87
+ * Emitted when the socket has failed in some way.
88
+ * @eventProperty
89
+ */
90
+ 'error': [Error];
91
+ }
92
+ /**
93
+ * Abstract socket used when communicating with Electrum servers.
94
+ */
95
+ interface ElectrumSocket extends EventEmitter<ElectrumSocketEvents>, ElectrumSocketEvents {
96
+ /**
97
+ * Utility function to provide a human accessible host identifier.
98
+ */
99
+ get hostIdentifier(): string;
100
+ /**
101
+ * Fully qualified domain name or IP address of the host
102
+ */
103
+ host: string;
104
+ /**
105
+ * Network port for the host to connect to, defaults to the standard TLS port
106
+ */
107
+ port: number;
108
+ /**
109
+ * If false, uses an unencrypted connection instead of the default on TLS
110
+ */
111
+ encrypted: boolean;
112
+ /**
113
+ * If no connection is established after `timeout` ms, the connection is terminated
114
+ */
115
+ timeout: number;
116
+ /**
117
+ * Connects to an Electrum server using the socket.
118
+ */
119
+ connect(): void;
120
+ /**
121
+ * Disconnects from the Electrum server from the socket.
122
+ */
123
+ disconnect(): void;
124
+ /**
125
+ * Write data to the Electrum server on the socket.
126
+ *
127
+ * @param data - Data to be written to the socket
128
+ * @param callback - Callback function to be called when the write has completed
129
+ */
130
+ write(data: Uint8Array | string, callback?: (err?: Error) => void): boolean;
131
+ }
132
+ /**
133
+ * @ignore
134
+ */
135
+ interface VersionRejected {
136
+ error: RPCError;
137
+ }
138
+ /**
139
+ * @ignore
140
+ */
141
+ interface VersionNegotiated {
142
+ software: string;
143
+ protocol: string;
144
+ }
145
+ /**
146
+ * @ignore
147
+ */
148
+ type VersionNegotiationResponse = VersionNegotiated | VersionRejected;
149
+ /**
150
+ * List of events emitted by the ElectrumConnection.
151
+ * @event
152
+ * @ignore
153
+ */
154
+ interface ElectrumConnectionEvents {
155
+ /**
156
+ * Emitted when any data has been received over the network.
157
+ * @eventProperty
158
+ */
159
+ 'received': [];
160
+ /**
161
+ * Emitted when a complete electrum message has been received over the network.
162
+ * @eventProperty
163
+ */
164
+ 'response': [RPCResponse];
165
+ /**
166
+ * Emitted when the connection has completed version negotiation.
167
+ * @eventProperty
168
+ */
169
+ 'version': [VersionNegotiationResponse];
170
+ /**
171
+ * Emitted when a network connection is initiated.
172
+ * @eventProperty
173
+ */
174
+ 'connecting': [];
175
+ /**
176
+ * Emitted when a network connection is successful.
177
+ * @eventProperty
178
+ */
179
+ 'connected': [];
180
+ /**
181
+ * Emitted when a network disconnection is initiated.
182
+ * @eventProperty
183
+ */
184
+ 'disconnecting': [];
185
+ /**
186
+ * Emitted when a network disconnection is successful.
187
+ * @eventProperty
188
+ */
189
+ 'disconnected': [];
190
+ /**
191
+ * Emitted when a network connect attempts to automatically reconnect.
192
+ * @eventProperty
193
+ */
194
+ 'reconnecting': [];
195
+ /**
196
+ * Emitted when the network has failed in some way.
197
+ * @eventProperty
198
+ */
199
+ 'error': [Error];
200
+ }
201
+ /**
202
+ * List of events emitted by the ElectrumClient.
203
+ * @event
204
+ * @ignore
205
+ */
206
+ interface ElectrumClientEvents {
207
+ /**
208
+ * Emitted when an electrum subscription statement has been received over the network.
209
+ * @eventProperty
210
+ */
211
+ 'notification': [RPCNotification];
212
+ /**
213
+ * Emitted when a network connection is initiated.
214
+ * @eventProperty
215
+ */
216
+ 'connecting': [];
217
+ /**
218
+ * Emitted when a network connection is successful.
219
+ * @eventProperty
220
+ */
221
+ 'connected': [];
222
+ /**
223
+ * Emitted when a network disconnection is initiated.
224
+ * @eventProperty
225
+ */
226
+ 'disconnecting': [];
227
+ /**
228
+ * Emitted when a network disconnection is successful.
229
+ * @eventProperty
230
+ */
231
+ 'disconnected': [];
232
+ /**
233
+ * Emitted when a network connect attempts to automatically reconnect.
234
+ * @eventProperty
235
+ */
236
+ 'reconnecting': [];
237
+ /**
238
+ * Emitted when the network has failed in some way.
239
+ * @eventProperty
240
+ */
241
+ 'error': [Error];
242
+ }
243
+ /**
244
+ * A list of possible responses to requests.
245
+ * @ignore
246
+ */
247
+ type RequestResponse = RPCParameter | RPCParameter[];
248
+ /**
249
+ * Request resolvers are used to process the response of a request. This takes either
250
+ * an error object or any stringified data, while the other parameter is omitted.
251
+ * @ignore
252
+ */
253
+ type RequestResolver = (error?: Error, data?: string) => void;
254
+ /**
255
+ * Typing for promise resolution.
256
+ * @ignore
257
+ */
258
+ type ResolveFunction<T> = (value: T | PromiseLike<T>) => void;
259
+ /**
260
+ * Typing for promise rejection.
261
+ * @ignore
262
+ */
263
+ type RejectFunction = (reason?: unknown) => void;
264
+ /**
265
+ * @ignore
266
+ */
267
+ declare const isVersionRejected: (object: VersionNegotiationResponse) => object is VersionRejected;
268
+ /**
269
+ * @ignore
270
+ */
271
+ declare const isVersionNegotiated: (object: VersionNegotiationResponse) => object is VersionNegotiated;
272
+ //#endregion
273
+ //#region source/electrum-client.d.ts
274
+ /**
275
+ * High-level Electrum client that lets applications send requests and subscribe to notification events from a server.
276
+ */
277
+ declare class ElectrumClient<ElectrumEvents extends ElectrumClientEvents> extends EventEmitter<ElectrumClientEvents | ElectrumEvents> implements ElectrumClientEvents {
278
+ application: string;
279
+ version: string;
280
+ socketOrHostname: ElectrumSocket | string;
281
+ options: ElectrumNetworkOptions;
282
+ /**
283
+ * The name and version of the server software indexing the blockchain.
284
+ */
285
+ software: string;
286
+ /**
287
+ * The genesis hash of the blockchain indexed by the server.
288
+ * @remarks This is only available after a 'server.features' call.
289
+ */
290
+ genesisHash: string;
291
+ /**
292
+ * The chain height of the blockchain indexed by the server.
293
+ * @remarks This is only available after a 'blockchain.headers.subscribe' call.
294
+ */
295
+ chainHeight: number;
296
+ /**
297
+ * Timestamp of when we last received data from the server indexing the blockchain.
298
+ */
299
+ lastReceivedTimestamp: number;
300
+ /**
301
+ * Number corresponding to the underlying connection status.
302
+ */
303
+ get status(): ConnectionStatus;
304
+ private connection;
305
+ private subscriptionMethods;
306
+ private requestId;
307
+ private requestResolvers;
308
+ private connectionLock;
309
+ /**
310
+ * Initializes an Electrum client.
311
+ *
312
+ * @param application - your application name, used to identify to the electrum host.
313
+ * @param version - protocol version to use with the host.
314
+ * @param socketOrHostname - pre-configured electrum socket or fully qualified domain name or IP number of the host
315
+ * @param options - ...
316
+ *
317
+ * @throws {Error} if `version` is not a valid version string.
318
+ */
319
+ constructor(application: string, version: string, socketOrHostname: ElectrumSocket | string, options?: ElectrumNetworkOptions);
320
+ get hostIdentifier(): string;
321
+ get encrypted(): boolean;
322
+ /**
323
+ * Connects to the remote server.
324
+ *
325
+ * @throws {Error} if the socket connection fails.
326
+ * @returns a promise resolving when the connection is established.
327
+ */
328
+ connect(): Promise<void>;
329
+ /**
330
+ * Disconnects from the remote server and removes all event listeners/subscriptions and open requests.
331
+ *
332
+ * @param force - disconnect even if the connection has not been fully established yet.
333
+ * @param retainSubscriptions - retain subscription data so they will be restored on reconnection.
334
+ *
335
+ * @returns true if successfully disconnected, or false if there was no connection.
336
+ */
337
+ disconnect(force?: boolean, retainSubscriptions?: boolean): Promise<boolean>;
338
+ /**
339
+ * Calls a method on the remote server with the supplied parameters.
340
+ *
341
+ * @param method - name of the method to call.
342
+ * @param parameters - one or more parameters for the method.
343
+ *
344
+ * @throws {Error} if the client is disconnected.
345
+ * @returns a promise that resolves with the result of the method or an Error.
346
+ */
347
+ request(method: string, ...parameters: RPCParameter[]): Promise<Error | RequestResponse>;
348
+ /**
349
+ * Subscribes to the method and payload at the server.
350
+ *
351
+ * @remarks the response for the subscription request is issued as a notification event.
352
+ *
353
+ * @param method - one of the subscribable methods the server supports.
354
+ * @param parameters - one or more parameters for the method.
355
+ *
356
+ * @throws {Error} if the client is disconnected.
357
+ * @returns a promise resolving when the subscription is established.
358
+ */
359
+ subscribe(method: string, ...parameters: RPCParameter[]): Promise<void>;
360
+ /**
361
+ * Unsubscribes to the method at the server and removes any callback functions
362
+ * when there are no more subscriptions for the method.
363
+ *
364
+ * @param method - a previously subscribed to method.
365
+ * @param parameters - one or more parameters for the method.
366
+ *
367
+ * @throws {Error} if no subscriptions exist for the combination of the provided `method` and `parameters.
368
+ * @throws {Error} if the client is disconnected.
369
+ * @returns a promise resolving when the subscription is removed.
370
+ */
371
+ unsubscribe(method: string, ...parameters: RPCParameter[]): Promise<void>;
372
+ /**
373
+ * Restores existing subscriptions without updating status or triggering manual callbacks.
374
+ *
375
+ * @throws {Error} if subscription data cannot be found for all stored event names.
376
+ * @throws {Error} if the client is disconnected.
377
+ * @returns a promise resolving to true when the subscriptions are restored.
378
+ *
379
+ * @ignore
380
+ */
381
+ private resubscribeOnConnect;
382
+ /**
383
+ * Parser messages from the remote server to resolve request promises and emit subscription events.
384
+ *
385
+ * @param message - the response message
386
+ *
387
+ * @throws {Error} if the message ID does not match an existing request.
388
+ * @ignore
389
+ */
390
+ response(message: RPCResponse): void;
391
+ /**
392
+ * Callback function that is called when connection to the Electrum server is lost.
393
+ * Aborts all active requests with an error message indicating that connection was lost.
394
+ *
395
+ * @ignore
396
+ */
397
+ onConnectionDisconnect(): Promise<void>;
398
+ /**
399
+ * Stores the server provider software version field on successful version negotiation.
400
+ *
401
+ * @ignore
402
+ */
403
+ storeSoftwareVersion(versionStatement: any): Promise<void>;
404
+ /**
405
+ * Updates the last received timestamp.
406
+ *
407
+ * @ignore
408
+ */
409
+ updateLastReceivedTimestamp(): Promise<void>;
410
+ /**
411
+ * Checks if the provided message is a response to a headers subscription,
412
+ * and if so updates the locally stored chain height value for this client.
413
+ *
414
+ * @ignore
415
+ */
416
+ updateChainHeightFromHeadersNotifications(message: any): Promise<void>;
417
+ /**
418
+ * Checks if the provided message is a response to a server.features request,
419
+ * and if so stores the genesis hash for this client locally.
420
+ *
421
+ * @ignore
422
+ */
423
+ storeGenesisHashFromFeaturesResponse(message: any): Promise<void>;
424
+ /**
425
+ * Helper function to synchronize state and events with the underlying connection.
426
+ */
427
+ handleConnectionStatusChanges(eventName: any): Promise<void>;
428
+ readonly connecting: [];
429
+ readonly connected: [];
430
+ readonly disconnecting: [];
431
+ readonly disconnected: [];
432
+ readonly reconnecting: [];
433
+ readonly notification: [RPCNotification];
434
+ readonly error: [Error];
435
+ }
436
+ //#endregion
437
+ export { ConnectionStatus, ElectrumClient, ElectrumClientEvents, ElectrumConnectionEvents, ElectrumNetworkOptions, ElectrumSocket, ElectrumSocketEvents, RejectFunction, RequestResolver, RequestResponse, ResolveFunction, VersionNegotiated, VersionNegotiationResponse, VersionRejected, isVersionNegotiated, isVersionRejected };
438
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../source/enums.ts","../source/rpc-interfaces.ts","../source/interfaces.ts","../source/electrum-client.ts"],"sourcesContent":[],"mappings":";;;;;;;AASA;;;;ACRA;AAGA;AAGiB,aDEL,gBAAA;ECIK,YAAA,GAAA,CAAA;EAeA,SAAA,GAAA,CAAA;EAMA,aAAQ,GAAA,CAAA;EAQR,UAAA,GAAA,CAAA;EAEZ,YAAA,GAAA,CAAA;;;;KA3CO,YAAA;KAGA,aAAA;UAGK,OAAA;EDEL,OAAA,EAAA,MAAA;;UCIK,eAAA,SAAwB;;EAZ7B,MAAA,CAAA,EAeF,YAfc,EAAA;AAGxB;AAwCK,UAhBY,YAAA,SAAqB,OAgB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