From 08c5dc03a74a12aac5daaec2bf8f368524562800 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 10 May 2023 08:27:21 +0200 Subject: [PATCH 1/2] feat: use `const-hex` instead of `hex` --- Cargo.toml | 2 +- abi/Cargo.toml | 3 +-- abi/src/util.rs | 15 +++++++-------- dyn-abi/src/eip712/typed_data.rs | 1 + primitives/Cargo.toml | 11 ++++++++--- primitives/src/bits/fixed.rs | 5 +---- 6 files changed, 19 insertions(+), 18 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dfd202291..19ae8d4a4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,7 +43,7 @@ syn = "2.0" arbitrary = { version = "1.3", default-features = false } arrayvec = { version = "0.7.2", default-features = false } bytes = { version = "1.4", default-features = false } -hex = { version = "0.4", default-features = false } +hex = { package = "const-hex", version = "1.2", default-features = false, features = ["hex"] } hex-literal = "0.4" proptest = { version = "1.1", default-features = false } proptest-derive = "0.3" diff --git a/abi/Cargo.toml b/abi/Cargo.toml index 679493921..2843a2ee5 100644 --- a/abi/Cargo.toml +++ b/abi/Cargo.toml @@ -28,5 +28,4 @@ hex-literal.workspace = true [features] default = ["std", "hex/alloc"] std = ["ethers-primitives/std", "hex/std", "serde?/std", "thiserror"] -# TODO: Can we remove std from here? -eip712-serde = ["std", "dep:serde"] +eip712-serde = ["dep:serde", "serde?/alloc"] diff --git a/abi/src/util.rs b/abi/src/util.rs index 4ca49988b..319ae386a 100644 --- a/abi/src/util.rs +++ b/abi/src/util.rs @@ -71,6 +71,7 @@ pub(crate) use serde_helper::*; #[cfg(feature = "eip712-serde")] mod serde_helper { + use alloc::string::String; use ethers_primitives::U256; use serde::{Deserialize, Deserializer}; @@ -78,9 +79,9 @@ mod serde_helper { #[derive(Deserialize, Debug, Clone)] #[serde(untagged)] pub(crate) enum StringifiedNumeric { - String(String), - U256(U256), Num(u64), + U256(U256), + String(String), } impl TryFrom for U256 { @@ -88,14 +89,12 @@ mod serde_helper { fn try_from(value: StringifiedNumeric) -> Result { match value { - StringifiedNumeric::U256(n) => Ok(n), StringifiedNumeric::Num(n) => Ok(U256::from(n)), + StringifiedNumeric::U256(n) => Ok(n), + // TODO: this is probably unreachable, due to ruint U256 deserializing from a string StringifiedNumeric::String(s) => { - if let Ok(val) = s.parse::() { - Ok(U256::from(val)) - } else if s.starts_with("0x") { - U256::from_str_radix(s.strip_prefix("0x").unwrap(), 16) - .map_err(|err| err.to_string()) + if let Some(s) = s.strip_prefix("0x") { + U256::from_str_radix(s, 16).map_err(|err| err.to_string()) } else { U256::from_str_radix(&s, 10).map_err(|err| err.to_string()) } diff --git a/dyn-abi/src/eip712/typed_data.rs b/dyn-abi/src/eip712/typed_data.rs index e7076e986..f98ac20d6 100644 --- a/dyn-abi/src/eip712/typed_data.rs +++ b/dyn-abi/src/eip712/typed_data.rs @@ -110,6 +110,7 @@ impl<'de> Deserialize<'de> for TypedData { D: serde::de::Deserializer<'de>, { #[derive(Deserialize)] + #[allow(missing_debug_implementations)] struct TypedDataHelper { #[serde(default)] domain: Eip712Domain, diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index 20558e3b1..4f78085b3 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -20,7 +20,7 @@ ruint = { workspace = true, features = ["rlp", "serde"] } # utility derive_more = "0.99" tiny-keccak = { workspace = true, features = ["keccak"] } -hex = { workspace = true, default-features = false } +hex.workspace = true # optional serde = { workspace = true, features = ["derive"], optional = true } @@ -40,5 +40,10 @@ default = ["std", "rlp", "serde", "hex/std"] std = ["serde/std", "ethers-rlp?/std", "bytes?/std", "proptest?/std", "dep:thiserror"] rlp = ["dep:ethers-rlp", "dep:bytes"] serde = ["dep:serde", "ruint/serde"] -arbitrary = ["ruint/arbitrary", "ruint/proptest", "dep:arbitrary", "dep:proptest", "dep:proptest-derive"] - +arbitrary = [ + "ruint/arbitrary", + "ruint/proptest", + "dep:arbitrary", + "dep:proptest", + "dep:proptest-derive", +] diff --git a/primitives/src/bits/fixed.rs b/primitives/src/bits/fixed.rs index cda21f3ea..84519fc2c 100644 --- a/primitives/src/bits/fixed.rs +++ b/primitives/src/bits/fixed.rs @@ -371,10 +371,7 @@ impl core::str::FromStr for FixedBytes { let s = s.strip_prefix("0x").unwrap_or(s); let mut buf = [0u8; N]; - hex::decode_to_slice(s, buf.as_mut())?; + hex::decode_to_slice(s, &mut buf)?; Ok(Self(buf)) } } - -#[cfg(test)] -mod test {} From eaa2fd8ae5ec95c36b6bde79122304297845e680 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 10 May 2023 08:48:02 +0200 Subject: [PATCH 2/2] fix: no-default-features --- abi/Cargo.toml | 4 ++-- abi/src/coder/token.rs | 5 +---- abi/src/util.rs | 2 +- primitives/Cargo.toml | 3 +-- primitives/src/bits/address.rs | 9 ++------- primitives/src/bits/fixed.rs | 4 ---- primitives/src/bits/macros.rs | 4 ---- primitives/src/bits/mod.rs | 2 -- primitives/src/bits/rlp.rs | 3 +-- primitives/src/bits/serialize.rs | 4 ++-- primitives/src/lib.rs | 2 +- primitives/src/signed/errors.rs | 27 ++++++++++++++++++++++----- primitives/src/signed/int.rs | 7 ++----- primitives/src/signed/ops.rs | 6 ++---- primitives/src/utils.rs | 6 +++--- 15 files changed, 40 insertions(+), 48 deletions(-) diff --git a/abi/Cargo.toml b/abi/Cargo.toml index 2843a2ee5..ca6fbd6e9 100644 --- a/abi/Cargo.toml +++ b/abi/Cargo.toml @@ -20,7 +20,7 @@ sol-type-parser.workspace = true hex = { workspace = true, features = ["alloc"] } thiserror = { workspace = true, optional = true } -serde = { workspace = true, optional = true } +serde = { workspace = true, optional = true, features = ["derive"] } [dev-dependencies] hex-literal.workspace = true @@ -28,4 +28,4 @@ hex-literal.workspace = true [features] default = ["std", "hex/alloc"] std = ["ethers-primitives/std", "hex/std", "serde?/std", "thiserror"] -eip712-serde = ["dep:serde", "serde?/alloc"] +eip712-serde = ["dep:serde", "serde?/alloc", "ethers-primitives/serde"] diff --git a/abi/src/coder/token.rs b/abi/src/coder/token.rs index 6f7833ff8..e3b949348 100644 --- a/abi/src/coder/token.rs +++ b/abi/src/coder/token.rs @@ -605,12 +605,9 @@ impl TokenSeq for () { #[cfg(test)] mod tests { - use ethers_primitives::B256; - use super::*; - #[cfg(not(feature = "std"))] - use crate::no_std_prelude::*; use crate::{sol_data, SolType}; + use ethers_primitives::B256; macro_rules! assert_type_check { ($sol:ty, $token:expr $(,)?) => { diff --git a/abi/src/util.rs b/abi/src/util.rs index 319ae386a..a89ac7bda 100644 --- a/abi/src/util.rs +++ b/abi/src/util.rs @@ -71,7 +71,7 @@ pub(crate) use serde_helper::*; #[cfg(feature = "eip712-serde")] mod serde_helper { - use alloc::string::String; + use alloc::string::{String, ToString}; use ethers_primitives::U256; use serde::{Deserialize, Deserializer}; diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index 4f78085b3..0c6171e7e 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -24,7 +24,6 @@ hex.workspace = true # optional serde = { workspace = true, features = ["derive"], optional = true } -thiserror = { workspace = true, optional = true } # rlp support ethers-rlp = { workspace = true, optional = true } @@ -37,7 +36,7 @@ proptest-derive = { workspace = true, optional = true } [features] default = ["std", "rlp", "serde", "hex/std"] -std = ["serde/std", "ethers-rlp?/std", "bytes?/std", "proptest?/std", "dep:thiserror"] +std = ["serde/std", "ethers-rlp?/std", "bytes?/std", "proptest?/std"] rlp = ["dep:ethers-rlp", "dep:bytes"] serde = ["dep:serde", "ruint/serde"] arbitrary = [ diff --git a/primitives/src/bits/address.rs b/primitives/src/bits/address.rs index 2d5255a5d..f861caa61 100644 --- a/primitives/src/bits/address.rs +++ b/primitives/src/bits/address.rs @@ -1,15 +1,9 @@ -#[cfg(feature = "std")] -use std::borrow::Borrow; - -#[cfg(not(feature = "std"))] +use crate::{utils::keccak256, wrap_fixed_bytes, FixedBytes}; use alloc::{ borrow::Borrow, - format, string::{String, ToString}, }; -use crate::{utils::keccak256, wrap_fixed_bytes, FixedBytes}; - /// Error type for address checksum validation #[derive(Debug, Copy, Clone)] pub enum AddressError { @@ -157,6 +151,7 @@ impl Address { #[cfg(test)] mod test { use super::Address; + use alloc::string::String; #[test] fn it_parses() { diff --git a/primitives/src/bits/fixed.rs b/primitives/src/bits/fixed.rs index 84519fc2c..f2f71220c 100644 --- a/primitives/src/bits/fixed.rs +++ b/primitives/src/bits/fixed.rs @@ -1,5 +1,4 @@ use core::{fmt, ops}; - use derive_more::{AsMut, AsRef, Deref, DerefMut, From, Index, IndexMut}; /// A bytearray of fixed length. @@ -65,7 +64,6 @@ impl<'a, const N: usize> From<&'a mut [u8; N]> for FixedBytes { impl From> for [u8; N] { #[inline] - #[track_caller] fn from(s: FixedBytes) -> Self { s.0 } @@ -73,7 +71,6 @@ impl From> for [u8; N] { impl AsRef<[u8]> for FixedBytes { #[inline] - #[track_caller] fn as_ref(&self) -> &[u8] { self.as_bytes() } @@ -81,7 +78,6 @@ impl AsRef<[u8]> for FixedBytes { impl AsMut<[u8]> for FixedBytes { #[inline] - #[track_caller] fn as_mut(&mut self) -> &mut [u8] { self.as_bytes_mut() } diff --git a/primitives/src/bits/macros.rs b/primitives/src/bits/macros.rs index f847688c3..e31b9e44d 100644 --- a/primitives/src/bits/macros.rs +++ b/primitives/src/bits/macros.rs @@ -70,7 +70,6 @@ macro_rules! wrap_fixed_bytes { impl<'a> From<[u8; $n]> for $name { #[inline] - #[track_caller] fn from(bytes: [u8; $n]) -> Self { Self(bytes.into()) } @@ -78,7 +77,6 @@ macro_rules! wrap_fixed_bytes { impl<'a> From<&'a [u8; $n]> for $name { #[inline] - #[track_caller] fn from(bytes: &'a [u8; $n]) -> Self { Self(bytes.into()) } @@ -86,7 +84,6 @@ macro_rules! wrap_fixed_bytes { impl AsRef<[u8]> for $name { #[inline] - #[track_caller] fn as_ref(&self) -> &[u8] { self.as_bytes() } @@ -94,7 +91,6 @@ macro_rules! wrap_fixed_bytes { impl AsMut<[u8]> for $name { #[inline] - #[track_caller] fn as_mut(&mut self) -> &mut [u8] { self.as_bytes_mut() } diff --git a/primitives/src/bits/mod.rs b/primitives/src/bits/mod.rs index f9980baaf..66f09b5aa 100644 --- a/primitives/src/bits/mod.rs +++ b/primitives/src/bits/mod.rs @@ -6,8 +6,6 @@ pub use fixed::FixedBytes; mod macros; -// pub(self) mod hex; - // code stolen from: https://docs.rs/impl-serde/0.4.0/impl_serde/ #[cfg(feature = "serde")] mod serialize; diff --git a/primitives/src/bits/rlp.rs b/primitives/src/bits/rlp.rs index d06232113..be4e68862 100644 --- a/primitives/src/bits/rlp.rs +++ b/primitives/src/bits/rlp.rs @@ -1,6 +1,5 @@ -use ethers_rlp::{Decodable, Encodable}; - use super::FixedBytes; +use ethers_rlp::{Decodable, Encodable}; impl Decodable for FixedBytes { fn decode(buf: &mut &[u8]) -> Result { diff --git a/primitives/src/bits/serialize.rs b/primitives/src/bits/serialize.rs index e4b5c3588..f0aeab976 100644 --- a/primitives/src/bits/serialize.rs +++ b/primitives/src/bits/serialize.rs @@ -1,6 +1,6 @@ -use core::result::Result; - use super::FixedBytes; +use alloc::string::String; +use core::result::Result; impl serde::Serialize for FixedBytes { fn serialize(&self, serializer: S) -> Result diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index bec45c151..95451fb3b 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -14,7 +14,7 @@ #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(not(feature = "std"))] +#[macro_use] extern crate alloc; mod bits; diff --git a/primitives/src/signed/errors.rs b/primitives/src/signed/errors.rs index 43ca8fea3..3426f0439 100644 --- a/primitives/src/signed/errors.rs +++ b/primitives/src/signed/errors.rs @@ -1,16 +1,14 @@ +use core::fmt; use ruint::BaseConvertError; /// The error type that is returned when parsing a signed integer. #[derive(Clone, Copy, Debug, PartialEq)] -#[cfg_attr(feature = "std", derive(thiserror::Error))] pub enum ParseSignedError { /// Error that occurs when an invalid digit is encountered while parsing. - #[cfg_attr(feature = "std", error("Parsing Error: {0}"))] Ruint(ruint::ParseError), /// Error that occurs when the number is too large or too small (negative) /// and does not fit in the target signed integer. - #[cfg_attr(feature = "std", error("number does not fit in the integer size"))] IntegerOverflow, } @@ -27,8 +25,27 @@ impl From for ParseSignedError { } } +#[cfg(feature = "std")] +impl std::error::Error for ParseSignedError {} + +impl fmt::Display for ParseSignedError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Self::Ruint(err) => write!(f, "Parsing Error: {err}"), + Self::IntegerOverflow => f.write_str("number does not fit in the integer size"), + } + } +} + /// The error type that is returned when conversion to or from a integer fails. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -#[cfg_attr(feature = "std", derive(thiserror::Error))] -#[cfg_attr(feature = "std", error("output of range integer conversion attempted"))] pub struct BigIntConversionError; + +#[cfg(feature = "std")] +impl std::error::Error for BigIntConversionError {} + +impl fmt::Display for BigIntConversionError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("output of range integer conversion attempted") + } +} diff --git a/primitives/src/signed/int.rs b/primitives/src/signed/int.rs index 426cab7d7..bd399a6a2 100644 --- a/primitives/src/signed/int.rs +++ b/primitives/src/signed/int.rs @@ -1,11 +1,8 @@ +use super::{errors, utils::*, Sign}; +use alloc::string::{String, ToString}; use core::fmt; use ruint::Uint; -#[cfg(not(feature = "std"))] -use alloc::{format, string::String}; - -use super::{errors, utils::*, Sign}; - /// Signed integer wrapping a `ruint::Uint`. /// /// This signed integer implementation is fully abstract across the number of diff --git a/primitives/src/signed/ops.rs b/primitives/src/signed/ops.rs index ad01a25c7..37ad09de5 100644 --- a/primitives/src/signed/ops.rs +++ b/primitives/src/signed/ops.rs @@ -1,11 +1,9 @@ -use ruint::Uint; - -use core::{cmp, iter, ops}; - use super::{ utils::{handle_overflow, twos_complement}, Sign, Signed, }; +use core::{cmp, iter, ops}; +use ruint::Uint; // ops impl impl Signed { diff --git a/primitives/src/utils.rs b/primitives/src/utils.rs index 97623ecdf..0a8d76678 100644 --- a/primitives/src/utils.rs +++ b/primitives/src/utils.rs @@ -1,8 +1,8 @@ -pub use tiny_keccak::{Hasher, Keccak}; - use crate::bits::FixedBytes; -/// Simple interface to keccak256 hash function +pub use tiny_keccak::{Hasher, Keccak}; + +/// Simple interface to the `keccak256` hash function. pub fn keccak256(bytes: impl AsRef<[u8]>) -> FixedBytes<32> { let mut output = [0u8; 32]; let mut hasher = Keccak::v256();