Skip to content

Commit

Permalink
Merge pull request #218 from getAlby/feat/info-notifications-tag
Browse files Browse the repository at this point in the history
feat: read supported notification types from wallet service info notifications tag
  • Loading branch information
rolznz authored Apr 10, 2024
2 parents f55216d + 1b15c47 commit 8ab2952
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
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

0 comments on commit 8ab2952

Please sign in to comment.