diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c6f47dec..beb7cdd83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ git # Deoxys Changelog ## Next release +- fix(compute): Fixed prepare_data_availability_modes computation - feat(rpc): add pending block to `get_block_with_receipts` rpc call - chore: update bonsai-trie (benefit from perf boost) - feat(rpc): add `get_block_with_receipts` rpc call diff --git a/crates/primitives/transactions/src/compute_hash.rs b/crates/primitives/transactions/src/compute_hash.rs index dbfd91601..e8c1f779e 100644 --- a/crates/primitives/transactions/src/compute_hash.rs +++ b/crates/primitives/transactions/src/compute_hash.rs @@ -2,6 +2,7 @@ use alloc::vec::Vec; use mp_felt::Felt252Wrapper; use mp_hashers::HasherT; +use starknet_api::core::calculate_contract_address; use starknet_api::data_availability::DataAvailabilityMode; use starknet_api::transaction::{ Calldata, DeclareTransaction, DeclareTransactionV0V1, DeclareTransactionV2, DeclareTransactionV3, @@ -59,12 +60,9 @@ fn prepare_data_availability_modes( fee_data_availability_mode: DataAvailabilityMode, ) -> FieldElement { let mut buffer = [0u8; 32]; - buffer[16..24].copy_from_slice(&(nonce_data_availability_mode as u32).to_be_bytes()); - buffer[24..].copy_from_slice(&(fee_data_availability_mode as u32).to_be_bytes()); - // buffer[64..96].copy_from_slice(&(nonce_data_availability_mode as u32).to_be_bytes()); - // buffer[96..].copy_from_slice(&(fee_data_availability_mode as u32).to_be_bytes()); + buffer[8..12].copy_from_slice(&(nonce_data_availability_mode as u32).to_be_bytes()); + buffer[12..16].copy_from_slice(&(fee_data_availability_mode as u32).to_be_bytes()); - // Safe to unwrap because we left most significant bit of the buffer empty FieldElement::from_bytes_be(&buffer).unwrap() } @@ -365,11 +363,15 @@ impl ComputeTransactionHash for DeployAccountTransactionV1 { ) -> TransactionHash { let constructor_calldata = convert_calldata(self.constructor_calldata.clone()); - let contract_address = Felt252Wrapper::from(calculate_contract_address( - Felt252Wrapper::from(self.contract_address_salt).into(), - Felt252Wrapper::from(self.class_hash).into(), - &constructor_calldata, - )) + let contract_address = Felt252Wrapper::from( + calculate_contract_address( + self.contract_address_salt, + self.class_hash, + &self.constructor_calldata, + Default::default(), + ) + .unwrap(), + ) .into(); let prefix = FieldElement::from_byte_slice_be(DEPLOY_ACCOUNT_PREFIX).unwrap(); let version = if offset_version { SIMULATE_TX_VERSION_OFFSET + FieldElement::ONE } else { FieldElement::ONE }; @@ -406,11 +408,15 @@ impl ComputeTransactionHash for DeployTransaction { let chain_id = chain_id.into(); let constructor_calldata = convert_calldata(self.constructor_calldata.clone()); - let contract_address = Felt252Wrapper::from(calculate_contract_address( - Felt252Wrapper::from(self.contract_address_salt).into(), - Felt252Wrapper::from(self.class_hash).into(), - &constructor_calldata, - )) + let contract_address = Felt252Wrapper::from( + calculate_contract_address( + self.contract_address_salt, + self.class_hash, + &self.constructor_calldata, + Default::default(), + ) + .unwrap(), + ) .into(); compute_hash_given_contract_address::( @@ -435,12 +441,15 @@ impl ComputeTransactionHash for DeployAccountTransactionV3 { let version = if offset_version { SIMULATE_TX_VERSION_OFFSET + FieldElement::THREE } else { FieldElement::THREE }; let constructor_calldata = convert_calldata(self.constructor_calldata.clone()); - - let contract_address = Felt252Wrapper::from(calculate_contract_address( - Felt252Wrapper::from(self.contract_address_salt).into(), - Felt252Wrapper::from(self.class_hash).into(), - &constructor_calldata, - )) + let contract_address = Felt252Wrapper::from( + calculate_contract_address( + self.contract_address_salt, + self.class_hash, + &self.constructor_calldata, + Default::default(), + ) + .unwrap(), + ) .into(); let gas_hash = compute_hash_on_elements(&[ FieldElement::from(self.tip.0), @@ -558,25 +567,3 @@ pub fn compute_hash_given_contract_address( .into() } } - -// Deploy Transaction custom function to compute the contract address -pub fn calculate_contract_address( - contract_address_salt: FieldElement, - class_hash: FieldElement, - constructor_calldata: &[FieldElement], -) -> FieldElement { - /// Cairo string for "STARKNET_CONTRACT_ADDRESS" - const PREFIX_CONTRACT_ADDRESS: FieldElement = - FieldElement::from_mont([3829237882463328880, 17289941567720117366, 8635008616843941496, 533439743893157637]); - // 2 ** 251 - 256 - const ADDR_BOUND: FieldElement = - FieldElement::from_mont([18446743986131443745, 160989183, 18446744073709255680, 576459263475590224]); - - starknet_core::crypto::compute_hash_on_elements(&[ - PREFIX_CONTRACT_ADDRESS, - FieldElement::ZERO, - contract_address_salt, - class_hash, - starknet_core::crypto::compute_hash_on_elements(constructor_calldata), - ]) % ADDR_BOUND -}