@@ -569,7 +569,8 @@ parser_error_t read_root_type_indices(parser_context_t *ctx, root_type_indices_t
569569 CHECK_INPUT (ctx );
570570
571571 // save complete borsh data
572- root_type_indices -> complete_borsh_data .ptr = ctx -> buffer .ptr ;
572+ root_type_indices -> complete_borsh_data .ptr = ctx -> buffer .ptr + ctx -> offset ;
573+ uint16_t offset_mem_complete_borsh_data = ctx -> offset ;
573574
574575 // read length
575576 CHECK_ERROR (read_u32 (ctx , & root_type_indices -> qty ));
@@ -586,7 +587,8 @@ parser_error_t read_root_type_indices(parser_context_t *ctx, root_type_indices_t
586587 root_type_indices -> indices .buffer .ptr = ptr_mem_indices ;
587588 root_type_indices -> indices .buffer .len = ctx -> offset - offset_mem_indices ;
588589
589- root_type_indices -> complete_borsh_data .len = ctx -> offset - offset_mem_indices + OFFSET_U32 ;
590+ root_type_indices -> complete_borsh_data .len = ctx -> offset - offset_mem_complete_borsh_data ;
591+ print_buffer_u8 (& root_type_indices -> complete_borsh_data , "root_type_indices.complete_borsh_data" );
590592
591593 return parser_ok ;
592594}
@@ -656,6 +658,10 @@ parser_error_t read_chain_data(parser_context_t *ctx, chain_data_t *chain_data)
656658 CHECK_INPUT (chain_data );
657659 CHECK_INPUT (ctx );
658660
661+ // save complete borsh data
662+ chain_data -> complete_borsh_data .ptr = ctx -> buffer .ptr + ctx -> offset ;
663+ uint16_t offset_mem = ctx -> offset ;
664+
659665 // read chain_id
660666 CHECK_ERROR (read_u64 (ctx , & chain_data -> chain_id ));
661667 print_u64 ("chain_data.chain_id:" , chain_data -> chain_id );
@@ -676,6 +682,8 @@ parser_error_t read_chain_data(parser_context_t *ctx, chain_data_t *chain_data)
676682 // read name_registries
677683 CHECK_ERROR (read_name_registries (ctx , & chain_data -> name_registries ));
678684
685+ chain_data -> complete_borsh_data .len = ctx -> offset - offset_mem ;
686+
679687 return parser_ok ;
680688}
681689
@@ -815,6 +823,18 @@ parser_error_t metadata_read(parser_context_t *ctx, parser_tx_t *txObj) {
815823 return parser_ok ;
816824}
817825
826+ parser_error_t compute_internal_data_hash (parser_tx_t * txObj , uint8_t internal_data_hash [CX_SHA256_SIZE ]) {
827+ CHECK_INPUT (txObj );
828+ CHECK_INPUT (internal_data_hash );
829+
830+ crypto_sha256_init ();
831+ crypto_sha256_update (txObj -> merkle_proofs .root_type_indices .complete_borsh_data .ptr , txObj -> merkle_proofs .root_type_indices .complete_borsh_data .len );
832+ crypto_sha256_update (txObj -> merkle_proofs .chain_data .complete_borsh_data .ptr , txObj -> merkle_proofs .chain_data .complete_borsh_data .len );
833+ crypto_sha256_final (internal_data_hash );
834+
835+ return parser_ok ;
836+ }
837+
818838// | borsh(leaves_data) | borsh(indices_leaves) | borsh(lemmas) | borsh(tree_size) | borsh(root_hash) | borsh(root_indexes) |
819839// borsh(chain_data)
820840parser_error_t merkle_proofs_read (parser_context_t * ctx , parser_tx_t * txObj ) {
@@ -877,7 +897,7 @@ parser_error_t merkle_proofs_read(parser_context_t *ctx, parser_tx_t *txObj) {
877897 // read root_type_indices
878898 CHECK_ERROR (read_root_type_indices (ctx , & txObj -> merkle_proofs .root_type_indices ));
879899
880- // read name_registries
900+ // read chain_data
881901 CHECK_ERROR (read_chain_data (ctx , & txObj -> merkle_proofs .chain_data ));
882902
883903 // // TODO: check that we have consumed all data
@@ -890,100 +910,15 @@ parser_error_t merkle_proofs_read(parser_context_t *ctx, parser_tx_t *txObj) {
890910
891911 CHECK_ERROR (verify_merkle_proofs (& txObj -> merkle_proofs ));
892912
893- return parser_ok ;
894- }
895-
896- parser_error_t get_leave_index (merkle_leaves_data_t * leaves , merkle_leaves_indices_t * indices , uint32_t schema_index ,
897- uint32_t field_index [], uint16_t * qty , uint16_t max_indexes ) {
898- CHECK_INPUT (leaves );
899- CHECK_INPUT (indices );
900- CHECK_INPUT (field_index );
901- CHECK_INPUT (qty );
902- * qty = 0 ;
903-
904- uint64_t index_vec = 0 ;
905- if (!schema_find_index (schema_index , indices , & index_vec )) {
906- return parser_schema_index_not_found ;
907- }
908-
909- CHECK_ERROR (schema_move_leaf_offset (leaves , index_vec ));
910-
911- // read len
912- uint32_t len = 0 ;
913- CHECK_ERROR (read_u32 (& leaves -> data , & len ));
914-
915- // read type
916- uint8_t type = 0 ;
917- CHECK_ERROR (read_u8 (& leaves -> data , & type ));
918-
919- switch (type ) {
920- case LINKING_SCHEME_ENUM :
921- print_string ("READING ENUM" );
922- schema_enum_t enum_type = {0 };
923- CHECK_ERROR (read_enum (& leaves -> data , & enum_type ));
924- CHECK_ERROR (get_variant_link_index (enum_type .variants , enum_type .variants_qty , field_index , qty , max_indexes ));
925- print_string ("READING ENUM DONE\n" );
926- break ;
927- case LINKING_SCHEME_STRUCT :
928- print_string ("READING STRUCT" );
929- schema_struct_t struct_type = {0 };
930- CHECK_ERROR (read_struct (& leaves -> data , & struct_type ));
931- CHECK_ERROR (get_named_link_index (struct_type .fields , struct_type .fields_qty , field_index , qty , max_indexes ));
932- print_string ("READING STRUCT DONE\n" );
933- break ;
934- case LINKING_SCHEME_TUPLE :
935- print_string ("READING TUPLE" );
936- schema_tuple_t tuple_type = {0 };
937- CHECK_ERROR (read_tuple (& leaves -> data , & tuple_type ));
938- CHECK_ERROR (get_unnamed_link_index (tuple_type .fields , tuple_type .fields_qty , field_index , qty , max_indexes ));
939- print_string ("READING TUPLE DONE\n" );
940- break ;
941- case LINKING_SCHEME_OPTION :
942- print_string ("READING OPTION" );
943- link_t option_type = {0 };
944- CHECK_ERROR (read_link (& leaves -> data , & option_type ));
945- if (option_type .tag == LINK_BY_INDEX ) {
946- field_index [(* qty )++ ] = option_type .data .by_index ;
947- }
948- print_string ("READING OPTION DONE\n" );
949- break ;
950- case LINKING_SCHEME_ARRAY :
951- print_string ("READING ARRAY" );
952- schema_array_t array_type = {0 };
953- CHECK_ERROR (read_array (& leaves -> data , & array_type ));
954- print_string ("READING ARRAY DONE\n" );
955- break ;
956- case LINKING_SCHEME_VEC :
957- print_string ("READING VEC" );
958- link_t vec_type = {0 };
959- CHECK_ERROR (read_link (& leaves -> data , & vec_type ));
960- if (vec_type .tag == LINK_BY_INDEX ) {
961- field_index [(* qty )++ ] = vec_type .data .by_index ;
962- }
963- print_string ("READING VEC DONE\n" );
964- break ;
965- case LINKING_SCHEME_MAP :
966- print_string ("READING MAP" );
967- link_t key = {0 };
968- link_t value = {0 };
969- CHECK_ERROR (read_link (& leaves -> data , & key ));
970- CHECK_ERROR (read_link (& leaves -> data , & value ));
971- if (value .tag == LINK_BY_INDEX ) {
972- field_index [(* qty )++ ] = value .data .by_index ;
973- }
974- print_string ("READING MAP DONE\n" );
975- break ;
976- default :
977- print_u8 ("UNKNOWN TYPE:" , type );
978- CHECK_ERROR (schema_reset_leaf_offset (leaves ));
979- return parser_unexpected_type ;
980- }
981-
982- for (uint16_t i = 0 ; i < * qty ; i ++ ) {
983- print_u32 ("field_index:" , field_index [i ]);
984- }
913+ // compute internal data hash
914+ uint8_t internal_data_hash [CX_SHA256_SIZE ];
915+ CHECK_ERROR (compute_internal_data_hash (txObj , internal_data_hash ));
916+ bytes_t internal_data_hash_bytes = {0 };
917+ internal_data_hash_bytes .ptr = internal_data_hash ;
918+ internal_data_hash_bytes .len = CX_SHA256_SIZE ;
919+ print_buffer (& internal_data_hash_bytes , "internal_data_hash" );
985920
986- CHECK_ERROR ( schema_reset_leaf_offset ( leaves ));
921+
987922
988923 return parser_ok ;
989924}
0 commit comments