From 8cb6be785bea65adc169c1daf54ecbd94e7e29b7 Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 26 Jul 2024 10:13:01 -0400 Subject: [PATCH 1/6] fix: place public key recovery behind k256 feat flag --- bin/client/Cargo.toml | 2 +- crates/derive/src/sources/blobs.rs | 7 ++++++- crates/derive/src/sources/calldata.rs | 7 ++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/bin/client/Cargo.toml b/bin/client/Cargo.toml index 04ccf8ad..ff07bfce 100644 --- a/bin/client/Cargo.toml +++ b/bin/client/Cargo.toml @@ -29,7 +29,7 @@ kona-common-proc = { path = "../../crates/common-proc", version = "0.0.2" } kona-preimage = { path = "../../crates/preimage", version = "0.0.2" } kona-primitives = { path = "../../crates/primitives", version = "0.0.1" } kona-mpt = { path = "../../crates/mpt", version = "0.0.2" } -kona-derive = { path = "../../crates/derive", default-features = false, version = "0.0.2" } +kona-derive = { path = "../../crates/derive", default-features = false, features = ["k256"], version = "0.0.2" } kona-executor = { path = "../../crates/executor", version = "0.0.1" } # external diff --git a/crates/derive/src/sources/blobs.rs b/crates/derive/src/sources/blobs.rs index 2379f142..8f2e5db8 100644 --- a/crates/derive/src/sources/blobs.rs +++ b/crates/derive/src/sources/blobs.rs @@ -1,7 +1,7 @@ //! Blob Data Source use crate::{ - traits::{AsyncIterator, BlobProvider, ChainProvider, SignedRecoverable}, + traits::{AsyncIterator, BlobProvider, ChainProvider}, types::{BlobData, BlockInfo, IndexedBlobHash, StageError, StageResult}, }; use alloc::{boxed::Box, vec::Vec}; @@ -11,6 +11,9 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use tracing::warn; +#[cfg(feature = "k256")] +use crate::traits::SignedRecoverable; + /// A data iterator that reads from a blob. #[derive(Debug, Clone)] pub struct BlobSource @@ -27,6 +30,7 @@ where /// Block Ref block_ref: BlockInfo, /// The L1 Signer. + #[allow(dead_code)] // Allow dead code for non-k256 builds. signer: Address, /// Data. data: Vec, @@ -84,6 +88,7 @@ where index += blob_hashes.map_or(0, |h| h.len()); continue; } + #[cfg(feature = "k256")] if tx.recover_public_key().unwrap_or_default() != self.signer { index += blob_hashes.map_or(0, |h| h.len()); continue; diff --git a/crates/derive/src/sources/calldata.rs b/crates/derive/src/sources/calldata.rs index 8d3c5ccb..7c581b56 100644 --- a/crates/derive/src/sources/calldata.rs +++ b/crates/derive/src/sources/calldata.rs @@ -1,7 +1,7 @@ //! CallData Source use crate::{ - traits::{AsyncIterator, ChainProvider, SignedRecoverable}, + traits::{AsyncIterator, ChainProvider}, types::{BlockInfo, StageError, StageResult}, }; use alloc::{boxed::Box, collections::VecDeque}; @@ -10,6 +10,9 @@ use alloy_primitives::{Address, Bytes, TxKind}; use anyhow::Result; use async_trait::async_trait; +#[cfg(feature = "k256")] +use crate::traits::SignedRecoverable; + /// A data iterator that reads from calldata. #[derive(Debug, Clone)] pub struct CalldataSource @@ -23,6 +26,7 @@ where /// Block Ref block_ref: BlockInfo, /// The L1 Signer. + #[allow(dead_code)] // Allow dead code for non-k256 builds. signer: Address, /// Current calldata. calldata: VecDeque, @@ -71,6 +75,7 @@ impl CalldataSource { if to != self.batch_inbox_address { return None; } + #[cfg(feature = "k256")] if tx.recover_public_key().ok()? != self.signer { return None; } From d493cb1d23a70d97765c1ca61a008eb631924307 Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 26 Jul 2024 10:38:47 -0400 Subject: [PATCH 2/6] remove shim --- crates/derive/src/sources/blobs.rs | 6 +----- crates/derive/src/sources/calldata.rs | 6 +----- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/crates/derive/src/sources/blobs.rs b/crates/derive/src/sources/blobs.rs index 8f2e5db8..c34e3281 100644 --- a/crates/derive/src/sources/blobs.rs +++ b/crates/derive/src/sources/blobs.rs @@ -11,9 +11,6 @@ use anyhow::{anyhow, Result}; use async_trait::async_trait; use tracing::warn; -#[cfg(feature = "k256")] -use crate::traits::SignedRecoverable; - /// A data iterator that reads from a blob. #[derive(Debug, Clone)] pub struct BlobSource @@ -88,8 +85,7 @@ where index += blob_hashes.map_or(0, |h| h.len()); continue; } - #[cfg(feature = "k256")] - if tx.recover_public_key().unwrap_or_default() != self.signer { + if tx.recover_signer().unwrap_or_default() != self.signer { index += blob_hashes.map_or(0, |h| h.len()); continue; } diff --git a/crates/derive/src/sources/calldata.rs b/crates/derive/src/sources/calldata.rs index 7c581b56..0ac9690d 100644 --- a/crates/derive/src/sources/calldata.rs +++ b/crates/derive/src/sources/calldata.rs @@ -10,9 +10,6 @@ use alloy_primitives::{Address, Bytes, TxKind}; use anyhow::Result; use async_trait::async_trait; -#[cfg(feature = "k256")] -use crate::traits::SignedRecoverable; - /// A data iterator that reads from calldata. #[derive(Debug, Clone)] pub struct CalldataSource @@ -75,8 +72,7 @@ impl CalldataSource { if to != self.batch_inbox_address { return None; } - #[cfg(feature = "k256")] - if tx.recover_public_key().ok()? != self.signer { + if tx.recover_signer().ok()? != self.signer { return None; } Some(data.to_vec().into()) From d98bd6417a3b452026082e97511ced73f382a4f5 Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 26 Jul 2024 10:42:07 -0400 Subject: [PATCH 3/6] remove shim --- crates/derive/src/traits/ecrecover.rs | 49 --------------------------- crates/derive/src/traits/mod.rs | 3 -- 2 files changed, 52 deletions(-) delete mode 100644 crates/derive/src/traits/ecrecover.rs diff --git a/crates/derive/src/traits/ecrecover.rs b/crates/derive/src/traits/ecrecover.rs deleted file mode 100644 index 3a7718a4..00000000 --- a/crates/derive/src/traits/ecrecover.rs +++ /dev/null @@ -1,49 +0,0 @@ -//! This module contains the [SignedRecoverable] trait. -//! -//! This trait exists to allow for alternative implementations of the `recover_public_key` method -//! for signed types that can supply the original message hash for public key recovery. By default, -//! it is implemented for [alloy_consensus::TxEnvelope] if the `k256` feature is enabled. - -use alloy_primitives::Address; -use anyhow::Result; - -#[cfg(feature = "k256")] -use alloy_consensus::TxEnvelope; -#[cfg(feature = "k256")] -use alloy_primitives::{Signature, B256}; -#[cfg(feature = "k256")] -use anyhow::anyhow; - -/// Represents a signed transaction that can be recovered. -pub trait SignedRecoverable { - /// Recovers the public key from the signature and the message hash. - fn recover_public_key(&self) -> Result
; -} - -#[cfg(feature = "k256")] -impl SignedRecoverable for TxEnvelope { - fn recover_public_key(&self) -> Result
{ - match self { - TxEnvelope::Legacy(signed_tx) => { - recover_public_key(*signed_tx.signature(), &signed_tx.signature_hash()) - } - TxEnvelope::Eip2930(signed_tx) => { - recover_public_key(*signed_tx.signature(), &signed_tx.signature_hash()) - } - TxEnvelope::Eip1559(signed_tx) => { - recover_public_key(*signed_tx.signature(), &signed_tx.signature_hash()) - } - TxEnvelope::Eip4844(signed_tx) => { - recover_public_key(*signed_tx.signature(), &signed_tx.signature_hash()) - } - _ => unreachable!("Impossible case"), - } - } -} - -/// Recovers the public key from a signature and a message hash. -#[cfg(feature = "k256")] -#[inline] -fn recover_public_key(sig: Signature, message_hash: &B256) -> Result
{ - sig.recover_address_from_prehash(message_hash).map_err(|e| anyhow!(e)) -} diff --git a/crates/derive/src/traits/mod.rs b/crates/derive/src/traits/mod.rs index 7ac17d7b..59d3075d 100644 --- a/crates/derive/src/traits/mod.rs +++ b/crates/derive/src/traits/mod.rs @@ -19,8 +19,5 @@ pub use providers::{ChainProvider, L2ChainProvider}; mod stages; pub use stages::{OriginAdvancer, OriginProvider, PreviousStage, ResettableStage}; -mod ecrecover; -pub use ecrecover::SignedRecoverable; - #[cfg(any(test, feature = "test-utils"))] pub mod test_utils; From 1db86a54f6ed4cc691a4ba71b0e11bec60a8f4dc Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 26 Jul 2024 11:16:38 -0400 Subject: [PATCH 4/6] remove k256 feat flag --- bin/client/Cargo.toml | 2 +- crates/derive/Cargo.toml | 11 +++++------ examples/trusted-sync/Cargo.toml | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/bin/client/Cargo.toml b/bin/client/Cargo.toml index ff07bfce..04ccf8ad 100644 --- a/bin/client/Cargo.toml +++ b/bin/client/Cargo.toml @@ -29,7 +29,7 @@ kona-common-proc = { path = "../../crates/common-proc", version = "0.0.2" } kona-preimage = { path = "../../crates/preimage", version = "0.0.2" } kona-primitives = { path = "../../crates/primitives", version = "0.0.1" } kona-mpt = { path = "../../crates/mpt", version = "0.0.2" } -kona-derive = { path = "../../crates/derive", default-features = false, features = ["k256"], version = "0.0.2" } +kona-derive = { path = "../../crates/derive", default-features = false, version = "0.0.2" } kona-executor = { path = "../../crates/executor", version = "0.0.1" } # external diff --git a/crates/derive/Cargo.toml b/crates/derive/Cargo.toml index 581a8c81..71df400c 100644 --- a/crates/derive/Cargo.toml +++ b/crates/derive/Cargo.toml @@ -10,11 +10,11 @@ homepage.workspace = true [dependencies] # Workspace Alloy Dependencies -alloy-consensus.workspace = true -alloy-primitives = { workspace = true, features = ["rlp"] } -alloy-rlp = { workspace = true, features = ["derive"] } alloy-eips.workspace = true -op-alloy-consensus.workspace = true +alloy-rlp = { workspace = true, features = ["derive"] } +alloy-consensus = { workspace = true, features = ["k256"] } +alloy-primitives = { workspace = true, features = ["rlp", "k256"] } +op-alloy-consensus = { workspace = true, features = ["k256"] } # Other Workspace Dependencies lru.workspace = true @@ -66,7 +66,7 @@ alloy-rpc-client = { version = "0.1", default-features = false } serde_json = { version = "1.0.117", default-features = false } [features] -default = ["serde", "k256"] +default = ["serde"] serde = [ "dep:serde", "kona-primitives/serde", @@ -74,7 +74,6 @@ serde = [ "alloy-consensus/serde", "op-alloy-consensus/serde" ] -k256 = ["alloy-primitives/k256", "alloy-consensus/k256", "op-alloy-consensus/k256"] metrics = ["dep:prometheus", "dep:lazy_static"] online = [ "dep:serde_json", diff --git a/examples/trusted-sync/Cargo.toml b/examples/trusted-sync/Cargo.toml index 83975523..169edba6 100644 --- a/examples/trusted-sync/Cargo.toml +++ b/examples/trusted-sync/Cargo.toml @@ -14,7 +14,7 @@ homepage.workspace = true anyhow.workspace = true tracing.workspace = true alloy-primitives = { workspace = true, features = ["serde"] } -kona-derive = { path = "../../crates/derive", version = "0.0.2", features = ["serde", "k256", "online", "metrics"] } +kona-derive = { path = "../../crates/derive", version = "0.0.2", features = ["serde", "online", "metrics"] } # Custom dependencies lazy_static = "1.5.0" From 16cd99d18c4aca0fef372fafe72f9112bb4640fe Mon Sep 17 00:00:00 2001 From: clabby Date: Fri, 26 Jul 2024 11:30:15 -0400 Subject: [PATCH 5/6] Update crates/derive/src/sources/calldata.rs --- crates/derive/src/sources/calldata.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/derive/src/sources/calldata.rs b/crates/derive/src/sources/calldata.rs index 0ac9690d..77aa2f8a 100644 --- a/crates/derive/src/sources/calldata.rs +++ b/crates/derive/src/sources/calldata.rs @@ -23,7 +23,6 @@ where /// Block Ref block_ref: BlockInfo, /// The L1 Signer. - #[allow(dead_code)] // Allow dead code for non-k256 builds. signer: Address, /// Current calldata. calldata: VecDeque, From 4ea00d4ac3df3ff0b5ccc1ee91b9bbbad026d9ab Mon Sep 17 00:00:00 2001 From: clabby Date: Fri, 26 Jul 2024 11:30:21 -0400 Subject: [PATCH 6/6] Update crates/derive/src/sources/blobs.rs --- crates/derive/src/sources/blobs.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/crates/derive/src/sources/blobs.rs b/crates/derive/src/sources/blobs.rs index c34e3281..fa5db69e 100644 --- a/crates/derive/src/sources/blobs.rs +++ b/crates/derive/src/sources/blobs.rs @@ -27,7 +27,6 @@ where /// Block Ref block_ref: BlockInfo, /// The L1 Signer. - #[allow(dead_code)] // Allow dead code for non-k256 builds. signer: Address, /// Data. data: Vec,