This repository was archived by the owner on Apr 21, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathwalletWorker.ts
More file actions
1610 lines (1489 loc) · 45.1 KB
/
walletWorker.ts
File metadata and controls
1610 lines (1489 loc) · 45.1 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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import initMutinyWallet, {
ActivityItem,
BudgetPeriod,
ChannelClosure,
FederationBalance,
FederationBalances,
FedimintSweepResult,
LnUrlParams,
MutinyBalance,
MutinyBip21RawMaterials,
MutinyChannel,
MutinyInvoice,
MutinyPeer,
MutinyWallet,
NwcProfile,
PaymentParams,
PendingNwcInvoice,
TagItem
} from "@mutinywallet/mutiny-wasm";
import { IActivityItem } from "~/components";
import { MutinyWalletSettingStrings } from "~/logic/mutinyWalletSetup";
import { FakeDirectMessage, OnChainTx } from "~/routes";
import {
DiscoveredFederation,
MutinyFederationIdentity
} from "~/routes/settings";
// For some reason {...invoice } doesn't bring across the paid field
function destructureInvoice(invoice: MutinyInvoice): MutinyInvoice {
return {
amount_sats: invoice.amount_sats,
bolt11: invoice.bolt11,
description: invoice.description,
expire: invoice.expire,
expired: invoice.expired,
fees_paid: invoice.fees_paid,
inbound: invoice.inbound,
labels: invoice.labels,
last_updated: invoice.last_updated,
paid: invoice.paid,
payee_pubkey: invoice.payee_pubkey,
payment_hash: invoice.payment_hash,
potential_hodl_invoice: invoice.potential_hodl_invoice,
preimage: invoice.preimage,
privacy_level: invoice.privacy_level,
status: invoice.status
} as MutinyInvoice;
}
let wallet: MutinyWallet | undefined;
export let wasm_initialized = false;
export let wallet_initialized = false;
export async function checkForWasm() {
try {
if (
typeof WebAssembly === "object" &&
typeof WebAssembly.instantiate === "function"
) {
const module = new WebAssembly.Module(
Uint8Array.of(0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)
);
if (!(module instanceof WebAssembly.Module)) {
throw new Error("Couldn't instantiate WASM Module");
}
} else {
throw new Error("No WebAssembly global object found");
}
} catch (e) {
console.error(e);
}
}
export async function initializeWasm() {
// Actually intialize the WASM, this should be the first thing that requires the WASM blob to be downloaded
// If WASM is already initialized, don't init twice
try {
const _sats_the_standard = MutinyWallet.convert_btc_to_sats(1);
console.debug("MutinyWallet WASM already initialized, skipping init");
wasm_initialized = true;
return;
} catch (e) {
console.debug("MutinyWallet WASM about to be initialized");
await initMutinyWallet();
console.debug("MutinyWallet WASM initialized");
}
}
export async function setupMutinyWallet(
settings: MutinyWalletSettingStrings,
password?: string,
safeMode?: boolean,
shouldZapHodl?: boolean,
nsec?: string
): Promise<boolean> {
console.log("Starting setup...");
// https://developer.mozilla.org/en-US/docs/Web/API/Storage_API
// Ask the browser to not clear storage
if (navigator.storage && navigator.storage.persist) {
navigator.storage.persist().then((persistent) => {
if (persistent) {
console.log(
"Storage will not be cleared except by explicit user action"
);
} else {
console.log(
"Storage may be cleared by the UA under storage pressure."
);
}
});
}
const {
network,
proxy,
esplora,
rgs,
lsp,
lsps_connection_string,
lsps_token,
auth,
subscriptions,
storage,
scorer,
primal_api,
blind_auth,
hermes
} = settings;
console.log("Initializing Mutiny Manager");
console.log("Using network", network);
console.log("Using proxy", proxy);
console.log("Using esplora address", esplora);
console.log("Using rgs address", rgs);
console.log("Using lsp address", lsp);
console.log("Using lsp connection string", lsps_connection_string);
console.log("Using lsp token", lsps_token);
console.log("Using auth address", auth);
console.log("Using subscriptions address", subscriptions);
console.log("Using storage address", storage);
console.log("Using scorer address", scorer);
console.log("Using primal api", primal_api);
console.log("Using blind auth", blind_auth);
console.log("Using hermes", hermes);
console.log(safeMode ? "Safe mode enabled" : "Safe mode disabled");
console.log(shouldZapHodl ? "Hodl zaps enabled" : "Hodl zaps disabled");
// Only use lsps if there's no lsp set
const shouldUseLSPS = !lsp && lsps_connection_string && lsps_token;
const mutinyWallet = await new MutinyWallet(
// Password
password ? password : undefined,
// Mnemonic
undefined,
proxy,
network,
esplora,
rgs,
shouldUseLSPS ? undefined : lsp,
shouldUseLSPS ? lsps_connection_string : undefined,
shouldUseLSPS ? lsps_token : undefined,
auth,
subscriptions,
storage,
scorer,
// Do not connect peers
undefined,
// Do not skip device lock
undefined,
// Safe mode
safeMode || undefined,
// Skip hodl invoices? (defaults to true, so if shouldZapHodl is true that's when we pass false)
shouldZapHodl ? false : undefined,
// Nsec override
nsec,
// Nip7 (not supported in web worker)
undefined,
// primal URL
primal_api || "https://primal-cache.mutinywallet.com/api",
/// blind auth url
blind_auth,
/// hermes url
hermes
);
wallet = mutinyWallet;
wallet_initialized = true;
return true;
}
/**
* Gets the current balance of the wallet.
* This includes both on-chain and lightning funds.
*
* This will not include any funds in an unconfirmed lightning channel.
* @returns {Promise<MutinyBalance>}
*/
export async function get_balance(): Promise<MutinyBalance> {
const balance = await wallet!.get_balance();
return {
federation: balance.federation,
lightning: balance.lightning,
confirmed: balance.confirmed,
unconfirmed: balance.unconfirmed,
force_close: balance.force_close
} as MutinyBalance;
}
/**
* Lists the federation id's of the federation clients in the manager.
* @returns {Promise<any>}
*/
export async function list_federations(): Promise<MutinyFederationIdentity[]> {
const federations = await wallet!.list_federations();
return federations as MutinyFederationIdentity[];
}
/**
* Checks whether or not the user is subscribed to Mutiny+.
* Submits a NWC string to keep the subscription active if not expired.
*
* Returns None if there's no subscription at all.
* Returns Some(u64) for their unix expiration timestamp, which may be in the
* past or in the future, depending on whether or not it is currently active.
* @returns {Promise<bigint | undefined>}
*/
export async function check_subscribed(): Promise<bigint | undefined> {
const subscribed = await wallet!.check_subscribed();
return subscribed;
}
/**
* Stops all of the nodes and background processes.
* Returns after node has been stopped.
* @returns {Promise<void>}
*/
export async function stop(): Promise<void> {
await wallet!.stop();
}
/**
* Clears storage and deletes all data.
*
* All data in VSS persists but the device lock is cleared.
* @returns {Promise<void>}
*/
export async function delete_all(): Promise<void> {
await wallet!.delete_all();
}
/**
* Gets the current bitcoin price in chosen Fiat.
* @param {string | undefined} [fiat]
* @returns {Promise<number>}
*/
export async function get_bitcoin_price(fiat: string): Promise<number> {
const price = await wallet!.get_bitcoin_price(fiat);
return price;
}
export async function get_tag_items(): Promise<TagItem[]> {
const tagItems = await wallet!.get_tag_items();
return tagItems;
}
/**
* Returns the network of the wallet.
* @returns {string}
*/
export async function get_network(): Promise<string> {
const network = await wallet!.get_network();
return network || "signet";
}
/**
* Returns the user's nostr profile data
* @returns {any}
*/
export async function get_nostr_profile(): Promise<NostrMetadata | undefined> {
if (wallet) {
const profile = wallet.get_nostr_profile();
return { ...profile };
} else {
return undefined;
}
}
/**
* Returns all the on-chain and lightning activity from the wallet.
* @param {number | undefined} [limit]
* @param {number | undefined} [offset]
*/
export async function get_activity(
limit: number,
offset?: number
): Promise<IActivityItem[]> {
const activity = await wallet!.get_activity(limit, offset);
return activity;
}
export async function get_contact_for_npub(
npub: string
): Promise<TagItem | undefined> {
const contact = await wallet!.get_contact_for_npub(npub);
if (!contact) return undefined;
return { ...contact?.value };
}
export async function create_new_contact(
name: string,
npub?: string,
ln_address?: string,
lnurl?: string,
image_url?: string
): Promise<string> {
const contactId = await wallet!.create_new_contact(
name,
npub,
ln_address,
lnurl,
image_url
);
return contactId;
}
export async function get_tag_item(id: string): Promise<TagItem | undefined> {
const tagItem = await wallet!.get_tag_item(id);
if (!tagItem) return undefined;
return { ...tagItem?.value };
}
/**
* Returns all the on-chain and lightning activity for a given label
* @param {string} label
* @returns {Promise<any>}
*/
export async function get_label_activity(
label: string
): Promise<IActivityItem[]> {
const activity = await wallet!.get_label_activity(label);
return activity;
}
/**
* Get dm conversation between us and given npub
* Returns a vector of messages sorted by newest first
* @param {string} npub
* @param {bigint} limit
* @param {bigint | undefined} [until]
* @param {bigint | undefined} [since]
* @returns {Promise<any>}
*/
export async function get_dm_conversation(
npub: string,
limit: bigint,
until?: bigint,
since?: bigint
): Promise<FakeDirectMessage[] | undefined> {
const dms = await wallet!.get_dm_conversation(npub, limit, until, since);
return [...dms];
}
/**
* Gets an invoice from the node manager.
* This includes sent and received invoices.
* @param {string} invoice
* @returns {Promise<MutinyInvoice>}
*/
export async function get_invoice(
bolt11: string
): Promise<MutinyInvoice | undefined> {
const invoice = await wallet!.get_invoice(bolt11);
if (!invoice) return undefined;
// For some reason {...invoice } doesn't bring across the paid field
return destructureInvoice(invoice);
}
/**
* Gets all contacts sorted by last used
* @returns {Promise<any>}
*/
export async function get_contacts_sorted(): Promise<TagItem[] | undefined> {
const contacts = await wallet!.get_contacts_sorted();
return contacts;
}
export async function edit_contact(
id: string,
name: string,
npub?: string,
ln_address?: string,
lnurl?: string,
image_url?: string
): Promise<void> {
await wallet!.edit_contact(id, name, npub, ln_address, lnurl, image_url);
}
export async function delete_contact(id: string): Promise<void> {
await wallet!.delete_contact(id);
}
/**
* Follows the npub on nostr if we're not already following
* @param {string} npub
* @returns {Promise<void>}
*/
export async function follow_npub(npub: string): Promise<void> {
await wallet!.follow_npub(npub);
}
/**
* Unfollows the npub on nostr if we're following them
*
* Returns true if we were following them before
* @param {string} npub
* @returns {Promise<void>}
*/
export async function unfollow_npub(npub: string): Promise<void> {
await wallet!.unfollow_npub(npub);
}
/**
* Sends a DM to the given npub
* @param {string} npub
* @param {string} message
* @returns {Promise<string>}
*/
export async function send_dm(
npub: string,
message: string
): Promise<string | undefined> {
const result = await wallet!.send_dm(npub, message);
return result;
}
/**
* Returns the user's npub
* @returns {Promise<string>}
*/
export async function get_npub(): Promise<string | undefined> {
const npub = await wallet!.get_npub();
return npub;
}
/**
* Decodes a lightning invoice into useful information.
* Will return an error if the invoice is for a different network.
* @param {string} invoice
* @param {string | undefined} [network]
* @returns {Promise<MutinyInvoice>}
*/
export async function decode_invoice(
invoice: string,
network?: string
): Promise<MutinyInvoice | undefined> {
const decoded = await wallet!.decode_invoice(invoice, network);
if (!decoded) return undefined;
return destructureInvoice(decoded);
}
/**
* Creates a BIP 21 invoice. This creates a new address and a lightning invoice.
* The lightning invoice may return errors related to the LSP. Check the error and
* fallback to `get_new_address` and warn the user that Lightning is not available.
*
*
* Errors that might be returned include:
*
* - [`MutinyJsError::LspGenericError`]: This is returned for various reasons, including if a
* request to the LSP server fails for any reason, or if the server returns
* a status other than 500 that can't be parsed into a `ProposalResponse`.
*
* - [`MutinyJsError::LspFundingError`]: Returned if the LSP server returns an error with
* a status of 500, indicating an "Internal Server Error", and a message
* stating "Cannot fund new channel at this time". This means that the LSP cannot support
* a new channel at this time.
*
* - [`MutinyJsError::LspAmountTooHighError`]: Returned if the LSP server returns an error with
* a status of 500, indicating an "Internal Server Error", and a message stating "Invoice
* amount is too high". This means that the LSP cannot support the amount that the user
* requested. The user should request a smaller amount from the LSP.
*
* - [`MutinyJsError::LspConnectionError`]: Returned if the LSP server returns an error with
* a status of 500, indicating an "Internal Server Error", and a message that starts with
* "Failed to connect to peer". This means that the LSP is not connected to our node.
*
* If the server returns a status of 500 with a different error message,
* a [`MutinyJsError::LspGenericError`] is returned.
* @param {bigint | undefined} amount
* @param {(string)[]} labels
* @returns {Promise<MutinyBip21RawMaterials>}
*/
export async function create_bip21(
amount: bigint | undefined,
labels: string[]
): Promise<MutinyBip21RawMaterials> {
const mbrw = await wallet!.create_bip21(amount, labels);
return {
...mbrw?.value
} as MutinyBip21RawMaterials;
}
/**
* Creates a lightning invoice. The amount should be in satoshis.
* If no amount is provided, the invoice will be created with no amount.
* If no description is provided, the invoice will be created with no description.
*
* If the manager has more than one node it will create a phantom invoice.
* If there is only one node it will create an invoice just for that node.
* @param {bigint} amount
* @param {(string)[]} labels
* @returns {Promise<MutinyInvoice>}
*/
export async function create_invoice(
amount: bigint,
labels: string[]
): Promise<MutinyInvoice | undefined> {
const invoice = await wallet!.create_invoice(amount, labels);
if (!invoice) return undefined;
return destructureInvoice(invoice);
}
/**
* Estimates the onchain fee for a transaction sweep our on-chain balance
* to the given address.
*
* The fee rate is in sat/vbyte.
* @param {string} destination_address
* @param {number | undefined} [fee_rate]
* @returns {bigint}
*/
export async function estimate_sweep_tx_fee(
address: string
): Promise<bigint | undefined> {
const fee = await wallet!.estimate_sweep_tx_fee(address);
return fee;
}
/**
* Estimates the onchain fee for a transaction sending to the given address.
* The amount is in satoshis and the fee rate is in sat/vbyte.
* @param {string} destination_address
* @param {bigint} amount
* @param {number | undefined} [fee_rate]
* @returns {bigint}
*/
export async function estimate_tx_fee(
address: string,
amount: bigint,
feeRate?: number
): Promise<bigint | undefined> {
const fee = await wallet!.estimate_tx_fee(address, amount, feeRate);
return fee;
}
/**
* Calls upon a LNURL to get the parameters for it.
* This contains what kind of LNURL it is (pay, withdrawal, auth, etc).
* @param {string} lnurl
* @returns {Promise<LnUrlParams>}
*/
export async function decode_lnurl(lnurl: string): Promise<LnUrlParams> {
const lnurlParams = await wallet!.decode_lnurl(lnurl);
// PAIN: this is supposed to be returning bigints, but it returns numbers instead
return {
...lnurlParams?.value
} as LnUrlParams;
}
/**
* Pays a lightning invoice from the selected node.
* An amount should only be provided if the invoice does not have an amount.
* The amount should be in satoshis.
* @param {string} invoice_str
* @param {bigint | undefined} amt_sats
* @param {(string)[]} labels
* @returns {Promise<MutinyInvoice>}
*/
export async function pay_invoice(
invoice_str: string,
amt_sats: bigint | undefined,
labels: string[]
): Promise<MutinyInvoice | undefined> {
const invoice = await wallet!.pay_invoice(invoice_str, amt_sats, labels);
if (!invoice) return undefined;
return destructureInvoice(invoice);
}
/**
* Calls upon a LNURL and pays it.
* This will fail if the LNURL is not a LNURL pay.
* @param {string} lnurl
* @param {bigint} amount_sats
* @param {string | undefined} zap_npub
* @param {(string)[]} labels
* @param {string | undefined} [comment]
* @param {string | undefined} [privacy_level]
* @returns {Promise<MutinyInvoice>}
*/
export async function lnurl_pay(
lnurl: string,
amount_sats: bigint,
zap_npub: string | undefined,
labels: string[],
comment?: string,
privacy_level?: string
): Promise<MutinyInvoice | undefined> {
const invoice = await wallet!.lnurl_pay(
lnurl,
amount_sats,
zap_npub,
labels,
comment,
privacy_level
);
if (!invoice) return undefined;
return destructureInvoice(invoice);
}
/**
* Sweeps all the funds from the wallet to the given address.
* The fee rate is in sat/vbyte.
*
* If a fee rate is not provided, one will be used from the fee estimator.
* @param {string} destination_address
* @param {(string)[]} labels
* @param {number | undefined} [fee_rate]
* @returns {Promise<string>}
*/
export async function sweep_wallet(
destination_address: string,
labels: string[],
fee_rate?: number
): Promise<string | undefined> {
const payment = await wallet!.sweep_wallet(
destination_address,
labels,
fee_rate
);
return payment;
}
export async function send_payjoin(
payjoin_uri: string,
amount: bigint,
labels: string[],
fee_rate?: number
): Promise<string | undefined> {
const payment = await wallet!.send_payjoin(
payjoin_uri,
amount,
labels,
fee_rate
);
return payment;
}
/**
* Sends an on-chain transaction to the given address.
* The amount is in satoshis and the fee rate is in sat/vbyte.
*
* If a fee rate is not provided, one will be used from the fee estimator.
* @param {string} destination_address
* @param {bigint} amount
* @param {(string)[]} labels
* @param {number | undefined} [fee_rate]
* @returns {Promise<string>}
*/
export async function send_to_address(
destination_address: string,
amount: bigint,
labels: string[],
fee_rate?: number
): Promise<string | undefined> {
const payment = await wallet!.send_to_address(
destination_address,
amount,
labels,
fee_rate
);
return payment;
}
/**
* Sends a spontaneous payment to a node from the selected node.
* The amount should be in satoshis.
* @param {string} to_node
* @param {bigint} amt_sats
* @param {string | undefined} message
* @param {(string)[]} labels
* @returns {Promise<MutinyInvoice>}
*/
export async function keysend(
to_node: string,
amt_sats: bigint,
message: string | undefined,
labels: string[]
): Promise<MutinyInvoice | undefined> {
const invoice = await wallet!.keysend(to_node, amt_sats, message, labels);
if (!invoice) return undefined;
return destructureInvoice(invoice);
}
/**
* Gets an invoice from the node manager.
* This includes sent and received invoices.
* @param {string} hash
* @returns {Promise<MutinyInvoice>}
*/
export async function get_invoice_by_hash(
hash: string
): Promise<MutinyInvoice> {
const invoice = await wallet!.get_invoice_by_hash(hash);
return destructureInvoice(invoice);
}
/**
* Gets an channel closure from the node manager.
* @param {string} user_channel_id
* @returns {Promise<ChannelClosure>}
*/
export async function get_channel_closure(
user_channel_id: string
): Promise<ChannelClosure> {
const channel_closure = await wallet!.get_channel_closure(user_channel_id);
return {
channel_id: channel_closure.channel_id,
node_id: channel_closure.node_id,
reason: channel_closure.reason,
timestamp: channel_closure.timestamp
} as ChannelClosure;
}
/**
* Gets the details of a specific on-chain transaction.
* @param {string} txid
* @returns {any}
*/
export async function get_transaction(txid: string): Promise<ActivityItem> {
// TODO: this is an ActivityItem right?
const transaction = await wallet!.get_transaction(txid);
return transaction as ActivityItem;
}
/**
* Gets a new bitcoin address from the wallet.
* Will generate a new address on every call.
*
* It is recommended to create a new address for every transaction.
* @param {(string)[]} labels
* @returns {MutinyBip21RawMaterials}
*/
export async function get_new_address(
labels: string[]
): Promise<MutinyBip21RawMaterials> {
const mbrw = await wallet!.get_new_address(labels);
return {
...mbrw?.value
} as MutinyBip21RawMaterials;
}
/**
* Checks if the given address has any transactions.
* If it does, it returns the details of the first transaction.
*
* This should be used to check if a payment has been made to an address.
* @param {string} address
* @returns {Promise<any>}
*/
export async function check_address(address: string): Promise<OnChainTx> {
const tx = await wallet!.check_address(address);
return tx as OnChainTx;
}
/**
* Lists all the channels for all the nodes in the node manager.
* @returns {Promise<any>}
*/
export async function list_channels(): Promise<MutinyChannel[]> {
const channels = await wallet!.list_channels();
return channels;
}
/**
* This should only be called when the user is setting up a new profile
* never for an existing profile
* @param {string | undefined} [name]
* @param {string | undefined} [img_url]
* @param {string | undefined} [lnurl]
* @param {string | undefined} [nip05]
* @returns {Promise<any>}
*/
export async function setup_new_profile(
name?: string,
img_url?: string,
lnurl?: string,
nip05?: string
): Promise<unknown> {
const profile = await wallet!.setup_new_profile(
name,
img_url,
lnurl,
nip05
);
return profile;
}
/**
* Queries our relays for federation announcements
* @returns {Promise<any>}
*/
export async function discover_federations(): Promise<
DiscoveredFederation[] | undefined
> {
const federations = await wallet!.discover_federations();
return federations;
}
/**
* Checks if we have recommended the given federation
* @param {string} federation_id
* @returns {Promise<boolean>}
*/
export async function has_recommended_federation(
federation_id: string
): Promise<boolean> {
const hasRecommended =
await wallet!.has_recommended_federation(federation_id);
return hasRecommended;
}
/**
* Adds a new federation based on its federation code
* @param {string} federation_code
* @returns {Promise<FederationIdentity>}
*/
export async function new_federation(inviteCode: string): Promise<unknown> {
const newFederation = await wallet!.new_federation(inviteCode);
return newFederation;
}
export type NostrMetadata = {
name?: string;
display_name?: string;
picture?: string;
lud16?: string;
nip05?: string;
deleted?: boolean;
};
/**
* Sets the user's nostr profile data
* @param {string | undefined} [name]
* @param {string | undefined} [img_url]
* @param {string | undefined} [lnurl]
* @param {string | undefined} [nip05]
* @returns {Promise<any>}
*/
export async function edit_nostr_profile(
name?: string,
img_url?: string,
lnurl?: string,
nip05?: string
): Promise<NostrMetadata> {
const profile = await wallet!.edit_nostr_profile(
name,
img_url,
lnurl,
nip05
);
return {
...profile
};
}
/**
* Uploads a profile pic to nostr.build and returns the uploaded file's URL
* @param {string} img_base64
* @returns {Promise<string>}
*/
export async function upload_profile_pic(data: string): Promise<string> {
const url = await wallet!.upload_profile_pic(data);
return url;
}
/**
* Lists all pending NWC invoices
* @returns {(PendingNwcInvoice)[]}
*/
export async function get_pending_nwc_invoices(): Promise<
PendingNwcInvoice[] | undefined
> {
const pending = await wallet!.get_pending_nwc_invoices();
// PAIN
// Have to rebuild the array because it's an array of pointers
const newPending: PendingNwcInvoice[] = [];
for (const pendingItem of pending) {
newPending.push({
amount_sats: pendingItem.amount_sats,
expiry: pendingItem.expiry,
id: pendingItem.id,
index: pendingItem.index,
invoice: pendingItem.invoice,
invoice_description: pendingItem.invoice_description,
npub: pendingItem.npub,
profile_name: pendingItem.profile_name
} as PendingNwcInvoice);
}
return newPending;
}
/**
* Deletes a nostr wallet connect profile
* @param {number} profile_index
* @returns {Promise<void>}
*/
export async function delete_nwc_profile(index: number): Promise<void> {
await wallet!.delete_nwc_profile(index);
}
/**
* Re-enables a disabled nwc profile
* @param {number} index
* @returns {Promise<void>}
*/
export async function enable_nwc_profile(index: number): Promise<void> {
await wallet!.enable_nwc_profile(index);
}
/**
* Get nostr wallet connect profiles
* @returns {(NwcProfile)[]}
*/
export async function get_nwc_profiles(): Promise<NwcProfile[]> {
const profiles = await wallet!.get_nwc_profiles();
// PAIN
// Have to rebuild the array because it's an array of pointers
const newProfiles: NwcProfile[] = [];
for (const profile of profiles) {
newProfiles.push({
...profile.value
} as NwcProfile);
}
return newProfiles;
}
/**
* Approves a nostr wallet auth request.
* Creates a new NWC profile and saves to storage.
* This will also broadcast the info event to the relay.
* @param {string} name
* @param {string} uri
* @param {bigint} budget
* @param {BudgetPeriod} period
* @returns {Promise<NwcProfile>}
*/
export async function approve_nostr_wallet_auth(
name: string,
uri: string
): Promise<NwcProfile> {
const profile = await wallet!.approve_nostr_wallet_auth(name, uri);
return profile;
}
/**
* Finds a nostr wallet connect profile by index
* @param {number} index
* @returns {Promise<NwcProfile>}
*/
export async function get_nwc_profile(index: number): Promise<NwcProfile> {
const profile = await wallet!.get_nwc_profile(index);
console.log("get_nwc_profile", profile);
return {
...profile.value
} as NwcProfile;
}
/**
* Approves an invoice and sends the payment
* @param {string} hash
* @returns {Promise<void>}
*/
export async function approve_invoice(hash: string): Promise<void> {
await wallet!.approve_invoice(hash);
}
/**
* Removes an invoice from the pending list, will also remove expired invoices
* @param {string} hash
* @returns {Promise<void>}
*/
export async function deny_invoice(hash: string): Promise<void> {
await wallet!.deny_invoice(hash);
}