From b7c252fea3e71cbe5db2c61f05b24c226053c729 Mon Sep 17 00:00:00 2001 From: gaetbout Date: Fri, 9 Aug 2024 14:43:36 +0200 Subject: [PATCH] fix v1 array --- Scarb.toml | 2 ++ src/examples/v1/struct_with_array.cairo | 12 +++++++----- src/examples/v1/struct_with_merkletree.cairo | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Scarb.toml b/Scarb.toml index 4f0a5b4..c7c6fd9 100644 --- a/Scarb.toml +++ b/Scarb.toml @@ -15,7 +15,9 @@ cairo_test = "2.7.0" SimpleStruct = "npx ts-node scripts/v0/SimpleStruct.ts" SimpleStructV1 = "npx ts-node scripts/v1/SimpleStruct.ts" StructWithArray = "npx ts-node scripts/v0/StructWithArray.ts" +StructWithArrayV1 = "npx ts-node scripts/v1/StructWithArray.ts" StructWithMerkletree = "npx ts-node scripts/v0/StructWithMerkletree.ts" +StructWithMerkletreeV1 = "npx ts-node scripts/v1/StructWithMerkletree.ts" StructWithU256 = "npx ts-node scripts/v0/StructWithU256.ts" StructWithU256V1 = "npx ts-node scripts/v1/StructWithU256.ts" format = "scarb fmt && npx prettier --write ." diff --git a/src/examples/v1/struct_with_array.cairo b/src/examples/v1/struct_with_array.cairo index 76f7fba..92038c3 100644 --- a/src/examples/v1/struct_with_array.cairo +++ b/src/examples/v1/struct_with_array.cairo @@ -5,7 +5,7 @@ use hash::{LegacyHash, HashStateTrait, HashStateExTrait}; use off_chain_signature::interfaces::{IOffChainMessageHash, IStructHash, v1::StarknetDomain}; const STRUCT_WITH_ARRAY_TYPE_HASH: felt252 = - selector!("StructWithArray(some_felt252:felt,some_array:felt*)"); + selector!("\"StructWithArray\"(\"some_felt252\":\"felt\",\"some_array\":\"felt*\")"); #[derive(Drop, Copy)] struct StructWithArray { @@ -41,9 +41,11 @@ impl StructHashStructWithArray of IStructHash { impl StructHashSpanFelt252 of IStructHash> { fn get_struct_hash(self: @Span) -> felt252 { - let mut call_data_state = LegacyHash::hash(0, *self); - call_data_state = LegacyHash::hash(call_data_state, (*self).len()); - call_data_state + let mut state = PoseidonTrait::new(); + for el in (*self) { + state = state.update_with(*el); + }; + state.finalize() } } @@ -65,7 +67,7 @@ mod tests { #[test] fn test_valid_hash() { // This value was computed using StarknetJS - let message_hash = 0x266b2350f2febce38581c6aa5b1afb829bb1466840400305fe51548ba32544e; + let message_hash = 0x723b50f650b3b037620c51955e482aeb7786fadeb0d7a384ef99278102f038f; let mut some_array = ArrayTrait::new(); some_array.append(4); some_array.append(2); diff --git a/src/examples/v1/struct_with_merkletree.cairo b/src/examples/v1/struct_with_merkletree.cairo index 7f2cdc5..8612da3 100644 --- a/src/examples/v1/struct_with_merkletree.cairo +++ b/src/examples/v1/struct_with_merkletree.cairo @@ -44,7 +44,7 @@ mod tests { #[test] fn test_valid_hash() { // This value was computed using StarknetJS - let message_hash = 0x4e4aa6e92e1250e5277a99686aa17c411b28b83fc948df78c10f848f1b0ad30; + let message_hash = 0x2a89d9f00b3ead36ea204b956bc9ac862a5e7e0f2ad2bf790322dda9690629e; let simple_struct = StructWithMerkletree { some_felt252: 712, some_merkletree_root: 0x1e3fb24d6eeb2fdf4308dd358adfb0169dcdb21b3c6bac8ca223a9af6a2bbd9