import { NIP05 } from '@nostrify/nostrify'; import { nip19 } from 'nostr-tools'; import { Conf } from '@/config.ts'; import { Debug } from '@/deps.ts'; import { SimpleLRU } from '@/utils/SimpleLRU.ts'; import { Time } from '@/utils/time.ts'; import { eventsDB } from '@/storages.ts'; import { fetchWorker } from '@/workers/fetch.ts'; const debug = Debug('ditto:nip05'); const nip05Cache = new SimpleLRU( async (key, { signal }) => { debug(`Lookup ${key}`); const [name, domain] = key.split('@'); try { if (domain === Conf.url.host) { const pointer = await localNip05Lookup(name); if (pointer) { debug(`Found: ${key} is ${pointer.pubkey}`); return pointer; } else { throw new Error(`Not found: ${key}`); } } else { const result = await NIP05.lookup(key, { fetch: fetchWorker, signal }); debug(`Found: ${key} is ${result.pubkey}`); return result; } } catch (e) { debug(`Not found: ${key}`); throw e; } }, { max: 500, ttl: Time.hours(1) }, ); async function localNip05Lookup(name: string): Promise { const [label] = await eventsDB.query([{ kinds: [1985], authors: [Conf.pubkey], '#L': ['nip05'], '#l': [`${name}@${Conf.url.host}`], limit: 1, }]); const pubkey = label?.tags.find(([name]) => name === 'p')?.[1]; if (pubkey) { return { pubkey, relays: [Conf.relay] }; } } export { localNip05Lookup, nip05Cache };