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

feat: add NWC client method to get wallet service supported methods #206

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions examples/nwc/client/get-wallet-service-supported-methods.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import * as crypto from "node:crypto"; // required in node.js
global.crypto = crypto; // required in node.js
import "websocket-polyfill"; // required in node.js

import * as readline from "node:readline/promises";
import { stdin as input, stdout as output } from "node:process";

import { nwc } from "../../../dist/index.module.js";

const rl = readline.createInterface({ input, output });

const nwcUrl =
process.env.NWC_URL ||
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));
rl.close();

const response = await nwc.NWCClient.getWalletServiceSupportedMethods(
nwc.NWCClient.parseWalletConnectUrl(nwcUrl),
);

console.info(response);
29 changes: 29 additions & 0 deletions src/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,35 @@ export class NWCClient {
return new NWCClient(options);
}

static async getWalletServiceSupportedMethods(
Copy link
Contributor

Choose a reason for hiding this comment

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

how about making on the instance? why do you think it should be on the class level?

client = NWCClient.withNewSecret(options);
await client.supportedMethods();

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. Sorry, I originally thought we could request the methods just using the relay URL, but then they can't be filtered by the wallet service pubkey (just in case).

Thanks!

options?: ConstructorParameters<typeof NWCClient>[0],
): Promise<Nip47Method[]> {
const client = NWCClient.withNewSecret(options);
await client.relay.connect();

const events = await client.relay.list(
[
{
kinds: [13194],
limit: 1,
authors: [client.walletPubkey],
},
],
{
eoseSubTimeout: 10000,
},
);

client.relay.close();

if (!events.length) {
throw new Error("no info event (kind 13194) returned from relay");
}
const result = events[0].content;
// delimiter is " " per spec, but Alby NWC originally returned ","
return result.split(/[ |,]/g) as Nip47Method[];
}

constructor(options?: NewNWCClientOptions) {
if (options && options.nostrWalletConnectUrl) {
options = {
Expand Down
Loading