diff --git a/src/utils/nip05.ts b/src/utils/nip05.ts index e94b36c..c572b4d 100644 --- a/src/utils/nip05.ts +++ b/src/utils/nip05.ts @@ -1,16 +1,23 @@ +import { Conf } from '@/config.ts'; import { Debug, NIP05, nip19 } from '@/deps.ts'; import { SimpleLRU } from '@/utils/SimpleLRU.ts'; import { Time } from '@/utils/time.ts'; +import { eventsDB } from '@/storages.ts'; const debug = Debug('ditto:nip05'); const nip05Cache = new SimpleLRU( async (key, { signal }) => { debug(`Lookup ${key}`); + const [name, domain] = key.split('@'); try { - const result = await NIP05.lookup(key, { fetch, signal }); - debug(`Found: ${key} is ${result.pubkey}`); - return result; + if (domain === Conf.url.host) { + return localNip05Lookup(name); + } else { + const result = await NIP05.lookup(key, { fetch, signal }); + debug(`Found: ${key} is ${result.pubkey}`); + return result; + } } catch (e) { debug(`Not found: ${key}`); throw e; @@ -19,4 +26,25 @@ const nip05Cache = new SimpleLRU( { max: 5000, ttl: Time.hours(1) }, ); +async function localNip05Lookup(name: string): Promise { + const { host } = Conf.url; + + const [label] = await eventsDB.query([{ + kinds: [1985], + authors: [Conf.pubkey], + '#L': ['nip05'], + '#l': [`${name}@${host}`], + }]); + + const pubkey = label?.tags.find(([name]) => name === 'p')?.[1]; + + if (pubkey) { + debug(`Found: ${name} is ${pubkey}`); + return { pubkey, relays: [Conf.relay] }; + } + + debug(`Not found: ${name}`); + throw new Error('Not found'); +} + export { nip05Cache };