Skip to content

Commit 4710871

Browse files
authored
Merge pull request #172 from getAlby/fix/reconnect-nwc
fix: connect on every nwc request to ensure reconnect happens
2 parents d60a958 + 995c8f3 commit 4710871

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@getalby/sdk",
3-
"version": "3.2.1",
3+
"version": "3.2.2",
44
"description": "The SDK to integrate with Nostr Wallet Connect and the Alby API",
55
"repository": "https://github.com/getAlby/js-sdk.git",
66
"bugs": "https://github.com/getAlby/js-sdk/issues",

src/webln/NostrWeblnProvider.ts

+21-19
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
118118
walletPubkey: string;
119119
options: NostrWebLNOptions;
120120
subscribers: Record<string, (payload: unknown) => void>;
121+
private _enabled = false;
121122

122123
static parseWalletConnectUrl(walletConnectUrl: string) {
123124
walletConnectUrl = walletConnectUrl
@@ -242,10 +243,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
242243
}
243244

244245
async enable() {
245-
if (this.connected) {
246-
return Promise.resolve();
247-
}
248-
await this.relay.connect();
246+
this._enabled = true;
249247
}
250248

251249
close() {
@@ -271,7 +269,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
271269
// WebLN compatible response
272270
// TODO: use NIP-47 get_info call
273271
async getInfo(): Promise<GetInfoResponse> {
274-
this.checkConnected();
272+
await this.checkConnected();
275273

276274
const supports = ["lightning", "nostr"];
277275
const version = "Alby JS SDK";
@@ -312,8 +310,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
312310
}
313311
}
314312

315-
getBalance() {
316-
this.checkConnected();
313+
async getBalance() {
314+
await this.checkConnected();
317315

318316
return this.executeNip47Request<GetBalanceResponse, { balance: number }>(
319317
"get_balance",
@@ -327,8 +325,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
327325
);
328326
}
329327

330-
sendPayment(invoice: string) {
331-
this.checkConnected();
328+
async sendPayment(invoice: string) {
329+
await this.checkConnected();
332330

333331
return this.executeNip47Request<SendPaymentResponse, Nip47PayResponse>(
334332
"pay_invoice",
@@ -340,8 +338,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
340338
);
341339
}
342340

343-
keysend(args: KeysendArgs) {
344-
this.checkConnected();
341+
async keysend(args: KeysendArgs) {
342+
await this.checkConnected();
345343

346344
return this.executeNip47Request<SendPaymentResponse, Nip47PayResponse>(
347345
"pay_keysend",
@@ -369,8 +367,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
369367
throw new Error("Method not implemented.");
370368
}
371369

372-
makeInvoice(args: string | number | RequestInvoiceArgs) {
373-
this.checkConnected();
370+
async makeInvoice(args: string | number | RequestInvoiceArgs) {
371+
await this.checkConnected();
374372

375373
const requestInvoiceArgs: RequestInvoiceArgs | undefined =
376374
typeof args === "object" ? (args as RequestInvoiceArgs) : undefined;
@@ -394,8 +392,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
394392
);
395393
}
396394

397-
lookupInvoice(args: LookupInvoiceArgs) {
398-
this.checkConnected();
395+
async lookupInvoice(args: LookupInvoiceArgs) {
396+
await this.checkConnected();
399397

400398
return this.executeNip47Request<LookupInvoiceResponse, Nip47Transaction>(
401399
"lookup_invoice",
@@ -412,8 +410,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
412410
);
413411
}
414412

415-
listTransactions(args: ListTransactionsArgs) {
416-
this.checkConnected();
413+
async listTransactions(args: ListTransactionsArgs) {
414+
await this.checkConnected();
417415

418416
// maybe we can tailor the response to our needs
419417
return this.executeNip47Request<
@@ -527,12 +525,16 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
527525
});
528526
}
529527

530-
private checkConnected() {
531-
if (!this.connected) {
528+
private async checkConnected() {
529+
if (!this._enabled) {
532530
throw new Error(
533531
"please call enable() and await the promise before calling this function",
534532
);
535533
}
534+
if (!this.secret) {
535+
throw new Error("Missing secret key");
536+
}
537+
await this.relay.connect();
536538
}
537539

538540
private executeNip47Request<T, R>(

0 commit comments

Comments
 (0)