From 28fde3fbf5c6803ded52aabca4b57845dec45ad3 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Mon, 6 Jan 2025 21:53:03 +0100 Subject: [PATCH] chore: move signedtx ext trait (#13677) --- .../src/transaction/signed.rs | 41 +++++++++++++++++- crates/primitives/src/transaction/mod.rs | 43 +++---------------- 2 files changed, 45 insertions(+), 39 deletions(-) diff --git a/crates/primitives-traits/src/transaction/signed.rs b/crates/primitives-traits/src/transaction/signed.rs index 53338dc4dcf7..ddc79829ad1c 100644 --- a/crates/primitives-traits/src/transaction/signed.rs +++ b/crates/primitives-traits/src/transaction/signed.rs @@ -5,7 +5,10 @@ use crate::{ FillTxEnv, InMemorySize, MaybeCompact, MaybeSerde, }; use alloc::{fmt, vec::Vec}; -use alloy_consensus::{transaction::PooledTransaction, SignableTransaction}; +use alloy_consensus::{ + transaction::{PooledTransaction, Recovered}, + SignableTransaction, +}; use alloy_eips::eip2718::{Decodable2718, Encodable2718}; use alloy_primitives::{keccak256, Address, PrimitiveSignature as Signature, TxHash, B256}; use core::hash::Hash; @@ -156,3 +159,39 @@ impl SignedTransaction for op_alloy_consensus::OpPooledTransaction { recover_signer_unchecked(self.signature(), signature_hash) } } + +/// Extension trait for [`SignedTransaction`] to convert it into [`Recovered`]. +pub trait SignedTransactionIntoRecoveredExt: SignedTransaction { + /// Tries to recover signer and return [`Recovered`] by cloning the type. + fn try_ecrecovered(&self) -> Option> { + let signer = self.recover_signer()?; + Some(Recovered::new_unchecked(self.clone(), signer)) + } + + /// Tries to recover signer and return [`Recovered`]. + /// + /// Returns `Err(Self)` if the transaction's signature is invalid, see also + /// [`SignedTransaction::recover_signer`]. + fn try_into_ecrecovered(self) -> Result, Self> { + match self.recover_signer() { + None => Err(self), + Some(signer) => Ok(Recovered::new_unchecked(self, signer)), + } + } + + /// Consumes the type, recover signer and return [`Recovered`] _without + /// ensuring that the signature has a low `s` value_ (EIP-2). + /// + /// Returns `None` if the transaction's signature is invalid. + fn into_ecrecovered_unchecked(self) -> Option> { + let signer = self.recover_signer_unchecked()?; + Some(Recovered::new_unchecked(self, signer)) + } + + /// Returns the [`Recovered`] transaction with the given sender. + fn with_signer(self, signer: Address) -> Recovered { + Recovered::new_unchecked(self, signer) + } +} + +impl SignedTransactionIntoRecoveredExt for T where T: SignedTransaction {} diff --git a/crates/primitives/src/transaction/mod.rs b/crates/primitives/src/transaction/mod.rs index 5070ad1b2662..c838f6593645 100644 --- a/crates/primitives/src/transaction/mod.rs +++ b/crates/primitives/src/transaction/mod.rs @@ -30,8 +30,11 @@ use op_alloy_consensus::TxDeposit; pub use pooled::PooledTransactionsElementEcRecovered; use rayon::prelude::{IntoParallelIterator, ParallelIterator}; pub use reth_primitives_traits::{ - transaction::error::{ - InvalidTransactionError, TransactionConversionError, TryFromRecoveredTransactionError, + transaction::{ + error::{ + InvalidTransactionError, TransactionConversionError, TryFromRecoveredTransactionError, + }, + signed::SignedTransactionIntoRecoveredExt, }, FillTxEnv, WithEncoded, }; @@ -1515,42 +1518,6 @@ impl<'a> arbitrary::Arbitrary<'a> for TransactionSigned { /// Type alias kept for backward compatibility. pub type TransactionSignedEcRecovered = RecoveredTx; -/// Extension trait for [`SignedTransaction`] to convert it into [`RecoveredTx`]. -pub trait SignedTransactionIntoRecoveredExt: SignedTransaction { - /// Tries to recover signer and return [`RecoveredTx`] by cloning the type. - fn try_ecrecovered(&self) -> Option> { - let signer = self.recover_signer()?; - Some(RecoveredTx::new_unchecked(self.clone(), signer)) - } - - /// Tries to recover signer and return [`RecoveredTx`]. - /// - /// Returns `Err(Self)` if the transaction's signature is invalid, see also - /// [`SignedTransaction::recover_signer`]. - fn try_into_ecrecovered(self) -> Result, Self> { - match self.recover_signer() { - None => Err(self), - Some(signer) => Ok(RecoveredTx::new_unchecked(self, signer)), - } - } - - /// Consumes the type, recover signer and return [`RecoveredTx`] _without - /// ensuring that the signature has a low `s` value_ (EIP-2). - /// - /// Returns `None` if the transaction's signature is invalid. - fn into_ecrecovered_unchecked(self) -> Option> { - let signer = self.recover_signer_unchecked()?; - Some(RecoveredTx::new_unchecked(self, signer)) - } - - /// Returns the [`RecoveredTx`] transaction with the given sender. - fn with_signer(self, signer: Address) -> RecoveredTx { - RecoveredTx::new_unchecked(self, signer) - } -} - -impl SignedTransactionIntoRecoveredExt for T where T: SignedTransaction {} - /// Bincode-compatible transaction type serde implementations. #[cfg(feature = "serde-bincode-compat")] pub mod serde_bincode_compat {