Skip to content

Commit

Permalink
test: move tests out of reth-primitives (#13636)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse authored Jan 3, 2025
1 parent b84a488 commit dbff11a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 52 deletions.
29 changes: 29 additions & 0 deletions crates/ethereum/primitives/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
28 changes: 28 additions & 0 deletions crates/primitives-traits/src/transaction/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
52 changes: 0 additions & 52 deletions crates/primitives/src/transaction/signature.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit dbff11a

Please sign in to comment.