Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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";
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 7 additions & 8 deletions src/NWCClient.ts → src/nwc/NWCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
EventTemplate,
Relay,
} from "nostr-tools";
import { NWCAuthorizationUrlOptions } from "./types";
import { NWCAuthorizationUrlOptions } from "../types";
import { hexToBytes, bytesToHex } from "@noble/hashes/utils";
import { Subscription } from "nostr-tools/lib/types/abstract-relay";
import { Nip47EncryptionType } from "./types";

type WithDTag = {
dTag: string;
Expand Down Expand Up @@ -243,16 +244,14 @@ export type NewNWCClientOptions = {
lud16?: string;
};

type EncryptionType = "nip04" | "nip44_v2";

export class NWCClient {
relay: Relay;
relayUrl: string;
secret: string | undefined;
lud16: string | undefined;
walletPubkey: string;
options: NWCOptions;
private _encryptionType: EncryptionType | undefined;
private _encryptionType: Nip47EncryptionType | undefined;

static parseWalletConnectUrl(walletConnectUrl: string): NWCOptions {
// makes it possible to parse with URL in the different environments (browser/node/...)
Expand Down Expand Up @@ -578,13 +577,13 @@ export class NWCClient {
const versionsTag = events[0].tags.find((t) => t[0] === "v");
const encryptionTag = events[0].tags.find((t) => t[0] === "encryption");

let encryptions: string[] = ["nip04" satisfies EncryptionType];
let encryptions: string[] = ["nip04" satisfies Nip47EncryptionType];
// TODO: Remove version tag after 01-06-2025
if (versionsTag && versionsTag[1].includes("1.0")) {
encryptions.push("nip44_v2" satisfies EncryptionType);
encryptions.push("nip44_v2" satisfies Nip47EncryptionType);
}
if (encryptionTag) {
encryptions = encryptionTag[1].split(" ") as EncryptionType[];
encryptions = encryptionTag[1].split(" ") as Nip47EncryptionType[];
}
return {
encryptions,
Expand Down Expand Up @@ -1246,7 +1245,7 @@ export class NWCClient {

private _findPreferredEncryptionType(
encryptions: string[],
): EncryptionType | null {
): Nip47EncryptionType | null {
if (encryptions.includes("nip44_v2")) {
return "nip44_v2";
}
Expand Down
Loading