Skip to content

Commit

Permalink
refactor(auth): change logout to logoutKGW
Browse files Browse the repository at this point in the history
To reflect the change between kwild private mode auth and KGW auth, the `auth.logout` method is now
`auth.logoutKGW`, as the JSON-RPC method it uses is only available on KGW.

BREAKING CHANGE: `auth.logout` is now `auth.logoutKGW`

Kwil.call refactor for public, kgw, private.

Signed-off-by: Ty D'Angelo <tydangelo18@gmail.com>

ci fix.

Signed-off-by: Ty D'Angelo <tydangelo18@gmail.com>

cleanup, remove unnecessary calls (#94)

Fix KGW double error bug in NodeKwil and fix CI to run in private and public mode.

Signed-off-by: Ty D'Angelo <tydangelo18@gmail.com>

CI fix and ActionInput bug fix.

Signed-off-by: Ty D'Angelo <tydangelo18@gmail.com>

CI fix.

Signed-off-by: Ty D'Angelo <tydangelo18@gmail.com>

CI package lock test.

Signed-off-by: Ty D'Angelo <tydangelo18@gmail.com>

CI hasFiles package lock json fix.

Signed-off-by: Ty D'Angelo <tydangelo18@gmail.com>
  • Loading branch information
KwilLuke committed Oct 10, 2024
1 parent 5a926e2 commit c5d89e4
Show file tree
Hide file tree
Showing 23 changed files with 940 additions and 616 deletions.
23 changes: 12 additions & 11 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
uses: actions/cache@v2
with:
path: ~/.npm
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
key: ${{ runner.os }}-node-${{ hashFiles('./package-lock.json') }}
restore-keys: |
${{ runner.os }}-node-
Expand All @@ -55,8 +55,8 @@ jobs:

- name: Set up KwilDB Docker in Public mode
run: |
git clone https://github.com/kwilteam/kwil-db.git /tmp/kwil-db
cd /tmp/kwil-db
git clone https://github.com/kwilteam/kwil-db.git /tmp/kwil-db-public
cd /tmp/kwil-db-public
go work init && go work use . ./test ./core
task build:docker
cd deployments/compose/kwil
Expand All @@ -79,16 +79,17 @@ jobs:
- name: Cleanup Docker
if: always()
run: |
docker compose -f /tmp/kwil-db/deployments/compose/kwil/docker-compose.yml down
docker compose -f /tmp/kwil-db-public/deployments/compose/kwil/docker-compose.yml down
docker system prune -af
- name: Set up KwilDB Docker in Private mode
run: |
cd /tmp/kwil-db
cd deployments/compose/kwil
echo "Starting kwil docker"
sed -i -e '/--autogen/a\
--app.private-rpc' docker-compose.yml
git clone https://github.com/kwilteam/kwil-db.git /tmp/kwil-db-private
cd /tmp/kwil-db-private
go work init && go work use . ./test ./core
task build:docker
cd -
cd .github/workflows
docker compose up -d
cd -
Expand All @@ -102,10 +103,10 @@ jobs:
chain_id=`curl -X POST http://localhost:8484/rpc/v1 -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "id": 1, "method": "user.chain_info", "params": {}}' | jq -r '.result.chain_id'`
echo "Chain ID: $chain_id"
echo "run KWIL-JS TEST in private mode against kwild(http:localhost:8484), with $chain_id"
PRIVATE_KEY=0000000000000000000000000000000000000000000000000000000000000001 CHAIN_ID=$chain_id GATEWAY_ON=false GAS_ON=false PRIVATE_MODE=true KWIL_PROVIDER=http://localhost:8484 npm run integration
PRIVATE_KEY=0000000000000000000000000000000000000000000000000000000000000001 CHAIN_ID=$chain_id GATEWAY_ON=FALSE GAS_ON=FALSE PRIVATE_MODE=TRUE KWIL_PROVIDER=http://localhost:8484 npm run integration
- name: Cleanup Docker
if: always()
run: |
docker compose -f /tmp/kwil-db/deployments/compose/kwil/docker-compose.yml down
docker compose -f .github/workflows/docker-compose.yml down
docker system prune -af
78 changes: 78 additions & 0 deletions .github/workflows/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
version: "3"

volumes:
pgkwil:
driver: local

services:
pg:
container_name: postgres-kwild-single
image: kwildb/postgres:16.4-1
ports:
- "15432:5432"
restart: always
user: postgres
environment:
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_INITDB_ARGS: --data-checksums
# Setting user/pass/db works, but we use docker-entrypoint-initdb.d instead for more control
# POSTGRES_USER: kwild
# POSTGRES_PASSWORD: kwild
# POSTGRES_DB: kwild
volumes:
- pgkwil:/var/lib/postgresql/data
networks:
kwilnet0:
ipv4_address: 172.5.100.3
healthcheck:
test: ["CMD-SHELL", "pg_isready"]
interval: 2s
timeout: 6s
retries: 10

kwild:
container_name: kwild-single
image: kwild:latest
build:
context: ../../../
dockerfile: ./build/package/docker/kwild.dockerfile
ports:
- "8484:8484"
- "26656:26656"
- "26657:26657"
environment:
- LOG=${LOG:-cometbft.log}
- KWILD_HOME=/app/.kwild
volumes:
- ./testnode/:/app/.kwild/
depends_on:
pg:
condition: service_healthy
networks:
kwilnet0:
ipv4_address: 172.5.100.2
command: |
--autogen
--root-dir=/app/.kwild
--app.private-rpc
--log.level=debug
--app.admin-listen-addr=/tmp/admin.socket
--chain.p2p.external-address=tcp://0.0.0.0:26656
--chain.rpc.listen-addr=tcp://0.0.0.0:26657
--app.pg-db-host=172.5.100.3
--app.pg-db-port=5432
--app.pg-db-user=kwild
--app.pg-db-pass=kwild
healthcheck:
test: ["CMD", "curl", "--fail-with-body", "-s", "http://127.0.0.1:8484/api/v1/health/user"]
interval: 2s
timeout: 6s
retries: 10

networks:
kwilnet0:
driver: bridge
ipam:
driver: default
config:
- subnet: 172.5.100.0/23
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ yarn.lock
/pkg
testapp/node_modules
/.husky
.DS_Store
.DS_Store
.npmrc
2 changes: 1 addition & 1 deletion MIGRATING.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const res = await kwil.getAccount('account_identifier');

/*
res.data = {
identifier: Array(),
identifier: Uint8Array(),
balance: '###',
nonce: '###'
}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kwilteam/kwil-js",
"version": "0.7.1",
"name": "@lukelamey/kwil-js",
"version": "0.8.0",
"description": "",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
Expand Down
80 changes: 73 additions & 7 deletions src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { objects } from '../utils/objects';
import { AuthBody, executeSign, Signature } from '../core/signature';
import { bytesToHex, hexToBytes, stringToBytes } from '../utils/serial';
import { base64ToBytes, bytesToBase64 } from '../utils/base64';
import { ActionBody } from '../core/action';
import { ActionBody, ActionInput } from '../core/action';
import { sha256BytesToBytes } from '../utils/crypto';
import { encodeArguments, kwilEncode } from '../utils/rlp';
import { encodeSingleArguments, kwilEncode } from '../utils/rlp';
import { UnencodedActionPayload } from '../core/payload';

interface AuthClient {
Expand Down Expand Up @@ -92,9 +92,10 @@ export class Auth<T extends EnvironmentType> {
* @param {ActionBody} actionBody - The body of the action to send. This should use the `ActionBody` interface.
* @returns A promise that resolves a privateSignature => privateSignature = {sig: string (Base64), type: AnySignatureType}
*/

public async authenticatePrivateMode(
signer: KwilSigner,
actionBody: ActionBody
actionBody: ActionBody,
signer: KwilSigner
): Promise<AuthBody> {
// get Challenge
const challenge = await this.authClient.challengeClient();
Expand All @@ -105,11 +106,24 @@ export class Auth<T extends EnvironmentType> {
throw new Error('Challenge data is undefined. Unable to authenticate in private mode.');
}

// Check if multiple inputs were provided
if (actionBody.inputs && actionBody.inputs.length > 1) {
throw new Error('Only one input is allowed for call requests. Please pass only one input and try again.');
}

// handle if the inputs are an array of ActionInput objects or an array of Entries objects
const cleanActionValues = actionBody?.inputs ?
actionBody.inputs.map((input) => {
return input instanceof ActionInput ? input.toEntries() : input;
}) : [];

const actionValues = actionBody?.inputs ? Object.values(cleanActionValues[0]) : [];

// create payload
const payload: UnencodedActionPayload<PayloadType.CALL_ACTION> = {
dbid: actionBody.dbid,
action: actionBody.name,
arguments: encodeArguments(actionBody),
arguments: encodeSingleArguments(actionValues),
};

const encodedPayload = kwilEncode(payload);
Expand Down Expand Up @@ -142,8 +156,60 @@ export class Auth<T extends EnvironmentType> {
};
return res;
}

public async logout(signer?: KwilSigner): Promise<GenericResponse<LogoutResponse<T>>> {
// public async authenticatePrivateMode(
// signer: KwilSigner,
// actionBody: ActionBody
// ): Promise<AuthBody> {
// // 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<PayloadType.CALL_ACTION> = {
// 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<BytesEncodingStatus.BASE64_ENCODED> = {
// 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<GenericResponse<LogoutResponse<T>>> {
const identifier = signer?.identifier || undefined;
return await this.authClient.logoutClient(identifier);
}
Expand Down
4 changes: 2 additions & 2 deletions src/builders/action_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { BytesEncodingStatus, EnvironmentType, PayloadType, ValueType } from '..
import { AnySignatureType, Signature, SignatureType, getSignatureType } from '../core/signature';
import { EncodedValue, UnencodedActionPayload } from '../core/payload';
import { Message } from '../core/message';
import { constructEncodedValues } from '../utils/rlp';
import { encodeNestedArguments } from '../utils/rlp';

interface CheckSchema {
dbid: string;
Expand Down Expand Up @@ -576,7 +576,7 @@ export class ActionBuilderImpl<T extends EnvironmentType> implements ActionBuild
}

// return this.constructEncodedValues(preparedActions);
return constructEncodedValues(preparedActions);
return encodeNestedArguments(preparedActions);
}

private assertNotBuilding(): void {
Expand Down
Loading

0 comments on commit c5d89e4

Please sign in to comment.