diff --git a/package.json b/package.json index d9f19c2..6b1e874 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "@lukelamey/kwil-js", - "version": "0.8.0", + "name": "@kwilteam/kwil-js", + "version": "0.7.1", "description": "", "main": "./dist/index.js", "types": "./dist/index.d.ts", diff --git a/src/api_client/client.ts b/src/api_client/client.ts index bad0055..a5620a8 100644 --- a/src/api_client/client.ts +++ b/src/api_client/client.ts @@ -245,7 +245,7 @@ export default class Client extends Api { // JsonRPCRequest to Determine mode (KGW or Private) const body = this.buildJsonRpcRequest(JSONRPCMethod.METHOD_HEALTH, {}); - const res = await super.post>(`rpc/v1`, body); + const res = await super.post>(`/rpc/v1`, body); return checkRes(res, (r) => r.result); } @@ -254,7 +254,7 @@ export default class Client extends Api { // JsonRPCRequest to generate a challenge const body = this.buildJsonRpcRequest(JSONRPCMethod.METHOD_CHALLENGE, {}); - const res = await super.post>(`rpc/v1`, body); + const res = await super.post>(`/rpc/v1`, body); return checkRes(res, (r) => r.result.challenge); } @@ -308,7 +308,7 @@ export default class Client extends Api { }, }); - const res = await super.post>(`rpc/v1`, body); + const res = await super.post>(`/rpc/v1`, body); const errorResponse = this.checkAuthError(res); if (errorResponse) { diff --git a/src/auth/auth.ts b/src/auth/auth.ts index 7e7b643..df22743 100644 --- a/src/auth/auth.ts +++ b/src/auth/auth.ts @@ -156,58 +156,6 @@ export class Auth { }; return res; } - // public async authenticatePrivateMode( - // signer: KwilSigner, - // actionBody: ActionBody - // ): Promise { - // // get Challenge - // const challenge = await this.authClient.challengeClient(); - // let msgChallenge = challenge.data as string; - - // // Check if challenge.data is undefined - // if (!msgChallenge) { - // throw new Error('Challenge data is undefined. Unable to authenticate in private mode.'); - // } - - // const actionValues = actionBody?.inputs ? Object.values(actionBody.inputs[0]) : []; - - // // create payload - // const payload: UnencodedActionPayload = { - // dbid: actionBody.dbid, - // action: actionBody.name, - // arguments: encodeSingleArguments(actionValues), - // }; - - // const encodedPayload = kwilEncode(payload); - // const base64Payload = bytesToBase64(encodedPayload); - - // // create the digest, which is the first bytes of the sha256 hash of the rlp-encoded payload - // const uInt8ArrayPayload = base64ToBytes(base64Payload); - // const digest = sha256BytesToBytes(uInt8ArrayPayload).subarray(0, 20); - // const msg = generateSignatureText( - // actionBody.dbid, - // actionBody.name, - // bytesToHex(digest), - // msgChallenge - // ); - - // const signature = await executeSign(stringToBytes(msg), signer.signer, signer.signatureType); - // const sig = bytesToBase64(signature); - - // const privateSignature: Signature = { - // sig: sig, - // type: signer.signatureType, - // }; - - // const byteChallenge = hexToBytes(msgChallenge); - // const base64Challenge = bytesToBase64(byteChallenge); // Challenge needs to be Base64 in the message - - // const res = { - // signature: privateSignature, - // challenge: base64Challenge, - // }; - // return res; - // } public async logoutKGW(signer?: KwilSigner): Promise>> { const identifier = signer?.identifier || undefined; diff --git a/src/client/node/nodeKwil.ts b/src/client/node/nodeKwil.ts index bf92f0b..7d7c080 100644 --- a/src/client/node/nodeKwil.ts +++ b/src/client/node/nodeKwil.ts @@ -51,29 +51,24 @@ export class NodeKwil extends Kwil { // Ensure auth mode is set await this.ensureAuthenticationMode(); - // Set Temporary Cookie - const tempCookie = this.setTemporaryCookie(actionBody); - + // kwil.call() if (this.authMode === AuthenticationMode.OPEN) { - // let response = await this.handlePublicMode(actionBody, kwilSigner); + const tempCookie = this.setTemporaryCookie(actionBody?.cookie) const message = await this.buildMessage(actionBody, kwilSigner); const response = await this.callClient(message); if (tempCookie) { - this.resetCookie(); + this.resetTempCookie(tempCookie) } - if (response.authCode === -901) { - // let kgwResponse = await this.handleKGW(actionBody, kwilSigner); - // return kgwResponse - await this.handleAuthenticateKGW(kwilSigner); + if (response.authCode === -901 && this.autoAuthenticate) { + await this.handleAuthenticateKGW(kwilSigner) return await this.callClient(message); } return response; } else if (this.authMode === AuthenticationMode.PRIVATE) { - return await this.handleAuthenticatePrivate(actionBody, kwilSigner); - // return privateResponse; + return await this.handleAuthenticatePrivate(actionBody, kwilSigner) } - throw new Error('Unexpected authentication mode or action body type.'); + throw new Error("Unexpected authentication mode or action body type."); } /** @@ -95,57 +90,22 @@ export class NodeKwil extends Kwil { * @returns the temporary cookie to handle for Node * @returns undefined if in PRIVATE mode */ - private setTemporaryCookie(actionBody: ActionBodyNode): string | undefined { - if (this.authMode === AuthenticationMode.OPEN && actionBody.cookie) { + private setTemporaryCookie(cookie?: string): string | undefined { + if (this.authMode === AuthenticationMode.OPEN && cookie) { const tempCookie = this.cookie; - this.cookie = actionBody.cookie; + this.cookie = cookie; return tempCookie; } return undefined; } - // private async handlePublicMode( - // actionBody: ActionBodyNode, - // kwilSigner?: KwilSigner - // ): Promise> { - // // 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> { - // // 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> { - // const privateResponse = await this.handleAuthenticatePrivate(actionBody, kwilSigner) - // return privateResponse; - // } - /** * Resets the temporary cookie * * @param tempCookie */ - private resetCookie(): void { - this.cookie = undefined; + private resetTempCookie(tempCookie: string): void { + this.cookie = tempCookie; } /** @@ -233,18 +193,20 @@ export class NodeKwil extends Kwil { * @returns */ - private async handleAuthenticateKGW(kwilSigner?: KwilSigner) { - if (this.autoAuthenticate) { - try { - // KGW AUTHENTICATION - if (!kwilSigner) { - throw new Error('KGW authentication requires a KwilSigner.'); - } - - await this.auth.authenticateKGW(kwilSigner); - } catch (error) { - console.error('Authentication failed:', error); + private async handleAuthenticateKGW( + kwilSigner?: KwilSigner + ) { + try { + // KGW AUTHENTICATION + if (!kwilSigner) { + throw new Error('KGW authentication requires a KwilSigner.'); } + + // set the cookie + const res = await this.auth.authenticateKGW(kwilSigner) + this.cookie = res.data?.cookie + } catch (error) { + throw new Error(`Authentication failed: ${error}`) } } @@ -280,75 +242,13 @@ export class NodeKwil extends Kwil { authPrivateModeRes.signature ); - const response = await this.callClient(message); - return response; + return await this.callClient(message); } } catch (error) { - console.error('Authentication failed:', error); - throw new Error('Authentication failed'); + throw new Error(`Authentication failed: ${error}`); } } - throw new Error('Authentication process did not complete successfully'); + 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 - * - * @param response - * @param message - * @param actionBody - * @param kwilSigner - * @returns - */ - // private async handleAuthentication( - // response: CallClientResponse, - // message: Message, - // actionBody: ActionBodyNode, - // kwilSigner?: KwilSigner - // ): Promise> { - // 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); - // } - // } - - // // PRIVATE MODE AUTHENTICATION - // 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 - // ); - // message = await this.buildMessage( - // actionBody, - // kwilSigner, - // authPrivateModeRes.challenge, - // authPrivateModeRes.signature - // ); - - // response = await this.callClient(message); - // } - // } catch (error) { - // console.error('Authentication failed:', error); - // } - // } - - // return response; - // } } diff --git a/src/client/web/webKwil.ts b/src/client/web/webKwil.ts index a12b6ad..a3549d3 100644 --- a/src/client/web/webKwil.ts +++ b/src/client/web/webKwil.ts @@ -64,24 +64,6 @@ export class WebKwil extends Kwil { } throw new Error("Unexpected authentication mode or action body type."); - - /** - * Call buildMessage() and callClient() if in Public Mode or running KGW in Public Mode - * buildMessage() and callClient() are called in handleAuthenticate() for Private Mode - */ - - // Build Message - // const message = await this.buildMessage(actionBody, kwilSigner); - - // kwil.call() - // let response = await this.callClient(message); - - // Handle Authentication if error - // if (this.authMode === AuthenticationMode.PRIVATE || response.authCode === -901) { - // response = await this.handleAuthentication(response, message, actionBody, kwilSigner); - // } - - // return response; } /** @@ -95,7 +77,7 @@ export class WebKwil extends Kwil { this.authMode = health.data?.mode; } } - + /** * Builds a message with a chainId, dbid, name, and description of the action. * NOT INCLUDED => challenge, sender, signature @@ -229,8 +211,7 @@ export class WebKwil extends Kwil { authPrivateModeRes.signature ); - const response = await this.callClient(message); - return response; + return await this.callClient(message); } } catch (error) { console.error('Authentication failed:', error); diff --git a/testing-functions/test.js b/testing-functions/test.js index 7a4e0ae..24547ae 100644 --- a/testing-functions/test.js +++ b/testing-functions/test.js @@ -70,7 +70,7 @@ async function test() { // testMultiLogout() - const dbid = kwil.getDBID(address, "sample") + const dbid = kwil.getDBID(address, "mydb") // await authenticate(kwil, kwilSigner) // broadcast(kwil, testDB, kwilSigner) // broadcastEd25519(kwil, testDB) @@ -108,7 +108,8 @@ async function test() { $date_string: "10-1-2024" } // executeGeneralAction(kwil, dbid, "create_post", kwilSigner, post) - executeGeneralView(kwil, dbid, "get_posts", {$user_id: 1,}, kwilSigner) + await executeGeneralView(kwil, dbid, "view_must_sign", null, kwilSigner) + await executeGeneralView(kwil, dbid, "view_must_sign", null, kwilSigner) } test()