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: rm alloy serde dep #7

Merged
merged 1 commit into from
Aug 27, 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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ alloy-primitives = { version = "0.8.0", default-features = false }
alloy-rlp = { version = "0.3", default-features = false }

# serde
alloy-serde = { version = "0.2" }
serde = { version = "1.0", default-features = false, features = ["derive", "alloc"] }
serde_json = { version = "1.0", default-features = false, features = ["alloc"] }

Expand Down
7 changes: 4 additions & 3 deletions crates/eip7702/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ alloy-primitives = { workspace = true, features = ["rlp"] }
alloy-rlp = { workspace = true, features = ["derive"] }

# serde
alloy-serde = { workspace = true, optional = true }
serde = { workspace = true, optional = true }

# arbitrary
Expand All @@ -32,15 +31,17 @@ arbitrary = { workspace = true, features = ["derive"], optional = true }
k256 = { workspace = true, optional = true }
rand = { workspace = true, optional = true }

[dev-dependencies]
serde_json.workspace = true

[features]
default = ["std"]
std = ["alloy-primitives/std", "alloy-rlp/std", "serde?/std"]
serde = ["dep:alloy-serde", "dep:serde", "alloy-primitives/serde"]
serde = ["dep:serde", "alloy-primitives/serde"]
arbitrary = [
"std",
"dep:arbitrary",
"dep:rand",
"alloy-primitives/arbitrary",
"alloy-serde?/arbitrary",
]
k256 = ["alloy-primitives/k256", "dep:k256"]
41 changes: 40 additions & 1 deletion crates/eip7702/src/auth_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct Authorization {
/// The address of the authorization.
pub address: Address,
/// The nonce for the authorization.
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
#[cfg_attr(feature = "serde", serde(with = "quantity"))]
pub nonce: u64,
}

Expand Down Expand Up @@ -276,6 +276,28 @@ impl Deref for RecoveredAuthorization {
}
}

#[cfg(feature = "serde")]
mod quantity {
use alloy_primitives::U64;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Serializes a primitive number as a "quantity" hex string.
pub(crate) fn serialize<S>(value: &u64, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
U64::from(*value).serialize(serializer)
}

/// Deserializes a primitive number from a "quantity" hex string.
pub(crate) fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error>
where
D: Deserializer<'de>,
{
U64::deserialize(deserializer).map(|value| value.to())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -321,6 +343,23 @@ mod tests {
assert_eq!(decoded, auth);
}

#[cfg(feature = "serde")]
#[test]
fn test_auth_json() {
let sig = r#"{"r":"0xc569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0","s":"0x1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05","yParity":"0x1"}"#;
let auth = SignedAuthorization {
inner: Authorization {
chain_id: U256::from(1u64),
address: Address::left_padding_from(&[6]),
nonce: 1,
},
signature: serde_json::from_str(sig).unwrap(),
};
let val = serde_json::to_string(&auth).unwrap();
let s = r#"{"chainId":"0x1","address":"0x0000000000000000000000000000000000000006","nonce":"0x1","r":"0xc569c92f176a3be1a6352dd5005bfc751dcb32f57623dd2a23693e64bf4447b0","s":"0x1a891b566d369e79b7a66eecab1e008831e22daa15f91a0a0cf4f9f28f47ee05","yParity":"0x1"}"#;
assert_eq!(val, s);
}

#[cfg(all(feature = "arbitrary", feature = "k256"))]
#[test]
fn test_arbitrary_auth() {
Expand Down