From ad7b09b43323d0ffc0a8ddabb43e4838a184c697 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Tue, 24 Feb 2026 13:18:42 -0800 Subject: formatting --- lib/nwc.ts | 138 ++++++++++++++++++++++++++----------------------------------- 1 file changed, 58 insertions(+), 80 deletions(-) (limited to 'lib/nwc.ts') diff --git a/lib/nwc.ts b/lib/nwc.ts index 47b632f7..b635f665 100644 --- a/lib/nwc.ts +++ b/lib/nwc.ts @@ -3,13 +3,7 @@ import { archive, db, g, gf } from "$lib/db"; import { generate } from "$lib/invoices"; import ln from "$lib/ln"; import { err, l, warn } from "$lib/logging"; -import { - handleZap, - serverPubkey, - serverPubkey2, - serverSecret, - serverSecret2, -} from "$lib/nostr"; +import { handleZap, serverPubkey, serverPubkey2, serverSecret, serverSecret2 } from "$lib/nostr"; import { sendInternal, sendKeysend, sendLightning } from "$lib/payments"; import { getBalance } from "$lib/tb"; import { fail, getInvoice, sleep } from "$lib/utils"; @@ -89,64 +83,62 @@ export default () => { r.on("error", () => {}); r.on("event", async (sub, ev) => { - try { - if (sub !== "nwc") return; - const now = Math.floor(Date.now() / 1000); - if (ev.created_at && now - ev.created_at > nwcEventMaxAgeSeconds) return; - if (await db.zScore(handledKey, ev.id)) return; - - db.zAdd(handledKey, { score: now, value: ev.id }); - db.zRemRangeByScore(handledKey, 0, now - nwcEventMaxAgeSeconds); - const size = await db.zCard(handledKey); - if (size > handledMaxSize) { - await db.zRemRangeByRank(handledKey, 0, size - handledMaxSize - 1); - } - let { content, pubkey } = ev; - const pk = ev.tags.find((t) => t[0] === "p")[1]; - const sk = serverKeys[pk]; - const { params, method } = JSON.parse( - await nip04.decrypt(sk, pubkey, content), - ); - - // console.log("nwc", method, params, pubkey); - - if (!methods.includes(method)) return; - try { - const app = await g(`app:${pubkey}`); - if (!app) fail("pubkey not found"); - const user = await g(`user:${app.uid}`); - - const result = await handle(method, params, ev, app, user); - const payload = JSON.stringify({ result_type: method, ...result }); - content = await nip04.encrypt(sk, pubkey, payload); - - let response: UnsignedEvent = { - created_at: Math.floor(Date.now() / 1000), - kind: 23195, - pubkey: serverPubkey, - tags: [ - ["p", pubkey], - ["e", ev.id], - ], - content, - }; - - response = await finalizeEvent(response, hexToBytes(sk)); - r.send(["EVENT", response]); + if (sub !== "nwc") return; + const now = Math.floor(Date.now() / 1000); + if (ev.created_at && now - ev.created_at > nwcEventMaxAgeSeconds) return; + if (await db.zScore(handledKey, ev.id)) return; + + db.zAdd(handledKey, { score: now, value: ev.id }); + db.zRemRangeByScore(handledKey, 0, now - nwcEventMaxAgeSeconds); + const size = await db.zCard(handledKey); + if (size > handledMaxSize) { + await db.zRemRangeByRank(handledKey, 0, size - handledMaxSize - 1); + } + let { content, pubkey } = ev; + const pk = ev.tags.find((t) => t[0] === "p")[1]; + const sk = serverKeys[pk]; + const { params, method } = JSON.parse(await nip04.decrypt(sk, pubkey, content)); + + // console.log("nwc", method, params, pubkey); + + if (!methods.includes(method)) return; + + try { + const app = await g(`app:${pubkey}`); + if (!app) fail("pubkey not found"); + const user = await g(`user:${app.uid}`); + + const result = await handle(method, params, ev, app, user); + const payload = JSON.stringify({ result_type: method, ...result }); + content = await nip04.encrypt(sk, pubkey, payload); + + let response: UnsignedEvent = { + created_at: Math.floor(Date.now() / 1000), + kind: 23195, + pubkey: serverPubkey, + tags: [ + ["p", pubkey], + ["e", ev.id], + ], + content, + }; + + response = await finalizeEvent(response, hexToBytes(sk)); + r.send(["EVENT", response]); + } catch (e) { + // err( + // "problem with nwc", + // pubkey, + // method, + // JSON.stringify(params), + // e.message, + // ); + } } catch (e) { - // err( - // "problem with nwc", - // pubkey, - // method, - // JSON.stringify(params), - // e.message, - // ); + // err("problem with nwc", e.message); } - } catch (e) { - // err("problem with nwc", e.message); - } - }); + }); } connect(); @@ -171,9 +163,7 @@ const handle = (method, params, ev, app, user) => const pids = await db.lRange(`${pubkey}:payments`, 0, -1); let payments = await Promise.all(pids.map((pid) => g(`payment:${pid}`))); - payments = payments.filter( - (p) => p?.created > Date.now() - periods[budget_renewal], - ); + payments = payments.filter((p) => p?.created > Date.now() - periods[budget_renewal]); const spent = payments.reduce( (a, b) => @@ -186,14 +176,7 @@ const handle = (method, params, ev, app, user) => ); if (!created) { - warn( - "old nwc token", - pubkey, - user?.username, - spent, - amount, - max_amount, - ); + warn("old nwc token", pubkey, user?.username, spent, amount, max_amount); return error({ code: "UNAUTHORIZED", message: `This NWC connection is no longer valid please create a new one at https://coinos.io/settings/nostr`, @@ -344,11 +327,7 @@ const handle = (method, params, ev, app, user) => expiry, }; - const { - hash, - created: created_at, - paymentHash, - } = await generate({ invoice, user }); + const { hash, created: created_at, paymentHash } = await generate({ invoice, user }); return result({ type: "incoming", @@ -401,8 +380,7 @@ const handle = (method, params, ev, app, user) => const { pays } = await ln.listpays({ bolt11: invoice, payment_hash }); - if (!pays.length) - return error({ code: "NOT_FOUND", message: "Invoice not found" }); + if (!pays.length) return error({ code: "NOT_FOUND", message: "Invoice not found" }); const { amount_msat: amount, -- cgit v1.2.3