Skip to content

Commit

Permalink
Merge pull request #172 from getAlby/fix/reconnect-nwc
Browse files Browse the repository at this point in the history
fix: connect on every nwc request to ensure reconnect happens
  • Loading branch information
rolznz authored Dec 22, 2023
2 parents d60a958 + 995c8f3 commit 4710871
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
40 changes: 21 additions & 19 deletions src/webln/NostrWeblnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
walletPubkey: string;
options: NostrWebLNOptions;
subscribers: Record<string, (payload: unknown) => void>;
private _enabled = false;

static parseWalletConnectUrl(walletConnectUrl: string) {
walletConnectUrl = walletConnectUrl
Expand Down Expand Up @@ -242,10 +243,7 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
}

async enable() {
if (this.connected) {
return Promise.resolve();
}
await this.relay.connect();
this._enabled = true;
}

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

const supports = ["lightning", "nostr"];
const version = "Alby JS SDK";
Expand Down Expand Up @@ -312,8 +310,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
}
}

getBalance() {
this.checkConnected();
async getBalance() {
await this.checkConnected();

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

sendPayment(invoice: string) {
this.checkConnected();
async sendPayment(invoice: string) {
await this.checkConnected();

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

keysend(args: KeysendArgs) {
this.checkConnected();
async keysend(args: KeysendArgs) {
await this.checkConnected();

return this.executeNip47Request<SendPaymentResponse, Nip47PayResponse>(
"pay_keysend",
Expand Down Expand Up @@ -369,8 +367,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;
Expand All @@ -394,8 +392,8 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
);
}

lookupInvoice(args: LookupInvoiceArgs) {
this.checkConnected();
async lookupInvoice(args: LookupInvoiceArgs) {
await this.checkConnected();

return this.executeNip47Request<LookupInvoiceResponse, Nip47Transaction>(
"lookup_invoice",
Expand All @@ -412,8 +410,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<
Expand Down Expand Up @@ -527,12 +525,16 @@ export class NostrWebLNProvider implements WebLNProvider, Nip07Provider {
});
}

private checkConnected() {
if (!this.connected) {
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");
}
await this.relay.connect();
}

private executeNip47Request<T, R>(
Expand Down

0 comments on commit 4710871

Please sign in to comment.