Skip to content

Commit

Permalink
fix(kwil): separate authentication errors from call errors
Browse files Browse the repository at this point in the history
There was a minor bug where, when a call failed in private mode, the call error was thrown as part
of the authentication error stack, regardless of whether the call failure was related to
authentication. This commit separates the logic so that only authentication errors are thrown in the
authentication stack.
  • Loading branch information
KwilLuke committed Oct 16, 2024
1 parent d7c7bb7 commit a43ca86
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export class Auth<T extends EnvironmentType> {

// Check if challenge.data is undefined
if (!msgChallenge) {
throw new Error('Challenge data is undefined. Unable to authenticate in private mode.');
throw new Error('Challenge data is undefined. Something went wrong.');
}

// Check if multiple inputs were provided
Expand Down
27 changes: 13 additions & 14 deletions src/client/node/nodeKwil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { ActionInput, Entries, ActionBodyNode } from '../../core/action';
import { ActionBuilder } from '../../core/builders';
import { AuthenticationMode, BytesEncodingStatus, EnvironmentType } from '../../core/enums';
import { KwilSigner } from '../../core/kwilSigner';
import { BaseMessage, CallClientResponse, Message, MsgReceipt } from '../../core/message';
import { BaseMessage, Message, MsgReceipt } from '../../core/message';
import { GenericResponse } from '../../core/resreq';
import { Kwil } from '../kwil';
import { Signature } from '../../core/signature';
import { AuthBody, Signature } from '../../core/signature';

export class NodeKwil extends Kwil<EnvironmentType.NODE> {
private autoAuthenticate: boolean;
Expand Down Expand Up @@ -65,10 +65,17 @@ export class NodeKwil extends Kwil<EnvironmentType.NODE> {
}
return response;
} else if (this.authMode === AuthenticationMode.PRIVATE) {
return await this.handleAuthenticatePrivate(actionBody, kwilSigner)
const authBody = await this.handleAuthenticatePrivate(actionBody, kwilSigner)
const message = await this.buildMessage(
actionBody,
kwilSigner,
authBody.challenge,
authBody.signature
);
return await this.callClient(message);
}

throw new Error("Unexpected authentication mode or action body type.");
throw new Error("Unexpected authentication mode. If you hit this error, please report it to the Kwil team.");
}

/**
Expand Down Expand Up @@ -222,7 +229,7 @@ export class NodeKwil extends Kwil<EnvironmentType.NODE> {
private async handleAuthenticatePrivate(
actionBody: ActionBodyNode,
kwilSigner?: KwilSigner
): Promise<CallClientResponse<MsgReceipt>> {
): Promise<AuthBody> {
if (this.autoAuthenticate) {
try {
// PRIVATE MODE AUTHENTICATION
Expand All @@ -231,18 +238,10 @@ export class NodeKwil extends Kwil<EnvironmentType.NODE> {
throw new Error('Private mode authentication requires a KwilSigner.');
}

const authPrivateModeRes = await this.auth.authenticatePrivateMode(
return await this.auth.authenticatePrivateMode(
actionBody,
kwilSigner
);
const message = await this.buildMessage(
actionBody,
kwilSigner,
authPrivateModeRes.challenge,
authPrivateModeRes.signature
);

return await this.callClient(message);
}
} catch (error) {
throw new Error(`Authentication failed: ${error}`);
Expand Down
32 changes: 15 additions & 17 deletions src/client/web/webKwil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { ActionBody, ActionBodyNode, ActionInput, Entries } from '../../core/act
import { ActionBuilder } from '../../core/builders';
import { AuthenticationMode, BytesEncodingStatus, EnvironmentType } from '../../core/enums';
import { KwilSigner } from '../../core/kwilSigner';
import { BaseMessage, CallClientResponse, Message, MsgReceipt } from '../../core/message';
import { BaseMessage, Message, MsgReceipt } from '../../core/message';
import { GenericResponse } from '../../core/resreq';
import { Signature } from '../../core/signature';
import { AuthBody, Signature } from '../../core/signature';
import { Kwil } from '../kwil';

export class WebKwil extends Kwil<EnvironmentType.BROWSER> {
Expand Down Expand Up @@ -60,10 +60,17 @@ export class WebKwil extends Kwil<EnvironmentType.BROWSER> {
}
return response;
} else if (this.authMode === AuthenticationMode.PRIVATE) {
return await this.handleAuthenticatePrivate(actionBody, kwilSigner);
const authBody = await this.handleAuthenticatePrivate(actionBody, kwilSigner);
const message = await this.buildMessage(
actionBody,
kwilSigner,
authBody.challenge,
authBody.signature
);
return await this.callClient(message);
}

throw new Error("Unexpected authentication mode or action body type.");
throw new Error("Unexpected authentication mode. If you hit this error, please report it to the Kwil team.");
}

/**
Expand Down Expand Up @@ -174,7 +181,7 @@ export class WebKwil extends Kwil<EnvironmentType.BROWSER> {
}
await this.auth.authenticateKGW(kwilSigner);
} catch (error) {
console.error('Authentication failed:', error);
throw new Error(`Authentication failed: ${error}`)
}
}
}
Expand All @@ -191,7 +198,7 @@ export class WebKwil extends Kwil<EnvironmentType.BROWSER> {
private async handleAuthenticatePrivate(
actionBody: ActionBodyNode,
kwilSigner?: KwilSigner
): Promise<CallClientResponse<MsgReceipt>> {
): Promise<AuthBody> {
if (this.autoAuthenticate) {
try {
// PRIVATE MODE AUTHENTICATION
Expand All @@ -200,22 +207,13 @@ export class WebKwil extends Kwil<EnvironmentType.BROWSER> {
throw new Error('Private mode authentication requires a KwilSigner.');
}

const authPrivateModeRes = await this.auth.authenticatePrivateMode(
return await this.auth.authenticatePrivateMode(
actionBody,
kwilSigner
);
const message = await this.buildMessage(
actionBody,
kwilSigner,
authPrivateModeRes.challenge,
authPrivateModeRes.signature
);

return await this.callClient(message);
}
} catch (error) {
console.error('Authentication failed:', error);
throw new Error('Authentication failed')
throw new Error(`Authentication failed: ${error}`)
}
}

Expand Down

0 comments on commit a43ca86

Please sign in to comment.