feat: lnurl handler
This commit is contained in:
parent
5e10b888dd
commit
a54c628c77
3
.gitignore
vendored
3
.gitignore
vendored
@ -31,4 +31,5 @@ yarn-error.log*
|
|||||||
!.yarn/sdks
|
!.yarn/sdks
|
||||||
!.yarn/versions
|
!.yarn/versions
|
||||||
|
|
||||||
dev-dist/
|
dev-dist/
|
||||||
|
.wranger/
|
48
functions/.well-known/lnurlp/[handle].ts
Normal file
48
functions/.well-known/lnurlp/[handle].ts
Normal 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"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
@ -24,5 +24,6 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": ["src"],
|
"include": ["src"],
|
||||||
|
"exclude": ["functions/**/*"],
|
||||||
"references": [{ "path": "./tsconfig.node.json" }]
|
"references": [{ "path": "./tsconfig.node.json" }]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user