-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: move tests out of reth-primitives (#13636)
- Loading branch information
Showing
3 changed files
with
57 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |