From 87c81ebe24735294e0666ba76c6ab34fe9b798ba Mon Sep 17 00:00:00 2001 From: Sjors Provoost Date: Mon, 16 Sep 2024 18:24:39 +0200 Subject: [PATCH] Support optional NIP-42 authentication --- src/config.ts | 3 +++ src/deps.ts | 11 ++++++++++- src/nostr/relay.ts | 14 +++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/config.ts b/src/config.ts index f648963..c401e7c 100644 --- a/src/config.ts +++ b/src/config.ts @@ -15,6 +15,9 @@ const Conf = { get publishRelays(): string[] { return deduplicate([Conf.nostrRelay, ...parseRelays(Deno.env.get('RELAY_PUBLISHERS'))]); }, + get nip42key(): string | undefined { + return Deno.env.get('NIP42_KEY'); + }, get poolRelays(): string[] { return parseRelays(Deno.env.get('RELAY_POOL')); }, diff --git a/src/deps.ts b/src/deps.ts index 4298c0c..5b708d0 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -19,7 +19,16 @@ export { type HTMLDocument, type Node, } from 'https://deno.land/x/deno_dom@v0.1.45/deno-dom-native.ts'; -export { type Filter, getEventHash, matchFilter, nip19, nip21, nip27, validateEvent } from 'npm:nostr-tools@^2.3.1'; +export { + type Filter, + getEventHash, + matchFilter, + nip19, + nip21, + nip27, + nip42, + validateEvent, +} from 'npm:nostr-tools@^2.3.1'; export { finalizeEvent, generateSecretKey, getPublicKey, verifyEvent } from 'npm:nostr-tools@^2.3.1/wasm'; export { z } from 'https://deno.land/x/zod@v3.22.4/mod.ts'; export { default as linkify } from 'npm:linkifyjs@^4.1.3'; diff --git a/src/nostr/relay.ts b/src/nostr/relay.ts index c3cfa8c..1255f29 100644 --- a/src/nostr/relay.ts +++ b/src/nostr/relay.ts @@ -1,7 +1,9 @@ import { Conf } from '@/config.ts'; -import { RelayPool } from '@/deps.ts'; +import { nip42, RelayPool } from '@/deps.ts'; +import { finalizeEvent } from '@/deps.ts'; import { nostrDate } from '../utils/parse.ts'; +import { decodeString } from 'https://deno.land/std@0.99.0/encoding/hex.ts'; import handleEvent from './handler.ts'; @@ -10,6 +12,16 @@ const pool = new RelayPool(Conf.publishRelays, { skipVerification: true, }); +if (Conf.nip42key) { + pool.onauth((relay: any, challenge: string) => { + console.debug('NIP42 AUTH', relay.url); + const auth = nip42.makeAuthEvent(relay.url, challenge); + const signed_event = finalizeEvent(auth, decodeString(Conf.nip42key!)); + relay.auth(signed_event); + // TODO: wait for OK reply or error + }); +} + pool.subscribe( [{ kinds: [0, 1, 3, 5, 6, 7, 9735], since: nostrDate() }], Conf.publishRelays, -- GitLab