Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate valid signed auth signatures #1041

Merged
merged 2 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion crates/eips/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ arbitrary = { workspace = true, features = ["derive"], optional = true }
proptest = { workspace = true, optional = true }
proptest-derive = { workspace = true, optional = true }

# for signed authorization list arbitrary
k256 = { workspace = true, optional = true }
Comment on lines +44 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is fine because if if the feature is enabled on alloy-primitives this dep is already part of the dep tree


[dev-dependencies]
alloy-primitives = { workspace = true, features = [
"rand",
Expand Down Expand Up @@ -70,7 +73,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"))]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could also make this work without the k256 feature but not useful imo

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