@@ -21,6 +21,7 @@ use std::{borrow::Cow, collections::BTreeMap, io};
2121/// Instead of doing a Mask with 250 bits, we are only masking the most significant byte.
2222pub const MASK_3 : u8 = 0x03 ;
2323
24+ /// Returns the contract entry points.
2425fn get_contract_entry_points (
2526 contract_class : & ContractClass ,
2627 entry_point_type : & EntryPointType ,
@@ -40,6 +41,7 @@ fn get_contract_entry_points(
4041 Ok ( entry_points. to_owned ( ) )
4142}
4243
44+ /// Recursively add extra spaces to Cairo named tuple representations in a JSON structure.
4345fn add_extra_space_to_cairo_named_tuples ( value : & mut serde_json:: Value ) {
4446 match value {
4547 serde_json:: Value :: Array ( v) => walk_array ( v) ,
@@ -48,12 +50,14 @@ fn add_extra_space_to_cairo_named_tuples(value: &mut serde_json::Value) {
4850 }
4951}
5052
53+ /// Helper function to walk through a JSON array and apply extra space to cairo named tuples.
5154fn walk_array ( array : & mut [ serde_json:: Value ] ) {
5255 for v in array. iter_mut ( ) {
5356 add_extra_space_to_cairo_named_tuples ( v) ;
5457 }
5558}
5659
60+ /// Helper function to walk through a JSON map and apply extra space to cairo named tuples.
5761fn walk_map ( object : & mut serde_json:: Map < String , serde_json:: Value > ) {
5862 for ( k, v) in object. iter_mut ( ) {
5963 match v {
@@ -68,6 +72,7 @@ fn walk_map(object: &mut serde_json::Map<String, serde_json::Value>) {
6872 }
6973}
7074
75+ /// Add extra space to named tuple type definition.
7176fn add_extra_space_to_named_tuple_type_definition < ' a > (
7277 key : & str ,
7378 value : & ' a str ,
@@ -79,6 +84,7 @@ fn add_extra_space_to_named_tuple_type_definition<'a>(
7984 }
8085}
8186
87+ /// Replaces ": " with " : " and " :" with " :" for Cairo-specific formatting.
8288fn add_extra_space_before_colon ( v : & str ) -> String {
8389 // This is required because if we receive an already correct ` : `, we will still
8490 // "repair" it to ` : ` which we then fix at the end.
@@ -88,11 +94,13 @@ fn add_extra_space_before_colon(v: &str) -> String {
8894struct KeccakWriter ( sha3:: Keccak256 ) ;
8995
9096impl std:: io:: Write for KeccakWriter {
97+ /// Write data into the Keccak256 hasher.
9198 fn write ( & mut self , buf : & [ u8 ] ) -> std:: io:: Result < usize > {
9299 self . 0 . update ( buf) ;
93100 Ok ( buf. len ( ) )
94101 }
95102
103+ /// No operation is required for flushing, as we finalize after writing.
96104 fn flush ( & mut self ) -> std:: io:: Result < ( ) > {
97105 // noop is fine, we'll finalize after the write phase
98106 Ok ( ( ) )
@@ -104,6 +112,7 @@ impl std::io::Write for KeccakWriter {
104112struct PythonDefaultFormatter ;
105113
106114impl serde_json:: ser:: Formatter for PythonDefaultFormatter {
115+ /// Handles formatting for array values.
107116 fn begin_array_value < W > ( & mut self , writer : & mut W , first : bool ) -> std:: io:: Result < ( ) >
108117 where
109118 W : ?Sized + std:: io:: Write ,
@@ -115,6 +124,7 @@ impl serde_json::ser::Formatter for PythonDefaultFormatter {
115124 }
116125 }
117126
127+ /// Handles formatting for object keys.
118128 fn begin_object_key < W > ( & mut self , writer : & mut W , first : bool ) -> std:: io:: Result < ( ) >
119129 where
120130 W : ?Sized + std:: io:: Write ,
@@ -126,13 +136,15 @@ impl serde_json::ser::Formatter for PythonDefaultFormatter {
126136 }
127137 }
128138
139+ /// Handles formatting for object values.
129140 fn begin_object_value < W > ( & mut self , writer : & mut W ) -> std:: io:: Result < ( ) >
130141 where
131142 W : ?Sized + std:: io:: Write ,
132143 {
133144 writer. write_all ( b": " )
134145 }
135146
147+ /// Custom logic for writing string fragments, handling non-ASCII characters.
136148 #[ inline]
137149 fn write_string_fragment < W > ( & mut self , writer : & mut W , fragment : & str ) -> io:: Result < ( ) >
138150 where
@@ -157,6 +169,7 @@ impl serde_json::ser::Formatter for PythonDefaultFormatter {
157169
158170#[ derive( serde:: Deserialize , serde:: Serialize ) ]
159171#[ serde( deny_unknown_fields) ]
172+
160173pub struct CairoContractDefinition < ' a > {
161174 /// Contract ABI, which has no schema definition.
162175 pub abi : serde_json:: Value ,
@@ -268,13 +281,15 @@ pub(crate) fn compute_hinted_class_hash(
268281 Ok ( truncated_keccak ( <[ u8 ; 32 ] >:: from ( hash. finalize ( ) ) ) )
269282}
270283
284+ /// Truncate the given Keccak hash to fit within Felt252's constraints.
271285pub ( crate ) fn truncated_keccak ( mut plain : [ u8 ; 32 ] ) -> Felt252 {
272286 // python code masks with (2**250 - 1) which starts 0x03 and is followed by 31 0xff in be
273287 // truncation is needed not to overflow the field element.
274288 plain[ 0 ] &= MASK_3 ;
275289 Felt252 :: from_bytes_be ( & plain)
276290}
277291
292+ /// Returns the hashed entry points of a contract class.
278293fn get_contract_entry_points_hashed (
279294 contract_class : & ContractClass ,
280295 entry_point_type : & EntryPointType ,
@@ -292,6 +307,7 @@ fn get_contract_entry_points_hashed(
292307 ) ?)
293308}
294309
310+ /// Compute the hash for a deprecated contract class.
295311pub fn compute_deprecated_class_hash (
296312 contract_class : & ContractClass ,
297313) -> Result < Felt252 , ContractAddressError > {
0 commit comments