aboutsummaryrefslogtreecommitdiffstats
path: root/names/server.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-11-07 13:46:30 -0700
committerAdam Soltys <[email protected]>2026-11-07 13:46:30 -0700
commit912944d6931065dc4e06d57dd28883a96f149057 (patch)
tree4c0fea9863a77bd543a449cf6bbf5fd1ff988f02 /names/server.js
parent2fef93f3f9bf646d66625deb09c8b5b278a32cae (diff)
downloadcoinosv3-server-912944d6931065dc4e06d57dd28883a96f149057.tar.gz
coinosv3-server-912944d6931065dc4e06d57dd28883a96f149057.zip
names: prepaid Lightning withdraw links (LNURL-withdraw, LUD-03). "Do we have lnurl withdraw links somewhere? can we add them?" → "Prepaid" (Adam, 2026-11-07). A self-custodial wallet asleep in a pocket can't answer a withdraw link days later, so the creator pays the whole amount up front (POST /withdraw/new → a CLN invoice for sat × uses; the settle loop, or the creator's POST /withdraw/funded checking listinvoices, opens the link) and this service pays each claim out of that reserve: GET /w/<id> serves the withdrawRequest (up to the per-claim amount, never past what's left), GET /w/<id>/cb?k1&pr pays it — a plain pay with the routing fee capped at max(10 sat, 1%) and borne here, or, for an invoice on this very node (any coinos wallet's Lightning receive), round the float top-up's circular route, read back with waitsendpay. The reserve is taken before a payment starts and handed back only on a definite failure; an unknown outcome stays reserved for a minute-ly listsendpays check. No invoice is ever paid twice, one claim per link at a time. The creator lists theirs (GET /withdraw/mine) and takes back the remainder with an invoice of their own (POST /withdraw/reclaim), which closes the link first and reopens it if that payment fails. All NIP-98 by the wallet key. Mainnet and (play money) mutinynet, on the hat stand's nodes. names/withdraw.test.js covers it against a fake node
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6Ts5Tktega8VbLg31q
Diffstat (limited to 'names/server.js')
-rw-r--r--names/server.js41
1 files changed, 41 insertions, 0 deletions
diff --git a/names/server.js b/names/server.js
index e12e71b..6336e2b 100644
--- a/names/server.js
+++ b/names/server.js
@@ -35,6 +35,7 @@ import { decodeBolt11 } from '../coinosv3/src/ark/lightning.js';
import { decodeOffer } from '../coinosv3/src/bolt12.js';
import { registrationIdentity } from './ownership.js';
import { openStore } from './store.js';
+import { withdrawService } from './withdraw.js';
const CFG = JSON.parse(readFileSync(process.env.NAMES_CONFIG
|| join(import.meta.dir, 'config.json'), 'utf8'));
@@ -696,6 +697,16 @@ function grantHat(pk, hat, net = 'mainnet', { equip = true } = {}) {
if (equip || !r.equipped) r.equipped = hat;
}
+// Prepaid LNURL-withdraw links (withdraw.js): funded on the same node as the
+// hats, mainnet or mutinynet; a mainnet claim to a coinos wallet's own
+// Lightning receive goes round the float top-up's circular route.
+const withdraws = withdrawService({
+ state, persist, log, lnFor: hatLn, ourNodeId,
+ payCircular, circularPick, canCircular: (node) => node === ln,
+ publicBase: PUBLIC_BASE,
+});
+setInterval(() => { withdraws.settlePending().catch((e) => log('withdraw settle check failed: ' + e.message)); }, 60_000);
+
async function settleLoop() {
if (!ln || !fwd) return;
state.lastPayIndex = state.lastPayIndex || 0;
@@ -709,6 +720,7 @@ async function settleLoop() {
}
state.lastPayIndex = inv.pay_index || state.lastPayIndex;
persist();
+ if (withdraws.onSettled(inv.payment_hash)) continue; // a withdraw link's funding: it stays here
// hat purchases settle here too — grant and keep the sats (that's the point)
const hatPending = state.hatInvoices && state.hatInvoices[inv.payment_hash];
if (hatPending) {
@@ -1664,6 +1676,35 @@ Bun.serve({
return json(hatRec(pk, net));
}
+ // ---- prepaid withdraw links (withdraw.js) ----
+ {
+ const m = url.pathname.match(/^\/w\/([A-Za-z0-9_-]{16,32})(\/cb)?$/);
+ if (m && req.method === 'GET') {
+ const nocache = (body) => { const r = json(body); r.headers.set('cache-control', 'no-store'); return r; };
+ if (!m[2]) return nocache(withdraws.params(m[1]));
+ if (!rateOk(ip, 20)) return nocache({ status: 'ERROR', reason: 'Too many attempts, try again in a minute' });
+ return nocache(await withdraws.callback(m[1], url.searchParams.get('k1') || '', url.searchParams.get('pr') || ''));
+ }
+ }
+ if (url.pathname.startsWith('/withdraw/')) {
+ const bodyText = req.method === 'POST' ? await req.text() : '';
+ const a = await checkNip98(req, url, bodyText);
+ if (a.error) return json({ error: a.error }, 401);
+ let body = {};
+ if (bodyText) { try { body = JSON.parse(bodyText); } catch { return json({ error: 'bad body' }, 400); } }
+ const fail = (e) => { log(`withdraw ${url.pathname}: ${e.message}`); return json({ error: e.message }, e.status || 500); };
+ try {
+ if (url.pathname === '/withdraw/mine' && req.method === 'GET') return json({ links: withdraws.mine(a.pubkey) });
+ if (url.pathname === '/withdraw/new' && req.method === 'POST') {
+ if (!rateOk(ip)) return json({ error: 'rate limited' }, 429);
+ return json(await withdraws.create({ pubkey: a.pubkey, sat: body.sat, uses: body.uses, note: body.note, net: hatNetOf(body.net) }));
+ }
+ if (url.pathname === '/withdraw/funded' && req.method === 'POST') return json(await withdraws.confirmFunded(String(body.id || ''), a.pubkey));
+ if (url.pathname === '/withdraw/reclaim' && req.method === 'POST') return json(await withdraws.reclaim(String(body.id || ''), a.pubkey, body.invoice));
+ } catch (e) { return fail(e); }
+ return json({ error: 'not found' }, 404);
+ }
+
if (url.pathname === '/hats/invoice' && req.method === 'POST') {
if (!rateOk(ip)) return json({ error: 'rate limited' }, 429);
const bodyText = await req.text();