Skip to main content

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, IsTyped2718};
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    + IsTyped2718
50{
51    /// Returns whether this is a system transaction.
52    ///
53    /// System transactions are created at the protocol level rather than by users. They are
54    /// typically used by L2s for special purposes (e.g., Optimism deposit transactions with type
55    /// 126) and may have different validation rules or fee handling compared to standard
56    /// user-initiated transactions.
57    fn is_system_tx(&self) -> bool {
58        false
59    }
60
61    /// Returns whether this transaction type can be __broadcasted__ as full transaction over the
62    /// network.
63    ///
64    /// Some transactions are not broadcastable as objects and only allowed to be broadcasted as
65    /// hashes, e.g. because they missing context (e.g. blob sidecar).
66    fn is_broadcastable_in_full(&self) -> bool {
67        // EIP-4844 transactions are not broadcastable in full, only hashes are allowed.
68        !self.is_eip4844()
69    }
70
71    /// Recover signer from signature and hash.
72    ///
73    /// Returns an error if the transaction's signature is invalid.
74    fn try_recover(&self) -> Result<Address, RecoveryError> {
75        self.recover_signer()
76    }
77
78    /// Recover signer from signature and hash _without ensuring that the signature has a low `s`
79    /// value_.
80    ///
81    /// Returns an error if the transaction's signature is invalid.
82    fn try_recover_unchecked(&self) -> Result<Address, RecoveryError> {
83        self.recover_signer_unchecked()
84    }
85
86    /// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with
87    /// tx type.
88    fn recalculate_hash(&self) -> B256 {
89        keccak256(self.encoded_2718())
90    }
91
92    /// Tries to recover signer and return [`Recovered`] by cloning the type.
93    #[auto_impl(keep_default_for(&, Arc))]
94    fn try_clone_into_recovered(&self) -> Result<Recovered<Self>, RecoveryError> {
95        self.recover_signer().map(|signer| Recovered::new_unchecked(self.clone(), signer))
96    }
97
98    /// Tries to recover signer and return [`Recovered`] by cloning the type.
99    #[auto_impl(keep_default_for(&, Arc))]
100    fn try_clone_into_recovered_unchecked(&self) -> Result<Recovered<Self>, RecoveryError> {
101        self.recover_signer_unchecked().map(|signer| Recovered::new_unchecked(self.clone(), signer))
102    }
103
104    /// Tries to recover signer and return [`Recovered`].
105    ///
106    /// Returns `Err(Self)` if the transaction's signature is invalid, see also
107    /// [`SignerRecoverable::recover_signer`].
108    #[auto_impl(keep_default_for(&, Arc))]
109    fn try_into_recovered(self) -> Result<Recovered<Self>, Self> {
110        match self.recover_signer() {
111            Ok(signer) => Ok(Recovered::new_unchecked(self, signer)),
112            Err(_) => Err(self),
113        }
114    }
115
116    /// Consumes the type, recover signer and return [`Recovered`] _without
117    /// ensuring that the signature has a low `s` value_ (EIP-2).
118    ///
119    /// Returns `RecoveryError` if the transaction's signature is invalid.
120    #[deprecated(note = "Use try_into_recovered_unchecked instead")]
121    #[auto_impl(keep_default_for(&, Arc))]
122    fn into_recovered_unchecked(self) -> Result<Recovered<Self>, RecoveryError> {
123        self.recover_signer_unchecked().map(|signer| Recovered::new_unchecked(self, signer))
124    }
125
126    /// Returns the [`Recovered`] transaction with the given sender.
127    ///
128    /// Note: assumes the given signer is the signer of this transaction.
129    #[auto_impl(keep_default_for(&, Arc))]
130    fn with_signer(self, signer: Address) -> Recovered<Self> {
131        Recovered::new_unchecked(self, signer)
132    }
133
134    /// Returns the [`Recovered`] transaction with the given signer, using a reference to self.
135    ///
136    /// Note: assumes the given signer is the signer of this transaction.
137    #[auto_impl(keep_default_for(&, Arc))]
138    fn with_signer_ref(&self, signer: Address) -> Recovered<&Self> {
139        Recovered::new_unchecked(self, signer)
140    }
141}
142
143impl<T> SignedTransaction for EthereumTxEnvelope<T>
144where
145    T: RlpEcdsaEncodableTx + SignableTransaction<Signature> + Unpin,
146    Self: Clone + PartialEq + Eq + Decodable + Decodable2718 + MaybeSerde + InMemorySize,
147{
148}
149
150#[cfg(feature = "op")]
151mod op {
152    use super::*;
153    use op_alloy_consensus::{OpPooledTransaction, OpTxEnvelope};
154
155    impl SignedTransaction for OpPooledTransaction {}
156
157    impl SignedTransaction for OpTxEnvelope {}
158}