diff --git a/ndk/src/relay/connectivity.ts b/ndk/src/relay/connectivity.ts index d8e0ca95..8462344c 100644 --- a/ndk/src/relay/connectivity.ts +++ b/ndk/src/relay/connectivity.ts @@ -74,9 +74,16 @@ export class NDKRelayConnectivity { this.debug("Authentication policy returned", !!res); if (res instanceof NDKEvent) { - this.relay.auth(async (evt: EventTemplate): Promise => { - return res.rawEvent() as VerifiedEvent; - }); + try { + await this.relay.auth(async (evt: EventTemplate): Promise => { + return res.rawEvent() as VerifiedEvent; + }); + this._status = NDKRelayStatus.CONNECTED; + this.ndkRelay.emit("authed"); + } catch (e) { + this.debug("Failed to authenticate", e); + this.ndkRelay.emit("authfail"); + } } if (res === true) { diff --git a/ndk/src/relay/index.ts b/ndk/src/relay/index.ts index 3f0e5c8a..3a257379 100644 --- a/ndk/src/relay/index.ts +++ b/ndk/src/relay/index.ts @@ -72,6 +72,7 @@ export interface NDKRelayConnectionStats { * @emits NDKRelay#eose when the relay has reached the end of stored events * @emits NDKRelay#auth when the relay requires authentication * @emits NDKRelay#authed when the relay has authenticated + * @emits NDKRelay#authfail when the relay has rejected the authentication * @emits NDKRelay#delayed-connect when the relay will wait before reconnecting */ export class NDKRelay extends EventEmitter<{ @@ -86,6 +87,7 @@ export class NDKRelay extends EventEmitter<{ notice: (notice: string) => void; auth: (challenge: string) => void; authed: () => void; + authfail: () => void; published: (event: NDKEvent) => void; "publish:failed": (event: NDKEvent, error: Error) => void; "delayed-connect": (delayInMs: number) => void; diff --git a/ndk/src/relay/pool/index.ts b/ndk/src/relay/pool/index.ts index 92ef3a01..6911f580 100644 --- a/ndk/src/relay/pool/index.ts +++ b/ndk/src/relay/pool/index.ts @@ -23,6 +23,9 @@ export type NDKPoolStats = { * @emit relay:connect - Emitted when a relay in the pool connects. * @emit relay:ready - Emitted when a relay in the pool is ready to serve requests. * @emit relay:disconnect - Emitted when a relay in the pool disconnects. + * @emit relay:auth - Emitted when a relay requests authentication. + * @emit relay:authed - Emitted when a successfully authenticated to a relay. + * @emit relay:authfail - Emitted when a authentication to a relay has failed. */ export class NDKPool extends EventEmitter<{ notice: (relay: NDKRelay, notice: string) => void; @@ -40,6 +43,7 @@ export class NDKPool extends EventEmitter<{ "relay:disconnect": (relay: NDKRelay) => void; "relay:auth": (relay: NDKRelay, challenge: string) => void; "relay:authed": (relay: NDKRelay) => void; + "relay:authfail": (relay: NDKRelay) => void; }> { // TODO: This should probably be an LRU cache public relays = new Map(); @@ -159,6 +163,7 @@ export class NDKPool extends EventEmitter<{ const flappingHandler = () => this.handleFlapping(relay); const authHandler = (challenge: string) => this.emit("relay:auth", relay, challenge); const authedHandler = () => this.emit("relay:authed", relay); + const authFailHandler = () => this.emit("relay:authfail", relay); // make sure to remove the old handlers before adding new ones relay.off("notice", noticeHandler); @@ -168,6 +173,7 @@ export class NDKPool extends EventEmitter<{ relay.off("flapping", flappingHandler); relay.off("auth", authHandler); relay.off("authed", authedHandler); + relay.off("authfail", authFailHandler); // add the handlers relay.on("notice", noticeHandler); @@ -177,6 +183,7 @@ export class NDKPool extends EventEmitter<{ relay.on("flapping", flappingHandler); relay.on("auth", authHandler); relay.on("authed", authedHandler); + relay.on("authfail", authFailHandler); // Update the cache adapter with the new relay status relay.on("delayed-connect", (delay: number) => {