chore: add hash+signer fn (#5493)

This commit is contained in:
Matthias Seitz
2023-11-19 16:19:00 +01:00
committed by GitHub
parent 14dd9e8150
commit 631eb2b624

View File

@ -1,8 +1,10 @@
//! Helper enum functions for `Transaction`, `TransactionSigned` and
//! `TransactionSignedEcRecovered`
use crate::{
Transaction, TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash,
Address, Transaction, TransactionSigned, TransactionSignedEcRecovered, TransactionSignedNoHash,
B256,
};
use std::ops::Deref;
/// Represents various different transaction formats used in reth.
///
@ -29,6 +31,26 @@ impl TransactionSignedVariant {
}
}
/// Returns the hash of the transaction
pub fn hash(&self) -> B256 {
match self {
TransactionSignedVariant::SignedNoHash(tx) => tx.hash(),
TransactionSignedVariant::Signed(tx) => tx.hash,
TransactionSignedVariant::SignedEcRecovered(tx) => tx.hash,
}
}
/// Returns the signer of the transaction.
///
/// If the transaction is of not of [TransactionSignedEcRecovered] it will be recovered.
pub fn signer(&self) -> Option<Address> {
match self {
TransactionSignedVariant::SignedNoHash(tx) => tx.recover_signer(),
TransactionSignedVariant::Signed(tx) => tx.recover_signer(),
TransactionSignedVariant::SignedEcRecovered(tx) => Some(tx.signer),
}
}
/// Returns [TransactionSigned] type
/// else None
pub fn as_signed(&self) -> Option<&TransactionSigned> {
@ -130,3 +152,11 @@ impl AsRef<Transaction> for TransactionSignedVariant {
self.as_raw()
}
}
impl Deref for TransactionSignedVariant {
type Target = Transaction;
fn deref(&self) -> &Self::Target {
self.as_raw()
}
}