diff options
| author | Adam Soltys <[email protected]> | 2026-11-25 09:25:18 -0700 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2026-11-25 09:25:18 -0700 |
| commit | 6d5f69a23b8e52513825465a06911d67702edec3 (patch) | |
| tree | eab409c6d3394c37f9b8b790e7f3e7f8b9ac4301 /lib/lightning.ts | |
| parent | 00799bb5ed16d3f2514ba68938e479110a1412a9 (diff) | |
| download | coinos-server-6d5f69a23b8e52513825465a06911d67702edec3.tar.gz coinos-server-6d5f69a23b8e52513825465a06911d67702edec3.zip | |
payments: hold Lightning deposits back from on-chain withdrawal
While hold:enabled is set, Lightning deposits above a daily free allowance
are held: they can still be spent over Lightning or sent internally, but not
withdrawn on-chain or over Lightning into our own on-chain liquidity. The
check and release run inside the atomic debit script, internal transfers
carry the hold to the receiver, and old holds expire via scripts/holds.ts.
The listener notes which peer each deposit arrived from for review.
sendOnchain now refuses frozen accounts before building the transaction or
locking its inputs, so a frozen account retrying withdrawals cannot hold
hot-wallet UTXOs and make other sends fail with locked outputs.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VMan6SEnsbshZrUDtTsxAX
Diffstat (limited to 'lib/lightning.ts')
| -rw-r--r-- | lib/lightning.ts | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/lib/lightning.ts b/lib/lightning.ts index 6c37ddf1..34c698cb 100644 --- a/lib/lightning.ts +++ b/lib/lightning.ts @@ -142,6 +142,15 @@ export async function listenForLightning() { } } + // Note which peer the deposit came in from, for reviewing deposit holds + // (lib/hold.ts). Peers in `freeze:inbound_peers` are flagged. + let holdNote; + try { + holdNote = await inboundNote(payment_hash); + } catch (e) { + warn("inbound peer lookup failed", payment_hash, e.message); + } + const paymentRequest = bolt11 || bolt12; await credit({ hash: paymentRequest, @@ -152,6 +161,7 @@ export async function listenForLightning() { ref: preimage, type: bolt12 ? PaymentType.bolt12 : PaymentType.lightning, payment_hash, + holdNote, }); // credit() may decline a duplicate idempotency claim. Only advance the @@ -402,3 +412,58 @@ export const fixBolt12 = async (_, res) => { res.send({}); }; + +// --- inbound peer lookup (see the listener) --- +// HTLCs are read incrementally by created index, so each lookup only fetches +// what arrived since the last one. +let htlcCursor = 0; +const inboundByHash = new Map<string, Set<string>>(); +let peers: { at: number; scid: Map<string, string>; watched: Set<string> } = { at: 0, scid: new Map(), watched: new Set() }; +const aliases = new Map<string, string>(); + +async function refreshPeers() { + if (Date.now() - peers.at < 60_000) return peers; + const { channels } = await ln.listpeerchannels(); + const scid = new Map<string, string>(); + for (const c of channels || []) if (c.short_channel_id) scid.set(c.short_channel_id, c.peer_id); + const watched = new Set<string>(await db.sMembers("freeze:inbound_peers")); + peers = { at: Date.now(), scid, watched }; + return peers; +} + +async function alias(id: string) { + if (!aliases.has(id)) { + try { + const { nodes } = await ln.listnodes(id); + aliases.set(id, nodes?.[0]?.alias || id.slice(0, 16)); + } catch { + return id.slice(0, 16); + } + } + return aliases.get(id)!; +} + +async function inboundNote(hash: string): Promise<string | undefined> { + if (!hash) return; + const { scid, watched } = await refreshPeers(); + if (!htlcCursor) { + const { created } = await ln.call("wait", { subsystem: "htlcs", indexname: "created", nextvalue: 0 }); + htlcCursor = Math.max(1, Number(created) - 5000); + } + for (;;) { + const { htlcs } = await ln.call("listhtlcs", { index: "created", start: htlcCursor, limit: 1000 }); + for (const h of htlcs || []) { + htlcCursor = Math.max(htlcCursor, Number(h.created_index) + 1); + if (h.direction !== "in") continue; + if (!inboundByHash.has(h.payment_hash)) inboundByHash.set(h.payment_hash, new Set()); + inboundByHash.get(h.payment_hash)!.add(h.short_channel_id); + } + if (!htlcs || htlcs.length < 1000) break; + } + if (inboundByHash.size > 50_000) + for (const k of [...inboundByHash.keys()].slice(0, 25_000)) inboundByHash.delete(k); + const ids = [...new Set([...(inboundByHash.get(hash) || [])].map((c) => scid.get(c)).filter(Boolean))] as string[]; + if (!ids.length) return; + const names = await Promise.all(ids.map(async (id) => `${await alias(id)}${watched.has(id) ? " (watched)" : ""}`)); + return `via ${names.join(", ")}`; +} |
