NIP05 DRYer
This commit is contained in:
parent
435c69e6a8
commit
a19f1aa6e8
|
@ -1,7 +1,6 @@
|
||||||
import { AppController } from '@/app.ts';
|
import { AppController } from '@/app.ts';
|
||||||
import { Conf } from '@/config.ts';
|
|
||||||
import { z } from '@/deps.ts';
|
import { z } from '@/deps.ts';
|
||||||
import { eventsDB } from '@/storages.ts';
|
import { localNip05Lookup } from '@/utils/nip05.ts';
|
||||||
|
|
||||||
const nameSchema = z.string().min(1).regex(/^\w+$/);
|
const nameSchema = z.string().min(1).regex(/^\w+$/);
|
||||||
|
|
||||||
|
@ -12,30 +11,20 @@ const nameSchema = z.string().min(1).regex(/^\w+$/);
|
||||||
const nostrController: AppController = async (c) => {
|
const nostrController: AppController = async (c) => {
|
||||||
const result = nameSchema.safeParse(c.req.query('name'));
|
const result = nameSchema.safeParse(c.req.query('name'));
|
||||||
const name = result.success ? result.data : undefined;
|
const name = result.success ? result.data : undefined;
|
||||||
|
const pointer = name ? await localNip05Lookup(name) : undefined;
|
||||||
|
|
||||||
if (!name) {
|
if (!name || !pointer) {
|
||||||
return c.json({ names: {}, relays: {} });
|
return c.json({ names: {}, relays: {} });
|
||||||
}
|
}
|
||||||
|
|
||||||
const [label] = await eventsDB.query([{
|
const { pubkey, relays } = pointer;
|
||||||
kinds: [1985],
|
|
||||||
authors: [Conf.pubkey],
|
|
||||||
'#L': ['nip05'],
|
|
||||||
'#l': [`${name}@${Conf.url.host}`],
|
|
||||||
}]);
|
|
||||||
|
|
||||||
const pubkey = label?.tags.find(([name]) => name === 'p')?.[1];
|
|
||||||
|
|
||||||
if (!label || !pubkey) {
|
|
||||||
return c.json({ names: {}, relays: {} });
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.json({
|
return c.json({
|
||||||
names: {
|
names: {
|
||||||
[name]: pubkey,
|
[name]: pubkey,
|
||||||
},
|
},
|
||||||
relays: {
|
relays: {
|
||||||
[pubkey]: [Conf.relay],
|
[pubkey]: relays,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
|
@ -3,6 +3,7 @@ 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';
|
import { eventsDB } from '@/storages.ts';
|
||||||
|
import { fetchWorker } from '@/workers/fetch.ts';
|
||||||
|
|
||||||
const debug = Debug('ditto:nip05');
|
const debug = Debug('ditto:nip05');
|
||||||
|
|
||||||
|
@ -12,9 +13,15 @@ const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
|
||||||
const [name, domain] = key.split('@');
|
const [name, domain] = key.split('@');
|
||||||
try {
|
try {
|
||||||
if (domain === Conf.url.host) {
|
if (domain === Conf.url.host) {
|
||||||
return localNip05Lookup(name);
|
const pointer = await localNip05Lookup(name);
|
||||||
|
if (pointer) {
|
||||||
|
debug(`Found: ${key} is ${pointer.pubkey}`);
|
||||||
|
return pointer;
|
||||||
} else {
|
} else {
|
||||||
const result = await NIP05.lookup(key, { fetch, signal });
|
throw new Error(`Not found: ${key}`);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
const result = await NIP05.lookup(key, { fetch: fetchWorker, signal });
|
||||||
debug(`Found: ${key} is ${result.pubkey}`);
|
debug(`Found: ${key} is ${result.pubkey}`);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -23,28 +30,23 @@ const nip05Cache = new SimpleLRU<string, nip19.ProfilePointer>(
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{ max: 5000, ttl: Time.hours(1) },
|
{ max: 500, ttl: Time.hours(1) },
|
||||||
);
|
);
|
||||||
|
|
||||||
async function localNip05Lookup(name: string): Promise<nip19.ProfilePointer> {
|
async function localNip05Lookup(name: string): Promise<nip19.ProfilePointer | undefined> {
|
||||||
const { host } = Conf.url;
|
|
||||||
|
|
||||||
const [label] = await eventsDB.query([{
|
const [label] = await eventsDB.query([{
|
||||||
kinds: [1985],
|
kinds: [1985],
|
||||||
authors: [Conf.pubkey],
|
authors: [Conf.pubkey],
|
||||||
'#L': ['nip05'],
|
'#L': ['nip05'],
|
||||||
'#l': [`${name}@${host}`],
|
'#l': [`${name}@${Conf.url.host}`],
|
||||||
|
limit: 1,
|
||||||
}]);
|
}]);
|
||||||
|
|
||||||
const pubkey = label?.tags.find(([name]) => name === 'p')?.[1];
|
const pubkey = label?.tags.find(([name]) => name === 'p')?.[1];
|
||||||
|
|
||||||
if (pubkey) {
|
if (pubkey) {
|
||||||
debug(`Found: ${name} is ${pubkey}`);
|
|
||||||
return { pubkey, relays: [Conf.relay] };
|
return { pubkey, relays: [Conf.relay] };
|
||||||
}
|
}
|
||||||
|
|
||||||
debug(`Not found: ${name}`);
|
|
||||||
throw new Error('Not found');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export { nip05Cache };
|
export { localNip05Lookup, nip05Cache };
|
||||||
|
|
Loading…
Reference in New Issue