|
| 1 | +//! API of a signed transaction. |
| 2 | +
|
| 3 | +use alloc::fmt; |
| 4 | +use core::hash::Hash; |
| 5 | + |
| 6 | +use alloy_consensus::Transaction; |
| 7 | +use alloy_eips::eip2718::{Decodable2718, Encodable2718}; |
| 8 | +use alloy_primitives::{keccak256, Address, TxHash, B256}; |
| 9 | + |
| 10 | +/// A signed transaction. |
| 11 | +pub trait SignedTransaction: |
| 12 | + fmt::Debug |
| 13 | + + Clone |
| 14 | + + PartialEq |
| 15 | + + Eq |
| 16 | + + Hash |
| 17 | + + Send |
| 18 | + + Sync |
| 19 | + + serde::Serialize |
| 20 | + + for<'a> serde::Deserialize<'a> |
| 21 | + + alloy_rlp::Encodable |
| 22 | + + alloy_rlp::Decodable |
| 23 | + + Encodable2718 |
| 24 | + + Decodable2718 |
| 25 | +{ |
| 26 | + /// Transaction type that is signed. |
| 27 | + type Transaction: Transaction; |
| 28 | + |
| 29 | + /// Signature type that results from signing transaction. |
| 30 | + type Signature; |
| 31 | + |
| 32 | + /// Returns reference to transaction hash. |
| 33 | + fn tx_hash(&self) -> &TxHash; |
| 34 | + |
| 35 | + /// Returns reference to transaction. |
| 36 | + fn transaction(&self) -> &Self::Transaction; |
| 37 | + |
| 38 | + /// Returns reference to signature. |
| 39 | + fn signature(&self) -> &Self::Signature; |
| 40 | + |
| 41 | + /// Recover signer from signature and hash. |
| 42 | + /// |
| 43 | + /// 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`. |
| 44 | + /// |
| 45 | + /// Note: |
| 46 | + /// |
| 47 | + /// This can fail for some early ethereum mainnet transactions pre EIP-2, use |
| 48 | + /// [`Self::recover_signer_unchecked`] if you want to recover the signer without ensuring that |
| 49 | + /// the signature has a low `s` value. |
| 50 | + fn recover_signer(&self) -> Option<Address>; |
| 51 | + |
| 52 | + /// Recover signer from signature and hash _without ensuring that the signature has a low `s` |
| 53 | + /// value_. |
| 54 | + /// |
| 55 | + /// Returns `None` if the transaction's signature is invalid, see also |
| 56 | + /// `reth_primitives::transaction::recover_signer_unchecked`. |
| 57 | + fn recover_signer_unchecked(&self) -> Option<Address>; |
| 58 | + |
| 59 | + /// Create a new signed transaction from a transaction and its signature. |
| 60 | + /// |
| 61 | + /// This will also calculate the transaction hash using its encoding. |
| 62 | + fn from_transaction_and_signature( |
| 63 | + transaction: Self::Transaction, |
| 64 | + signature: Self::Signature, |
| 65 | + ) -> Self; |
| 66 | + |
| 67 | + /// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with |
| 68 | + /// tx type. |
| 69 | + fn recalculate_hash(&self) -> B256 { |
| 70 | + keccak256(self.encoded_2718()) |
| 71 | + } |
| 72 | +} |
0 commit comments