Skip to content

Commit c9bf970

Browse files
committed
feat: add access control conditions schemas package with zod
1 parent 6f29395 commit c9bf970

32 files changed

+1139
-77
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"extends": ["../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"overrides": [
5+
{
6+
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
7+
"rules": {}
8+
},
9+
{
10+
"files": ["*.ts", "*.tsx"],
11+
"rules": {}
12+
},
13+
{
14+
"files": ["*.js", "*.jsx"],
15+
"rules": {}
16+
},
17+
{
18+
"files": ["*.json"],
19+
"parser": "jsonc-eslint-parser",
20+
"rules": {
21+
"@nx/dependency-checks": "error"
22+
}
23+
}
24+
]
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Lit Protocol Access Control Condition definitions
2+
3+
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.
4+
5+
## Condition definition
6+
7+
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)
8+
9+
## Condition types
10+
11+
The following condition types are defined:
12+
13+
- EVM Basic Conditions
14+
- EVM Custom Contract Conditions
15+
- Solana RPC Conditions
16+
- Cosmos or Kyve Conditions
17+
18+
Each has its own Zod schema that defines their properties.
19+
20+
## Derived types
21+
22+
Each condition type has a derived type that can be used to type the condition object when using Typescript.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable */
2+
export default {
3+
displayName: 'access-control-conditions-schemas',
4+
preset: '../../jest.preset.js',
5+
globals: {
6+
'ts-jest': {
7+
tsconfig: '<rootDir>/tsconfig.spec.json',
8+
},
9+
},
10+
transform: {
11+
'^.+\\.[t]s$': 'ts-jest',
12+
},
13+
moduleFileExtensions: ['ts', 'js', 'html'],
14+
coverageDirectory:
15+
'../../coverage/packages/access-control-conditions-schemas',
16+
setupFilesAfterEnv: ['../../jest.setup.js'],
17+
};
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "@lit-protocol/access-control-conditions-schemas",
3+
"license": "MIT",
4+
"homepage": "https://github.com/Lit-Protocol/js-sdk",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/LIT-Protocol/js-sdk"
8+
},
9+
"keywords": [
10+
"library"
11+
],
12+
"bugs": {
13+
"url": "https://github.com/LIT-Protocol/js-sdk/issues"
14+
},
15+
"publishConfig": {
16+
"access": "public",
17+
"directory": "../../dist/packages/access-control-conditions-schemas"
18+
},
19+
"gitHead": "0d7334c2c55f448e91fe32f29edc5db8f5e09e4b",
20+
"tags": [
21+
"universal"
22+
],
23+
"version": "7.0.0-alpha.8",
24+
"main": "./dist/src/index.js",
25+
"typings": "./dist/src/index.d.ts"
26+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"name": "access-control-conditions-schemas",
3+
"$schema": "../../node_modules/nx/access-control-conditions-schemas/project-schema.json",
4+
"sourceRoot": "packages/access-control-conditions-schemas/src",
5+
"projectType": "library",
6+
"targets": {
7+
"build": {
8+
"executor": "@nx/js:tsc",
9+
"outputs": ["{options.outputPath}"],
10+
"options": {
11+
"outputPath": "dist/packages/access-control-conditions-schemas",
12+
"main": "packages/access-control-conditions-schemas/src/index.ts",
13+
"tsConfig": "packages/access-control-conditions-schemas/tsconfig.lib.json",
14+
"assets": ["packages/access-control-conditions-schemas/*.md"],
15+
"updateBuildableProjectDepsInPackageJson": true
16+
}
17+
},
18+
"generateDoc": {
19+
"executor": "nx:run-commands",
20+
"options": {
21+
"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"
22+
}
23+
},
24+
"lint": {
25+
"executor": "@nx/linter:eslint",
26+
"outputs": ["{options.outputFile}"],
27+
"options": {
28+
"lintFilePatterns": [
29+
"packages/access-control-conditions-schemas/**/*.ts"
30+
]
31+
}
32+
},
33+
"testPackage": {
34+
"executor": "@nx/jest:jest",
35+
"outputs": [
36+
"{workspaceRoot}/coverage/packages/access-control-conditions-schemas"
37+
],
38+
"options": {
39+
"jestConfig": "packages/access-control-conditions-schemas/jest.config.ts",
40+
"passWithNoTests": true
41+
}
42+
},
43+
"testWatch": {
44+
"executor": "@nx/jest:jest",
45+
"outputs": [
46+
"{workspaceRoot}/coverage/packages/access-control-conditions-schemas"
47+
],
48+
"options": {
49+
"jestConfig": "packages/access-control-conditions-schemas/jest.config.ts",
50+
"passWithNoTests": true,
51+
"watch": true
52+
}
53+
}
54+
},
55+
"tags": []
56+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export * from './lib/access-control-conditions';
2+
export * from './lib/AtomAcc';
3+
export * from './lib/EvmBasicAcc';
4+
export * from './lib/EvmContractAcc';
5+
export * from './lib/OperatorAcc';
6+
export * from './lib/SolAcc';
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { AtomAccSchema } from './AtomAcc';
2+
3+
describe('AtomAccSchema', () => {
4+
const validAtomAcc = {
5+
conditionType: 'cosmos',
6+
path: '/cosmos/bank/v1beta1/balances/:userAddress',
7+
chain: 'cosmos',
8+
returnValueTest: {
9+
key: '$.balances[0].amount',
10+
comparator: '>=',
11+
value: '1000000',
12+
},
13+
};
14+
15+
it('should validate a valid Atom access control condition', () => {
16+
expect(AtomAccSchema.safeParse(validAtomAcc).success).toBeTruthy();
17+
});
18+
19+
it('should not validate an invalid Atom access control condition', () => {
20+
expect(
21+
AtomAccSchema.safeParse({
22+
...validAtomAcc,
23+
path: undefined,
24+
}).success
25+
).toBeFalsy();
26+
27+
expect(
28+
AtomAccSchema.safeParse({
29+
...validAtomAcc,
30+
chain: 'invalidChain',
31+
}).success
32+
).toBeFalsy();
33+
34+
expect(
35+
AtomAccSchema.safeParse({
36+
...validAtomAcc,
37+
returnValueTest: undefined,
38+
}).success
39+
).toBeFalsy();
40+
});
41+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { z } from 'zod';
2+
3+
import { ChainEnumAtom, ReturnValueTestSchema } from './common';
4+
5+
export const AtomAccSchema = z
6+
.object({
7+
conditionType: z.literal('cosmos').optional(),
8+
path: z.string(),
9+
chain: ChainEnumAtom,
10+
method: z.string().optional(),
11+
parameters: z.array(z.string()).optional(),
12+
returnValueTest: ReturnValueTestSchema,
13+
})
14+
.strict();
15+
16+
export type AtomAcc = z.infer<typeof AtomAccSchema>;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { EvmBasicAccSchema } from './EvmBasicAcc';
2+
3+
describe('EvmBasicAccSchema', () => {
4+
const validEVMBasicAcc = {
5+
conditionType: 'evmBasic',
6+
contractAddress: '0x50D8EB685a9F262B13F28958aBc9670F06F819d9',
7+
standardContractType: 'MolochDAOv2.1',
8+
chain: 'ethereum',
9+
method: 'members',
10+
parameters: [':userAddress'],
11+
returnValueTest: {
12+
comparator: '=',
13+
value: 'true',
14+
},
15+
};
16+
17+
it('should validate a valid EVM basic access control condition', () => {
18+
expect(EvmBasicAccSchema.safeParse(validEVMBasicAcc).success).toBeTruthy();
19+
expect(
20+
EvmBasicAccSchema.safeParse({
21+
...validEVMBasicAcc,
22+
conditionType: undefined,
23+
}).success
24+
).toBeTruthy();
25+
});
26+
27+
it('should not validate an invalid EVM basic access control conditions', () => {
28+
expect(
29+
EvmBasicAccSchema.safeParse({
30+
...validEVMBasicAcc,
31+
chain: 'invalidChain',
32+
}).success
33+
).toBeFalsy();
34+
35+
expect(
36+
EvmBasicAccSchema.safeParse({
37+
...validEVMBasicAcc,
38+
chain: undefined,
39+
}).success
40+
).toBeFalsy();
41+
42+
expect(
43+
EvmBasicAccSchema.safeParse({
44+
...validEVMBasicAcc,
45+
chain: 1,
46+
}).success
47+
).toBeFalsy();
48+
49+
expect(
50+
EvmBasicAccSchema.safeParse({
51+
...validEVMBasicAcc,
52+
method: undefined,
53+
}).success
54+
).toBeFalsy();
55+
56+
expect(
57+
EvmBasicAccSchema.safeParse({
58+
...validEVMBasicAcc,
59+
contractAddress: undefined,
60+
}).success
61+
).toBeFalsy();
62+
63+
expect(
64+
EvmBasicAccSchema.safeParse({
65+
...validEVMBasicAcc,
66+
standardContractType: undefined,
67+
}).success
68+
).toBeFalsy();
69+
});
70+
});
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { z } from 'zod';
2+
3+
import { EvmChainEnum, ReturnValueTestSchema } from './common';
4+
5+
const StandardContractTypeEnum = z.enum([
6+
'',
7+
'ERC20',
8+
'ERC721',
9+
'ERC721MetadataName',
10+
'ERC1155',
11+
'CASK',
12+
'Creaton',
13+
'POAP',
14+
'timestamp',
15+
'MolochDAOv2.1',
16+
'ProofOfHumanity',
17+
'SIWE',
18+
'PKPPermissions',
19+
'LitAction',
20+
]);
21+
22+
export const EvmBasicAccSchema = z
23+
.object({
24+
conditionType: z.literal('evmBasic').optional(),
25+
contractAddress: z.string(),
26+
chain: EvmChainEnum,
27+
standardContractType: StandardContractTypeEnum,
28+
method: z.string(),
29+
parameters: z.array(z.string()),
30+
returnValueTest: ReturnValueTestSchema.omit({ key: true }),
31+
})
32+
.strict();
33+
34+
export type EvmBasicAcc = z.infer<typeof EvmBasicAccSchema>;

0 commit comments

Comments
 (0)