nip05Cache: lookup local nip05 from direct database call

This commit is contained in:
Alex Gleason 2024-03-27 18:39:50 -05:00
parent 0cfef1cb59
commit 435c69e6a8
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 31 additions and 3 deletions

View File

@ -1,16 +1,23 @@
import { Conf } from '@/config.ts';
import { Debug, NIP05, nip19 } from '@/deps.ts'; import { Debug, NIP05, nip19 } from '@/deps.ts';
import { SimpleLRU } from '@/utils/SimpleLRU.ts'; import { SimpleLRU } from '@/utils/SimpleLRU.ts';
import { Time } from '@/utils/time.ts'; import { Time } from '@/utils/time.ts';
import { eventsDB } from '@/storages.ts';
const debug = Debug('ditto:nip05'); const debug = Debug('ditto:nip05');
const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>( const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
async (key, { signal }) => { async (key, { signal }) => {
debug(`Lookup ${key}`); debug(`Lookup ${key}`);
const [name, domain] = key.split('@');
try { try {
if (domain === Conf.url.host) {
return localNip05Lookup(name);
} else {
const result = await NIP05.lookup(key, { fetch, signal }); const result = await NIP05.lookup(key, { fetch, signal });
debug(`Found: ${key} is ${result.pubkey}`); debug(`Found: ${key} is ${result.pubkey}`);
return result; return result;
}
} catch (e) { } catch (e) {
debug(`Not found: ${key}`); debug(`Not found: ${key}`);
throw e; throw e;
@ -19,4 +26,25 @@ const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
{ max: 5000, ttl: Time.hours(1) }, { max: 5000, ttl: Time.hours(1) },
); );
async function localNip05Lookup(name: string): Promise<nip19.ProfilePointer> {
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 }; export { nip05Cache };