substr/handlers/naddr.ts

26 lines
675 B
TypeScript

import { Context } from "@oak/oak";
import { nip19 } from "@nostr/tools";
import { log } from "../log.ts";
import { lookupUsernameByPubkey } from "../directory.ts";
import notFoundHandler from "../handlers/not-found.ts";
const naddrHandler = async function (ctx: Context) {
const naddr = ctx.params.path;
try {
const r = nip19.decode(naddr);
const username = await lookupUsernameByPubkey(r.data.pubkey);
if (username && r.data.identifier) {
ctx.response.redirect(`/@${username}/${r.data.identifier}`);
} else {
notFoundHandler(ctx);
}
} catch (e) {
log(e, "yellow");
notFoundHandler(ctx);
}
};
export default naddrHandler;