Skip to content
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

WIP: feat: start using zod for schema validation and type definitions #660

Draft
wants to merge 33 commits into
base: staging/v7
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
a2c6169
feat: start using zod for schema validation and type definitions
FedericoAmura Sep 27, 2024
e279b02
fix: replace Storage with a generic implementation schema
FedericoAmura Sep 30, 2024
093716f
feat: schemas and derived types for a bunch of constants including li…
FedericoAmura Oct 1, 2024
7e1e778
Merge branch 'refs/heads/staging/v7' into feature/lit-3796-js-sdk-res…
FedericoAmura Oct 3, 2024
ca7690f
feat: move schemas into their own package
FedericoAmura Oct 3, 2024
deaf97d
feat: add missing package.json file
FedericoAmura Oct 3, 2024
5fdd978
feat: properly organize constants, schemas and types (in that order) …
FedericoAmura Oct 7, 2024
42dd84f
feat: a lot of new schemas and types derivation from them
FedericoAmura Oct 9, 2024
2111f65
fix: define NodeAttestation to complete and fix LitNodeClient schema
FedericoAmura Oct 11, 2024
c12361c
Merge branch 'staging/v7' into feature/lit-3796-js-sdk-research-to-us…
FedericoAmura Oct 11, 2024
47f9692
feat: merge
FedericoAmura Oct 11, 2024
095744a
feat: add params validations on lit node client and lit core and slig…
FedericoAmura Oct 15, 2024
73137e7
fix: pkp wallet connect test type
FedericoAmura Oct 15, 2024
043cc03
feat: new schema file organization
FedericoAmura Oct 15, 2024
7bf56bd
feat: some anys removed
FedericoAmura Oct 15, 2024
15df0d8
feat: properly typified getNodePromises
FedericoAmura Oct 15, 2024
5c0c6ad
feat: use vmtype constant
FedericoAmura Oct 15, 2024
1f67314
feat: remove extra property not used
FedericoAmura Oct 15, 2024
e210686
feat: force either `code` or `ipfsId` passed to executeJs but not both
FedericoAmura Oct 15, 2024
bf0ea0b
feat: a few cleans
FedericoAmura Oct 15, 2024
d156ab6
feat: encryption and decryption schema typing
FedericoAmura Oct 16, 2024
b9ad188
feat: encryption adn acc schemas moved to their own file. Plus a lot …
FedericoAmura Oct 21, 2024
8c9e01a
feat: mark unused types as deprecated
FedericoAmura Oct 21, 2024
a461e08
feat: pkp wallet schemas
FedericoAmura Oct 21, 2024
733d9ca
Merge branch 'refs/heads/staging/v7' into feature/lit-3796-js-sdk-res…
FedericoAmura Oct 21, 2024
fcb83c3
fix: format
FedericoAmura Oct 21, 2024
058cbfc
Merge branch 'refs/heads/staging/v7' into feature/lit-3796-js-sdk-res…
FedericoAmura Oct 23, 2024
64a9fc7
feat: a bunch of fixes and merge conflicts resolution
FedericoAmura Oct 23, 2024
49042f1
fix: recap session capability object fixes
FedericoAmura Oct 24, 2024
6f29395
Merge branch 'staging/v7' into feature/lit-3796-js-sdk-research-to-us…
FedericoAmura Oct 24, 2024
c9bf970
feat: add access control conditions schemas package with zod
FedericoAmura Oct 25, 2024
b56419d
Merge branch 'refs/heads/staging/v7' into feature/lit-3796-js-sdk-res…
FedericoAmura Oct 25, 2024
d6b2765
feat: yarn lock update post base branch merge
FedericoAmura Oct 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const testPkpEthersWithEoaSessionSigsToSignWithAuthContext = async (
pkpPubKey: alice.pkp.publicKey,
litNodeClient: devEnv.litNodeClient,
authContext: {
client: devEnv.litNodeClient,
getSessionSigsProps: {
authNeededCallback: async function (
params: AuthCallbackParams
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@
"tslib": "^2.7.0",
"tweetnacl": "^1.0.3",
"tweetnacl-util": "^0.15.1",
"uint8arrays": "^4.0.3"
"uint8arrays": "^4.0.3",
"zod": "^3.23.8",
"zod-validation-error": "^3.4.0"
},
"devDependencies": {
"@nrwl/devkit": "19.6.3",
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": "7.0.0-alpha.8",
"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();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { z } from 'zod';

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

const StandardContractTypeEnum = z.enum([
'',
'ERC20',
'ERC721',
'ERC721MetadataName',
'ERC1155',
'CASK',
'Creaton',
'POAP',
'timestamp',
'MolochDAOv2.1',
'ProofOfHumanity',
'SIWE',
'PKPPermissions',
'LitAction',
]);

export const EvmBasicAccSchema = z
.object({
conditionType: z.literal('evmBasic').optional(),
contractAddress: z.string(),
chain: EvmChainEnum,
standardContractType: StandardContractTypeEnum,
method: z.string(),
parameters: z.array(z.string()),
returnValueTest: ReturnValueTestSchema.omit({ key: true }),
})
.strict();

export type EvmBasicAcc = z.infer<typeof EvmBasicAccSchema>;
Loading
Loading