chore(sdk): SignedTransaction abstraction (#11432)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Emilia Hane
2024-10-18 11:43:23 +02:00
committed by GitHub
parent cb604826b7
commit cfd066c071
5 changed files with 84 additions and 1 deletions

View File

@ -24,7 +24,7 @@ pub mod receipt;
pub use receipt::Receipt; pub use receipt::Receipt;
pub mod transaction; pub mod transaction;
pub use transaction::Transaction; pub use transaction::{signed::SignedTransaction, Transaction};
mod integer_list; mod integer_list;
pub use integer_list::{IntegerList, IntegerListError}; pub use integer_list::{IntegerList, IntegerListError};

View File

View File

@ -1,5 +1,7 @@
//! Transaction abstraction //! Transaction abstraction
pub mod signed;
use alloc::fmt; use alloc::fmt;
use reth_codecs::Compact; use reth_codecs::Compact;

View File

@ -0,0 +1,72 @@
//! API of a signed transaction.
use alloc::fmt;
use core::hash::Hash;
use alloy_consensus::Transaction;
use alloy_eips::eip2718::{Decodable2718, Encodable2718};
use alloy_primitives::{keccak256, Address, TxHash, B256};
/// A signed transaction.
pub trait SignedTransaction:
fmt::Debug
+ Clone
+ PartialEq
+ Eq
+ Hash
+ Send
+ Sync
+ serde::Serialize
+ for<'a> serde::Deserialize<'a>
+ alloy_rlp::Encodable
+ alloy_rlp::Decodable
+ Encodable2718
+ Decodable2718
{
/// Transaction type that is signed.
type Transaction: Transaction;
/// Signature type that results from signing transaction.
type Signature;
/// Returns reference to transaction hash.
fn tx_hash(&self) -> &TxHash;
/// Returns reference to transaction.
fn transaction(&self) -> &Self::Transaction;
/// Returns reference to signature.
fn signature(&self) -> &Self::Signature;
/// Recover signer from signature and hash.
///
/// 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`.
///
/// Note:
///
/// This can fail for some early ethereum mainnet transactions pre EIP-2, use
/// [`Self::recover_signer_unchecked`] if you want to recover the signer without ensuring that
/// the signature has a low `s` value.
fn recover_signer(&self) -> Option<Address>;
/// Recover signer from signature and hash _without ensuring that the signature has a low `s`
/// value_.
///
/// Returns `None` if the transaction's signature is invalid, see also
/// `reth_primitives::transaction::recover_signer_unchecked`.
fn recover_signer_unchecked(&self) -> Option<Address>;
/// Create a new signed transaction from a transaction and its signature.
///
/// This will also calculate the transaction hash using its encoding.
fn from_transaction_and_signature(
transaction: Self::Transaction,
signature: Self::Signature,
) -> Self;
/// Calculate transaction hash, eip2728 transaction does not contain rlp header and start with
/// tx type.
fn recalculate_hash(&self) -> B256 {
keccak256(self.encoded_2718())
}
}

View File

@ -0,0 +1,9 @@
//! Abstractions of primitive data types
pub mod block;
pub mod transaction;
pub use block::{body::BlockBody, Block};
pub use transaction::signed::SignedTransaction;
pub use alloy_consensus::BlockHeader;