diff options
Diffstat (limited to 'src/lib/parse.ts')
| -rw-r--r-- | src/lib/parse.ts | 50 |
1 files changed, 30 insertions, 20 deletions
diff --git a/src/lib/parse.ts b/src/lib/parse.ts index ba264be7..e8d8bafd 100644 --- a/src/lib/parse.ts +++ b/src/lib/parse.ts @@ -1,20 +1,36 @@ import { PUBLIC_DOMAIN } from "$env/static/public"; import { decode } from "$lib/bip21"; -import { get, isLiquid, post, sats } from "$lib/utils"; +import { auth, get, isLiquid, post, sats } from "$lib/utils"; import { redirect } from "@sveltejs/kit"; import { validate } from "bitcoin-address-validation"; -export default async (s, host) => { +export default async (s, host, cookies) => { if (!s) return; let t = s.trim(); let amount; + let invoice; let user; if (t.startsWith("http")) redirect(307, t); if (t.startsWith(host)) redirect(307, `http://${t}`); + if (t.includes("lightning=") || t.includes("lno=")) { + const url = new URL(t); + const params = new URLSearchParams(url.search); + t = params.get("lightning") || params.get("lno"); + } + if (t.startsWith("lightning:")) t = t.replace("lightning:", ""); - if (t.endsWith(`@${PUBLIC_DOMAIN}`)) t = t.split("@")[0]; + // Our own domain is no longer only ours: coinos v3 payment addresses live + // here too and are paid over lightning like anyone else's. Only strip the + // domain (and pay internally) when the name really is a local account. + if (t.includes("@") && t.split("@").pop() === PUBLIC_DOMAIN) { + const name = t.split("@")[0]; + try { + const u = await get(`/users/${name}`); + if (u && !u.anon) t = name; + } catch (e) {} + } if (t.includes("@") && t.includes(".")) { try { t = await get(`/encode?address=${t}`); @@ -51,16 +67,12 @@ export default async (s, host) => { if (t.toLowerCase().startsWith("lnurl")) redirect(307, `/ln/${t}`); if (t.includes(":")) t = t.split(":")[1]; - if (t.includes("lightning=")) { - const url = new URL(t); - const params = new URLSearchParams(url.search); - t = params.get("lightning"); - } - - if (t.toLowerCase().startsWith("ln")) { + if (t.toLowerCase().startsWith("lnr")) { + const { iid } = await post("/sendinvoice", { invreq: t }, auth(cookies)); + invoice = { id: iid }; + } else if (t.toLowerCase().startsWith("ln")) { try { - const inv = await get(`/invoice/${t}`); - if (inv.user.username === "mint") throw new Error("mint payment"); + invoice = await get(`/invoice/${t}`); } catch (e) { if (t.toLowerCase().startsWith("lno")) { const { offer_amount_msat: a } = await get(`/decode/${t}`); @@ -70,6 +82,8 @@ export default async (s, host) => { } } + if (invoice) redirect(307, `/invoice/${invoice.id}`); + if (t.match(/^🥜([\uFE00-\uFE0F]|[\uE0100-\uE01EF]+)$/)) { t = Array.from(t) .slice(1) @@ -91,10 +105,8 @@ export default async (s, host) => { .join(""); } - if (t.startsWith("cashu")) { - const { id } = await post("/cash", { token: t }); - redirect(307, `/ecash/${id}`); - } + // The token rides in the URL; the ecash page asks the server for its status. + if (t.startsWith("cashu")) redirect(307, `/ecash/${encodeURIComponent(t)}`); if (t.startsWith("creq")) redirect(307, `/send/ecash/${t}`); @@ -115,11 +127,9 @@ export default async (s, host) => { if (fund) redirect(307, `/send/fund/${t}`); - // invoice - let invoice; try { - invoice = await get(`/invoice/${t}`); + invoice ||= await get(`/invoice/${t}`); } catch (e) {} - if (invoice) redirect(307, `/send/invoice/${invoice.id}`); + if (invoice) redirect(307, `/invoice/${invoice.id}`); }; |
