substr/handlers/npub.ts
2024-10-23 00:27:20 +02:00

24 lines
587 B
TypeScript

import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { lookupUsernameByPubkey } from "../directory.ts";
import notFoundHandler from "../handlers/not-found.ts";
const npubHandler = async function (ctx: Context) {
const npub = ctx.state.path;
try {
const pubkey = nip19.decode(npub).data as string;
const username = await lookupUsernameByPubkey(pubkey);
if (username) {
ctx.response.redirect(`/@${username}`);
} else {
notFoundHandler(ctx);
}
} catch (_e) {
notFoundHandler(ctx);
}
};
export default npubHandler;