diff --git a/crates/primitives/src/eip4844.rs b/crates/primitives/src/eip4844.rs index bf1132eab1ee6..3ec610781e51a 100644 --- a/crates/primitives/src/eip4844.rs +++ b/crates/primitives/src/eip4844.rs @@ -1,7 +1,16 @@ //! Helpers for working with EIP-4844 blob fee +use crate::{kzg::KzgCommitment, H256, constants::eip4844::{TARGET_DATA_GAS_PER_BLOCK, VERSIONED_HASH_VERSION_KZG}}; + +/// Calculates the versioned hash for a KzgCommitment +/// +/// Specified in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension) +pub fn kzg_to_versioned_hash(commitment: KzgCommitment) -> H256 { + use sha2::{Sha256, Digest}; + let mut res = Sha256::digest(commitment.as_slice()); + res[0] = VERSIONED_HASH_VERSION_KZG; + H256::from_slice(&res) +} -use crate::{H256, constants::eip4844::{TARGET_DATA_GAS_PER_BLOCK, VERSIONED_HASH_VERSION_KZG}}; -use c_kzg::KzgCommitment; /// Calculates the excess data gas for the next block, after applying the current set of blobs on /// top of the excess data gas. /// @@ -10,15 +19,3 @@ pub fn calculate_excess_blob_gas(parent_excess_blob_gas: u64, parent_blob_gas_us let excess_blob_gas = parent_excess_blob_gas + parent_blob_gas_used; excess_blob_gas.saturating_sub(TARGET_DATA_GAS_PER_BLOCK) } - -/// Calculates the versioned hash for a KzgCommitment -/// -/// Specified in [EIP-4844](https://eips.ethereum.org/EIPS/eip-4844#header-extension) -pub fn kzg_to_versioned_hash(commitment: KzgCommitment) -> H256 { - use sha2::Digest; - let mut hasher = sha2::Sha256::new(); - hasher.update(commitment.as_slice()); - let res = &mut hasher.finalize()[..]; - res[0] = VERSIONED_HASH_VERSION_KZG; - H256::from_slice(&res) -} \ No newline at end of file