@@ -36,6 +36,37 @@ const NWCs: Record<string, NostrWebLNOptions> = {
36
36
} ,
37
37
} ;
38
38
39
+ // TODO: move to webln-types package
40
+ export interface ListTransactionsArgs {
41
+ from ?: number ;
42
+ until ?: number ;
43
+ limit ?: number ;
44
+ offset ?: number ;
45
+ unpaid ?: boolean ;
46
+ type ?: "incoming" | "outgoing" ;
47
+ }
48
+
49
+ // TODO: move to webln-types package
50
+ export interface Transaction {
51
+ type : string ;
52
+ invoice : string ;
53
+ description : string ;
54
+ description_hash : string ;
55
+ preimage : string ;
56
+ payment_hash : string ;
57
+ amount : number ;
58
+ fees_paid : number ;
59
+ settled_at : number ;
60
+ created_at : number ;
61
+ expires_at : number ;
62
+ metadata ?: Record < string , unknown > ;
63
+ }
64
+
65
+ // TODO: move to webln-types package
66
+ export interface ListTransactionsResponse {
67
+ transactions : Transaction [ ] ;
68
+ }
69
+
39
70
interface NostrWebLNOptions {
40
71
authorizationUrl ?: string ; // the URL to the NWC interface for the user to confirm the session
41
72
relayUrl : string ;
@@ -58,6 +89,11 @@ type Nip47GetInfoResponse = {
58
89
methods : string [ ] ;
59
90
} ;
60
91
92
+ type Nip47ListTransactionsResponse = {
93
+ transactions : Nip47Transaction [ ] ;
94
+ } ;
95
+ type Nip47Transaction = Transaction ;
96
+
61
97
type Nip47PayResponse = {
62
98
preimage : string ;
63
99
} ;
@@ -69,6 +105,7 @@ const nip47ToWeblnRequestMap = {
69
105
pay_invoice : "sendPayment" ,
70
106
pay_keysend : "payKeysend" ,
71
107
lookup_invoice : "lookupInvoice" ,
108
+ list_transactions : "listTransactions" ,
72
109
} ;
73
110
74
111
export class NostrWebLNProvider implements WebLNProvider , Nip07Provider {
@@ -336,7 +373,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
336
373
throw new Error ( "No amount specified" ) ;
337
374
}
338
375
339
- return this . executeNip47Request < MakeInvoiceResponse , { invoice : string } > (
376
+ return this . executeNip47Request < MakeInvoiceResponse , Nip47Transaction > (
340
377
"make_invoice" ,
341
378
{
342
379
amount : amount * 1000 , // NIP-47 uses msat
@@ -353,14 +390,33 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
353
390
lookupInvoice ( args : LookupInvoiceArgs ) {
354
391
this . checkConnected ( ) ;
355
392
393
+ return this . executeNip47Request < LookupInvoiceResponse , Nip47Transaction > (
394
+ "lookup_invoice" ,
395
+ args ,
396
+ ( result ) => ! ! result . invoice ,
397
+ ( result ) => ( {
398
+ paymentRequest : result . invoice ,
399
+ paid : ! ! result . settled_at ,
400
+ } ) ,
401
+ ) ;
402
+ }
403
+
404
+ listTransactions ( args : ListTransactionsArgs ) {
405
+ this . checkConnected ( ) ;
406
+
407
+ // maybe we can tailor the response to our needs
356
408
return this . executeNip47Request <
357
- LookupInvoiceResponse ,
358
- { invoice : string ; paid : boolean }
409
+ ListTransactionsResponse ,
410
+ Nip47ListTransactionsResponse
359
411
> (
360
- "lookup_invoice " ,
412
+ "list_transactions " ,
361
413
args ,
362
- ( result ) => result . invoice !== undefined && result . paid !== undefined ,
363
- ( result ) => ( { paymentRequest : result . invoice , paid : result . paid } ) ,
414
+ ( response ) => ! ! response . transactions ,
415
+ ( response ) => ( {
416
+ transactions : response . transactions . map (
417
+ mapNip47TransactionToTransaction ,
418
+ ) ,
419
+ } ) ,
364
420
) ;
365
421
}
366
422
@@ -532,7 +588,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
532
588
}
533
589
// @ts -ignore // event is still unknown in nostr-tools
534
590
if ( event . kind == 23195 && response . result ) {
535
- //console.log ("NIP-47 result", response.result);
591
+ // console.info ("NIP-47 result", response.result);
536
592
if ( resultValidator ( response . result ) ) {
537
593
resolve ( resultMapper ( response . result ) ) ;
538
594
this . notify ( weblnMethod , response . result ) ;
@@ -572,4 +628,17 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
572
628
}
573
629
}
574
630
631
+ function mapNip47TransactionToTransaction (
632
+ transaction : Nip47Transaction ,
633
+ ) : Transaction {
634
+ return {
635
+ ...transaction ,
636
+ // NWC uses msats - convert to sats for webln
637
+ amount : Math . floor ( transaction . amount / 1000 ) ,
638
+ fees_paid : transaction . fees_paid
639
+ ? Math . floor ( transaction . fees_paid / 1000 )
640
+ : 0 ,
641
+ } ;
642
+ }
643
+
575
644
export const NWC = NostrWebLNProvider ;
0 commit comments