Skip to content

Commit

Permalink
Kwil.call refactor for public, kgw, private.
Browse files Browse the repository at this point in the history
Signed-off-by: Ty D'Angelo <tydangelo18@gmail.com>
  • Loading branch information
tydangelo18 committed Oct 8, 2024
1 parent 87ecccc commit 5bc4415
Show file tree
Hide file tree
Showing 5 changed files with 663 additions and 359 deletions.
168 changes: 154 additions & 14 deletions src/client/node/nodeKwil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,38 @@ export class NodeKwil extends Kwil<EnvironmentType.NODE> {
// Set Temporary Cookie
const tempCookie = this.setTemporaryCookie(actionBody);

// Build Message
const message = await this.buildMessage(actionBody, kwilSigner);

// kwil.call()
let response = await this.callClient(message);
// // Build Message
// const message = await this.buildMessage(actionBody, kwilSigner);
// // kwil.call()
// let response = await this.callClient(message);
if (this.authMode === AuthenticationMode.OPEN) {
let response = await this.handlePublicMode(actionBody, kwilSigner);
if (tempCookie) {
this.resetCookie();
}
if (response.authCode === -901) {
let kgwResponse = await this.handleKGW(actionBody, kwilSigner);
return kgwResponse
}
return response;
} else if (this.authMode === AuthenticationMode.PRIVATE) {
let privateResponse = await this.handlePrivateMode(actionBody, kwilSigner)
return privateResponse;
}

// Reset Cookie if in PUBLIC mode
if (this.authMode === AuthenticationMode.OPEN && tempCookie) {
this.resetCookie();
}
// if (this.authMode === AuthenticationMode.OPEN && tempCookie) {
// this.resetCookie();
// }

// Handle Authentication if error
response = await this.handleAuthentication(response, message, actionBody, kwilSigner);
// if (this.authMode === AuthenticationMode.PRIVATE || response.authCode === -901) {
// response = await this.handleAuthentication(response, message, actionBody, kwilSigner);
// }
// Add a default return statement to handle unexpected cases
throw new Error("Unexpected authentication mode or action body type.");


return response;
}

/**
Expand Down Expand Up @@ -99,6 +116,41 @@ export class NodeKwil extends Kwil<EnvironmentType.NODE> {
return undefined;
}

private async handlePublicMode(
actionBody: ActionBodyNode,
kwilSigner?: KwilSigner
): Promise<CallClientResponse<MsgReceipt>> {
// Build Message
const message = await this.buildMessage(actionBody, kwilSigner);
// kwil.call()
const response = await this.callClient(message);

return response;
}

private async handleKGW(
actionBody: ActionBodyNode,
kwilSigner?: KwilSigner
): Promise<CallClientResponse<MsgReceipt>> {
// Build Message
const message = await this.buildMessage(actionBody, kwilSigner);
// kwil.call()
const response = await this.callClient(message);

// authenticate kgw on failure
const kgwResponse = await this.handleAuthenticateKGW(response, message, kwilSigner);

return kgwResponse;
}

private async handlePrivateMode(
actionBody: ActionBodyNode,
kwilSigner?: KwilSigner
): Promise<CallClientResponse<MsgReceipt>> {
const privateResponse = await this.handleAuthenticatePrivate(actionBody, kwilSigner)
return privateResponse;
}

/**
* Resets the temporary cookie
*
Expand Down Expand Up @@ -183,6 +235,88 @@ export class NodeKwil extends Kwil<EnvironmentType.NODE> {
.publicKey(kwilSigner.identifier);
}

/**
* Checks authentication errors for PUBLIC (KGW)
* Signs message and then retries request for successful response
*
* @param response
* @param message
* @param kwilSigner
* @returns
*/

private async handleAuthenticateKGW(
response: CallClientResponse<MsgReceipt>,
message: Message,
kwilSigner?: KwilSigner
): Promise<CallClientResponse<MsgReceipt>> {
if (this.autoAuthenticate) {
try {
// KGW AUTHENTICATION
if (response.authCode === -901) {
if (!kwilSigner) {
throw new Error('KGW authentication requires a KwilSigner.');
}

const authRes = await this.auth.authenticateKGW(kwilSigner);
if (authRes.status === 200 && this.authMode === AuthenticationMode.OPEN) {
// set the cookie
this.cookie = authRes.data?.cookie;
// call the message again
response = await this.callClient(message);
}
}
} catch (error) {
console.error('Authentication failed:', error);
}
}
return response;
}

/**
* Checks authentication errors for PRIVATE mode
* Signs message and then retries request for successful response
*
* @param actionBody
* @param kwilSigner
* @returns
*/

private async handleAuthenticatePrivate(
actionBody: ActionBodyNode,
kwilSigner?: KwilSigner
): Promise<CallClientResponse<MsgReceipt>> {
if (this.autoAuthenticate) {
try {
// PRIVATE MODE AUTHENTICATION
if (this.authMode === AuthenticationMode.PRIVATE) {
if (!kwilSigner) {
throw new Error('Private mode authentication requires a KwilSigner.');
}

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

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

throw new Error('Authentication process did not complete successfully')
}

/**
* Checks authentication errors for either PUBLIC (KGW) or PRIVATE mode
* Signs message and then retries request for successful response
Expand All @@ -199,10 +333,14 @@ export class NodeKwil extends Kwil<EnvironmentType.NODE> {
actionBody: ActionBodyNode,
kwilSigner?: KwilSigner
): Promise<CallClientResponse<MsgReceipt>> {
if (this.autoAuthenticate && kwilSigner) {
if (this.autoAuthenticate) {
try {
// KGW AUTHENTICATION
if (response.authCode === -901) {
if (!kwilSigner) {
throw new Error('KGW authentication requires a KwilSigner.');
}

const authRes = await this.auth.authenticateKGW(kwilSigner);
if (authRes.status === 200 && this.authMode === AuthenticationMode.OPEN) {
// set the cookie
Expand All @@ -213,7 +351,11 @@ export class NodeKwil extends Kwil<EnvironmentType.NODE> {
}

// PRIVATE MODE AUTHENTICATION
if (response.authCode === -1001 && message.body.payload) {
if (this.authMode === AuthenticationMode.PRIVATE && message.body.payload) {
if (!kwilSigner) {
throw new Error('Private mode authentication requires a KwilSigner.');
}

const authPrivateModeRes = await this.auth.authenticatePrivateMode(
kwilSigner,
actionBody
Expand All @@ -230,8 +372,6 @@ export class NodeKwil extends Kwil<EnvironmentType.NODE> {
} catch (error) {
console.error('Authentication failed:', error);
}
} else if (!kwilSigner) {
throw new Error('Action requires authentication. Pass a KwilSigner to authenticate');
}

return response;
Expand Down
Loading

0 comments on commit 5bc4415

Please sign in to comment.