From a54c628c7778f9aafaea76286da3cc53390e7534 Mon Sep 17 00:00:00 2001 From: Kieran Date: Sun, 17 Mar 2024 19:09:30 +0000 Subject: [PATCH] feat: lnurl handler --- .gitignore | 3 +- functions/.well-known/lnurlp/[handle].ts | 48 ++++++++++++++++++++++++ tsconfig.json | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 functions/.well-known/lnurlp/[handle].ts diff --git a/.gitignore b/.gitignore index b251c5f..ec2ecb6 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,5 @@ yarn-error.log* !.yarn/sdks !.yarn/versions -dev-dist/ \ No newline at end of file +dev-dist/ +.wranger/ \ No newline at end of file diff --git a/functions/.well-known/lnurlp/[handle].ts b/functions/.well-known/lnurlp/[handle].ts new file mode 100644 index 0000000..67a9cca --- /dev/null +++ b/functions/.well-known/lnurlp/[handle].ts @@ -0,0 +1,48 @@ +interface Env { } + +interface NostrJson { + names: Record; + relays?: Record>; + nip46?: Record>; +} + +async function fetchNip05Pubkey(name: string, domain: string, timeout = 2_000): Promise { + if (!name || !domain) { + return undefined; + } + try { + const res = await fetch(`https://${domain}/.well-known/nostr.json?name=${encodeURIComponent(name)}`, { + signal: AbortSignal.timeout(timeout), + }); + const data: NostrJson = await res.json(); + const match = Object.keys(data.names).find(n => { + return n.toLowerCase() === name.toLowerCase(); + }); + return match ? data.names[match] : undefined; + } catch { + // ignored + } + return undefined; +} + +export const onRequest: PagesFunction = async context => { + const handle = context.params.handle as string | undefined; + let pubkey = handle; + if (handle && handle.length !== 64) { + const nip5 = await fetchNip05Pubkey(handle, "zap.stream"); + if (nip5) { + pubkey = nip5; + } + } + + const response = await fetch(`https://api.zap.stream/.well-known/lnurlp/${pubkey}`); + const results = await response.text(); + const responseHeaders = Object.fromEntries(response.headers.entries()); + return new Response(results, { + headers: { + ...responseHeaders, + "access-control-allow-origin": "*", + "cache-control": "no-store" + } + }); +} \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 59f38c1..9f4fe8a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -24,5 +24,6 @@ } }, "include": ["src"], + "exclude": ["functions/**/*"], "references": [{ "path": "./tsconfig.node.json" }] }