Skip to content

Commit 745f1c9

Browse files
authored
fix: re-subscribe after relay disconnections in NWAClient (#331)
* fix: re-subscribe after relay disconnections in NWAClient * fix: make error message and comment specific to NWA
1 parent 7431e39 commit 745f1c9

File tree

3 files changed

+79
-37
lines changed

3 files changed

+79
-37
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": "4.1.0",
3+
"version": "4.1.1",
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/NWAClient.ts

+77-36
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Nip47NotificationType,
88
NWCClient,
99
} from "./NWCClient";
10+
import { Subscription } from "nostr-tools/lib/types/abstract-relay";
1011

1112
export type NWAOptions = {
1213
relayUrl: string;
@@ -170,47 +171,87 @@ export class NWAClient {
170171
}): Promise<{
171172
unsub: () => void;
172173
}> {
173-
await this._checkConnected();
174-
175-
const sub = this.relay.subscribe(
176-
[
177-
{
178-
kinds: [13194], // NIP-47 info event
179-
"#p": [this.options.appPubkey],
180-
},
181-
],
182-
{
183-
// eoseTimeout: 10000,
184-
},
185-
);
174+
let subscribed = true;
175+
let endPromise: (() => void) | undefined;
176+
let onRelayDisconnect: (() => void) | undefined;
177+
let sub: Subscription | undefined;
178+
(async () => {
179+
while (subscribed) {
180+
try {
181+
await this._checkConnected();
186182

187-
const unsub = () => {
188-
sub.close();
189-
this.relay.close();
190-
};
183+
const sub = this.relay.subscribe(
184+
[
185+
{
186+
kinds: [13194], // NIP-47 info event
187+
"#p": [this.options.appPubkey],
188+
},
189+
],
190+
{
191+
// eoseTimeout: 10000,
192+
},
193+
);
194+
console.info("subscribed to relay");
191195

192-
sub.onevent = async (event) => {
193-
const client = new NWCClient({
194-
relayUrl: this.options.relayUrl,
195-
secret: this.appSecretKey,
196-
walletPubkey: event.pubkey,
197-
});
198-
199-
// try to fetch the lightning address
200-
try {
201-
const info = await client.getInfo();
202-
client.options.lud16 = info.lud16;
203-
client.lud16 = info.lud16;
204-
} catch (error) {
205-
console.error("failed to fetch get_info", error);
206-
}
196+
const unsub = () => {
197+
sub.close();
198+
this.relay.close();
199+
};
207200

208-
args.onSuccess(client);
209-
unsub();
210-
};
201+
sub.onevent = async (event) => {
202+
const client = new NWCClient({
203+
relayUrl: this.options.relayUrl,
204+
secret: this.appSecretKey,
205+
walletPubkey: event.pubkey,
206+
});
207+
208+
// try to fetch the lightning address
209+
try {
210+
const info = await client.getInfo();
211+
client.options.lud16 = info.lud16;
212+
client.lud16 = info.lud16;
213+
} catch (error) {
214+
console.error("failed to fetch get_info", error);
215+
}
216+
217+
args.onSuccess(client);
218+
unsub();
219+
};
220+
221+
await new Promise<void>((resolve) => {
222+
endPromise = () => {
223+
resolve();
224+
};
225+
onRelayDisconnect = () => {
226+
console.info("relay disconnected");
227+
endPromise?.();
228+
};
229+
this.relay.onclose = onRelayDisconnect;
230+
});
231+
if (onRelayDisconnect !== undefined) {
232+
this.relay.onclose = null;
233+
}
234+
} catch (error) {
235+
console.error(
236+
"error subscribing to info event",
237+
error || "unknown relay error",
238+
);
239+
}
240+
if (subscribed) {
241+
// wait a second and try re-connecting
242+
// any events during this period will be lost
243+
// unless using a relay that keeps events until client reconnect
244+
await new Promise((resolve) => setTimeout(resolve, 1000));
245+
}
246+
}
247+
})();
211248

212249
return {
213-
unsub,
250+
unsub: () => {
251+
subscribed = false;
252+
endPromise?.();
253+
sub?.close();
254+
},
214255
};
215256
}
216257

src/NWCClient.ts

+1
Original file line numberDiff line numberDiff line change
@@ -881,6 +881,7 @@ export class NWCClient {
881881
if (subscribed) {
882882
// wait a second and try re-connecting
883883
// any notifications during this period will be lost
884+
// unless using a relay that keeps events until client reconnect
884885
await new Promise((resolve) => setTimeout(resolve, 1000));
885886
}
886887
}

0 commit comments

Comments
 (0)