@@ -118,6 +118,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
118
118
walletPubkey : string ;
119
119
options : NostrWebLNOptions ;
120
120
subscribers : Record < string , ( payload : unknown ) => void > ;
121
+ private _enabled = false ;
121
122
122
123
static parseWalletConnectUrl ( walletConnectUrl : string ) {
123
124
walletConnectUrl = walletConnectUrl
@@ -242,10 +243,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
242
243
}
243
244
244
245
async enable ( ) {
245
- if ( this . connected ) {
246
- return Promise . resolve ( ) ;
247
- }
248
- await this . relay . connect ( ) ;
246
+ this . _enabled = true ;
249
247
}
250
248
251
249
close ( ) {
@@ -271,7 +269,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
271
269
// WebLN compatible response
272
270
// TODO: use NIP-47 get_info call
273
271
async getInfo ( ) : Promise < GetInfoResponse > {
274
- this . checkConnected ( ) ;
272
+ await this . checkConnected ( ) ;
275
273
276
274
const supports = [ "lightning" , "nostr" ] ;
277
275
const version = "Alby JS SDK" ;
@@ -312,8 +310,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
312
310
}
313
311
}
314
312
315
- getBalance ( ) {
316
- this . checkConnected ( ) ;
313
+ async getBalance ( ) {
314
+ await this . checkConnected ( ) ;
317
315
318
316
return this . executeNip47Request < GetBalanceResponse , { balance : number } > (
319
317
"get_balance" ,
@@ -327,8 +325,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
327
325
) ;
328
326
}
329
327
330
- sendPayment ( invoice : string ) {
331
- this . checkConnected ( ) ;
328
+ async sendPayment ( invoice : string ) {
329
+ await this . checkConnected ( ) ;
332
330
333
331
return this . executeNip47Request < SendPaymentResponse , Nip47PayResponse > (
334
332
"pay_invoice" ,
@@ -340,8 +338,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
340
338
) ;
341
339
}
342
340
343
- keysend ( args : KeysendArgs ) {
344
- this . checkConnected ( ) ;
341
+ async keysend ( args : KeysendArgs ) {
342
+ await this . checkConnected ( ) ;
345
343
346
344
return this . executeNip47Request < SendPaymentResponse , Nip47PayResponse > (
347
345
"pay_keysend" ,
@@ -369,8 +367,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
369
367
throw new Error ( "Method not implemented." ) ;
370
368
}
371
369
372
- makeInvoice ( args : string | number | RequestInvoiceArgs ) {
373
- this . checkConnected ( ) ;
370
+ async makeInvoice ( args : string | number | RequestInvoiceArgs ) {
371
+ await this . checkConnected ( ) ;
374
372
375
373
const requestInvoiceArgs : RequestInvoiceArgs | undefined =
376
374
typeof args === "object" ? ( args as RequestInvoiceArgs ) : undefined ;
@@ -394,8 +392,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
394
392
) ;
395
393
}
396
394
397
- lookupInvoice ( args : LookupInvoiceArgs ) {
398
- this . checkConnected ( ) ;
395
+ async lookupInvoice ( args : LookupInvoiceArgs ) {
396
+ await this . checkConnected ( ) ;
399
397
400
398
return this . executeNip47Request < LookupInvoiceResponse , Nip47Transaction > (
401
399
"lookup_invoice" ,
@@ -412,8 +410,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
412
410
) ;
413
411
}
414
412
415
- listTransactions ( args : ListTransactionsArgs ) {
416
- this . checkConnected ( ) ;
413
+ async listTransactions ( args : ListTransactionsArgs ) {
414
+ await this . checkConnected ( ) ;
417
415
418
416
// maybe we can tailor the response to our needs
419
417
return this . executeNip47Request <
@@ -527,12 +525,16 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
527
525
} ) ;
528
526
}
529
527
530
- private checkConnected ( ) {
531
- if ( ! this . connected ) {
528
+ private async checkConnected ( ) {
529
+ if ( ! this . _enabled ) {
532
530
throw new Error (
533
531
"please call enable() and await the promise before calling this function" ,
534
532
) ;
535
533
}
534
+ if ( ! this . secret ) {
535
+ throw new Error ( "Missing secret key" ) ;
536
+ }
537
+ await this . relay . connect ( ) ;
536
538
}
537
539
538
540
private executeNip47Request < T , R > (
0 commit comments