feat: lnurl handler
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Kieran 2024-03-17 19:09:30 +00:00
parent 5e10b888dd
commit a54c628c77
3 changed files with 51 additions and 1 deletions

3
.gitignore vendored
View File

@ -31,4 +31,5 @@ yarn-error.log*
!.yarn/sdks
!.yarn/versions
dev-dist/
dev-dist/
.wranger/

View File

@ -0,0 +1,48 @@
interface Env { }
interface NostrJson {
names: Record<string, string>;
relays?: Record<string, Array<string>>;
nip46?: Record<string, Array<string>>;
}
async function fetchNip05Pubkey(name: string, domain: string, timeout = 2_000): Promise<string | undefined> {
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<Env> = 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"
}
});
}

View File

@ -24,5 +24,6 @@
}
},
"include": ["src"],
"exclude": ["functions/**/*"],
"references": [{ "path": "./tsconfig.node.json" }]
}