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

chore: feature-gate getrandom, document in README.md #71

Merged
merged 2 commits into from
Jun 7, 2023
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: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
5 changes: 4 additions & 1 deletion crates/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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 }
Expand All @@ -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 = [
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/src/bits/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,14 @@ impl<const N: usize> FixedBytes<N> {
}

/// 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<Self, getrandom::Error> {
let mut bytes: [_; N] = super::impl_core::uninit_array();
getrandom::getrandom_uninit(&mut bytes)?;
Expand Down
44 changes: 32 additions & 12 deletions crates/primitives/src/bits/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, $crate::private::getrandom::Error> {
$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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<Self, $crate::private::getrandom::Error> {
$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")]
Expand Down
2 changes: 2 additions & 0 deletions crates/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down