diff --git a/examples/nwc/client/get-budget.js b/examples/nwc/client/get-budget.js new file mode 100644 index 0000000..7f597c0 --- /dev/null +++ b/examples/nwc/client/get-budget.js @@ -0,0 +1,23 @@ +import "../../crypto-polyfill.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.getBudget(); + +console.info(response); + +client.close(); diff --git a/src/NWCClient.ts b/src/NWCClient.ts index 255c964..afa9ee6 100644 --- a/src/NWCClient.ts +++ b/src/NWCClient.ts @@ -24,6 +24,7 @@ type WithOptionalId = { type Nip47SingleMethod = | "get_info" | "get_balance" + | "get_budget" | "make_invoice" | "pay_invoice" | "pay_keysend" @@ -47,6 +48,16 @@ export type Nip47GetInfoResponse = { notifications?: Nip47NotificationType[]; }; +export type Nip47GetBudgetResponse = + | { + used_budget: number; // msats + total_budget: number; // msats + renews_at?: number; // timestamp + renewal_period: "daily" | "weekly" | "monthly" | "yearly" | "never"; + } + // eslint-disable-next-line @typescript-eslint/ban-types + | {}; + export type Nip47GetBalanceResponse = { balance: number; // msats }; @@ -490,6 +501,20 @@ export class NWCClient { } } + async getBudget(): Promise { + try { + const result = await this.executeNip47Request( + "get_budget", + {}, + (result) => result !== undefined, + ); + return result; + } catch (error) { + console.error("Failed to request get_budget", error); + throw error; + } + } + async getBalance(): Promise { try { const result = await this.executeNip47Request( diff --git a/src/webln/NostrWeblnProvider.ts b/src/webln/NostrWeblnProvider.ts index b386603..08d9be0 100644 --- a/src/webln/NostrWeblnProvider.ts +++ b/src/webln/NostrWeblnProvider.ts @@ -63,7 +63,10 @@ type Nip07Provider = { signEvent(event: UnsignedEvent): Promise; }; -const nip47ToWeblnRequestMap: Record = { +const nip47ToWeblnRequestMap: Record< + Exclude, + WebLNMethod +> = { get_info: "getInfo", get_balance: "getBalance", make_invoice: "makeInvoice",