Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: connect on every nwc request to ensure reconnect happens #172

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do those need to be awaited now?

is it good to mix async/await with returned promisses?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's just the same as doing

const result = await ...;
return result;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx.

"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