diff options
| author | Adam Soltys <[email protected]> | 2026-02-24 13:18:42 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-02-24 13:18:42 -0800 |
| commit | ad7b09b43323d0ffc0a8ddabb43e4838a184c697 (patch) | |
| tree | fbf2a59f81771b2938f474c8e7b35a497d2960e4 /lib/nwc.ts | |
| parent | 9e97e040617bfa0b40150c0eba298bf186bbeeaf (diff) | |
| download | coinos-server-ad7b09b43323d0ffc0a8ddabb43e4838a184c697.tar.gz coinos-server-ad7b09b43323d0ffc0a8ddabb43e4838a184c697.zip | |
formatting
Diffstat (limited to 'lib/nwc.ts')
| -rw-r--r-- | lib/nwc.ts | 126 |
1 files changed, 52 insertions, 74 deletions
@@ -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; + 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), - ); + 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); + // console.log("nwc", method, params, pubkey); - if (!methods.includes(method)) return; + 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}`); + 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); + 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, - }; + 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]); + 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, |
