reth_primitives_traits/transaction/
signed.rs

1//! API of a signed transaction.
2
3use crate::{FillTxEnv, InMemorySize, MaybeCompact, MaybeSerde};
4use alloc::{fmt, vec::Vec};
5use alloy_eips::{
6    eip2718::{Decodable2718, Encodable2718},
7    eip712::Decodable712,
8};
9use alloy_primitives::{keccak256, Address, PrimitiveSignature, TxHash, B256};
10use core::hash::Hash;
11
12/// Helper trait that unifies all behaviour required by block to support full node operations.
13pub trait FullSignedTx: SignedTransaction + FillTxEnv + MaybeCompact {}
14
15impl<T> FullSignedTx for T where T: SignedTransaction + FillTxEnv + MaybeCompact {}
16
17/// A signed transaction.
18#[auto_impl::auto_impl(&, Arc)]
19pub trait SignedTransaction:
20    Send
21    + Sync
22    + Unpin
23    + Clone
24    + fmt::Debug
25    + PartialEq
26    + Eq
27    + Hash
28    + alloy_rlp::Encodable
29    + alloy_rlp::Decodable
30    + Encodable2718
31    + Decodable2718
32    + Decodable712
33    + alloy_consensus::Transaction
34    + MaybeSerde
35    + InMemorySize
36{
37    /// Returns reference to transaction hash.
38    fn tx_hash(&self) -> &TxHash;
39
40    /// Returns reference to signature.
41    fn signature(&self) -> &PrimitiveSignature;
42
43    /// Returns whether this transaction type can be __broadcasted__ as full transaction over the
44    /// network.
45    ///
46    /// Some transactions are not broadcastable as objects and only allowed to be broadcasted as
47    /// hashes, e.g. because they missing context (e.g. blob sidecar).
48    fn is_broadcastable_in_full(&self) -> bool {
49        // EIP-4844 transactions are not broadcastable in full, only hashes are allowed.
50        !self.is_eip4844()
51    }
52
53    /// Recover signer from signature and hash.
54    ///
55    /// Returns `None` if the transaction's signature is invalid following [EIP-2](https://eips.ethereum.org/EIPS/eip-2), see also `reth_primitives::transaction::recover_signer`.
56    ///
57    /// Note:
58    ///
59    /// This can fail for some early ethereum mainnet transactions pre EIP-2, use
60    /// [`Self::recover_signer_unchecked`] if you want to recover the signer without ensuring that
61    /// the signature has a low `s` value.
62    fn recover_signer(&self) -> Option<Address>;
63
64    /// Recover signer from signature and hash _without ensuring that the signature has a low `s`
65    /// value_.
66    ///
67    /// Returns `None` if the transaction's signature is invalid, see also
68    /// `reth_primitives::transaction::recover_signer_unchecked`.
69    fn recover_signer_unchecked(&self) -> Option<Address> {
70        self.recover_signer_unchecked_with_buf(&mut Vec::new())
71    }
72
73    /// Same as [`Self::recover_signer_unchecked`] but receives a buffer to operate on. This is used
74    /// during batch recovery to avoid allocating a new buffer for each transaction.
75    fn recover_signer_unchecked_with_buf(&self, buf: &mut Vec<u8>) -> Option<Address>;
76
77    /// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with
78    /// tx type.
79    fn recalculate_hash(&self) -> B256 {
80        keccak256(self.encoded_2718())
81    }
82}