From 3cb549b7f1883f7229ff4d403154beb71d62d6f7 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 22 Feb 2024 11:24:42 +0700 Subject: [PATCH 1/2] feat: add NWC client method to get wallet service supported methods --- .../get-wallet-service-supported-methods.js | 21 ++++++++++++++ src/NWCClient.ts | 29 +++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 examples/nwc/client/get-wallet-service-supported-methods.js diff --git a/examples/nwc/client/get-wallet-service-supported-methods.js b/examples/nwc/client/get-wallet-service-supported-methods.js new file mode 100644 index 0000000..3e45dad --- /dev/null +++ b/examples/nwc/client/get-wallet-service-supported-methods.js @@ -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); diff --git a/src/NWCClient.ts b/src/NWCClient.ts index 8fd4c7c..3a53326 100644 --- a/src/NWCClient.ts +++ b/src/NWCClient.ts @@ -181,6 +181,35 @@ export class NWCClient { return new NWCClient(options); } + static async getWalletServiceSupportedMethods( + options?: ConstructorParameters[0], + ): Promise { + 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 = { From 68f15cecd1228c31384f2069644d2d15c3b9c1e4 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 22 Feb 2024 21:14:32 +0700 Subject: [PATCH 2/2] fix: make getWalletServiceSupportedMethods a standard method --- .../get-wallet-service-supported-methods.js | 9 ++-- src/NWCClient.ts | 53 +++++++++---------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/examples/nwc/client/get-wallet-service-supported-methods.js b/examples/nwc/client/get-wallet-service-supported-methods.js index 3e45dad..f724272 100644 --- a/examples/nwc/client/get-wallet-service-supported-methods.js +++ b/examples/nwc/client/get-wallet-service-supported-methods.js @@ -14,8 +14,11 @@ const nwcUrl = (await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): ")); rl.close(); -const response = await nwc.NWCClient.getWalletServiceSupportedMethods( - nwc.NWCClient.parseWalletConnectUrl(nwcUrl), -); +const client = new nwc.NWCClient({ + nostrWalletConnectUrl: nwcUrl, +}); +const response = await client.getWalletServiceSupportedMethods(); console.info(response); + +client.close(); diff --git a/src/NWCClient.ts b/src/NWCClient.ts index 3a53326..6906037 100644 --- a/src/NWCClient.ts +++ b/src/NWCClient.ts @@ -181,35 +181,6 @@ export class NWCClient { return new NWCClient(options); } - static async getWalletServiceSupportedMethods( - options?: ConstructorParameters[0], - ): Promise { - 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 = { @@ -395,6 +366,30 @@ export class NWCClient { }); } + async getWalletServiceSupportedMethods(): Promise { + await this._checkConnected(); + + const events = await this.relay.list( + [ + { + kinds: [13194], + limit: 1, + authors: [this.walletPubkey], + }, + ], + { + eoseSubTimeout: 10000, + }, + ); + + 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[]; + } + async getInfo(): Promise { try { const result = await this.executeNip47Request(