diff options
Diffstat (limited to 'names/server.js')
| -rw-r--r-- | names/server.js | 41 |
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(); |
