From dccea34b8a2ffdd76181954d1bb680611395a63c Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 7 Jun 2023 19:49:36 +0200 Subject: [PATCH] chore: feature-gate `getrandom`, document in README.md (#71) * docs: document wasm32-unknown-unknown getrandom failures * chore: feature-gate `getrandom` --- Cargo.toml | 1 + README.md | 18 ++++++++++-- crates/primitives/Cargo.toml | 5 +++- crates/primitives/src/bits/fixed.rs | 2 ++ crates/primitives/src/bits/macros.rs | 44 ++++++++++++++++++++-------- crates/primitives/src/lib.rs | 2 ++ 6 files changed, 56 insertions(+), 16 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 83b29080d9..b09a294e27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,6 +48,7 @@ thiserror = "1.0" arbitrary = { version = "1.3", default-features = false } arrayvec = { version = "0.7.2", default-features = false } bytes = { version = "1.4", default-features = false } +getrandom = "0.2" hex = { package = "const-hex", version = ">=1.5", default-features = false, features = ["alloc"] } once_cell = "1.17" proptest = "1.1" diff --git a/README.md b/README.md index 951821d4ec..3985848208 100644 --- a/README.md +++ b/README.md @@ -59,17 +59,29 @@ Alloy project. Pull requests will not be merged unless CI passes, so please ensure that your contribution follows the linting rules and passes clippy. -## Note on WASM +## WASM support -We provide full support for all the `wasm*-*` targets. If any crate does not +We provide full support for all the `wasm*-*` targets. If a crate does not build on a WASM target, please [open an issue]. +When building for the `wasm32-unknown-unknown` target and the `"getrandom"` +feature is enabled, compilation for the `getrandom` crate will fail. This is +expected: see [their documentation][getrandom] for more details. + +To fix this, either disable the `"getrandom"` feature on `alloy-core` or add +`getrandom` to your dependencies with the `"js"` feature enabled: + +```toml +getrandom = { version = "0.2", features = ["js"] } +``` + There is currently no plan to provide an official JS/TS-accessible library interface, as we believe [viem] or [ethers.js] serve that need very well. +[open an issue]: https://github.com/alloy-rs/core/issues/new +[getrandom]: https://docs.rs/getrandom/#webassembly-support [viem]: https://viem.sh [ethers.js]: https://docs.ethers.io/v6/ -[open an issue]: https://github.com/alloy-rs/core/issues/new ## Note on `no_std` diff --git a/crates/primitives/Cargo.toml b/crates/primitives/Cargo.toml index e308f20779..d140570278 100644 --- a/crates/primitives/Cargo.toml +++ b/crates/primitives/Cargo.toml @@ -23,7 +23,6 @@ ruint = { workspace = true, features = ["serde"] } # utility bytes.workspace = true -getrandom = "0.2" hex.workspace = true itoa = "1" tiny-keccak = { workspace = true, features = ["keccak"] } @@ -37,6 +36,9 @@ alloy-rlp = { workspace = true, optional = true } # serde serde = { workspace = true, optional = true } +# getrandom +getrandom = { workspace = true, optional = true } + # arbitrary arbitrary = { workspace = true, features = ["derive"], optional = true } proptest = { workspace = true, optional = true } @@ -49,6 +51,7 @@ serde_json.workspace = true [features] default = ["std", "rlp", "serde"] std = ["bytes/std", "hex/std", "alloy-rlp?/std", "proptest?/std", "serde/std"] +getrandom = ["dep:getrandom"] rlp = ["dep:alloy-rlp", "ruint/fastrlp"] serde = ["dep:serde", "bytes/serde", "hex/serde", "ruint/serde"] arbitrary = [ diff --git a/crates/primitives/src/bits/fixed.rs b/crates/primitives/src/bits/fixed.rs index 65ed2c7a51..c9452bdf12 100644 --- a/crates/primitives/src/bits/fixed.rs +++ b/crates/primitives/src/bits/fixed.rs @@ -133,12 +133,14 @@ impl FixedBytes { } /// Instantiates a new fixed hash with cryptographically random content. + #[cfg(feature = "getrandom")] #[inline] pub fn random() -> Self { Self::try_random().unwrap() } /// Instantiates a new fixed hash with cryptographically random content. + #[cfg(feature = "getrandom")] pub fn try_random() -> Result { let mut bytes: [_; N] = super::impl_core::uninit_array(); getrandom::getrandom_uninit(&mut bytes)?; diff --git a/crates/primitives/src/bits/macros.rs b/crates/primitives/src/bits/macros.rs index a864c3c69a..97a2f7c82b 100644 --- a/crates/primitives/src/bits/macros.rs +++ b/crates/primitives/src/bits/macros.rs @@ -119,18 +119,6 @@ macro_rules! wrap_fixed_bytes { Self($crate::FixedBytes::with_last_byte(x)) } - /// Instantiates a new fixed hash with cryptographically random content. - #[inline] - pub fn random() -> Self { - Self($crate::FixedBytes::random()) - } - - /// Instantiates a new fixed hash with cryptographically random content. - #[inline] - pub fn try_random() -> ::core::result::Result { - $crate::FixedBytes::try_random().map(Self) - } - /// Returns a new fixed hash where all bits are set to the given byte. #[inline] pub const fn repeat_byte(byte: u8) -> Self { @@ -166,16 +154,19 @@ macro_rules! wrap_fixed_bytes { pub fn as_fixed_bytes_mut(&mut self) -> &mut [u8; $n] { self.0.as_fixed_bytes_mut() } + /// Returns the inner bytes array. #[inline] pub const fn to_fixed_bytes(self) -> [u8; $n] { self.0.to_fixed_bytes() } + /// Returns a constant raw pointer to the value. #[inline] pub const fn as_ptr(&self) -> *const u8 { self.as_bytes().as_ptr() } + /// Returns a mutable raw pointer to the value. #[inline] pub fn as_mut_ptr(&mut self) -> *mut u8 { @@ -247,12 +238,41 @@ macro_rules! wrap_fixed_bytes { } } + $crate::impl_getrandom!($name); $crate::impl_rlp!($name); $crate::impl_serde!($name); $crate::impl_arbitrary!($name, $n); }; } +#[doc(hidden)] +#[macro_export] +#[cfg(feature = "getrandom")] +macro_rules! impl_getrandom { + ($t:ty) => { + impl $t { + /// Instantiates a new fixed hash with cryptographically random content. + #[inline] + pub fn random() -> Self { + Self($crate::FixedBytes::random()) + } + + /// Instantiates a new fixed hash with cryptographically random content. + #[inline] + pub fn try_random() -> ::core::result::Result { + $crate::FixedBytes::try_random().map(Self) + } + } + }; +} + +#[doc(hidden)] +#[macro_export] +#[cfg(not(feature = "getrandom"))] +macro_rules! impl_getrandom { + ($t:ty) => {}; +} + #[doc(hidden)] #[macro_export] #[cfg(feature = "rlp")] diff --git a/crates/primitives/src/lib.rs b/crates/primitives/src/lib.rs index c0b0c3bd33..7cc5da8716 100644 --- a/crates/primitives/src/lib.rs +++ b/crates/primitives/src/lib.rs @@ -43,6 +43,8 @@ pub use ruint::{self, uint, Uint}; #[doc(hidden)] pub mod private { pub use derive_more; + + #[cfg(feature = "getrandom")] pub use getrandom; #[cfg(feature = "rlp")]