From 4e9c0c5198c7884cfd97cb4495f6156632e3124a Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 5 Feb 2026 16:44:47 -0800 Subject: payment forwarding --- routes/payments.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'routes') diff --git a/routes/payments.ts b/routes/payments.ts index e3cd8e5e..a93a1882 100644 --- a/routes/payments.ts +++ b/routes/payments.ts @@ -1,6 +1,6 @@ import config from "$config"; import api from "$lib/api"; -import { sendArk } from "$lib/ark"; +import { getArkAddress, sendArk } from "$lib/ark"; import { requirePin } from "$lib/auth"; import { archive, db, g, gf, s } from "$lib/db"; import { getTx } from "$lib/esplora"; @@ -21,7 +21,7 @@ import { sendOnchain, } from "$lib/payments"; import { emit } from "$lib/sockets"; -import { fundDebit, getBalance, getCredit, tbConfirm } from "$lib/tb"; +import { fundDebit, getBalance, getCredit, tbConfirm, tbCredit } from "$lib/tb"; import { PaymentType } from "$lib/types"; import { SATS, @@ -63,7 +63,7 @@ export default { const recipient = invoice ? await getUser(invoice.uid) : undefined; if (payreq) { if (invoice && recipient.username !== "mint") { - if (invoice.aid === user.id) fail("Cannot send to self"); + if (invoice.aid === (aid || user.id)) fail("Cannot send to self"); hash = payreq; if (!amount) ({ amount } = invoice); } else { @@ -809,6 +809,10 @@ export default { } }, + async arkAddress(_, res) { + res.send(await getArkAddress()); + }, + async arkReceive(req, res) { try { const { body, user } = req; @@ -821,13 +825,16 @@ export default { if (invoice.uid !== user?.id) fail("Unauthorized"); invoice.received += amount; - const { aid, type, currency } = invoice; + let { aid, type, currency } = invoice; const rates = await g("rates"); const rate = rates[currency]; + // Always credit custodial balance (Ark funds received by server) + await tbCredit(uid, uid, type, amount, false); + const p = { id: v4(), - aid: aid, + aid: uid, iid, amount, hash, @@ -842,10 +849,10 @@ export default { const m = db.multi(); m.set(`invoice:${invoice.id}`, JSON.stringify(invoice)) .set(`payment:${p.id}`, JSON.stringify(p)) - .lPush(`${aid || uid}:payments`, p.id) - .set(`${aid || uid}:payments:last`, p.created); + .lPush(`${uid}:payments`, p.id) + .set(`${uid}:payments:last`, p.created); - if (hash) m.set(`payment:${aid || uid}:${hash}`, p.id); + if (hash) m.set(`payment:${aid}:${hash}`, p.id); await m.exec(); @@ -878,6 +885,9 @@ export default { continue; } + // Skip received/change VTXOs — handled by arkReceive + if (tx.amount > 0) continue; + const p = { id: v4(), aid, -- cgit v1.2.3