Skip to content

Commit

Permalink
fix: re-subscribe after relay disconnections in NWAClient (#331)
Browse files Browse the repository at this point in the history
* fix: re-subscribe after relay disconnections in NWAClient

* fix: make error message and comment specific to NWA
  • Loading branch information
rolznz authored Mar 3, 2025
1 parent 7431e39 commit 745f1c9
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 37 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": "4.1.0",
"version": "4.1.1",
"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
113 changes: 77 additions & 36 deletions src/NWAClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Nip47NotificationType,
NWCClient,
} from "./NWCClient";
import { Subscription } from "nostr-tools/lib/types/abstract-relay";

export type NWAOptions = {
relayUrl: string;
Expand Down Expand Up @@ -170,47 +171,87 @@ export class NWAClient {
}): Promise<{
unsub: () => void;
}> {
await this._checkConnected();

const sub = this.relay.subscribe(
[
{
kinds: [13194], // NIP-47 info event
"#p": [this.options.appPubkey],
},
],
{
// eoseTimeout: 10000,
},
);
let subscribed = true;
let endPromise: (() => void) | undefined;
let onRelayDisconnect: (() => void) | undefined;
let sub: Subscription | undefined;
(async () => {
while (subscribed) {
try {
await this._checkConnected();

const unsub = () => {
sub.close();
this.relay.close();
};
const sub = this.relay.subscribe(
[
{
kinds: [13194], // NIP-47 info event
"#p": [this.options.appPubkey],
},
],
{
// eoseTimeout: 10000,
},
);
console.info("subscribed to relay");

sub.onevent = async (event) => {
const client = new NWCClient({
relayUrl: this.options.relayUrl,
secret: this.appSecretKey,
walletPubkey: event.pubkey,
});

// try to fetch the lightning address
try {
const info = await client.getInfo();
client.options.lud16 = info.lud16;
client.lud16 = info.lud16;
} catch (error) {
console.error("failed to fetch get_info", error);
}
const unsub = () => {
sub.close();
this.relay.close();
};

args.onSuccess(client);
unsub();
};
sub.onevent = async (event) => {
const client = new NWCClient({
relayUrl: this.options.relayUrl,
secret: this.appSecretKey,
walletPubkey: event.pubkey,
});

// try to fetch the lightning address
try {
const info = await client.getInfo();
client.options.lud16 = info.lud16;
client.lud16 = info.lud16;
} catch (error) {
console.error("failed to fetch get_info", error);
}

args.onSuccess(client);
unsub();
};

await new Promise<void>((resolve) => {
endPromise = () => {
resolve();
};
onRelayDisconnect = () => {
console.info("relay disconnected");
endPromise?.();
};
this.relay.onclose = onRelayDisconnect;
});
if (onRelayDisconnect !== undefined) {
this.relay.onclose = null;
}
} catch (error) {
console.error(
"error subscribing to info event",
error || "unknown relay error",
);
}
if (subscribed) {
// wait a second and try re-connecting
// any events during this period will be lost
// unless using a relay that keeps events until client reconnect
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
})();

return {
unsub,
unsub: () => {
subscribed = false;
endPromise?.();
sub?.close();
},
};
}

Expand Down
1 change: 1 addition & 0 deletions src/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,7 @@ export class NWCClient {
if (subscribed) {
// wait a second and try re-connecting
// any notifications during this period will be lost
// unless using a relay that keeps events until client reconnect
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
Expand Down

0 comments on commit 745f1c9

Please sign in to comment.