diff --git a/src/config.ts b/src/config.ts index f6489639883fe201eb3abbd16e198d3bfcfb0ac8..c401e7c3643cac1864501d685efa7b18528646f9 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 4298c0ccd174e52176719326fd7c686aff9148a7..5b708d04ca3e469e16a83b42be328397922d978c 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 c3cfa8ce1477555426d9815af2631f448eed3dc7..1255f29369f35a9e8cf4f75f148ad24f88e88fca 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,