refactor: merge signed tx traits (#14622)

This commit is contained in:
Léa Narzis
2025-02-24 14:43:31 +01:00
committed by GitHub
parent 480c33f932
commit 340d7ba008
16 changed files with 53 additions and 66 deletions

View File

@ -2,7 +2,7 @@
use crate::{
block::{error::SealedBlockRecoveryError, SealedBlock},
transaction::signed::{RecoveryError, SignedTransactionIntoRecoveredExt},
transaction::signed::{RecoveryError, SignedTransaction},
Block, BlockBody, InMemorySize, SealedHeader,
};
use alloc::vec::Vec;

View File

@ -99,6 +99,41 @@ pub trait SignedTransaction:
fn recalculate_hash(&self) -> B256 {
keccak256(self.encoded_2718())
}
/// Tries to recover signer and return [`Recovered`] by cloning the type.
#[auto_impl(keep_default_for(&, Arc))]
fn try_clone_into_recovered(&self) -> Result<Recovered<Self>, RecoveryError> {
self.recover_signer().map(|signer| 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`].
#[auto_impl(keep_default_for(&, Arc))]
fn try_into_recovered(self) -> Result<Recovered<Self>, Self> {
match self.recover_signer() {
Ok(signer) => Ok(Recovered::new_unchecked(self, signer)),
Err(_) => Err(self),
}
}
/// 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.
#[auto_impl(keep_default_for(&, Arc))]
fn into_recovered_unchecked(self) -> Result<Recovered<Self>, RecoveryError> {
self.recover_signer_unchecked().map(|signer| Recovered::new_unchecked(self, signer))
}
/// Returns the [`Recovered`] transaction with the given sender.
///
/// Note: assumes the given signer is the signer of this transaction.
#[auto_impl(keep_default_for(&, Arc))]
fn with_signer(self, signer: Address) -> Recovered<Self> {
Recovered::new_unchecked(self, signer)
}
}
impl SignedTransaction for PooledTransaction {
@ -183,42 +218,6 @@ impl SignedTransaction for op_alloy_consensus::OpPooledTransaction {
}
}
/// 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_clone_into_recovered(&self) -> Result<Recovered<Self>, RecoveryError> {
self.recover_signer().map(|signer| 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_recovered(self) -> Result<Recovered<Self>, Self> {
match self.recover_signer() {
Ok(signer) => Ok(Recovered::new_unchecked(self, signer)),
Err(_) => Err(self),
}
}
/// 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_recovered_unchecked(self) -> Result<Recovered<Self>, RecoveryError> {
self.recover_signer_unchecked().map(|signer| Recovered::new_unchecked(self, signer))
}
/// Returns the [`Recovered`] transaction with the given sender.
///
/// Note: assumes the given signer is the signer of this transaction.
fn with_signer(self, signer: Address) -> Recovered<Self> {
Recovered::new_unchecked(self, signer)
}
}
impl<T> SignedTransactionIntoRecoveredExt for T where T: SignedTransaction {}
/// Opaque error type for sender recovery.
#[derive(Debug, Default, thiserror::Error)]
#[error("Failed to recover the signer")]