Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes: #246 - auth was not being emitted when using nip07 signer #248

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions ndk/src/relay/connectivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,16 @@ export class NDKRelayConnectivity {
this.debug("Authentication policy returned", !!res);

if (res instanceof NDKEvent) {
this.relay.auth(async (evt: EventTemplate): Promise<VerifiedEvent> => {
return res.rawEvent() as VerifiedEvent;
});
try {
await this.relay.auth(async (evt: EventTemplate): Promise<VerifiedEvent> => {
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) {
Expand Down
2 changes: 2 additions & 0 deletions ndk/src/relay/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<{
Expand All @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions ndk/src/relay/pool/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<WebSocket["url"], NDKRelay>();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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) => {
Expand Down