@@ -128,6 +128,36 @@ impl ContractClass {
128
128
} )
129
129
}
130
130
131
+ /// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON and a class hash.
132
+ ///
133
+ /// This constructor avoids the need to recompute the class hash from the program JSON,
134
+ /// but it does not verify that the given class hash is correct.
135
+ ///
136
+ /// This could return a wrong [`ContractClass`] if the given class hash is wrong.
137
+ ///
138
+ /// # Parameters
139
+ /// - `program_json`: The JSON of the compiled Cairo 0 program.
140
+ /// - `hinted_class_hash`: The class hash of the program.
141
+ ///
142
+ /// # Returns
143
+ /// A [`ContractClass`] parsed from the given JSON and class hash.
144
+ pub fn from_program_json_and_class_hash (
145
+ program_json : & str ,
146
+ hinted_class_hash : Felt252 ,
147
+ ) -> Result < Self , ContractClassError > {
148
+ let contract_class: starknet_api:: deprecated_contract_class:: ContractClass =
149
+ serde_json:: from_str ( program_json) . map_err ( |_| ContractClassError :: ParseError ) ?;
150
+ let program = to_cairo_runner_program ( contract_class. program )
151
+ . map_err ( |e| ContractClassError :: ProgramError ( e. to_string ( ) ) ) ?;
152
+ let entry_points_by_type = convert_entry_points ( contract_class. entry_points_by_type ) ;
153
+ Ok ( ContractClass {
154
+ hinted_class_hash,
155
+ program,
156
+ entry_points_by_type,
157
+ abi : contract_class. abi ,
158
+ } )
159
+ }
160
+
131
161
/// Parses a [`ContractClass`] from a compiled Cairo 0 program's JSON
132
162
/// at the given file path.
133
163
pub fn from_path < F > ( path : F ) -> Result < Self , ProgramError >
@@ -393,4 +423,22 @@ mod tests {
393
423
394
424
res. expect ( "should be able to read file" ) ;
395
425
}
426
+
427
+ #[ test]
428
+ fn test_from_program_json_and_class_hash_should_equal_from_path ( ) {
429
+ let program_json = include_str ! ( "../../../../starknet_programs/fibonacci.json" ) ;
430
+ let contract_class_from_path = ContractClass :: from_path ( "starknet_programs/fibonacci.json" )
431
+ . expect ( "should be able to read file" ) ;
432
+
433
+ let contract_class_from_program_json_and_class_hash =
434
+ ContractClass :: from_program_json_and_class_hash (
435
+ program_json,
436
+ contract_class_from_path. hinted_class_hash . clone ( ) ,
437
+ )
438
+ . expect ( "should be able to read file" ) ;
439
+ assert_eq ! (
440
+ contract_class_from_path,
441
+ contract_class_from_program_json_and_class_hash
442
+ ) ;
443
+ }
396
444
}
0 commit comments