Skip to content

feat: add new schemas package using zod, bring acc validation package… #801

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

Merged
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
1 change: 0 additions & 1 deletion local-tests/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { fileURLToPath } from 'url';

const ALLOW_LIST = [
'ethers',
'@lit-protocol/accs-schemas',
'@lit-protocol/contracts',
'crypto',
'secp256k1',
Expand Down
1 change: 0 additions & 1 deletion local-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"@cypress/code-coverage": "^3.10.0",
"@cypress/react": "^6.2.0",
"@cypress/webpack-dev-server": "^2.3.0",
"@lit-protocol/accs-schemas": "0.0.7",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NICE!

"@metamask/eth-sig-util": "5.0.2",
"@mysten/sui.js": "^0.37.1",
"@playwright/test": "^1.25.2",
Expand Down
14 changes: 7 additions & 7 deletions local-tests/setup/accs/accs.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
LPACC_EVM_ATOM,
LPACC_EVM_BASIC,
LPACC_SOL,
} from '@lit-protocol/accs-schemas';
AtomAcc,
EvmBasicAcc,
SolAcc,
} from '@lit-protocol/access-control-conditions-schemas';

export namespace AccessControlConditions {
export const getEvmBasicAccessControlConditions = ({
userAddress,
}): LPACC_EVM_BASIC[] => {
}): EvmBasicAcc[] => {
return [
{
contractAddress: '',
Expand All @@ -25,7 +25,7 @@ export namespace AccessControlConditions {

export const getSolBasicAccessControlConditions = ({
userAddress,
}): LPACC_SOL[] => {
}): SolAcc[] => {
return [
{
method: '',
Expand All @@ -45,7 +45,7 @@ export namespace AccessControlConditions {

export const getCosmosBasicAccessControlConditions = ({
userAddress,
}): LPACC_EVM_ATOM[] => {
}): AtomAcc[] => {
return [
{
conditionType: 'cosmos',
Expand Down
8 changes: 4 additions & 4 deletions local-tests/tests/wrapped-keys/util.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LIT_NETWORKS_KEYS } from '@lit-protocol/types';
import { LIT_CHAINS } from '@lit-protocol/constants';
import { LIT_CHAINS, LIT_NETWORK } from '@lit-protocol/constants';
import { ethers } from 'ethers';
import { config } from '@lit-protocol/wrapped-keys';
import {
Expand Down Expand Up @@ -51,7 +51,7 @@ export function getChainForNetwork(network: LIT_NETWORKS_KEYS): {
chainId: number;
} {
switch (network) {
case 'naga-dev':
case LIT_NETWORK.NagaDev:
return {
chain: 'yellowstone',
chainId: LIT_CHAINS['yellowstone'].chainId,
Expand All @@ -66,9 +66,9 @@ export function getGasParamsForNetwork(network: LIT_NETWORKS_KEYS): {
gasLimit: number;
} {
switch (network) {
case 'naga-dev':
case LIT_NETWORK.NagaDev:
return { gasLimit: 5000000 };
case 'custom':
case LIT_NETWORK.Custom:
return { gasLimit: 5000000 };
default:
throw new Error(`Cannot identify chain params for ${network}`);
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
"@cosmjs/proto-signing": "0.30.1",
"@cosmjs/stargate": "0.30.1",
"@dotenvx/dotenvx": "^1.6.4",
"@lit-protocol/accs-schemas": "^0.0.22",
"@lit-protocol/contracts": "^0.0.86",
"@metamask/eth-sig-util": "5.0.2",
"@mysten/sui.js": "^0.37.1",
Expand All @@ -53,7 +52,6 @@
"@walletconnect/utils": "2.9.2",
"@walletconnect/web3wallet": "1.8.8",
"abitype": "^1.0.8",
"ajv": "^8.12.0",
"base64url": "^3.0.1",
"bech32": "^2.0.0",
"cbor-web": "^9.0.2",
Expand All @@ -70,7 +68,9 @@
"tslib": "^2.7.0",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.1",
"uint8arrays": "^4.0.3"
"uint8arrays": "^4.0.3",
"zod": "^3.24.2",
"zod-validation-error": "^3.4.0"
},
"devDependencies": {
"@nx/devkit": "17.3.0",
Expand Down
25 changes: 25 additions & 0 deletions packages/access-control-conditions-schemas/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.json"],
"parser": "jsonc-eslint-parser",
"rules": {
"@nx/dependency-checks": "error"
}
}
]
}
22 changes: 22 additions & 0 deletions packages/access-control-conditions-schemas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Lit Protocol Access Control Condition definitions

The Lit Protocol Access Control Condition definitions are a set of Zod schemas that define the conditions that can be used to control access to a resource.

## Condition definition

A condition definition is a Zod schema that defines a condition that can be used to control access to a resource. More information can be found in the [Lit Protocol Access Control Condition definition specification](https://developer.litprotocol.com/v3/sdk/access-control/condition-types/unified-access-control-conditions)

## Condition types

The following condition types are defined:

- EVM Basic Conditions
- EVM Custom Contract Conditions
- Solana RPC Conditions
- Cosmos or Kyve Conditions

Each has its own Zod schema that defines their properties.

## Derived types

Each condition type has a derived type that can be used to type the condition object when using Typescript.
17 changes: 17 additions & 0 deletions packages/access-control-conditions-schemas/jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* eslint-disable */
export default {
displayName: 'access-control-conditions-schemas',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[t]s$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'js', 'html'],
coverageDirectory:
'../../coverage/packages/access-control-conditions-schemas',
setupFilesAfterEnv: ['../../jest.setup.js'],
};
26 changes: 26 additions & 0 deletions packages/access-control-conditions-schemas/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "@lit-protocol/access-control-conditions-schemas",
"license": "MIT",
"homepage": "https://github.com/Lit-Protocol/js-sdk",
"repository": {
"type": "git",
"url": "https://github.com/LIT-Protocol/js-sdk"
},
"keywords": [
"library"
],
"bugs": {
"url": "https://github.com/LIT-Protocol/js-sdk/issues"
},
"publishConfig": {
"access": "public",
"directory": "../../dist/packages/access-control-conditions-schemas"
},
"gitHead": "0d7334c2c55f448e91fe32f29edc5db8f5e09e4b",
"tags": [
"universal"
],
"version": "8.0.0-alpha.0",
"main": "./dist/src/index.js",
"typings": "./dist/src/index.d.ts"
}
56 changes: 56 additions & 0 deletions packages/access-control-conditions-schemas/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "access-control-conditions-schemas",
"$schema": "../../node_modules/nx/access-control-conditions-schemas/project-schema.json",
"sourceRoot": "packages/access-control-conditions-schemas/src",
"projectType": "library",
"targets": {
"build": {
"executor": "@nx/js:tsc",
"outputs": ["{options.outputPath}"],
"options": {
"outputPath": "dist/packages/access-control-conditions-schemas",
"main": "packages/access-control-conditions-schemas/src/index.ts",
"tsConfig": "packages/access-control-conditions-schemas/tsconfig.lib.json",
"assets": ["packages/access-control-conditions-schemas/*.md"],
"updateBuildableProjectDepsInPackageJson": true
}
},
"generateDoc": {
"executor": "nx:run-commands",
"options": {
"command": "yarn typedoc --entryPointStrategy expand packages/access-control-conditions-schemas/src --exclude packages/access-control-conditions-schemas/src/**/*.spec.** --tsconfig packages/access-control-conditions-schemas/tsconfig.json"
}
},
"lint": {
"executor": "@nx/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": [
"packages/access-control-conditions-schemas/**/*.ts"
]
}
},
"testPackage": {
"executor": "@nx/jest:jest",
"outputs": [
"{workspaceRoot}/coverage/packages/access-control-conditions-schemas"
],
"options": {
"jestConfig": "packages/access-control-conditions-schemas/jest.config.ts",
"passWithNoTests": true
}
},
"testWatch": {
"executor": "@nx/jest:jest",
"outputs": [
"{workspaceRoot}/coverage/packages/access-control-conditions-schemas"
],
"options": {
"jestConfig": "packages/access-control-conditions-schemas/jest.config.ts",
"passWithNoTests": true,
"watch": true
}
}
},
"tags": []
}
6 changes: 6 additions & 0 deletions packages/access-control-conditions-schemas/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from './lib/access-control-conditions';
export * from './lib/AtomAcc';
export * from './lib/EvmBasicAcc';
export * from './lib/EvmContractAcc';
export * from './lib/OperatorAcc';
export * from './lib/SolAcc';
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { AtomAccSchema } from './AtomAcc';

describe('AtomAccSchema', () => {
const validAtomAcc = {
conditionType: 'cosmos',
path: '/cosmos/bank/v1beta1/balances/:userAddress',
chain: 'cosmos',
returnValueTest: {
key: '$.balances[0].amount',
comparator: '>=',
value: '1000000',
},
};

it('should validate a valid Atom access control condition', () => {
expect(AtomAccSchema.safeParse(validAtomAcc).success).toBeTruthy();
});

it('should not validate an invalid Atom access control condition', () => {
expect(
AtomAccSchema.safeParse({
...validAtomAcc,
path: undefined,
}).success
).toBeFalsy();

expect(
AtomAccSchema.safeParse({
...validAtomAcc,
chain: 'invalidChain',
}).success
).toBeFalsy();

expect(
AtomAccSchema.safeParse({
...validAtomAcc,
returnValueTest: undefined,
}).success
).toBeFalsy();
});
});
16 changes: 16 additions & 0 deletions packages/access-control-conditions-schemas/src/lib/AtomAcc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { z } from 'zod';

import { ChainEnumAtom, ReturnValueTestSchema } from './common';

export const AtomAccSchema = z
.object({
conditionType: z.literal('cosmos').optional(),
path: z.string(),
chain: ChainEnumAtom,
method: z.string().optional(),
parameters: z.array(z.string()).optional(),
returnValueTest: ReturnValueTestSchema,
})
.strict();

export type AtomAcc = z.infer<typeof AtomAccSchema>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { EvmBasicAccSchema } from './EvmBasicAcc';

describe('EvmBasicAccSchema', () => {
const validEVMBasicAcc = {
conditionType: 'evmBasic',
contractAddress: '0x50D8EB685a9F262B13F28958aBc9670F06F819d9',
standardContractType: 'MolochDAOv2.1',
chain: 'ethereum',
method: 'members',
parameters: [':userAddress'],
returnValueTest: {
comparator: '=',
value: 'true',
},
};

it('should validate a valid EVM basic access control condition', () => {
expect(EvmBasicAccSchema.safeParse(validEVMBasicAcc).success).toBeTruthy();
expect(
EvmBasicAccSchema.safeParse({
...validEVMBasicAcc,
conditionType: undefined,
}).success
).toBeTruthy();
});

it('should not validate an invalid EVM basic access control conditions', () => {
expect(
EvmBasicAccSchema.safeParse({
...validEVMBasicAcc,
chain: 'invalidChain',
}).success
).toBeFalsy();

expect(
EvmBasicAccSchema.safeParse({
...validEVMBasicAcc,
chain: undefined,
}).success
).toBeFalsy();

expect(
EvmBasicAccSchema.safeParse({
...validEVMBasicAcc,
chain: 1,
}).success
).toBeFalsy();

expect(
EvmBasicAccSchema.safeParse({
...validEVMBasicAcc,
method: undefined,
}).success
).toBeFalsy();

expect(
EvmBasicAccSchema.safeParse({
...validEVMBasicAcc,
contractAddress: undefined,
}).success
).toBeFalsy();

expect(
EvmBasicAccSchema.safeParse({
...validEVMBasicAcc,
standardContractType: undefined,
}).success
).toBeFalsy();
});
});
Loading
Loading