From 0e15e174c564c4ba4e9cc3874b675fbbcc597c1e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sun, 9 Jul 2023 12:55:37 -0500 Subject: [PATCH] Add nostr.json (NIP-05) --- src/app.ts | 3 +++ src/config.ts | 3 +++ src/controllers/well-known/nostr.ts | 34 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+) create mode 100644 src/controllers/well-known/nostr.ts diff --git a/src/app.ts b/src/app.ts index 9104e2bb..175eb575 100644 --- a/src/app.ts +++ b/src/app.ts @@ -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); diff --git a/src/config.ts b/src/config.ts index d07b7909..fd208c07 100644 --- a/src/config.ts +++ b/src/config.ts @@ -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'; }, diff --git a/src/controllers/well-known/nostr.ts b/src/controllers/well-known/nostr.ts new file mode 100644 index 00000000..b1b84336 --- /dev/null +++ b/src/controllers/well-known/nostr.ts @@ -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 };