From c544b9910beb27a987c9150605e30c12ea4c5cea Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 21 Dec 2023 23:32:07 +0700 Subject: [PATCH 1/3] fix: connect on every nwc request to ensure reconnect happens --- package.json | 2 +- src/webln/NostrWeblnProvider.ts | 40 +++++++++++++++------------------ 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/package.json b/package.json index 957cc09..c618a7d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@getalby/sdk", - "version": "3.2.1", + "version": "3.2.2", "description": "The SDK to integrate with Nostr Wallet Connect and the Alby API", "repository": "https://github.com/getAlby/js-sdk.git", "bugs": "https://github.com/getAlby/js-sdk/issues", diff --git a/src/webln/NostrWeblnProvider.ts b/src/webln/NostrWeblnProvider.ts index ee4bb53..155bd3b 100644 --- a/src/webln/NostrWeblnProvider.ts +++ b/src/webln/NostrWeblnProvider.ts @@ -242,10 +242,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { } async enable() { - if (this.connected) { - return Promise.resolve(); - } - await this.relay.connect(); + return Promise.resolve(); } close() { @@ -271,7 +268,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { // WebLN compatible response // TODO: use NIP-47 get_info call async getInfo(): Promise { - this.checkConnected(); + await this.checkConnected(); const supports = ["lightning", "nostr"]; const version = "Alby JS SDK"; @@ -312,8 +309,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { } } - getBalance() { - this.checkConnected(); + async getBalance() { + await this.checkConnected(); return this.executeNip47Request( "get_balance", @@ -327,8 +324,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { ); } - sendPayment(invoice: string) { - this.checkConnected(); + async sendPayment(invoice: string) { + await this.checkConnected(); return this.executeNip47Request( "pay_invoice", @@ -340,8 +337,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { ); } - keysend(args: KeysendArgs) { - this.checkConnected(); + async keysend(args: KeysendArgs) { + await this.checkConnected(); return this.executeNip47Request( "pay_keysend", @@ -369,8 +366,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { throw new Error("Method not implemented."); } - makeInvoice(args: string | number | RequestInvoiceArgs) { - this.checkConnected(); + async makeInvoice(args: string | number | RequestInvoiceArgs) { + await this.checkConnected(); const requestInvoiceArgs: RequestInvoiceArgs | undefined = typeof args === "object" ? (args as RequestInvoiceArgs) : undefined; @@ -394,8 +391,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { ); } - lookupInvoice(args: LookupInvoiceArgs) { - this.checkConnected(); + async lookupInvoice(args: LookupInvoiceArgs) { + await this.checkConnected(); return this.executeNip47Request( "lookup_invoice", @@ -412,8 +409,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { ); } - listTransactions(args: ListTransactionsArgs) { - this.checkConnected(); + async listTransactions(args: ListTransactionsArgs) { + await this.checkConnected(); // maybe we can tailor the response to our needs return this.executeNip47Request< @@ -527,12 +524,11 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { }); } - private checkConnected() { - if (!this.connected) { - throw new Error( - "please call enable() and await the promise before calling this function", - ); + private async checkConnected() { + if (!this.secret) { + throw new Error("Missing secret key"); } + await this.relay.connect(); } private executeNip47Request( From b57156bea176e32cb7b1b9e071f06dd46a301a15 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 21 Dec 2023 23:43:38 +0700 Subject: [PATCH 2/3] fix: require enable to be called first in nwc webln provider --- src/webln/NostrWeblnProvider.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/webln/NostrWeblnProvider.ts b/src/webln/NostrWeblnProvider.ts index 155bd3b..c9a8bcb 100644 --- a/src/webln/NostrWeblnProvider.ts +++ b/src/webln/NostrWeblnProvider.ts @@ -118,6 +118,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { walletPubkey: string; options: NostrWebLNOptions; subscribers: Record void>; + private _enabled = false; static parseWalletConnectUrl(walletConnectUrl: string) { walletConnectUrl = walletConnectUrl @@ -242,6 +243,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { } async enable() { + this._enabled = true; return Promise.resolve(); } @@ -525,6 +527,11 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { } private async checkConnected() { + if (!this._enabled) { + throw new Error( + "please call enable() and await the promise before calling this function", + ); + } if (!this.secret) { throw new Error("Missing secret key"); } From 995c8f33e89825490ff73d06192427faeedf042e Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 21 Dec 2023 23:49:59 +0700 Subject: [PATCH 3/3] fix: remove unnecessary promise resolve --- src/webln/NostrWeblnProvider.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/webln/NostrWeblnProvider.ts b/src/webln/NostrWeblnProvider.ts index c9a8bcb..cd78c35 100644 --- a/src/webln/NostrWeblnProvider.ts +++ b/src/webln/NostrWeblnProvider.ts @@ -244,7 +244,6 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider { async enable() { this._enabled = true; - return Promise.resolve(); } close() {