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: read supported notification types from wallet service info notifications tag #218

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 24 additions & 0 deletions examples/nwc/client/get-wallet-service-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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 client = new nwc.NWCClient({
nostrWalletConnectUrl: nwcUrl,
});
const response = await client.getWalletServiceInfo();

console.info(response);

client.close();
33 changes: 29 additions & 4 deletions src/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Nip47SingleMethod =
type Nip47MultiMethod = "multi_pay_invoice" | "multi_pay_keysend";

export type Nip47Method = Nip47SingleMethod | Nip47MultiMethod;
export type Nip47Capability = Nip47Method | "notifications";

export type Nip47GetInfoResponse = {
alias: string;
Expand Down Expand Up @@ -100,6 +101,8 @@ export type Nip47Transaction = {
metadata?: Record<string, unknown>;
};

export type Nip47NotificationType = Nip47Notification["notification_type"];

export type Nip47Notification = {
notification_type: "payment_received";
notification: Nip47Transaction;
Expand Down Expand Up @@ -408,7 +411,22 @@ export class NWCClient {
});
}

async getWalletServiceSupportedMethods(): Promise<Nip47Method[]> {
/**
* @deprecated please use getWalletServiceInfo. Deprecated since v3.5.0. Will be removed in v4.0.0.
*/
async getWalletServiceSupportedMethods(): Promise<Nip47Capability[]> {
console.warn(
"getWalletServiceSupportedMethods is deprecated. Please use getWalletServiceInfo instead.",
);
const info = await this.getWalletServiceInfo();

return info.capabilities;
}

async getWalletServiceInfo(): Promise<{
capabilities: Nip47Capability[];
notifications: Nip47NotificationType[];
}> {
await this._checkConnected();

const events = await this.relay.list(
Expand All @@ -427,9 +445,16 @@ export class NWCClient {
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[];
const content = events[0].content;
const notificationsTag = events[0].tags.find(
(t) => t[0] === "notifications",
);
return {
// delimiter is " " per spec, but Alby NWC originally returned ","
capabilities: content.split(/[ |,]/g) as Nip47Method[],
notifications: (notificationsTag?.[1]?.split(" ") ||
[]) as Nip47NotificationType[],
};
}

async getInfo(): Promise<Nip47GetInfoResponse> {
Expand Down
Loading