diff --git a/examples/nwc/client/create-connection.js b/examples/nwc/client/create-connection.js new file mode 100644 index 0000000..bec68c0 --- /dev/null +++ b/examples/nwc/client/create-connection.js @@ -0,0 +1,51 @@ +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"; +import { generateSecretKey, getPublicKey } from "nostr-tools"; + +import { bytesToHex } from "@noble/hashes/utils"; + +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, +}); + +let secretKey = generateSecretKey(); +let pubkey = getPublicKey(secretKey); + +const response = await client.createConnection({ + pubkey, + name: "Test created app from JS SDK " + new Date().toISOString(), + methods: [ + "get_info", + "get_balance", + "get_budget", + "make_invoice", + "pay_invoice", + "lookup_invoice", + "list_transactions", + "sign_message", + ], +}); + +console.info(response); + +client.close(); + +const childClient = new nwc.NWCClient({ + relayUrl: client.relayUrl, + secret: bytesToHex(secretKey), + walletPubkey: response.wallet_pubkey, +}); + +const info = await childClient.getInfo(); +console.info("Got info from created app", info); diff --git a/src/NWCClient.ts b/src/NWCClient.ts index 700283d..5d16933 100644 --- a/src/NWCClient.ts +++ b/src/NWCClient.ts @@ -30,12 +30,19 @@ type Nip47SingleMethod = | "pay_keysend" | "lookup_invoice" | "list_transactions" - | "sign_message"; + | "sign_message" + | "create_connection"; type Nip47MultiMethod = "multi_pay_invoice" | "multi_pay_keysend"; export type Nip47Method = Nip47SingleMethod | Nip47MultiMethod; export type Nip47Capability = Nip47Method | "notifications"; +export type BudgetRenewalPeriod = + | "daily" + | "weekly" + | "monthly" + | "yearly" + | "never"; export type Nip47GetInfoResponse = { alias: string; @@ -53,7 +60,7 @@ export type Nip47GetBudgetResponse = used_budget: number; // msats total_budget: number; // msats renews_at?: number; // timestamp - renewal_period: "daily" | "weekly" | "monthly" | "yearly" | "never"; + renewal_period: BudgetRenewalPeriod; } // eslint-disable-next-line @typescript-eslint/ban-types | {}; @@ -167,6 +174,20 @@ export type Nip47SignMessageRequest = { message: string; }; +export type Nip47CreateConnectionRequest = { + pubkey: string; + name: string; + methods: string[]; + budget?: { budget: number; renewal_period: BudgetRenewalPeriod }; + expires_at?: number; + isolated?: boolean; + metadata: unknown; +}; + +export type Nip47CreateConnectionResponse = { + wallet_pubkey: string; +}; + export type Nip47SignMessageResponse = { message: string; signature: string; @@ -581,6 +602,7 @@ export class NWCClient { throw error; } } + async signMessage( request: Nip47SignMessageRequest, ): Promise { @@ -598,6 +620,24 @@ export class NWCClient { } } + async createConnection( + request: Nip47CreateConnectionRequest, + ): Promise { + try { + const result = + await this.executeNip47Request( + "create_connection", + request, + (result) => !!result.wallet_pubkey, + ); + + return result; + } catch (error) { + console.error("Failed to request create_connection", error); + throw error; + } + } + async multiPayInvoice( request: Nip47MultiPayInvoiceRequest, ): Promise { diff --git a/src/webln/NostrWeblnProvider.ts b/src/webln/NostrWeblnProvider.ts index 29d0396..d3cdfab 100644 --- a/src/webln/NostrWeblnProvider.ts +++ b/src/webln/NostrWeblnProvider.ts @@ -65,7 +65,7 @@ type Nip07Provider = { }; const nip47ToWeblnRequestMap: Record< - Exclude, + Exclude, WebLNMethod > = { get_info: "getInfo",