Skip to content

Commit

Permalink
feat: generate valid signed auth signatures (#1041)
Browse files Browse the repository at this point in the history
* feat: generate valid signed auth signatures

* feature gate the auth arbitrary
  • Loading branch information
Rjected authored Jul 12, 2024
1 parent dfb8650 commit 594a271
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
5 changes: 4 additions & 1 deletion crates/eips/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
21 changes: 11 additions & 10 deletions crates/eips/src/eip7702/auth_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
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::<Authorization>()?;
let parity = u.arbitrary::<Parity>()?;
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 })
}
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit 594a271

Please sign in to comment.