From e68c302e1082da29020d1efa932f986032ae5ce2 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Fri, 7 Feb 2025 22:23:44 -0800 Subject: nwc apps endpoints --- lib/nwc.ts | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'lib') diff --git a/lib/nwc.ts b/lib/nwc.ts index 4055a9f4..9e1ad597 100644 --- a/lib/nwc.ts +++ b/lib/nwc.ts @@ -52,11 +52,11 @@ export default () => { if (!methods.includes(method)) return; - const uid = await g(pubkey); - const user = await g(`user:${uid}`); + const app = await g(pubkey); + const user = await g(`user:${app.uid}`); try { - const result = await handle(method, params, user, ev); + const result = await handle(method, params, ev, app, user); const payload = JSON.stringify({ result_type: method, ...result }); content = await nip04.encrypt(sk, pubkey, payload); @@ -77,7 +77,7 @@ export default () => { err( "problem with nwc", pubkey, - user?.username, + app?.name, JSON.stringify(params), e.message, ); @@ -89,7 +89,7 @@ export default () => { }); }; -const handle = (method, params, user, ev) => +const handle = (method, params, ev, app, user) => ({ async pay_invoice() { const { invoice: pr } = params; @@ -102,13 +102,17 @@ const handle = (method, params, user, ev) => const recipient = await g(`user:${invoice.uid}`); if (recipient?.username !== "mint") { - const { id: preimage } = await sendInternal({ + const { id: pid } = await sendInternal({ amount, invoice, recipient, sender: user, }); + const preimage = pid; + if (app.pubkey !== user.pubkey) + await db.lPush(`${pubkey}:payments`, pid); + if (invoice.memo?.includes("9734")) { const { invoices } = await ln.listinvoices({ invstring: pr }); const inv = invoices[0]; @@ -124,12 +128,15 @@ const handle = (method, params, user, ev) => return result({ preimage }); } } - await sendLightning({ + + const { id: pid } = await sendLightning({ amount, user, pr, }); + if (app.pubkey !== user.pubkey) await db.lPush(`${pubkey}:payments`, pid); + for (let i = 0; i < 10; i++) { const { pays } = await ln.listpays(pr); const p = pays.find((p) => p.status === "complete"); @@ -144,7 +151,8 @@ const handle = (method, params, user, ev) => }, async pay_keysend() { - const { amount, pubkey, tlv_records: extratlvs } = params; + const { amount: amount_msat, pubkey, tlv_records: extratlvs } = params; + const amount = Math.round(amount_msat / 1000); try { const { payment_hash } = await sendKeysend({ @@ -276,7 +284,9 @@ const handle = (method, params, user, ev) => async list_transactions() { const { from, until, limit = 10, offset = 0, type } = params; - const payments = await db.lRange(`${user.id}:payments`, 0, -1); + const { pubkey } = app; + const k = pubkey === user.pubkey ? user.id : pubkey; + const payments = await db.lRange(`${k}:payments`, 0, -1); let transactions = []; for (const pid of payments) { -- cgit v1.2.3