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: add create_connection method to NWCClient #284

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
51 changes: 51 additions & 0 deletions examples/nwc/client/create-connection.js
Original file line number Diff line number Diff line change
@@ -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);
44 changes: 42 additions & 2 deletions src/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
| {};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -581,6 +602,7 @@ export class NWCClient {
throw error;
}
}

async signMessage(
request: Nip47SignMessageRequest,
): Promise<Nip47SignMessageResponse> {
Expand All @@ -598,6 +620,24 @@ export class NWCClient {
}
}

async createConnection(
request: Nip47CreateConnectionRequest,
): Promise<Nip47CreateConnectionResponse> {
try {
const result =
await this.executeNip47Request<Nip47CreateConnectionResponse>(
"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<Nip47MultiPayInvoiceResponse> {
Expand Down
2 changes: 1 addition & 1 deletion src/webln/NostrWeblnProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ type Nip07Provider = {
};

const nip47ToWeblnRequestMap: Record<
Exclude<Nip47Method, "get_budget">,
Exclude<Nip47Method, "get_budget" | "create_connection">,
WebLNMethod
> = {
get_info: "getInfo",
Expand Down
Loading