Skip to content
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
7 changes: 6 additions & 1 deletion examples/nwc/client/make-invoice.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ const rl = readline.createInterface({ input, output });
const nwcUrl =
process.env.NWC_URL ||
(await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): "));

const amount =
parseInt((await rl.question("Amount in sats (default 1 sat): ")) || "1") *
1000;

rl.close();

const client = new nwc.NWCClient({
nostrWalletConnectUrl: nwcUrl,
});

const response = await client.makeInvoice({
amount: 1000, // in millisats
amount, // in millisats
description: "NWC Client example",
});

Expand Down
57 changes: 57 additions & 0 deletions examples/nwc/wallet-service/example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import "websocket-polyfill"; // required in node.js

import { generateSecretKey, getPublicKey } from "nostr-tools";
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";

const walletServiceSecretKey = bytesToHex(generateSecretKey());
const walletServicePubkey = getPublicKey(hexToBytes(walletServiceSecretKey));

const clientSecretKey = bytesToHex(generateSecretKey());
const clientPubkey = getPublicKey(hexToBytes(clientSecretKey));

const relayUrl = "wss://relay.getalby.com/v1";

const nwcUrl = `nostr+walletconnect://${walletServicePubkey}?relay=${relayUrl}&secret=${clientSecretKey}`;

console.info("enter this NWC URL in a client: ", nwcUrl);

import { nwc } from "../../../dist/index.module.js";

const walletService = new nwc.NWCWalletService({
relayUrl,
});

await walletService.publishWalletServiceInfoEvent(
walletServiceSecretKey,
["get_info"],
[],
);

const keypair = new nwc.NWCWalletServiceKeyPair(
walletServiceSecretKey,
clientPubkey,
);

const unsub = await walletService.subscribe(keypair, {
getInfo: () => {
return Promise.resolve({
result: {
methods: ["get_info"],
alias: "Alby Hub",
//... add other fields here
},
error: undefined,
});
},
// ... handle other NIP-47 methods here
});

console.info("Waiting for events...");
process.on("SIGINT", function () {
console.info("Caught interrupt signal");

unsub();
walletService.close();

process.exit();
});
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export * as auth from "./auth";
export * as types from "./types";
export * as webln from "./webln";
export { Client } from "./client";
export * as nwc from "./NWCClient";
export * as nwa from "./NWAClient";
export * as nwa from "./nwc/NWAClient";
export * as nwc from "./nwc";
2 changes: 1 addition & 1 deletion src/NWAClient.test.ts → src/nwc/NWAClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import "websocket-polyfill";
import { NWAClient } from "./NWAClient";
import { bytesToHex, hexToBytes } from "@noble/hashes/utils";
import { generateSecretKey, getPublicKey } from "nostr-tools";
import { Nip47Method, Nip47NotificationType } from "./NWCClient";
import { Nip47Method, Nip47NotificationType } from "./types";

describe("NWA URI", () => {
test("constructs correct connection URI with custom app secret key", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/NWAClient.ts → src/nwc/NWAClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import {
Nip47Method,
Nip47NetworkError,
Nip47NotificationType,
NWCClient,
} from "./NWCClient";
} from "./types";
import { NWCClient } from "./NWCClient";
import { Subscription } from "nostr-tools/lib/types/abstract-relay";

export type NWAOptions = {
Expand Down
File renamed without changes.
Loading