aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/lnurl.js10
-rw-r--r--tools/lnurl-cors-fallback-test.js44
2 files changed, 53 insertions, 1 deletions
diff --git a/src/lnurl.js b/src/lnurl.js
index 134edcf..53b98df 100644
--- a/src/lnurl.js
+++ b/src/lnurl.js
@@ -75,6 +75,8 @@ export function zapTargetFromProfile(profile, pk) {
return null;
}
+const LNURLP_FETCH = 'https://names.coinos.io/lnurlp-fetch';
+
function safeEncode(url) { try { return encodeLnurl(url); } catch { return null; } }
// Errors name the server: the recipient's Lightning address lives on THEIR
@@ -104,7 +106,13 @@ async function fetchJson(url, ms = 8000) {
// An lnurl that turns out to be a withdraw (LUD-03) is not an error here:
// the thrown error carries its terms (`withdraw`) for the caller to take up.
export async function fetchPayParams(url) {
- const d = await fetchJson(url);
+ let d;
+ try { d = await fetchJson(url); } catch (e) {
+ // A host the browser can't follow, e.g. speed.app's redirect to its API
+ // carries no CORS header: ask the registrar to fetch the same address.
+ if (!/^couldn’t reach /.test(e.message) || !/^https:\/\/[^/]+\/\.well-known\/lnurlp\//.test(url)) throw e;
+ d = await fetchJson(`${LNURLP_FETCH}?url=${encodeURIComponent(url)}`, 12000).catch(() => { throw e; });
+ }
if (!d || d.tag !== 'payRequest' || !d.callback) {
if (d && d.status === 'ERROR') throw new Error(d.reason || 'LNURL error');
const w = withdrawParamsOf(d);
diff --git a/tools/lnurl-cors-fallback-test.js b/tools/lnurl-cors-fallback-test.js
new file mode 100644
index 0000000..bc3923c
--- /dev/null
+++ b/tools/lnurl-cors-fallback-test.js
@@ -0,0 +1,44 @@
+// A Lightning address whose host the browser can't fetch (speed.app answers
+// with a redirect that has no CORS header, so fetch throws a TypeError) is
+// fetched through the registrar's /lnurlp-fetch instead; any other failure
+// stays as it was. Run: bun tools/lnurl-cors-fallback-test.js
+import assert from 'node:assert/strict';
+import { fetchPayParams } from '../src/lnurl.js';
+
+const pay = { tag: 'payRequest', callback: 'https://api.example/cb', minSendable: 1000, maxSendable: 1e9, metadata: '[]' };
+let calls = [];
+const install = (handler) => { calls = []; globalThis.fetch = async (url) => { calls.push(String(url)); return handler(String(url)); }; };
+
+// the browser can't follow the host's redirect → the registrar's copy
+install((u) => {
+ if (u.startsWith('https://speed.example/')) throw new TypeError('Failed to fetch');
+ if (u.startsWith('https://names.coinos.io/lnurlp-fetch?url=')) {
+ assert.equal(decodeURIComponent(u.split('url=')[1]), 'https://speed.example/.well-known/lnurlp/bob');
+ return Response.json(pay);
+ }
+ throw new Error('unexpected ' + u);
+});
+const p = await fetchPayParams('https://speed.example/.well-known/lnurlp/bob');
+assert.equal(p.callback, 'https://api.example/cb');
+assert.equal(calls.length, 2);
+
+// a host that works is fetched directly, once
+install(() => Response.json(pay));
+await fetchPayParams('https://ok.example/.well-known/lnurlp/bob');
+assert.deepEqual(calls, ['https://ok.example/.well-known/lnurlp/bob']);
+
+// the host's own error isn't retried elsewhere
+install(() => Response.json({ status: 'ERROR', reason: 'no such user' }, { status: 404 }));
+await assert.rejects(fetchPayParams('https://ok.example/.well-known/lnurlp/nobody'), /no such user/);
+assert.equal(calls.length, 1);
+
+// unreachable both ways: the original "couldn't reach" stands
+install((u) => { if (u.includes('names.coinos.io')) return Response.json({ status: 'ERROR', reason: "couldn't reach that address" }, { status: 502 }); throw new TypeError('Failed to fetch'); });
+await assert.rejects(fetchPayParams('https://down.example/.well-known/lnurlp/bob'), /couldn’t reach down\.example/);
+
+// an lnurl1 that isn't a Lightning address path isn't sent to the registrar
+install(() => { throw new TypeError('Failed to fetch'); });
+await assert.rejects(fetchPayParams('https://down.example/lnurl/pay/abc'), /couldn’t reach/);
+assert.equal(calls.length, 1);
+
+console.log('lnurl-cors-fallback-test: ok');