-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathres.ts
47 lines (42 loc) · 1.46 KB
/
res.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { NDKEvent } from "@nostr-dev-kit/ndk";
import { NDKKind } from "@nostr-dev-kit/ndk";
import { NDKNWCWallet } from ".";
import { NDKNWCResponseBase } from "./types";
import { NDKNWCResponseMap } from "./types";
export async function waitForResponse<M extends keyof NDKNWCResponseMap>(
this: NDKNWCWallet,
requestId: string
): Promise<NDKNWCResponseBase<NDKNWCResponseMap[M]>> {
if (!this.pool) throw new Error("Wallet not initialized");
return new Promise((resolve, reject) => {
const sub = this.ndk.subscribe(
{
kinds: [NDKKind.NostrWalletConnectRes],
"#e": [requestId],
limit: 1
},
{ groupable: false, pool: this.pool }, this.relaySet
);
sub.on("event", async (event: NDKEvent) => {
try {
await event.decrypt(event.author, this.signer);
const content = JSON.parse(event.content);
sub.stop();
if (content.error) {
reject(content);
} else {
resolve(content);
}
} catch (e: any) {
sub.stop();
reject({
result_type: "error",
error: {
code: "failed_to_parse_response",
message: e.message
}
});
}
});
});
}