Skip to content

Commit 6070756

Browse files
committed
feat: migrate accs validator to new accs schemas package and remove ajv from sdk
1 parent f3d85d4 commit 6070756

File tree

9 files changed

+59
-247
lines changed

9 files changed

+59
-247
lines changed

local-tests/build.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { fileURLToPath } from 'url';
44

55
const ALLOW_LIST = [
66
'ethers',
7-
'@lit-protocol/accs-schemas',
87
'@lit-protocol/contracts',
98
'crypto',
109
'secp256k1',

local-tests/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
"@cypress/code-coverage": "^3.10.0",
2121
"@cypress/react": "^6.2.0",
2222
"@cypress/webpack-dev-server": "^2.3.0",
23-
"@lit-protocol/accs-schemas": "0.0.7",
2423
"@metamask/eth-sig-util": "5.0.2",
2524
"@mysten/sui.js": "^0.37.1",
2625
"@playwright/test": "^1.25.2",

local-tests/setup/accs/accs.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {
2-
LPACC_EVM_ATOM,
3-
LPACC_EVM_BASIC,
4-
LPACC_SOL,
5-
} from '@lit-protocol/accs-schemas';
2+
AtomAcc,
3+
EvmBasicAcc,
4+
SolAcc,
5+
} from '@lit-protocol/access-control-conditions-schemas';
66

