From c66363dcd0f21ccc0eaa0adf6d421fe9ef3cee29 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Tue, 27 Aug 2024 12:15:22 +0200 Subject: [PATCH] feat: rm alloy serde dep --- Cargo.toml | 1 - crates/eip7702/Cargo.toml | 7 +++--- crates/eip7702/src/auth_list.rs | 41 ++++++++++++++++++++++++++++++++- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3a82991..4ea9dbb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/crates/eip7702/Cargo.toml b/crates/eip7702/Cargo.toml index 4bf568b..f9e0897 100644 --- a/crates/eip7702/Cargo.toml +++ b/crates/eip7702/Cargo.toml @@ -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 @@ -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"] diff --git a/crates/eip7702/src/auth_list.rs b/crates/eip7702/src/auth_list.rs index cecc7eb..80f759e 100644 --- a/crates/eip7702/src/auth_list.rs +++ b/crates/eip7702/src/auth_list.rs @@ -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, } @@ -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(value: &u64, serializer: S) -> Result + 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 + where + D: Deserializer<'de>, + { + U64::deserialize(deserializer).map(|value| value.to()) + } +} + #[cfg(test)] mod tests { use super::*; @@ -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() {