nip05Cache: lookup local nip05 from direct database call
This commit is contained in:
parent
0cfef1cb59
commit
435c69e6a8
|
@ -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<string, nip19.ProfilePointer>(
|
||||
async (key, { signal }) => {
|
||||
debug(`Lookup ${key}`);
|
||||
const [name, domain] = key.split('@');
|
||||
try {
|
||||
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<string, nip19.ProfilePointer>(
|
|||
{ 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 };
|
||||
|
|
Loading…
Reference in New Issue