This repository was archived by the owner on Feb 9, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathldk.ts
More file actions
984 lines (916 loc) · 27.2 KB
/
ldk.ts
File metadata and controls
984 lines (916 loc) · 27.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
import {
NativeModules,
NativeEventEmitter,
Platform,
EmitterSubscription,
} from 'react-native';
import { err, ok, Result } from './utils/result';
import {
EEventTypes,
ELdkLogLevels,
TAddPeerReq,
TChannel,
TInvoice,
TFeeUpdateReq,
TInitChannelManagerReq,
TInitConfig,
TLogListener,
TPaymentReq,
TSyncTipReq,
TCreatePaymentReq,
TSetTxConfirmedReq,
TSetTxUnconfirmedReq,
TInitNetworkGraphReq,
TCloseChannelReq,
TSpendOutputsReq,
TNetworkGraphChannelInfo,
TNetworkGraphNodeInfo,
TFileReadRes,
TFileReadReq,
TFileWriteReq,
TClaimableBalance,
} from './utils/types';
import { extractPaymentRequest } from './utils/helpers';
const LINKING_ERROR =
"The package 'react-native-ldk' doesn't seem to be linked. Make sure: \n\n" +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo managed workflow\n';
const NativeLDK =
NativeModules?.Ldk ??
new Proxy(
{},
{
get(): void {
throw new Error(LINKING_ERROR);
},
},
);
class LDK {
private readonly logListeners: TLogListener[];
private readonly ldkEvent: NativeEventEmitter;
constructor() {
this.logListeners = [];
this.ldkEvent = new NativeEventEmitter(NativeModules.LdkEventEmitter);
}
/**
* Sets the wallet storage path. Will create directories if they do not exist.
* @param path
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async setAccountStoragePath(path: string): Promise<Result<string>> {
try {
const res = await NativeLDK.setAccountStoragePath(path);
this.writeDebugToLog('setAccountStoragePath');
return ok(res);
} catch (e) {
this.writeErrorToLog('setAccountStoragePath', e);
return err(e);
}
}
/**
* Connected and disconnected blocks must be provided
* https://docs.rs/lightning/latest/lightning/chain/chainmonitor/struct.ChainMonitor.html
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async initChainMonitor(): Promise<Result<string>> {
try {
const res = await NativeLDK.initChainMonitor();
this.writeDebugToLog('initChainMonitor');
return ok(res);
} catch (e) {
this.writeErrorToLog('initChainMonitor', e);
return err(e);
}
}
/**
* Private key for node. Used to derive node public key. 32-byte entropy.
* https://docs.rs/lightning/latest/lightning/chain/keysinterface/struct.KeysManager.html
* @param seed
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async initKeysManager(seed: string): Promise<Result<string>> {
try {
const res = await NativeLDK.initKeysManager(seed);
this.writeDebugToLog('initKeysManager');
return ok(res);
} catch (e) {
this.writeErrorToLog('initKeysManager', e);
return err(e);
}
}
/**
* Accepts array of hex encoded channel monitors from storage.
* If blank array is set then initChannelManager will init new channelManager.
* @param channelMonitors
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async loadChannelMonitors(
channelMonitors: string[],
): Promise<Result<string>> {
try {
const res = await NativeLDK.loadChannelMonitors(channelMonitors);
this.writeDebugToLog(
'loadChannelMonitors',
`Loaded ${channelMonitors.length} monitors`,
);
return ok(res);
} catch (e) {
this.writeErrorToLog('loadChannelMonitors', e);
return err(e);
}
}
/**
* Inits the network graph from previous cache or syncs from scratch using genesis block hash.
* By passing in rapidGossipSyncUrl p2p gossip sync will be disabled in favor out rapid gossip sync.
* For local regtest p2p works fine but for mainnet it is better to enable rapid gossip sync.
* https://docs.rs/lightning/latest/lightning/routing/network_graph/struct.NetworkGraph.html
* @param genesisHash
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async initNetworkGraph({
genesisHash,
rapidGossipSyncUrl,
}: TInitNetworkGraphReq): Promise<Result<string>> {
try {
const res = await NativeLDK.initNetworkGraph(
genesisHash,
rapidGossipSyncUrl ?? '',
);
this.writeDebugToLog(
'initNetworkGraph',
`Rapid gossip sync ${rapidGossipSyncUrl ? 'Enabled' : 'Disabled'}`,
);
return ok(res);
} catch (e) {
this.writeErrorToLog('initNetworkGraph', e);
return err(e);
}
}
/**
* Builds UserConfig, ChannelConfig, ChannelHandshakeConfig and ChannelHandshakeLimits.
* More settings can be added to configure the below structs.
* https://docs.rs/lightning/latest/lightning/util/config/struct.UserConfig.html
* https://docs.rs/lightning/latest/lightning/util/config/struct.ChannelConfig.html
* https://docs.rs/lightning/latest/lightning/util/config/struct.ChannelHandshakeConfig.html
* https://docs.rs/lightning/latest/lightning/util/config/struct.ChannelHandshakeLimits.html
*
* @param acceptInboundChannels
* @param manuallyAcceptInboundChannels
* @param announcedChannels
* @param minChannelHandshakeDepth
* @param forceAnnouncedChannelPreference
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async initConfig(conf: TInitConfig): Promise<Result<string>> {
const {
acceptInboundChannels,
manuallyAcceptInboundChannels,
announcedChannels,
minChannelHandshakeDepth,
forceAnnouncedChannelPreference,
} = conf;
try {
const res = await NativeLDK.initConfig(
acceptInboundChannels,
manuallyAcceptInboundChannels,
announcedChannels,
minChannelHandshakeDepth,
forceAnnouncedChannelPreference,
);
this.writeDebugToLog('initConfig', conf);
return ok(res);
} catch (e) {
this.writeErrorToLog('initConfig', e);
return err(e);
}
}
/**
* Starts channel manager for current network and best block.
* Accepts array of hex encoded channel manager and channel monitors from storage.
* NOTE: If empty channelManagerSerialized string then initChannelManager will create a new channel manager.
* https://docs.rs/lightning/latest/lightning/ln/channelmanager/index.html
* @param network
* @param channelManagerSerialized
* @param channelMonitorsSerialized
* @param bestBlock
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async initChannelManager(
data: TInitChannelManagerReq,
): Promise<Result<string>> {
const { network, bestBlock } = data;
try {
const res = await NativeLDK.initChannelManager(
network,
bestBlock.hash,
bestBlock.height,
);
this.writeDebugToLog('initChannelManager', data);
return ok(res);
} catch (e) {
this.writeErrorToLog('initChannelManager', e);
return err(e);
}
}
/**
* Unsets all LDK components. Can be used to safely shutdown node.
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async reset(): Promise<Result<string>> {
try {
const res = await NativeLDK.reset();
this.writeDebugToLog('reset');
return ok(res);
} catch (e) {
this.writeErrorToLog('reset', e);
return err(e);
}
}
/**
* Switch different log levels on/off
* https://docs.rs/lightning/latest/lightning/util/logger/enum.Level.html
* @param level
* @param active
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async setLogLevel(
level: ELdkLogLevels,
active: boolean,
): Promise<Result<string>> {
try {
const res = await NativeLDK.setLogLevel(level, active);
return ok(res);
} catch (e) {
return err(e);
}
}
/**
* If set will write all LDK logging to file.
* @param level
* @param active
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async setLogFilePath(path: string): Promise<Result<string>> {
try {
const res = await NativeLDK.setLogFilePath(path);
return ok(res);
} catch (e) {
return err(e);
}
}
/**
* Write a line to current LDK log file
* @param line
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async writeToLogFile(
type: 'error' | 'info' | 'debug',
line: string,
): Promise<Result<string>> {
try {
const res = await NativeLDK.writeToLogFile(
`${type.toUpperCase()} (JS): ${line}`,
);
return ok(res);
} catch (e) {
return err(e);
}
}
/**
* Writes a JS error to log file
* @param funcName
* @param error
*/
private writeErrorToLog(funcName: string, error: any | Error): void {
this.writeToLogFile('error', `${funcName}() failed: ${error}`).catch(
console.error,
);
}
/**
* Writes a debug line to log file
* @param funcName
* @param data
*/
private writeDebugToLog(funcName: string, data: string | object = ''): void {
this.writeToLogFile(
'debug',
`${funcName}() success. ${data ? JSON.stringify(data) : ''}`,
).catch(console.error);
}
/**
* Provide fee rate information on a number of time horizons.
* https://docs.rs/lightning/latest/lightning/chain/chaininterface/enum.ConfirmationTarget.html
* @param high
* @param normal
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async updateFees(fees: TFeeUpdateReq): Promise<Result<string>> {
const { highPriority, normal, background } = fees;
try {
const res = await NativeLDK.updateFees(highPriority, normal, background);
this.writeDebugToLog('updateFees', fees);
return ok(res);
} catch (e) {
this.writeErrorToLog('updateFees', e);
return err(e);
}
}
/**
* Sets current best block on channelManager and chainMonitor
* @param header
* @param height
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async syncToTip(tip: TSyncTipReq): Promise<Result<string>> {
const { header, height } = tip;
try {
const res = await NativeLDK.syncToTip(header, height);
this.writeDebugToLog('syncToTip', tip);
return ok(res);
} catch (e) {
this.writeErrorToLog('syncToTip', e);
return err(e);
}
}
/**
* Connect to remote peer
* @param pubKey
* @param address
* @param port
* @param timeout (Android only)
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async addPeer(peer: TAddPeerReq): Promise<Result<string>> {
const { pubKey, address, port, timeout } = peer;
try {
const res = await NativeLDK.addPeer(address, port, pubKey, timeout);
this.writeDebugToLog('addPeer', peer);
return ok(res);
} catch (e) {
this.writeErrorToLog('addPeer', e);
return err(e);
}
}
/**
* Updates a watched transaction as confirmed
* @param txId
* @param transaction
* @param height
* @param pos
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async setTxConfirmed({
header,
txData,
height,
}: TSetTxConfirmedReq): Promise<Result<string>> {
try {
const res = await NativeLDK.setTxConfirmed(header, txData, height);
this.writeDebugToLog('setTxConfirmed');
return ok(res);
} catch (e) {
this.writeErrorToLog('setTxConfirmed', e);
return err(e);
}
}
/**
* Updates a watched transaction as unconfirmed in the event of a reorg
* @param txId
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async setTxUnconfirmed({
txId,
}: TSetTxUnconfirmedReq): Promise<Result<string>> {
try {
const res = await NativeLDK.setTxUnconfirmed(txId);
this.writeDebugToLog('setTxUnconfirmed');
return ok(res);
} catch (e) {
this.writeErrorToLog('setTxConfirmed', e);
return err(e);
}
}
/**
* Close channel cooperatively or use force=true to force close channel
* https://docs.rs/lightning/0.0.109/lightning/ln/channelmanager/struct.ChannelManager.html#method.close_channel
* @param channelId
* @param counterpartyNodeId
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async closeChannel({
channelId,
counterPartyNodeId,
force,
}: TCloseChannelReq): Promise<Result<string>> {
//For cooperative closes make sure we're connected to peer
if (!force) {
const peersRes = await this.listPeers();
if (peersRes.isErr()) {
return err(peersRes.error);
}
const connectedNodeId = peersRes.value.find(
(nodeId) => nodeId === counterPartyNodeId,
);
if (!connectedNodeId) {
const peerDisconnectedError =
'Cannot cooperatively close channel as peer is not connected.';
this.writeErrorToLog('closeChannel', peerDisconnectedError);
return err(peerDisconnectedError);
}
}
try {
const res = await NativeLDK.closeChannel(
channelId,
counterPartyNodeId,
!!force,
);
this.writeDebugToLog('closeChannel');
return ok(res);
} catch (e) {
this.writeErrorToLog('closeChannel', e);
return err(e);
}
}
/**
* Use LDK key manager to spend spendable outputs
* https://docs.rs/lightning/latest/lightning/chain/keysinterface/struct.KeysManager.html#method.spend_spendable_outputs
* @param descriptors
* @param outputs
* @param change_destination_script
* @param feerate_sat_per_1000_weight
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>} (Hex string of the transaction)
*/
async spendOutputs({
descriptorsSerialized,
outputs,
change_destination_script,
feerate_sat_per_1000_weight,
}: TSpendOutputsReq): Promise<Result<string>> {
try {
const res = await NativeLDK.spendOutputs(
descriptorsSerialized,
outputs,
change_destination_script,
feerate_sat_per_1000_weight,
);
this.writeDebugToLog('spendOutputs');
return ok(res);
} catch (e) {
this.writeErrorToLog('spendOutputs', e);
return err(e);
}
}
/**
* Decodes a bolt11 payment request
* @param paymentRequest
* @returns {Promise<Ok<any> | Err<unknown>>}
*/
async decode({ paymentRequest }: TPaymentReq): Promise<Result<TInvoice>> {
const cleanedPaymentRequest = extractPaymentRequest(paymentRequest);
try {
const res = await NativeLDK.decode(cleanedPaymentRequest);
this.writeDebugToLog('decode');
return ok(res);
} catch (e) {
this.writeErrorToLog('decode', e);
return err(e);
}
}
/**
* Creates bolt11 payment request
* @param amountSats
* @param description
* @returns {Promise<Ok<Ok<TInvoice> | Err<TInvoice>> | Err<unknown>>}
*/
async createPaymentRequest({
amountSats,
description,
expiryDeltaSeconds,
}: TCreatePaymentReq): Promise<Result<TInvoice>> {
//TODO confirm we have enough incoming capacity
try {
const res = await NativeLDK.createPaymentRequest(
amountSats || 0,
description,
expiryDeltaSeconds,
);
this.writeDebugToLog('createPaymentRequest');
return ok(res);
} catch (e) {
this.writeErrorToLog('createPaymentRequest', e);
return err(e);
}
}
/**
* https://docs.rs/lightning/latest/lightning/ln/channelmanager/struct.ChannelManager.html#method.process_pending_htlc_forwards
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async processPendingHtlcForwards(): Promise<Result<string>> {
try {
const res = await NativeLDK.processPendingHtlcForwards();
this.writeDebugToLog('processPendingHtlcForwards');
return ok(res);
} catch (e) {
this.writeErrorToLog('processPendingHtlcForwards', e);
return err(e);
}
}
/**
* https://docs.rs/lightning/latest/lightning/ln/channelmanager/struct.ChannelManager.html#method.claim_funds
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
* @param paymentPreimage
*/
async claimFunds(paymentPreimage: string): Promise<Result<string>> {
try {
const res = await NativeLDK.claimFunds(paymentPreimage);
this.writeDebugToLog('claimFunds');
return ok(res);
} catch (e) {
this.writeErrorToLog('claimFunds', e);
return err(e);
}
}
/**
* Pays a bolt11 payment request and returns paymentId
* @param paymentRequest
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async pay({
paymentRequest: anyPaymentRequest,
amountSats,
}: TPaymentReq): Promise<Result<string>> {
const paymentRequest = extractPaymentRequest(anyPaymentRequest);
//If no usable channels don't even attempt payment
const channelsRes = await this.listUsableChannels();
if (channelsRes.isOk() && channelsRes.value.length === 0) {
const noUsableChannelsError = 'No usable channels found';
this.writeErrorToLog('pay', noUsableChannelsError);
return err(noUsableChannelsError);
}
try {
const res = await NativeLDK.pay(paymentRequest, amountSats || 0);
this.writeDebugToLog('pay');
return ok(res);
} catch (e) {
let resultError = err(e);
//Add additional context to the error message for debugging if routing is failing.
if (resultError.code === 'invoice_payment_fail_routing') {
const decodedRes = await this.decode({ paymentRequest });
if (decodedRes.isErr()) {
//Return original payment error if we can't decode
this.writeErrorToLog('pay', e);
return err(e);
}
const { route_hints, recover_payee_pub_key, amount_satoshis } =
decodedRes.value;
let usefulMessage = `${resultError.error.message}.`;
//Check route hints
if (route_hints.length === 0) {
usefulMessage = `${usefulMessage} No route hints found in payment request.`;
}
//Check if node is in our network graph
const graphRes = await this.networkGraphNodes([recover_payee_pub_key]);
if (graphRes.isOk()) {
if (graphRes.value.length === 0) {
usefulMessage = `${usefulMessage} Node not found in network graph.`;
}
}
//Check we have enough outgoing balance in a single usable channel
if (channelsRes.isOk()) {
let highestOutgoing = 0;
channelsRes.value.forEach(({ outbound_capacity_sat }) => {
if (outbound_capacity_sat > highestOutgoing) {
highestOutgoing = outbound_capacity_sat;
}
});
let amountToSendSats = amountSats || amount_satoshis || 0;
if (amountToSendSats > highestOutgoing) {
usefulMessage = `${usefulMessage} Not enough outgoing capacity.`;
}
}
this.writeErrorToLog('pay', usefulMessage);
return err(usefulMessage);
}
this.writeErrorToLog('pay', e);
return err(e);
}
}
/**
* Abandons a payment
* @param paymentId
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async abandonPayment(paymentId: string): Promise<Result<string>> {
try {
const res = await NativeLDK.abandonPayment(paymentId);
this.writeDebugToLog('abandonPayment', paymentId);
return ok(res);
} catch (e) {
this.writeErrorToLog('abandonPayment', e);
return err(e);
}
}
//TODO pay_pubkey
/**
* Listen on LDK events
* @param event
* @param callback
*/
onEvent(
event: EEventTypes,
callback: (res: any) => void,
): EmitterSubscription {
return this.ldkEvent.addListener(event, callback);
}
/**
* Fetches LDK and c bindings version
* @returns {Promise<Ok<any> | Err<unknown>>}
*/
async version(): Promise<Result<{ c_bindings: string; ldk: string }>> {
try {
const res = await NativeLDK.version();
this.writeDebugToLog('version');
return ok(JSON.parse(res));
} catch (e) {
this.writeErrorToLog('version', e);
return err(e);
}
}
/**
* Node public key
* @returns {Promise<Err<unknown> | Ok<Ok<string> | Err<string>>>}
*/
async nodeId(): Promise<Result<string>> {
try {
const res = await NativeLDK.nodeId();
this.writeDebugToLog('nodeId');
return ok(res);
} catch (e) {
this.writeErrorToLog('nodeId', e);
return err(e);
}
}
/**
* List of peer node IDs
* @returns {Promise<Ok<Ok<string[]> | Err<string[]>> | Err<unknown>>}
*/
async listPeers(): Promise<Result<string[]>> {
try {
const res = await NativeLDK.listPeers();
this.writeDebugToLog('listPeers', `Peers: ${res.length}`);
return ok(res);
} catch (e) {
this.writeErrorToLog('listPeers', e);
return err(e);
}
}
/**
* Returns array of channels
* https://docs.rs/lightning/latest/lightning/ln/channelmanager/struct.ChannelDetails.html
* @returns {Promise<Ok<Ok<TChannel[]> | Err<TChannel[]>> | Err<unknown>>}
*/
async listChannels(): Promise<Result<TChannel[]>> {
try {
const res = await NativeLDK.listChannels();
this.writeDebugToLog('listChannels', `Channels: ${res.length}`);
return ok(res);
} catch (e) {
this.writeErrorToLog('listChannels', e);
return err(e);
}
}
/**
* Returns array of usable channels
* https://docs.rs/lightning/latest/lightning/ln/channelmanager/struct.ChannelDetails.html
* @returns {Promise<Ok<Ok<TChannel[]> | Err<TChannel[]>> | Err<unknown>>}
*/
async listUsableChannels(): Promise<Result<TChannel[]>> {
try {
const res = await NativeLDK.listUsableChannels();
this.writeDebugToLog(
'listUsableChannels',
`Usable channels: ${res.length}`,
);
return ok(res);
} catch (e) {
this.writeErrorToLog('listUsableChannels', e);
return err(e);
}
}
/**
* Lists all files in channel directory to be backed up.
* Will include past and current channels.
* @returns {Promise<Ok<Ok<string[]> | Err<string[]>> | Err<unknown>>}
*/
async listChannelFiles(): Promise<Result<string[]>> {
try {
const res = await NativeLDK.listChannelFiles();
this.writeDebugToLog('listChannelFiles', `Files: ${res.length}`);
return ok(res);
} catch (e) {
this.writeErrorToLog('listChannelFiles', e);
return err(e);
}
}
/**
* Fetches list of all node IDs in network graph
* https://docs.rs/lightning/latest/lightning/routing/gossip/struct.ReadOnlyNetworkGraph.html#method.nodes
* @returns {Promise<Ok<Ok<string[]> | Err<string[]>> | Err<unknown>>}
*/
async networkGraphListNodeIds(): Promise<Result<string[]>> {
try {
const res = await NativeLDK.networkGraphListNodeIds();
this.writeDebugToLog('networkGraphListNodeIds');
return ok(res);
} catch (e) {
this.writeErrorToLog('networkGraphListNodeIds', e);
return err(e);
}
}
/**
* Fetches array of node details from network graph
* https://docs.rs/lightning/latest/lightning/routing/gossip/struct.ChannelInfo.html
* @param nodeIds string[]
* @returns {Promise<Ok<Ok<string> | Err<string>> | Err<unknown>>}
*/
async networkGraphNodes(
nodeIds: string[],
): Promise<Result<TNetworkGraphNodeInfo[]>> {
try {
const res = await NativeLDK.networkGraphNodes(nodeIds);
this.writeDebugToLog('networkGraphNodes');
return ok(res);
} catch (e) {
this.writeErrorToLog('networkGraphNodes', e);
return err(e);
}
}
/**
* Fetches full list of nodes and their details
* @param nodeId
* @returns {Promise<Ok<Ok<TNetworkGraphChannelInfo> | Err<string>> | Err<unknown>>}
*/
async completeGraphNodes(): Promise<Result<TNetworkGraphNodeInfo[]>> {
try {
const res = await this.networkGraphListNodeIds();
if (res.isErr()) {
return err(res.error);
}
if (res.value.length > 100) {
const tooManyError = `Too many nodes to query (${res.value.length})`;
this.writeErrorToLog('completeGraphNodes', tooManyError);
return err(tooManyError);
}
const nodeRes = await this.networkGraphNodes(res.value);
if (nodeRes.isErr()) {
this.writeErrorToLog('completeGraphNodes', nodeRes.error);
return err(nodeRes.error);
}
this.writeDebugToLog('completeGraphNodes');
return ok(nodeRes.value);
} catch (e) {
this.writeErrorToLog('networkGraphNodes', e);
return err(e);
}
}
/**
* Fetches list of all short channel IDs in network graph
* https://docs.rs/lightning/latest/lightning/routing/gossip/struct.ReadOnlyNetworkGraph.html#method.channels
* @returns {Promise<Ok<string[]>> | Err<unknown>>}
*/
async networkGraphListChannels(): Promise<Result<string[]>> {
try {
const res = await NativeLDK.networkGraphListChannels();
this.writeDebugToLog('networkGraphListChannels');
return ok(res);
} catch (e) {
this.writeErrorToLog('networkGraphNodes', e);
return err(e);
}
}
/**
* Fetches channel details from network graph
* https://docs.rs/lightning/latest/lightning/routing/gossip/struct.ChannelInfo.html
* @param shortChannelId
* @returns {Promise<Ok<Ok<string> | Err<string>> | Err<unknown>>}
*/
async networkGraphChannel(
shortChannelId: string,
): Promise<Result<TNetworkGraphChannelInfo>> {
try {
const res = await NativeLDK.networkGraphChannel(shortChannelId);
this.writeDebugToLog('networkGraphChannel');
return ok({ ...res, shortChannelId });
} catch (e) {
this.writeErrorToLog('networkGraphChannel', e);
return err(e);
}
}
/**
* Fetches full list of channels and their details
* @param shortChannelId
* @returns {Promise<Ok<Ok<TNetworkGraphChannelInfo> | Err<string>> | Err<unknown>>}
*/
async completeGraphChannels(): Promise<Result<TNetworkGraphChannelInfo[]>> {
try {
const res = await this.networkGraphListChannels();
let channels: TNetworkGraphChannelInfo[] = [];
if (res.isErr()) {
this.writeErrorToLog('completeGraphChannels', res.error);
return err(res.error);
}
for (let index = 0; index < res.value.length; index++) {
const channelRes = await this.networkGraphChannel(res.value[index]);
if (channelRes.isErr()) {
this.writeErrorToLog('completeGraphChannels', channelRes.error);
return err(channelRes.error);
}
channels.push(channelRes.value);
}
this.writeDebugToLog('completeGraphChannels');
return ok(channels);
} catch (e) {
this.writeErrorToLog('completeGraphChannels', e);
return err(e);
}
}
/**
* Fetches a list of all channels LDK has ever had. Use ignoreOpenChannels to get
* pending balances for channels no longer included in list_channels (closed/closing channels).
* https://docs.rs/lightning/latest/lightning/chain/chainmonitor/struct.ChainMonitor.html#method.get_claimable_balances
* @param ignoreOpenChannels
* @returns {Promise<Ok<Ok<TClaimableBalance[]> | Err<TClaimableBalance[]>> | Err<unknown>>}
*/
async claimableBalances(
ignoreOpenChannels: boolean,
): Promise<Result<TClaimableBalance[]>> {
try {
const res = await NativeLDK.claimableBalances(ignoreOpenChannels);
this.writeDebugToLog('claimableBalances', res);
return ok(res);
} catch (e) {
this.writeErrorToLog('claimableBalances', e);
return err(e);
}
}
/**
* Write string to file in current directory set by setAccountStoragePath.
* If format is set to "hex" then it is assumed content is a hex string and
* the raw bytes will be saved to file.
* @param fileName
* @param path (optional) will use current account path as default if not provided
* @param content
* @param format
* @returns {Promise<Ok<boolean> | Err<unknown>>}
*/
async writeToFile({
fileName,
path,
content,
format,
}: TFileWriteReq): Promise<Result<boolean>> {
try {
await NativeLDK.writeToFile(
fileName,
path || '',
content,
format || 'string',
);
this.writeDebugToLog('writeToFile', fileName);
return ok(true);
} catch (e) {
this.writeErrorToLog('writeToFile', e);
return err(e);
}
}
/**
* Read from file in current directory set by setAccountStoragePath.
* If format is set to "hex" then it is assumed content of file is raw
* bytes and hex version will be returned as result.
* Will return empty string if file does not exist yet.
* @param fileName
* @param path (optional) will use current account path as default if not provided
* @param format
* @returns {Promise<Ok<{content: string, timestamp: number}> | Err<unknown>>}
*/
async readFromFile({
fileName,
format,
path,
}: TFileReadReq): Promise<Result<TFileReadRes>> {
try {
const res: TFileReadRes = await NativeLDK.readFromFile(
fileName,
path || '',
format || 'string',
);
this.writeDebugToLog('readFromFile', fileName);
return ok({ ...res, timestamp: Math.round(res.timestamp) });
} catch (e) {
this.writeErrorToLog('readFromFile', e);
return err(e);
}
}
}
export default new LDK();