From dbff11aa7014d54851647f79ec4b7fbb7025bd9d Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 3 Jan 2025 18:04:18 +0100 Subject: [PATCH] test: move tests out of reth-primitives (#13636) --- crates/ethereum/primitives/src/transaction.rs | 29 +++++++++++ .../src/transaction/signature.rs | 28 ++++++++++ .../primitives/src/transaction/signature.rs | 52 ------------------- 3 files changed, 57 insertions(+), 52 deletions(-) diff --git a/crates/ethereum/primitives/src/transaction.rs b/crates/ethereum/primitives/src/transaction.rs index 97e71f11edd0..d1221aeffe62 100644 --- a/crates/ethereum/primitives/src/transaction.rs +++ b/crates/ethereum/primitives/src/transaction.rs @@ -572,3 +572,32 @@ impl SignedTransaction for TransactionSigned { recover_signer_unchecked(&self.signature, signature_hash) } } + +#[cfg(test)] +mod tests { + use super::*; + use alloy_eips::eip7702::constants::SECP256K1N_HALF; + use alloy_primitives::hex; + + #[test] + fn eip_2_reject_high_s_value() { + // This pre-homestead transaction has a high `s` value and should be rejected by the + // `recover_signer` method: + // https://etherscan.io/getRawTx?tx=0x9e6e19637bb625a8ff3d052b7c2fe57dc78c55a15d258d77c43d5a9c160b0384 + // + // Block number: 46170 + let raw_tx = hex!("f86d8085746a52880082520894c93f2250589a6563f5359051c1ea25746549f0d889208686e75e903bc000801ba034b6fdc33ea520e8123cf5ac4a9ff476f639cab68980cd9366ccae7aef437ea0a0e517caa5f50e27ca0d1e9a92c503b4ccb039680c6d9d0c71203ed611ea4feb33"); + let tx = TransactionSigned::decode_2718(&mut &raw_tx[..]).unwrap(); + let signature = tx.signature(); + + // make sure we know it's greater than SECP256K1N_HALF + assert!(signature.s() > SECP256K1N_HALF); + + // recover signer, expect failure + let hash = *tx.tx_hash(); + assert!(recover_signer(signature, hash).is_none()); + + // use unchecked, ensure it succeeds (the signature is valid if not for EIP-2) + assert!(recover_signer_unchecked(signature, hash).is_some()); + } +} diff --git a/crates/primitives-traits/src/transaction/signature.rs b/crates/primitives-traits/src/transaction/signature.rs index 1ff56671bf77..06bbb6db14dd 100644 --- a/crates/primitives-traits/src/transaction/signature.rs +++ b/crates/primitives-traits/src/transaction/signature.rs @@ -2,3 +2,31 @@ /// Re-exported signature type pub use alloy_primitives::PrimitiveSignature as Signature; + +#[cfg(test)] +mod tests { + use crate::crypto::secp256k1::recover_signer; + use alloy_primitives::{Address, PrimitiveSignature as Signature, B256, U256}; + use std::str::FromStr; + + #[test] + fn test_recover_signer() { + let signature = Signature::new( + U256::from_str( + "18515461264373351373200002665853028612451056578545711640558177340181847433846", + ) + .unwrap(), + U256::from_str( + "46948507304638947509940763649030358759909902576025900602547168820602576006531", + ) + .unwrap(), + false, + ); + let hash = + B256::from_str("daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53") + .unwrap(); + let signer = recover_signer(&signature, hash).unwrap(); + let expected = Address::from_str("0x9d8a62f656a8d1615c1294fd71e9cfb3e4855a4f").unwrap(); + assert_eq!(expected, signer); + } +} diff --git a/crates/primitives/src/transaction/signature.rs b/crates/primitives/src/transaction/signature.rs index 03b6327df2e9..cc5df1d74ca2 100644 --- a/crates/primitives/src/transaction/signature.rs +++ b/crates/primitives/src/transaction/signature.rs @@ -1,53 +1 @@ pub use reth_primitives_traits::crypto::secp256k1::{recover_signer, recover_signer_unchecked}; - -#[cfg(test)] -mod tests { - use crate::transaction::signature::{recover_signer, recover_signer_unchecked}; - use alloy_eips::{eip2718::Decodable2718, eip7702::constants::SECP256K1N_HALF}; - use alloy_primitives::{hex, Address, PrimitiveSignature as Signature, B256, U256}; - use reth_primitives_traits::SignedTransaction; - use std::str::FromStr; - - #[test] - fn test_recover_signer() { - let signature = Signature::new( - U256::from_str( - "18515461264373351373200002665853028612451056578545711640558177340181847433846", - ) - .unwrap(), - U256::from_str( - "46948507304638947509940763649030358759909902576025900602547168820602576006531", - ) - .unwrap(), - false, - ); - let hash = - B256::from_str("daf5a779ae972f972197303d7b574746c7ef83eadac0f2791ad23db92e4c8e53") - .unwrap(); - let signer = recover_signer(&signature, hash).unwrap(); - let expected = Address::from_str("0x9d8a62f656a8d1615c1294fd71e9cfb3e4855a4f").unwrap(); - assert_eq!(expected, signer); - } - - #[test] - fn eip_2_reject_high_s_value() { - // This pre-homestead transaction has a high `s` value and should be rejected by the - // `recover_signer` method: - // https://etherscan.io/getRawTx?tx=0x9e6e19637bb625a8ff3d052b7c2fe57dc78c55a15d258d77c43d5a9c160b0384 - // - // Block number: 46170 - let raw_tx = hex!("f86d8085746a52880082520894c93f2250589a6563f5359051c1ea25746549f0d889208686e75e903bc000801ba034b6fdc33ea520e8123cf5ac4a9ff476f639cab68980cd9366ccae7aef437ea0a0e517caa5f50e27ca0d1e9a92c503b4ccb039680c6d9d0c71203ed611ea4feb33"); - let tx = crate::transaction::TransactionSigned::decode_2718(&mut &raw_tx[..]).unwrap(); - let signature = tx.signature(); - - // make sure we know it's greater than SECP256K1N_HALF - assert!(signature.s() > SECP256K1N_HALF); - - // recover signer, expect failure - let hash = tx.hash(); - assert!(recover_signer(signature, hash).is_none()); - - // use unchecked, ensure it succeeds (the signature is valid if not for EIP-2) - assert!(recover_signer_unchecked(signature, hash).is_some()); - } -}