diff --git a/packages/common/src/interfaces/nostr-relay-options.interface.ts b/packages/common/src/interfaces/nostr-relay-options.interface.ts index 79a334aa..14ab518d 100644 --- a/packages/common/src/interfaces/nostr-relay-options.interface.ts +++ b/packages/common/src/interfaces/nostr-relay-options.interface.ts @@ -5,9 +5,16 @@ import { Logger } from './logger.interface'; * Options for NostrRelay */ export type NostrRelayOptions = { + /** + * Hostname of the Nostr Relay server. If not set, NIP-42 is not enabled. + * More info: https://github.com/nostr-protocol/nips/blob/master/42.md + */ + hostname?: string; /** * Domain name of the Nostr Relay server. If not set, NIP-42 is not enabled. * More info: https://github.com/nostr-protocol/nips/blob/master/42.md + * + * @deprecated Use hostname instead */ domain?: string; /** diff --git a/packages/core/__test__/nostr-relay.spec.ts b/packages/core/__test__/nostr-relay.spec.ts index 0bc24516..7db83e5f 100644 --- a/packages/core/__test__/nostr-relay.spec.ts +++ b/packages/core/__test__/nostr-relay.spec.ts @@ -19,7 +19,7 @@ describe('NostrRelay', () => { beforeEach(() => { nostrRelay = new NostrRelay({} as EventRepository, { - domain: 'test', + hostname: 'test', }); client = { @@ -118,7 +118,7 @@ describe('NostrRelay', () => { it('should not cache handle result', async () => { const nostrRelayWithoutCache = new NostrRelay({} as EventRepository, { - domain: 'test', + hostname: 'test', eventHandlingResultCacheTtl: 0, }); const event = { id: 'eventId' } as Event; @@ -253,20 +253,20 @@ describe('NostrRelay', () => { }); it('should handle req successfully if NIP-42 is not enabled and filter contains encrypted direct message kind', async () => { - const nostrRelayWithoutDomain = new NostrRelay({} as EventRepository); + const nostrRelayWithoutHostname = new NostrRelay({} as EventRepository); const subscriptionId: SubscriptionId = 'subscriptionId'; const filters: Filter[] = [{ kinds: [4] }]; const events = [{ id: 'a', kind: 4 }] as Event[]; - const ctx = nostrRelayWithoutDomain['getClientContext'](client); + const ctx = nostrRelayWithoutHostname['getClientContext'](client); const mockSubscribe = jest - .spyOn(nostrRelayWithoutDomain['subscriptionService'], 'subscribe') + .spyOn(nostrRelayWithoutHostname['subscriptionService'], 'subscribe') .mockImplementation(); const mockFind = jest - .spyOn(nostrRelayWithoutDomain['eventService'], 'find') + .spyOn(nostrRelayWithoutHostname['eventService'], 'find') .mockResolvedValue(events); - const result = await nostrRelayWithoutDomain.handleReqMessage( + const result = await nostrRelayWithoutHostname.handleReqMessage( client, subscriptionId, filters, @@ -330,11 +330,11 @@ describe('NostrRelay', () => { ); }); - it('should return directly if domain is not set', async () => { - const nostrRelayWithoutDomain = new NostrRelay({} as EventRepository); + it('should return directly if hostname is not set', async () => { + const nostrRelayWithoutHostname = new NostrRelay({} as EventRepository); const signedEvent = { id: 'eventId' } as Event; - nostrRelayWithoutDomain.handleAuthMessage(client, signedEvent); + nostrRelayWithoutHostname.handleAuthMessage(client, signedEvent); expect(client.send).toHaveBeenCalledWith( JSON.stringify([MessageType.OK, signedEvent.id, true, '']), @@ -405,10 +405,10 @@ describe('NostrRelay', () => { }); describe('isAuthorized', () => { - it('should return true if domain is not set', () => { - const nostrRelayWithoutDomain = new NostrRelay({} as EventRepository); + it('should return true if hostname is not set', () => { + const nostrRelayWithoutHostname = new NostrRelay({} as EventRepository); - expect(nostrRelayWithoutDomain.isAuthorized(client)).toBeTruthy(); + expect(nostrRelayWithoutHostname.isAuthorized(client)).toBeTruthy(); }); it('should return false if client is not authenticated', () => { diff --git a/packages/core/src/nostr-relay.ts b/packages/core/src/nostr-relay.ts index 0ac7bbe1..8205ac69 100644 --- a/packages/core/src/nostr-relay.ts +++ b/packages/core/src/nostr-relay.ts @@ -42,7 +42,7 @@ export class NostrRelay { private readonly eventHandlingLazyCache: | LazyCache> | undefined; - private readonly domain?: string; + private readonly hostname?: string; private readonly pluginManagerService: PluginManagerService; private readonly clientContexts = new Map(); @@ -59,6 +59,9 @@ export class NostrRelay { ) { this.options = options; + // if hostname is not set, it means that NIP-42 is not enabled + this.hostname = options.hostname ?? options.domain; + const logger = options.logger ?? new ConsoleLoggerService(); logger.setLogLevel(options.logLevel ?? LogLevel.INFO); @@ -66,7 +69,7 @@ export class NostrRelay { this.subscriptionService = new SubscriptionService( this.clientContexts, logger, - !!options.domain, + !!this.hostname, ); this.eventService = new EventService( eventRepository, @@ -89,9 +92,6 @@ export class NostrRelay { ttl: options.eventHandlingResultCacheTtl, }); } - - // if domain is not set, it means that NIP-42 is not enabled - this.domain = options.domain; } /** @@ -112,7 +112,7 @@ export class NostrRelay { */ handleConnection(client: Client): void { const ctx = this.getClientContext(client); - if (this.domain) { + if (this.hostname) { ctx.sendMessage(createOutgoingAuthMessage(ctx.id)); } } @@ -283,7 +283,7 @@ export class NostrRelay { signedEvent: Event, ): HandleAuthMessageResult { const ctx = this.getClientContext(client); - if (!this.domain) { + if (!this.hostname) { ctx.sendMessage(createOutgoingOkMessage(signedEvent.id, true)); return { success: true }; } @@ -291,7 +291,7 @@ export class NostrRelay { const validateErrorMsg = EventUtils.isSignedEventValid( signedEvent, ctx.id, - this.domain, + this.hostname, ); if (validateErrorMsg) { ctx.sendMessage( @@ -312,7 +312,7 @@ export class NostrRelay { * @param client Client instance, usually a WebSocket */ isAuthorized(client: Client): boolean { - return this.domain ? !!this.getClientContext(client).pubkey : true; + return this.hostname ? !!this.getClientContext(client).pubkey : true; } /** @@ -363,7 +363,7 @@ export class NostrRelay { ): Promise { const events: Event[] = []; if ( - this.domain && + this.hostname && filters.some(filter => FilterUtils.hasEncryptedDirectMessageKind(filter), ) && @@ -375,7 +375,7 @@ export class NostrRelay { } (await this.eventService.find(filters)).forEach(event => { - if (this.domain && !EventUtils.checkPermission(event, pubkey)) { + if (this.hostname && !EventUtils.checkPermission(event, pubkey)) { return; } events.push(event);