Skip to content

Commit

Permalink
feat: port generate_blob_sidecar (alloy-rs#1511)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Oct 18, 2024
1 parent 0cb3cf6 commit 25d6a80
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions crates/eips/src/eip4844/sidecar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,34 @@ impl BlobTransactionSidecar {
self.commitments.len() * BYTES_PER_COMMITMENT + // commitments
self.proofs.len() * BYTES_PER_PROOF // proofs
}

/// Tries to create a new [`BlobTransactionSidecar`] from the given blobs.
#[cfg(all(feature = "kzg", any(test, feature = "arbitrary")))]
pub fn try_from_blobs(blobs: Vec<c_kzg::Blob>) -> Result<Self, c_kzg::Error> {
use crate::eip4844::env_settings::EnvKzgSettings;
use c_kzg::{KzgCommitment, KzgProof};

let kzg_settings = EnvKzgSettings::Default;

let commitments = blobs
.iter()
.map(|blob| {
KzgCommitment::blob_to_kzg_commitment(&blob.clone(), kzg_settings.get())
.map(|blob| blob.to_bytes())
})
.collect::<Result<Vec<_>, _>>()?;

let proofs = blobs
.iter()
.zip(commitments.iter())
.map(|(blob, commitment)| {
KzgProof::compute_blob_kzg_proof(blob, commitment, kzg_settings.get())
.map(|blob| blob.to_bytes())
})
.collect::<Result<Vec<_>, _>>()?;

Ok(Self::from_kzg(blobs, commitments, proofs))
}
}

impl Encodable for BlobTransactionSidecar {
Expand Down

0 comments on commit 25d6a80

Please sign in to comment.