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' ;
410import { InvalidArgumentException } from '@lit-protocol/constants' ;
5- import { checkSchema } from '@lit-protocol/misc' ;
611import {
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 */
4647export 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 */
7657export 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 */
10667export 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 */
13677export 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 */
16687export 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