alloy_consensus/transaction/
hashable.rs

1use alloy_eips::Typed2718;
2use alloy_primitives::{Signature, TxHash};
3
4use crate::transaction::RlpEcdsaEncodableTx;
5
6/// Generic trait to get a transaction hash from any signature type
7pub trait TxHashable<S>: Typed2718 {
8    /// Calculate the transaction hash for the given signature and type.
9    fn tx_hash_with_type(&self, signature: &S, ty: u8) -> TxHash;
10
11    /// Calculate the transaction hash for the given signature.
12    fn tx_hash(&self, signature: &S) -> TxHash {
13        self.tx_hash_with_type(signature, self.ty())
14    }
15}
16
17impl<T> TxHashable<Signature> for T
18where
19    T: RlpEcdsaEncodableTx,
20{
21    /// Calculate the transaction hash for the given signature and type.
22    fn tx_hash_with_type(&self, signature: &Signature, ty: u8) -> TxHash {
23        RlpEcdsaEncodableTx::tx_hash_with_type(self, signature, ty)
24    }
25}
26
27#[cfg(test)]
28mod tests {
29    use super::*;
30    use crate::{Signed, TxLegacy};
31    use alloy_primitives::U256;
32
33    #[test]
34    fn hashable_signed() {
35        let tx = TxLegacy::default();
36        let signature = Signature::new(U256::from(1u64), U256::from(1u64), false);
37        let signed = Signed::new_unhashed(tx, signature);
38        let _hash = signed.hash();
39    }
40}