diff --git a/examples/nwc/client/hold-invoice.ts b/examples/nwc/client/hold-invoice.ts index 9eb0a939..84dba0f1 100644 --- a/examples/nwc/client/hold-invoice.ts +++ b/examples/nwc/client/hold-invoice.ts @@ -3,7 +3,7 @@ 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 { NWCClient } from "@getalby/sdk/nwc"; +import { Nip47Notification, NWCClient } from "@getalby/sdk/nwc"; const rl = readline.createInterface({ input, output }); @@ -21,7 +21,7 @@ const client = new NWCClient({ nostrWalletConnectUrl: nwcUrl, }); -const toHexString = (bytes) => +const toHexString = (bytes: Uint8Array) => bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), ""); const preimageBytes = crypto.getRandomValues(new Uint8Array(32)); @@ -41,7 +41,7 @@ const response = await client.makeHoldInvoice({ console.info(response.invoice); -const onNotification = async (notification) => { +const onNotification = async (notification: Nip47Notification) => { if (notification.notification.payment_hash !== paymentHash) { console.info("Skipping unrelated notification", notification); return; diff --git a/examples/nwc/client/wrapped-invoice.ts b/examples/nwc/client/wrapped-invoice.ts new file mode 100644 index 00000000..a3c7f50f --- /dev/null +++ b/examples/nwc/client/wrapped-invoice.ts @@ -0,0 +1,80 @@ +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 { Nip47Notification, NWCClient } from "@getalby/sdk/nwc"; +import { Invoice } from "@getalby/lightning-tools"; + +console.warn( + "Alby Hub WARNING: This currently only works with Alby Hub LND backend or self payments between sub-wallets\n", +); + +const rl = readline.createInterface({ input, output }); + +const nwcUrl = + process.env.NWC_URL || + (await rl.question("Nostr Wallet Connect URL (nostr+walletconnect://...): ")); + +const upstreamInvoice = await rl.question("Upstream Invoice: "); + +const paymentHash = new Invoice({ pr: upstreamInvoice.trim() }).paymentHash; +console.info("Payment hash:", paymentHash); + +const amount = + parseInt( + (await rl.question("Extra amount in sats (default 1 sat): ")) || "1", + ) * 1000; + +rl.close(); + +const client = new NWCClient({ + nostrWalletConnectUrl: nwcUrl, +}); + +const response = await client.makeHoldInvoice({ + amount, // in millisats + description: "NWC HODL invoice example", + payment_hash: paymentHash, + // or set a 256-bit description hash: + //description_hash: "a40f2b27a4414044995b26b73eb5aa66688b5f18d6a8a2513827d9a116ad95f1", +}); + +console.info(response.invoice); + +const onNotification = async (notification: Nip47Notification) => { + if (notification.notification.payment_hash !== paymentHash) { + console.info("Skipping unrelated notification", notification); + return; + } + console.info( + "HOLD invoice accepted! It can be settled or canceled before block " + + notification.notification.settle_deadline, + ); + + console.info("Paying upstream invoice", upstreamInvoice); + + const { preimage } = await client.payInvoice({ + invoice: upstreamInvoice, + }); + + console.info("Paid upstream invoice", preimage); + + await client.settleHoldInvoice({ preimage }); + + process.exit(); +}; + +const unsub = await client.subscribeNotifications(onNotification, [ + "hold_invoice_accepted", +]); + +console.info("Waiting for payment to be made..."); +process.on("SIGINT", function () { + console.info("Caught interrupt signal"); + + unsub(); + client.close(); + + process.exit(); +});