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

fix(eips): make SignedAuthorizationList arbitrary less fallible #1084

Merged
merged 1 commit into from
Jul 19, 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
2 changes: 2 additions & 0 deletions crates/eips/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ arbitrary = { workspace = true, features = ["derive"], optional = true }

# for signed authorization list arbitrary
k256 = { workspace = true, optional = true }
rand = { workspace = true, optional = true }

[dev-dependencies]
alloy-primitives = { workspace = true, features = [
Expand Down Expand Up @@ -80,6 +81,7 @@ arbitrary = [
"std",
"kzg-sidecar",
"dep:arbitrary",
"dep:rand",
"alloy-primitives/arbitrary",
"alloy-serde?/arbitrary",
]
21 changes: 15 additions & 6 deletions crates/eips/src/eip7702/auth_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,15 @@ impl Deref for SignedAuthorization {
#[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 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)?;
use k256::{
ecdsa::{signature::hazmat::PrehashSigner, SigningKey},
NonZeroScalar,
};
use rand::{rngs::StdRng, SeedableRng};

let rng_seed = u.arbitrary::<[u8; 32]>()?;
let mut rand_gen = StdRng::from_seed(rng_seed);
let signing_key: SigningKey = NonZeroScalar::random(&mut rand_gen).into();

let inner = u.arbitrary::<Authorization>()?;
let signature_hash = inner.signature_hash();
Expand Down Expand Up @@ -307,7 +312,6 @@ impl Deref for OptionalNonce {
mod tests {
use super::*;
use alloy_primitives::{hex, Signature};
use arbitrary::Arbitrary;
use core::str::FromStr;

fn test_encode_decode_roundtrip(auth: Authorization) {
Expand Down Expand Up @@ -367,10 +371,15 @@ mod tests {
assert_eq!(decoded, auth);
}

#[cfg(feature = "k256")]
#[cfg(all(feature = "arbitrary", feature = "k256"))]
#[test]
fn test_arbitrary_auth() {
use arbitrary::Arbitrary;
let mut unstructured = arbitrary::Unstructured::new(b"unstructured auth");
// try this multiple times
let _auth = SignedAuthorization::arbitrary(&mut unstructured).unwrap();
let _auth = SignedAuthorization::arbitrary(&mut unstructured).unwrap();
let _auth = SignedAuthorization::arbitrary(&mut unstructured).unwrap();
let _auth = SignedAuthorization::arbitrary(&mut unstructured).unwrap();
}
}
5 changes: 5 additions & 0 deletions crates/eips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#[macro_use]
extern crate alloc;

// To ensure no unused imports, since signed auth list requires arbitrary _and_ k256 features, but
// is only enabled using the `arbitrary` feature.
#[cfg(all(not(feature = "k256"), feature = "arbitrary"))]
use rand as _;

pub mod eip1559;
pub use eip1559::calc_next_block_base_fee;

Expand Down