From e26aae8d23b616683112002aaf39727624913d56 Mon Sep 17 00:00:00 2001 From: elnosh Date: Thu, 18 Apr 2024 10:05:05 -0500 Subject: [PATCH 1/2] add multi pay invoice nwc request --- src/components/NWC.tsx | 57 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/components/NWC.tsx b/src/components/NWC.tsx index 32c970e..2bb21e8 100644 --- a/src/components/NWC.tsx +++ b/src/components/NWC.tsx @@ -68,6 +68,44 @@ async function publishZapRequest(bolt11: string, nwc: NWCInfo) { // return []; } + +type MultiInvoiceParam = { + id: string; + invoice: string; +}; + +async function publishMultiInvoiceRequest(invoices: string[], nwc: NWCInfo) { + const signer = new NDKPrivateKeySigner(nwc.secret); + const ndk = new NDK({ explicitRelayUrls: [nwc.relay], signer }); + + console.log("connecting to ndk"); + await ndk.connect(); + + let invoicesParam: MultiInvoiceParam[] = []; + + for (var invoice of invoices) { + let invoiceParam: MultiInvoiceParam = {id: invoice, invoice} + invoicesParam.push(invoiceParam) + } + + const event = new NDKEvent(ndk); + event.kind = 23194; + event.content = JSON.stringify({ + method: "multi_pay_invoice", + params: { + invoices: invoicesParam, + }, + }); + event.tags = [["p", nwc.npubHex]]; + + await event.encrypt(undefined, signer); + + await event.sign(); + console.log("publishing zap request", event.rawEvent()); + + await event.publish(); +} + async function fetchBolt11(): Promise<{ bolt11: string }> { const res = await fetch(`${FAUCET_API_URL}/api/bolt11`, { method: "POST", @@ -100,6 +138,22 @@ function Zapper(props: { nwc: NWCInfo }) { setLoading(false); } + async function handleMultiZap() { + setLoading(true); + let invoices = []; + try { + // create 3 invoices for the multi invoice request + for (let i = 0; i < 3; i++) { + const bolt11 = (await fetchBolt11()).bolt11; + invoices.push(bolt11); + } + await publishMultiInvoiceRequest(invoices, props.nwc); + } catch (e) { + console.error(e); + } + setLoading(false); + } + return (
@@ -115,6 +169,9 @@ function Zapper(props: { nwc: NWCInfo }) { +
From 1a8431aff912004f8a8b36049393669fd255880d Mon Sep 17 00:00:00 2001 From: elnosh Date: Thu, 18 Apr 2024 11:06:40 -0500 Subject: [PATCH 2/2] nwc keysend requests --- src/components/NWC.tsx | 76 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 73 insertions(+), 3 deletions(-) diff --git a/src/components/NWC.tsx b/src/components/NWC.tsx index 2bb21e8..09c9432 100644 --- a/src/components/NWC.tsx +++ b/src/components/NWC.tsx @@ -99,13 +99,12 @@ async function publishMultiInvoiceRequest(invoices: string[], nwc: NWCInfo) { event.tags = [["p", nwc.npubHex]]; await event.encrypt(undefined, signer); - await event.sign(); - console.log("publishing zap request", event.rawEvent()); - + console.log("publishing multi zap request", event.rawEvent()); await event.publish(); } + async function fetchBolt11(): Promise<{ bolt11: string }> { const res = await fetch(`${FAUCET_API_URL}/api/bolt11`, { method: "POST", @@ -153,6 +152,71 @@ function Zapper(props: { nwc: NWCInfo }) { } setLoading(false); } + + async function handleKeysend() { + setLoading(true); + + const signer = new NDKPrivateKeySigner(props.nwc.secret); + const ndk = new NDK({ explicitRelayUrls: [props.nwc.relay], signer }); + + console.log("connecting to ndk"); + await ndk.connect(); + + const event = new NDKEvent(ndk); + event.kind = 23194; + event.content = JSON.stringify({ + method: "pay_keysend", + params: { + amount: 42 * 1000, + pubkey: "02465ed5be53d04fde66c9418ff14a5f2267723810176c9212b722e542dc1afb1b" + }, + }); + event.tags = [["p", props.nwc.npubHex]]; + + await event.encrypt(undefined, signer); + await event.sign(); + console.log("publishing keysend request", event.rawEvent()); + await event.publish(); + + setLoading(false); + } + + async function handleMultiKeysend() { + setLoading(true); + + const signer = new NDKPrivateKeySigner(props.nwc.secret); + const ndk = new NDK({ explicitRelayUrls: [props.nwc.relay], signer }); + + console.log("connecting to ndk"); + await ndk.connect(); + + let keysendParams = []; + for (let i = 0; i < 3; i++) { + let keysendParam = { + id: String(Math.floor(Math.random() * 10000000)), + pubkey: "02465ed5be53d04fde66c9418ff14a5f2267723810176c9212b722e542dc1afb1b", + amount: 42 * 1000 + } + keysendParams.push(keysendParam) + } + + const event = new NDKEvent(ndk); + event.kind = 23194; + event.content = JSON.stringify({ + method: "multi_pay_keysend", + params: { + keysends: keysendParams + }, + }); + event.tags = [["p", props.nwc.npubHex]]; + + await event.encrypt(undefined, signer); + await event.sign(); + console.log("publishing multi keysend request", event.rawEvent()); + await event.publish(); + + setLoading(false); + } return (
@@ -172,6 +236,12 @@ function Zapper(props: { nwc: NWCInfo }) { + +