Add nostr.json (NIP-05)

This commit is contained in:
Alex Gleason 2023-07-09 12:55:37 -05:00
parent 7808565d71
commit 0e15e174c5
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 40 additions and 0 deletions

View File

@ -27,6 +27,7 @@ import {
} from './controllers/api/statuses.ts';
import { streamingController } from './controllers/api/streaming.ts';
import { indexController } from './controllers/site.ts';
import { nostrController } from './controllers/well-known/nostr.ts';
import { auth19, requireAuth } from './middleware/auth19.ts';
import { auth98 } from './middleware/auth98.ts';
@ -56,6 +57,8 @@ app.get('/api/v1/streaming/', streamingController);
app.use('*', cors({ origin: '*', exposeHeaders: ['link'] }), auth19, auth98());
app.get('/.well-known/nostr.json', nostrController);
app.get('/api/v1/instance', instanceController);
app.get('/api/v1/apps/verify_credentials', appCredentialsController);

View File

@ -3,6 +3,9 @@ const Conf = {
get nsec() {
return Deno.env.get('DITTO_NSEC');
},
get relay() {
return Deno.env.get('DITTO_RELAY');
},
get localDomain() {
return Deno.env.get('LOCAL_DOMAIN') || 'http://localhost:8000';
},

View File

@ -0,0 +1,34 @@
import { db } from '@/db.ts';
import { z } from '@/deps.ts';
import type { AppController } from '@/app.ts';
import { Conf } from '../../config.ts';
const nameSchema = z.string().min(1).regex(/^[\w_]+$/);
/**
* Serves NIP-05's nostr.json.
* https://github.com/nostr-protocol/nips/blob/master/05.md
*/
const nostrController: AppController = async (c) => {
try {
const name = nameSchema.parse(c.req.query('name'));
const user = await db.users.findFirst({ where: { username: name } });
const relay = Conf.relay;
return c.json({
names: {
[user.username]: user.pubkey,
},
relays: relay
? {
[user.pubkey]: [relay],
}
: {},
});
} catch (_e) {
return c.json({ names: {}, relays: {} });
}
};
export { nostrController };