aboutsummaryrefslogtreecommitdiffstats
path: root/names/preview.js
diff options
context:
space:
mode:
Diffstat (limited to 'names/preview.js')
-rw-r--r--names/preview.js37
1 files changed, 37 insertions, 0 deletions
diff --git a/names/preview.js b/names/preview.js
index b9af975..4979532 100644
--- a/names/preview.js
+++ b/names/preview.js
@@ -131,3 +131,40 @@ export async function fetchPreview(str) {
}
return null;
}
+
+// A Lightning address's pay parameters (LUD-16 step one), for the wallet when
+// the browser can't fetch them itself: some hosts answer
+// /.well-known/lnurlp/<name> with a redirect that carries no CORS header
+// (speed.app → appapi.tryspeed.com, 2026-12-04), and a browser refuses to
+// follow it. Same guards as previews: public hosts only, rechecked after every
+// redirect, a capped body. Only that one path, only JSON; null when unusable.
+export const LNURLP_PATH = /^\/\.well-known\/lnurlp\/[A-Za-z0-9._+-]{1,64}$/;
+export async function fetchLnurlPay(str, fetchFn = fetch, resolve = publicUrl) {
+ let u = await resolve(str);
+ if (!u || u.protocol !== 'https:' || !LNURLP_PATH.test(u.pathname)) return null;
+ for (let hop = 0; u && hop < MAX_HOPS; hop++) {
+ let res;
+ try {
+ res = await fetchFn(u.href, {
+ redirect: 'manual',
+ signal: AbortSignal.timeout(TIMEOUT_MS),
+ headers: { 'user-agent': 'Mozilla/5.0 (compatible; coinos-lnurl/1.0; +https://coinos.io)', accept: 'application/json' },
+ });
+ } catch { return null; }
+ if (res.status >= 300 && res.status < 400 && res.headers.get('location')) {
+ let next;
+ try { next = new URL(res.headers.get('location'), u.href).href; } catch { return null; }
+ try { await res.body?.cancel(); } catch {}
+ u = await resolve(next);
+ if (u && u.protocol !== 'https:') return null;
+ continue;
+ }
+ let d;
+ try { d = JSON.parse(await readCapped(res)); } catch { return null; }
+ if (!d || typeof d !== 'object') return null;
+ if (d.tag === 'payRequest' && typeof d.callback === 'string' && /^https:\/\//.test(d.callback)) return d;
+ if (String(d.status).toUpperCase() === 'ERROR') return { status: 'ERROR', reason: String(d.reason || 'error').slice(0, 300) };
+ return null;
+ }
+ return null;
+}