Perform NIP-05 lookups

This commit is contained in:
Alex Gleason 2023-04-29 17:49:03 -05:00
parent d84e593e53
commit 64102699ac
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 27 additions and 23 deletions

View File

@ -1,9 +1,12 @@
import { type AppController } from '@/app.ts';
import { nip05 } from '@/deps.ts';
import { getAuthor } from '../client.ts';
import { toAccount } from '../transmute.ts';
import { bech32ToPubkey } from '../utils.ts';
import type { Event } from '../event.ts';
const credentialsController: AppController = async (c) => {
const pubkey = c.get('pubkey')!;
@ -33,17 +36,9 @@ const accountLookupController: AppController = async (c) => {
return c.json({ error: 'Missing `acct` query parameter.' }, 422);
}
if (acct.includes('@')) {
// TODO: NIP-05 handling
return c.json({ error: 'NIP-05 lookups not yet implemented.' }, 422);
}
const pubkey = bech32ToPubkey(acct);
if (pubkey) {
const event = await getAuthor(pubkey);
if (event) {
return c.json(toAccount(event));
}
const event = await lookupAccount(acct);
if (event) {
return c.json(toAccount(event));
}
return c.json({ error: 'Could not find user.' }, 404);
@ -56,15 +51,23 @@ const accountSearchController: AppController = async (c) => {
return c.json({ error: 'Missing `q` query parameter.' }, 422);
}
const pubkey = bech32ToPubkey(q);
if (pubkey) {
const event = await getAuthor(pubkey);
if (event) {
return c.json([toAccount(event)]);
}
const event = await lookupAccount(decodeURIComponent(q));
if (event) {
return c.json([toAccount(event)]);
}
return c.json([]);
};
/** Resolve a bech32 or NIP-05 identifier to an account. */
async function lookupAccount(value: string): Promise<Event<0> | undefined> {
console.log(`Looking up ${value}`);
const pubkey = bech32ToPubkey(value) || (await nip05.queryProfile(value))?.pubkey;
if (pubkey) {
return getAuthor(pubkey);
}
}
export { accountController, accountLookupController, accountSearchController, credentialsController };

View File

@ -8,26 +8,26 @@ import { eventDateComparator, nostrNow } from './utils.ts';
const pool = new RelayPool(poolRelays);
/** Get a Nostr event by its ID. */
const getEvent = async (id: string): Promise<SignedEvent | null> => {
const getEvent = async (id: string): Promise<SignedEvent | undefined> => {
const event = await (pool.getEventById(id, poolRelays, 0) as Promise<SignedEvent>);
return event?.id === id ? event : null;
return event?.id === id ? event : undefined;
};
/** Get a Nostr `set_medatadata` event for a user's pubkey. */
const getAuthor = async (pubkey: string): Promise<SignedEvent<0> | null> => {
const getAuthor = async (pubkey: string): Promise<SignedEvent<0> | undefined> => {
const author = new Author(pool, poolRelays, pubkey);
const event: SignedEvent<0> | null = await new Promise((resolve) => author.metaData(resolve, 0));
return event?.pubkey === pubkey ? event : null;
return event?.pubkey === pubkey ? event : undefined;
};
/** Get users the given pubkey follows. */
const getFollows = (pubkey: string): Promise<SignedEvent<3> | null> => {
const getFollows = (pubkey: string): Promise<SignedEvent<3> | undefined> => {
return new Promise((resolve) => {
pool.subscribe(
[{ authors: [pubkey], kinds: [3] }],
poolRelays,
(event: SignedEvent<3> | null) => {
resolve(event?.pubkey === pubkey ? event : null);
resolve(event?.pubkey === pubkey ? event : undefined);
},
undefined,
undefined,

View File

@ -14,6 +14,7 @@ export {
type Filter,
getEventHash,
getPublicKey,
nip05,
nip19,
nip21,
signEvent as getSignature,