reth_primitives_traits/transaction/
signed.rs

1//! API of a signed transaction.
2
3use crate::{InMemorySize, MaybeCompact, MaybeSerde, MaybeSerdeBincodeCompat};
4use alloc::fmt;
5use alloy_consensus::{
6    transaction::{Recovered, RlpEcdsaEncodableTx, SignerRecoverable, TxHashRef},
7    EthereumTxEnvelope, SignableTransaction,
8};
9use alloy_eips::eip2718::{Decodable2718, Encodable2718};
10use alloy_primitives::{keccak256, Address, Signature, B256};
11use alloy_rlp::{Decodable, Encodable};
12use core::hash::Hash;
13
14pub use alloy_consensus::crypto::RecoveryError;
15
16/// Helper trait that unifies all behaviour required by block to support full node operations.
17pub trait FullSignedTx: SignedTransaction + MaybeCompact + MaybeSerdeBincodeCompat {}
18impl<T> FullSignedTx for T where T: SignedTransaction + MaybeCompact + MaybeSerdeBincodeCompat {}
19
20/// A signed transaction.
21///
22/// # Recovery Methods
23///
24/// This trait provides two types of recovery methods:
25/// - Standard methods (e.g., `try_recover`) - enforce EIP-2 low-s signature requirement
26/// - Unchecked methods (e.g., `try_recover_unchecked`) - skip EIP-2 validation for pre-EIP-2
27///   transactions
28///
29/// Use unchecked methods only when dealing with historical pre-EIP-2 transactions.
30#[auto_impl::auto_impl(&, Arc)]
31pub trait SignedTransaction:
32    Send
33    + Sync
34    + Unpin
35    + Clone
36    + fmt::Debug
37    + PartialEq
38    + Eq
39    + Hash
40    + Encodable
41    + Decodable
42    + Encodable2718
43    + Decodable2718
44    + alloy_consensus::Transaction
45    + MaybeSerde
46    + InMemorySize
47    + SignerRecoverable
48    + TxHashRef
49{
50    /// Returns whether this transaction type can be __broadcasted__ as full transaction over the
51    /// network.
52    ///
53    /// Some transactions are not broadcastable as objects and only allowed to be broadcasted as
54    /// hashes, e.g. because they missing context (e.g. blob sidecar).
55    fn is_broadcastable_in_full(&self) -> bool {
56        // EIP-4844 transactions are not broadcastable in full, only hashes are allowed.
57        !self.is_eip4844()
58    }
59
60    /// Recover signer from signature and hash.
61    ///
62    /// Returns an error if the transaction's signature is invalid.
63    fn try_recover(&self) -> Result<Address, RecoveryError> {
64        self.recover_signer()
65    }
66
67    /// Recover signer from signature and hash _without ensuring that the signature has a low `s`
68    /// value_.
69    ///
70    /// Returns an error if the transaction's signature is invalid.
71    fn try_recover_unchecked(&self) -> Result<Address, RecoveryError> {
72        self.recover_signer_unchecked()
73    }
74
75    /// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with
76    /// tx type.
77    fn recalculate_hash(&self) -> B256 {
78        keccak256(self.encoded_2718())
79    }
80
81    /// Tries to recover signer and return [`Recovered`] by cloning the type.
82    #[auto_impl(keep_default_for(&, Arc))]
83    fn try_clone_into_recovered(&self) -> Result<Recovered<Self>, RecoveryError> {
84        self.recover_signer().map(|signer| Recovered::new_unchecked(self.clone(), signer))
85    }
86
87    /// Tries to recover signer and return [`Recovered`] by cloning the type.
88    #[auto_impl(keep_default_for(&, Arc))]
89    fn try_clone_into_recovered_unchecked(&self) -> Result<Recovered<Self>, RecoveryError> {
90        self.recover_signer_unchecked().map(|signer| Recovered::new_unchecked(self.clone(), signer))
91    }
92
93    /// Tries to recover signer and return [`Recovered`].
94    ///
95    /// Returns `Err(Self)` if the transaction's signature is invalid, see also
96    /// [`SignerRecoverable::recover_signer`].
97    #[auto_impl(keep_default_for(&, Arc))]
98    fn try_into_recovered(self) -> Result<Recovered<Self>, Self> {
99        match self.recover_signer() {
100            Ok(signer) => Ok(Recovered::new_unchecked(self, signer)),
101            Err(_) => Err(self),
102        }
103    }
104
105    /// Consumes the type, recover signer and return [`Recovered`] _without
106    /// ensuring that the signature has a low `s` value_ (EIP-2).
107    ///
108    /// Returns `RecoveryError` if the transaction's signature is invalid.
109    #[deprecated(note = "Use try_into_recovered_unchecked instead")]
110    #[auto_impl(keep_default_for(&, Arc))]
111    fn into_recovered_unchecked(self) -> Result<Recovered<Self>, RecoveryError> {
112        self.recover_signer_unchecked().map(|signer| Recovered::new_unchecked(self, signer))
113    }
114
115    /// Returns the [`Recovered`] transaction with the given sender.
116    ///
117    /// Note: assumes the given signer is the signer of this transaction.
118    #[auto_impl(keep_default_for(&, Arc))]
119    fn with_signer(self, signer: Address) -> Recovered<Self> {
120        Recovered::new_unchecked(self, signer)
121    }
122
123    /// Returns the [`Recovered`] transaction with the given signer, using a reference to self.
124    ///
125    /// Note: assumes the given signer is the signer of this transaction.
126    #[auto_impl(keep_default_for(&, Arc))]
127    fn with_signer_ref(&self, signer: Address) -> Recovered<&Self> {
128        Recovered::new_unchecked(self, signer)
129    }
130}
131
132impl<T> SignedTransaction for EthereumTxEnvelope<T>
133where
134    T: RlpEcdsaEncodableTx + SignableTransaction<Signature> + Unpin,
135    Self: Clone + PartialEq + Eq + Decodable + Decodable2718 + MaybeSerde + InMemorySize,
136{
137}
138
139#[cfg(feature = "op")]
140mod op {
141    use super::*;
142    use op_alloy_consensus::{OpPooledTransaction, OpTxEnvelope};
143
144    impl SignedTransaction for OpPooledTransaction {}
145
146    impl SignedTransaction for OpTxEnvelope {}
147}