aboutsummaryrefslogtreecommitdiffstats
path: root/names/server.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-12-03 21:37:06 -0700
committerAdam Soltys <[email protected]>2026-12-03 21:37:06 -0700
commit9cb28c714529130a9abf01db489335f764cc853a (patch)
treecf61e9d21f4a9f215b6e69f6a894fea870b1f172 /names/server.js
parentac1999d50b83bd7a8d5068abebcb0e1756a63eeb (diff)
downloadcoinosv3-server-9cb28c714529130a9abf01db489335f764cc853a.tar.gz
coinosv3-server-9cb28c714529130a9abf01db489335f764cc853a.zip
names: /lnurlp-fetch fetches a Lightning address's pay parameters for a browser that can't.
"another thing is coinos cannot reach speed.app wallet, not sure why" (Shark, relayed by Adam 2026-12-04). speed.app answers /.well-known/lnurlp/<name> with a 301 to appapi.tryspeed.com that carries no Access-Control-Allow-Origin, and a browser won't follow a cross-origin redirect without it: from v3.coinos.io the fetch fails ("Failed to fetch"), though the target itself allows any origin. GET /lnurlp-fetch?url=https://<host>/.well-known/lnurlp/<name> fetches it server-side with the link previews' guards (public hosts only, rechecked after every redirect, capped body), https only, that one path only, and returns the pay parameters (200), the host's own error (404) or 502. Rate-limited, cached 5 minutes (30 s for failures). New lnurl-proxy.test.js passes. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VMan6WoqaQDYdpWkatPCar
Diffstat (limited to 'names/server.js')
-rw-r--r--names/server.js23
1 files changed, 22 insertions, 1 deletions
diff --git a/names/server.js b/names/server.js
index f12b08f..d49c390 100644
--- a/names/server.js
+++ b/names/server.js
@@ -27,7 +27,7 @@ import { HDKey } from '@scure/bip32';
import { mnemonicToSeedSync } from '@scure/bip39';
import { lnBackend } from '../bridge/ln.js';
import { ArkManager } from '../coinosv3/src/ark/manager.js';
-import { fetchPreview } from './preview.js';
+import { fetchPreview, fetchLnurlPay } from './preview.js';
import { dnsUpdate } from './dns-update.js';
import { translate, translateDeepl, languages } from './translate.js';
import { decodeAddress, arkIdFromServerPubkey } from '../coinosv3/src/ark/proto.js';
@@ -1317,6 +1317,7 @@ function rateOk(ip, limit = 10) {
// HTTP
// ---------------------------------------------------------------------------
+const lnurlFetches = new Map(); // /lnurlp-fetch: url → { d, until }
const json = (body, status = 200) => {
if (status >= 400) log(`→ ${status} ${JSON.stringify(body).slice(0, 120)}`);
return new Response(JSON.stringify(body), {
@@ -1460,6 +1461,26 @@ Bun.serve({
// A link in a post, as the card its page describes for itself (Open
// Graph). Cached a day either way: a page with nothing to show is asked
// again only after an hour. Public data; the CDN may cache it too.
+ // A Lightning address's pay parameters fetched for the wallet when the
+ // browser can't (a redirect without CORS: speed.app). GET ?url=https://
+ // host/.well-known/lnurlp/name → the host's JSON, or 502.
+ if (url.pathname === '/lnurlp-fetch' && req.method === 'GET') {
+ if (!rateOk(ip, 60)) return json({ error: 'rate limited' }, 429);
+ const target = (url.searchParams.get('url') || '').trim();
+ if (!/^https:\/\/\S{4,300}$/.test(target)) return json({ error: 'bad url' }, 400);
+ let hit = lnurlFetches.get(target);
+ if (!hit || Date.now() > hit.until) {
+ const d = await fetchLnurlPay(target).catch(() => null);
+ hit = { d, until: Date.now() + (d?.tag ? 300_000 : 30_000) };
+ lnurlFetches.set(target, hit);
+ if (lnurlFetches.size > 2000) lnurlFetches.delete(lnurlFetches.keys().next().value);
+ }
+ return new Response(JSON.stringify(hit.d || { status: 'ERROR', reason: "couldn't reach that address" }), {
+ status: hit.d?.tag ? 200 : hit.d ? 404 : 502,
+ headers: { 'content-type': 'application/json', 'access-control-allow-origin': '*', 'cache-control': 'no-store' },
+ });
+ }
+
if (url.pathname === '/preview' && req.method === 'GET') {
if (!rateOk(ip, 120)) return json({ error: 'rate limited' }, 429);
const target = (url.searchParams.get('url') || '').trim();