substr/handlers/npub.ts

27 lines
605 B
TypeScript

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