77
export namespace AccessControlConditions {
88
export const getEvmBasicAccessControlConditions = ({
99
userAddress,
10-
}): LPACC_EVM_BASIC[] => {
10+
}): EvmBasicAcc[] => {
1111
return [
1212
{
1313
contractAddress: '',
@@ -25,7 +25,7 @@ export namespace AccessControlConditions {
2525

2626
export const getSolBasicAccessControlConditions = ({
2727
userAddress,
28-
}): LPACC_SOL[] => {
28+
}): SolAcc[] => {
2929
return [
3030
{
3131
method: '',
@@ -45,7 +45,7 @@ export namespace AccessControlConditions {
4545

4646
export const getCosmosBasicAccessControlConditions = ({
4747
userAddress,
48-
}): LPACC_EVM_ATOM[] => {
48+
}): AtomAcc[] => {
4949
return [
5050
{
5151
conditionType: 'cosmos',

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
"@cosmjs/proto-signing": "0.30.1",
4141
"@cosmjs/stargate": "0.30.1",
4242
"@dotenvx/dotenvx": "^1.6.4",
43-
"@lit-protocol/accs-schemas": "^0.0.22",
4443
"@lit-protocol/contracts": "^0.0.86",
4544
"@metamask/eth-sig-util": "5.0.2",
4645
"@mysten/sui.js": "^0.37.1",
@@ -53,7 +52,6 @@
5352
"@walletconnect/utils": "2.9.2",
5453
"@walletconnect/web3wallet": "1.8.8",
5554
"abitype": "^1.0.8",
56-
"ajv": "^8.12.0",
5755
"base64url": "^3.0.1",
5856
"bech32": "^2.0.0",
5957
"cbor-web": "^9.0.2",

packages/access-control-conditions/src/lib/validator.spec.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,20 +312,20 @@ describe('validator.ts', () => {
312312
error = e as LitErrorClass;
313313
}
314314

315-
expect(error!.kind).toBe(LIT_ERROR['INVALID_PARAM_TYPE'].kind);
316-
expect(error!.code).toBe(LIT_ERROR['INVALID_PARAM_TYPE'].code);
315+
expect(error!.kind).toBe(LIT_ERROR['INVALID_ARGUMENT_EXCEPTION'].kind);
316+
expect(error!.code).toBe(LIT_ERROR['INVALID_ARGUMENT_EXCEPTION'].code);
317317
});
318318

319319
it('should throw when schema has invalid fields', async () => {
320320
// Disable TS here to test invalid fields
321321
const evmBasicAccessControlConditions: AccessControlConditions = [
322322
{
323-
// @ts-ignore
323+
// @ts-expect-error we are testing wrong values
324324
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
325325
contractAddress: 0x7a250d5630b4cf539739df2c5dacb4c659f2488d,
326-
// @ts-ignore
326+
// @ts-expect-error we are testing wrong values
327327
standardContractType: 'AMM',
328-
// @ts-ignore
328+
// @ts-expect-error we are testing wrong values
329329
chain: 'bitcoin',
330330
method: 'eth_getBalance',
331331
parameters: [':userAddress', 'latest'],
@@ -346,8 +346,8 @@ describe('validator.ts', () => {
346346
}
347347

348348
expect(error).toBeDefined();
349-
expect(error!.kind).toBe(LIT_ERROR['INVALID_PARAM_TYPE'].kind);
350-
expect(error!.code).toBe(LIT_ERROR['INVALID_PARAM_TYPE'].code);
349+
expect(error!.kind).toBe(LIT_ERROR['INVALID_ARGUMENT_EXCEPTION'].kind);
350+
expect(error!.code).toBe(LIT_ERROR['INVALID_ARGUMENT_EXCEPTION'].code);
351351
});
352352

353353
it('should throw when schema of a nested ACC does not validate', async () => {
@@ -405,7 +405,7 @@ describe('validator.ts', () => {
405405
}
406406

407407
expect(error).toBeDefined();
408-
expect(error!.kind).toBe(LIT_ERROR['INVALID_PARAM_TYPE'].kind);
409-
expect(error!.code).toBe(LIT_ERROR['INVALID_PARAM_TYPE'].code);
408+
expect(error!.kind).toBe(LIT_ERROR['INVALID_ARGUMENT_EXCEPTION'].kind);
409+
expect(error!.code).toBe(LIT_ERROR['INVALID_ARGUMENT_EXCEPTION'].code);
410410
});
411411
});
Lines changed: 40 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,53 @@
1-
import { JSONSchemaType } from 'ajv';
1+
import { fromError, isZodErrorLike } from 'zod-validation-error';
22

3-
import { loadSchema } from '@lit-protocol/accs-schemas';
3+
import {
4+
EvmBasicConditionsSchema,
5+
EvmContractConditionsSchema,
6+
MultipleAccessControlConditionsSchema,
7+
SolRpcConditionsSchema,
8+
UnifiedConditionsSchema,
9+
} from '@lit-protocol/access-control-conditions-schemas';
410
import { InvalidArgumentException } from '@lit-protocol/constants';
5-
import { checkSchema } from '@lit-protocol/misc';
611
import {
712
AccessControlConditions,
8-
ConditionType,
913
EvmContractConditions,
1014
MultipleAccessControlConditions,
1115
SolRpcConditions,
1216
UnifiedAccessControlConditions,
1317
} from '@lit-protocol/types';
1418

15-
const SCHEMA_NAME_MAP: { [K in ConditionType]: string } = {
16-
cosmos: 'LPACC_ATOM',
17-
evmBasic: 'LPACC_EVM_BASIC',
18-
evmContract: 'LPACC_EVM_CONTRACT',
19-
solRpc: 'LPACC_SOL',
20-
};
19+
function formatZodError(accs: unknown, e: unknown): never {
20+
throw new InvalidArgumentException(
21+
{
22+
info: {
23+
accs,
24+
},
25+
cause: isZodErrorLike(e) ? fromError(e) : e,
26+
},
27+
'Invalid access control conditions. Check error cause for more details.'
28+
);
29+
}
2130

22-
async function getSchema<T>(
23-
accType: ConditionType
24-
): Promise<JSONSchemaType<T>> {
31+
async function validateSchema<T>(
32+
accs: T,
33+
schema: { parse: (arg: unknown) => void }
34+
): Promise<true> {
2535
try {
26-
const schemaName = SCHEMA_NAME_MAP[accType];
27-
return loadSchema(schemaName) as Promise<JSONSchemaType<T>>;
28-
} catch (err) {
29-
throw new InvalidArgumentException(
30-
{
31-
info: {
32-
accType,
33-
},
34-
},
35-
`No schema found for condition type %s`,
36-
accType
37-
);
36+
schema.parse(accs);
37+
} catch (e) {
38+
formatZodError(accs, e);
3839
}
40+
return true;
3941
}
4042

4143
/**
42-
* CHANGE: This function should be removed in favor of {@link validateAccessControlConditionsSchema}.
43-
* However, since `MultipleAccessControlConditions` is deeply intertwined with other types,
44-
* we will revisit this later. At the moment, the `lit-core` will be using this function.
44+
* Validates Multiple access control conditions schema
45+
* @param { MultipleAccessControlConditions } accs
4546
*/
4647
export const validateAccessControlConditions = async (
47-
params: MultipleAccessControlConditions
48-
): Promise<boolean> => {
49-
// ========== Prepare Params ==========
50-
const {
51-
accessControlConditions,
52-
evmContractConditions,
53-
solRpcConditions,
54-
unifiedAccessControlConditions,
55-
} = params;
56-
57-
if (accessControlConditions) {
58-
await validateAccessControlConditionsSchema(accessControlConditions);
59-
} else if (evmContractConditions) {
60-
await validateEVMContractConditionsSchema(evmContractConditions);
61-
} else if (solRpcConditions) {
62-
await validateSolRpcConditionsSchema(solRpcConditions);
63-
} else if (unifiedAccessControlConditions) {
64-
await validateUnifiedAccessControlConditionsSchema(
65-
unifiedAccessControlConditions
66-
);
67-
}
68-
69-
return true;
48+
accs: MultipleAccessControlConditions
49+
): Promise<true> => {
50+
return validateSchema(accs, MultipleAccessControlConditionsSchema);
7051
};
7152

7253
/**
@@ -75,28 +56,8 @@ export const validateAccessControlConditions = async (
7556
*/
7657
export const validateAccessControlConditionsSchema = async (
7758
accs: AccessControlConditions
78-
): Promise<boolean> => {
79-
for (const acc of accs) {
80-
// conditions can be nested to make boolean expressions
81-
if (Array.isArray(acc)) {
82-
await validateAccessControlConditionsSchema(acc);
83-
continue;
84-
}
85-
86-
if ('operator' in acc) {
87-
// condition is operator, skip
88-
continue;
89-
}
90-
91-
checkSchema(
92-
acc,
93-
await getSchema('evmBasic'),
94-
'accessControlConditions',
95-
'validateAccessControlConditionsSchema'
96-
);
97-
}
98-
99-
return true;
59+
): Promise<true> => {
60+
return validateSchema(accs, EvmBasicConditionsSchema);
10061
};
10162

10263
/**
@@ -105,28 +66,8 @@ export const validateAccessControlConditionsSchema = async (
10566
*/
10667
export const validateEVMContractConditionsSchema = async (
10768
accs: EvmContractConditions
108-
): Promise<boolean> => {
109-
for (const acc of accs) {
110-
// conditions can be nested to make boolean expressions
111-
if (Array.isArray(acc)) {
112-
await validateEVMContractConditionsSchema(acc);
113-
continue;
114-
}
115-
116-
if ('operator' in acc) {
117-
// condition is operator, skip
118-
continue;
119-
}
120-
121-
checkSchema(
122-
acc,
123-
await getSchema('evmContract'),
124-
'evmContractConditions',
125-
'validateEVMContractConditionsSchema'
126-
);
127-
}
128-
129-
return true;
69+
): Promise<true> => {
70+
return validateSchema(accs, EvmContractConditionsSchema);
13071
};
13172

13273
/**
@@ -135,28 +76,8 @@ export const validateEVMContractConditionsSchema = async (
13576
*/
13677
export const validateSolRpcConditionsSchema = async (
13778
accs: SolRpcConditions
138-
): Promise<boolean> => {
139-
for (const acc of accs) {
140-
// conditions can be nested to make boolean expressions
141-
if (Array.isArray(acc)) {
142-
await validateSolRpcConditionsSchema(acc);
143-
continue;
144-
}
145-
146-
if ('operator' in acc) {
147-
// condition is operator, skip
148-
continue;
149-
}
150-
151-
checkSchema(
152-
acc,
153-
await getSchema('solRpc'),
154-
'solRpcConditions',
155-
'validateSolRpcConditionsSchema'
156-
);
157-
}
158-
159-
return true;
79+
): Promise<true> => {
80+
return validateSchema(accs, SolRpcConditionsSchema);
16081
};
16182

16283
/**
@@ -165,53 +86,6 @@ export const validateSolRpcConditionsSchema = async (
16586
*/
16687
export const validateUnifiedAccessControlConditionsSchema = async (
16788
accs: UnifiedAccessControlConditions
168-
): Promise<boolean> => {
169-
for (const acc of accs) {
170-
// conditions can be nested to make boolean expressions
171-
if (Array.isArray(acc)) {
172-
await validateUnifiedAccessControlConditionsSchema(acc);
173-
continue;
174-
}
175-
176-
if ('operator' in acc) {
177-
// condition is operator, skip
178-
continue;
179-
}
180-
181-
let schema: JSONSchemaType<any> | undefined;
182-
switch (acc.conditionType) {
183-
case 'evmBasic':
184-
schema = await getSchema('evmBasic');
185-
break;
186-
case 'evmContract':
187-
schema = await getSchema('evmContract');
188-
break;
189-
case 'solRpc':
190-
schema = await getSchema('solRpc');
191-
break;
192-
case 'cosmos':
193-
schema = await getSchema('cosmos');
194-
break;
195-
}
196-
if (schema) {
197-
checkSchema(
198-
acc,
199-
schema,
200-
'accessControlConditions',
201-
'validateUnifiedAccessControlConditionsSchema'
202-
);
203-
} else {
204-
throw new InvalidArgumentException(
205-
{
206-
info: {
207-
acc,
208-
},
209-
},
210-
`Missing schema to validate condition type %s`,
211-
acc.conditionType
212-
);
213-
}
214-
}
215-
216-
return true;
89+
): Promise<true> => {
90+
return validateSchema(accs, UnifiedConditionsSchema);
21791
};

0 commit comments

Comments
 (0)