From 912944d6931065dc4e06d57dd28883a96f149057 Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Sat, 7 Nov 2026 13:46:30 -0700 Subject: 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/ serves the withdrawRequest (up to the per-claim amount, never past what's left), GET /w//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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VMan6Ts5Tktega8VbLg31q --- names/withdraw.test.js | 141 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 names/withdraw.test.js (limited to 'names/withdraw.test.js') diff --git a/names/withdraw.test.js b/names/withdraw.test.js new file mode 100644 index 0000000..80da774 --- /dev/null +++ b/names/withdraw.test.js @@ -0,0 +1,141 @@ +// bun test names/withdraw.test.js +import { test, expect } from 'bun:test'; +import { withdrawService } from './withdraw.js'; + +const OUR = '02' + 'aa'.repeat(32), THEIRS = '03' + 'bb'.repeat(32); +const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); + +function rig({ payBehaviour = 'ok', circularCap = 1e9 } = {}) { + const state = {}; + const invoices = {}; // our invoices: hash -> { status } + const pays = {}; // outbound: hash -> status + const fakes = {}; // bolt11 string -> decoded + let n = 0; + const ln = { + async call(m, p) { + if (m === 'invoice') { const hash = 'f' + (++n); invoices[hash] = { status: 'unpaid' }; return { bolt11: 'lnfund' + hash, payment_hash: hash }; } + if (m === 'listinvoices') return { invoices: invoices[p.payment_hash] ? [invoices[p.payment_hash]] : [] }; + if (m === 'decode') { const d = fakes[p.string]; if (!d) throw new Error('bad'); return d; } + if (m === 'pay') { + const d = fakes[p.bolt11]; + if (payBehaviour === 'fail') { pays[d.payment_hash] = 'failed'; throw new Error('no route'); } + if (payBehaviour === 'hang') { pays[d.payment_hash] = 'pending'; throw new Error('timed out'); } + pays[d.payment_hash] = 'complete'; + return { status: 'complete', amount_msat: d.amount_msat, amount_sent_msat: d.amount_msat + 3000 }; + } + if (m === 'waitsendpay') { pays[p.payment_hash] = 'complete'; return { status: 'complete' }; } + if (m === 'listsendpays') return { payments: pays[p.payment_hash] ? [{ status: pays[p.payment_hash] }] : [] }; + throw new Error('unexpected ' + m); + }, + }; + let circular = 0; + const svc = withdrawService({ + state, persist: () => {}, lnFor: () => ln, ourNodeId: async () => OUR, + circularPick: async () => ({ cap: circularCap }), payCircular: async () => { circular++; return { feeMsat: 1500 }; }, + publicBase: 'https://names.test', + }); + const invoice = (sat, payee = THEIRS, extra = {}) => { + const hash = 'c' + (++n); const s = 'lnbc' + hash; + fakes[s] = { valid: true, payment_hash: hash, amount_msat: sat * 1000, payee, created_at: Math.floor(Date.now() / 1000), expiry: 3600, ...extra }; + return s; + }; + return { svc, state, invoices, pays, invoice, circular: () => circular }; +} + +async function funded(r, opts) { + const c = await r.svc.create({ pubkey: 'me', ...opts }); + expect(r.svc.params(c.id).status).toBe('ERROR'); // unfunded is invisible + r.invoices[c.paymentHash].status = 'paid'; + expect(r.svc.onSettled(c.paymentHash)).toBe(true); + return c; +} + +test('a funded link pays a claim, then is used up', async () => { + const r = rig(); + const c = await funded(r, { sat: 1000 }); + const p = r.svc.params(c.id); + expect(p.tag).toBe('withdrawRequest'); + expect(p.maxWithdrawable).toBe(1_000_000); + expect(p.callback).toBe(`https://names.test/w/${c.id}/cb`); + expect(await r.svc.callback(c.id, 'nope', r.invoice(1000))).toMatchObject({ status: 'ERROR' }); + expect(await r.svc.callback(c.id, p.k1, r.invoice(1001))).toMatchObject({ status: 'ERROR' }); + expect(await r.svc.callback(c.id, p.k1, r.invoice(1000))).toEqual({ status: 'OK' }); + await sleep(5); + const v = r.svc.mine('me')[0]; + expect(v.remainingSat).toBe(0); + expect(v.claims[0].status).toBe('paid'); + expect(r.svc.params(c.id).status).toBe('ERROR'); + expect(await r.svc.callback(c.id, p.k1, r.invoice(1))).toMatchObject({ status: 'ERROR' }); +}); + +test('multi-use: each claim up to the per-claim amount, no invoice paid twice', async () => { + const r = rig(); + const c = await funded(r, { sat: 500, uses: 3 }); + const k1 = r.svc.params(c.id).k1; + const inv = r.invoice(500); + expect((await r.svc.callback(c.id, k1, inv)).status).toBe('OK'); + expect((await r.svc.callback(c.id, k1, inv)).status).toBe('ERROR'); + expect((await r.svc.callback(c.id, k1, r.invoice(200))).status).toBe('OK'); + await sleep(5); + const v = r.svc.mine('me')[0]; + expect(v.used).toBe(2); + expect(v.remainingSat).toBe(800); + expect(r.svc.params(c.id).maxWithdrawable).toBe(500_000); +}); + +test('an invoice on our own node goes round the circular route', async () => { + const r = rig(); + const c = await funded(r, { sat: 1000 }); + expect((await r.svc.callback(c.id, r.svc.params(c.id).k1, r.invoice(1000, OUR))).status).toBe('OK'); + await sleep(5); + expect(r.circular()).toBe(1); + expect(r.svc.mine('me')[0].claims[0]).toMatchObject({ status: 'paid' }); +}); + +test('a failed payout goes back on the link', async () => { + const r = rig({ payBehaviour: 'fail' }); + const c = await funded(r, { sat: 1000 }); + expect((await r.svc.callback(c.id, r.svc.params(c.id).k1, r.invoice(1000))).status).toBe('OK'); + await sleep(5); + const v = r.svc.mine('me')[0]; + expect(v.remainingSat).toBe(1000); + expect(v.used).toBe(0); + expect(r.svc.params(c.id).tag).toBe('withdrawRequest'); +}); + +test('an unresolved payout stays reserved', async () => { + const r = rig({ payBehaviour: 'hang' }); + const c = await funded(r, { sat: 1000 }); + await r.svc.callback(c.id, r.svc.params(c.id).k1, r.invoice(1000)); + await sleep(5); + expect(r.svc.mine('me')[0]).toMatchObject({ remainingSat: 0 }); +}); + +test('expired and amountless invoices are refused', async () => { + const r = rig(); + const c = await funded(r, { sat: 1000 }); + const k1 = r.svc.params(c.id).k1; + expect((await r.svc.callback(c.id, k1, r.invoice(100, THEIRS, { created_at: 1000, expiry: 60 }))).status).toBe('ERROR'); + expect((await r.svc.callback(c.id, k1, r.invoice(0))).status).toBe('ERROR'); +}); + +test('reclaim pays the rest to the creator and closes the link', async () => { + const r = rig(); + const c = await funded(r, { sat: 400, uses: 2 }); + const k1 = r.svc.params(c.id).k1; + await r.svc.callback(c.id, k1, r.invoice(400)); + await sleep(5); + await expect(r.svc.reclaim(c.id, 'someone-else', r.invoice(400, OUR))).rejects.toThrow('not your link'); + const out = await r.svc.reclaim(c.id, 'me', r.invoice(400, OUR)); + expect(out).toMatchObject({ status: 'closed', remainingSat: 0, paid: true }); + expect(r.svc.params(c.id).status).toBe('ERROR'); +}); + +test('confirmFunded checks the invoice', async () => { + const r = rig(); + const c = await r.svc.create({ pubkey: 'me', sat: 10 }); + await expect(r.svc.confirmFunded(c.id, 'me')).rejects.toThrow('not paid'); + r.invoices[c.paymentHash].status = 'paid'; + expect((await r.svc.confirmFunded(c.id, 'me')).status).toBe('open'); + expect(r.svc.onSettled(c.paymentHash)).toBe(false); // the settle loop arriving later is a no-op +}); -- cgit v1.2.3