From 594a271b4ae23fdb1a961d9223ad6ef8858477a7 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Fri, 12 Jul 2024 04:43:55 -0400 Subject: [PATCH] feat: generate valid signed auth signatures (#1041) * feat: generate valid signed auth signatures * feature gate the auth arbitrary --- crates/eips/Cargo.toml | 5 ++++- crates/eips/src/eip7702/auth_list.rs | 21 +++++++++++---------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/eips/Cargo.toml b/crates/eips/Cargo.toml index 07c984c2f00..4c2a1866ca5 100644 --- a/crates/eips/Cargo.toml +++ b/crates/eips/Cargo.toml @@ -39,6 +39,9 @@ ethereum_ssz = { workspace = true, optional = true } # arbitrary arbitrary = { workspace = true, features = ["derive"], optional = true } +# for signed authorization list arbitrary +k256 = { workspace = true, optional = true } + [dev-dependencies] alloy-primitives = { workspace = true, features = [ "rand", @@ -66,7 +69,7 @@ serde = [ kzg = ["kzg-sidecar", "sha2", "dep:derive_more", "dep:c-kzg", "dep:once_cell"] kzg-sidecar = ["sha2"] sha2 = ["dep:sha2"] -k256 = ["alloy-primitives/k256"] +k256 = ["alloy-primitives/k256", "dep:k256"] ssz = [ "std", "dep:ethereum_ssz", diff --git a/crates/eips/src/eip7702/auth_list.rs b/crates/eips/src/eip7702/auth_list.rs index 5604508a619..e7a6d106e83 100644 --- a/crates/eips/src/eip7702/auth_list.rs +++ b/crates/eips/src/eip7702/auth_list.rs @@ -174,21 +174,21 @@ impl Deref for SignedAuthorization { } } -#[cfg(any(test, feature = "arbitrary"))] +#[cfg(all(any(test, feature = "arbitrary"), feature = "k256"))] impl<'a> arbitrary::Arbitrary<'a> for SignedAuthorization { fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { - use alloy_primitives::{b256, Parity}; + use k256::ecdsa::{signature::hazmat::PrehashSigner, SigningKey}; + let key_bytes = u.arbitrary::<[u8; 32]>()?; + let signing_key = SigningKey::from_bytes(&key_bytes.into()) + .map_err(|_| arbitrary::Error::IncorrectFormat)?; let inner = u.arbitrary::()?; - let parity = u.arbitrary::()?; + let signature_hash = inner.signature_hash(); - // TODO: find an easy way to generate random signatures - let signature = Signature::from_rs_and_parity( - b256!("c569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0").into(), - b256!("1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05").into(), - parity, - ) - .map_err(|_| arbitrary::Error::IncorrectFormat)?; + let (recoverable_sig, recovery_id) = + signing_key.sign_prehash(signature_hash.as_ref()).unwrap(); + let signature = Signature::from_signature_and_parity(recoverable_sig, recovery_id) + .map_err(|_| arbitrary::Error::IncorrectFormat)?; Ok(Self { inner, signature }) } @@ -358,6 +358,7 @@ mod tests { assert_eq!(decoded, auth); } + #[cfg(feature = "k256")] #[test] fn test_arbitrary_auth() { let mut unstructured = arbitrary::Unstructured::new(b"unstructured auth");