From 6d5f69a23b8e52513825465a06911d67702edec3 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Wed, 25 Nov 2026 09:25:18 -0700 Subject: 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 Claude-Session: https://claude.ai/code/session_01VMan6SEnsbshZrUDtTsxAX --- lib/db.ts | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'lib/db.ts') diff --git a/lib/db.ts b/lib/db.ts index e03fb810..f3edba11 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -12,20 +12,39 @@ local tip = tonumber(ARGV[2]) local fee = tonumber(ARGV[3]) local ourfee = tonumber(ARGV[4]) local frozen = tonumber(ARGV[5]) +-- Optional deposit hold (see lib/hold.ts). Held sats can leave over Lightning +-- or internally but not on-chain; spending them releases the hold, and the +-- amount released is returned so the receiver of an internal send inherits it. +local heldKey = ARGV[6] +local onchain = ARGV[7] == '1' +local held = 0 +if heldKey and heldKey ~= '' then held = tonumber(redis.call('get', heldKey) or '0') end local balance = tonumber(redis.call('get', balanceKey) or '0') local credit = tonumber(redis.call('get', creditKey) or '0') local covered = math.min(credit, ourfee) ourfee = ourfee - covered +local total = amount + tip + fee + ourfee -if balance - frozen < amount + tip + fee + ourfee then - return {err = insufficientString .. ' ⚡️' .. balance - frozen .. ' / ' .. amount + tip + fee + ourfee} +if balance - frozen < total then + return {err = insufficientString .. ' ⚡️' .. balance - frozen .. ' / ' .. total} +end +if onchain and held > 0 and balance - math.max(frozen, held) < total then + return {err = 'HELD:' .. held .. ':' .. math.max(0, balance - math.max(frozen, held))} end redis.call('decrby', creditKey, tostring(math.floor(covered))) -redis.call('decrby', balanceKey, tostring(math.floor(amount + tip + fee + ourfee))) - +redis.call('decrby', balanceKey, tostring(math.floor(total))) + +if heldKey and heldKey ~= '' then + local used = 0 + if not onchain and held > 0 then + used = math.min(held, math.floor(total)) + redis.call('decrby', heldKey, tostring(used)) + end + return {ourfee, used} +end return ourfee `; -- cgit v1.2.3