Skip to content

Commit

Permalink
fix(eips): make SignedAuthorizationList arbitrary less fallible (allo…
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected authored and ben186 committed Jul 27, 2024
1 parent a8c9fb8 commit 8a726c1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
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

0 comments on commit 8a726c1

Please sign in to comment.