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/hold.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/hold.ts')
| -rw-r--r-- | lib/hold.ts | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/lib/hold.ts b/lib/hold.ts new file mode 100644 index 00000000..9404d0c8 --- /dev/null +++ b/lib/hold.ts @@ -0,0 +1,70 @@ +// Deposit holds. +// +// While `hold:enabled` is set, every Lightning (and bolt12) deposit adds to +// the receiving account's `held:<id>`. Held sats can still be spent over +// Lightning or sent to another coinos account, but not withdrawn on-chain +// (bitcoin, liquid) or over Lightning into our own on-chain liquidity (the +// nodes in `hold:exit_nodes`). Spending held sats over Lightning releases +// that much of the hold. An internal transfer of held sats moves the hold to +// the receiver, so passing funds through a second account doesn't clear it. +// The check and the release happen inside the atomic debit script in +// lib/db.ts. A failed send that's reversed gets its hold back. +// +// The first `hold:free_sat` (default 50k) of Lightning deposits per account +// in any 24h aren't held at all, and holds older than `hold:expire_hours` +// (default 72h) are released by `holds.ts expire` (hourly cron) unless the +// account is frozen. Big recent deposits are what we want to look at; small +// ones were mostly false positives. +// +// Accounts with a hold are listed in the `held` set, and each account keeps a +// short log of what added to or lifted its hold (`held:log:<id>`). Review and +// lift holds with scripts/holds.ts. +import { db } from "$lib/db"; + +export const holdKey = (id: string) => `held:${id}`; +const LOG_MAX = 100; + +// Add to a hold inside the caller's MULTI so the balance and the hold change +// together. +export const addHold = (m: any, id: string, amount: number, entry: Record<string, any>) => { + m.incrBy(holdKey(id), amount) + .sAdd("held", id) + .lPush(`held:log:${id}`, JSON.stringify({ at: Date.now(), amount, ...entry })) + .lTrim(`held:log:${id}`, 0, LOG_MAX - 1); +}; + +export const HOLD_FREE_SAT = 50_000; +export const HOLD_EXPIRE_HOURS = 72; + +// How much of a Lightning deposit falls inside the account's daily free +// allowance. Counts the deposit against the allowance as a side effect. +export const holdAllowance = async (id: string, amount: number) => { + const cap = Number(await db.get("hold:free_sat") ?? HOLD_FREE_SAT); + if (!(cap > 0) || !(amount > 0)) return 0; + const k = `holdfree:${id}`; + const used = await db.incrBy(k, amount); + if (used === amount) await db.expire(k, 86400); + return Math.max(0, Math.min(amount, cap - (used - amount))); +}; + +export const logHold = async (id: string, entry: Record<string, any>) => { + await db + .multi() + .lPush(`held:log:${id}`, JSON.stringify({ at: Date.now(), ...entry })) + .lTrim(`held:log:${id}`, 0, LOG_MAX - 1) + .exec(); +}; + +// Lightning destinations that are really our own on-chain liquidity (the +// swap/Ark node). Cached briefly: it's checked on every Lightning send. +let exitNodes = { at: 0, set: new Set<string>() }; +export const isExitNode = async (pubkey?: string) => { + if (!pubkey) return false; + if (Date.now() - exitNodes.at > 60_000) + exitNodes = { at: Date.now(), set: new Set(await db.sMembers("hold:exit_nodes")) }; + return exitNodes.set.has(pubkey); +}; + +export const heldMessage = (held: number, available: number) => + `⚡️${held} of your balance came from recent Lightning deposits and can't be withdrawn on-chain right now. ` + + `You can still send it over Lightning. Available for on-chain withdrawal: ⚡️${available}.`; |
