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

Fix: getrandom dependency issue in WASM build #213

Merged
merged 7 commits into from
Apr 16, 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
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ members = [
]

[workspace.package]
version = "0.14.0"
version = "0.15.0"
edition = "2021"

[workspace.dependencies]
Expand Down Expand Up @@ -60,7 +60,7 @@ parking_lot = "0.12"
proc-macro2 = "1.0"
quote = "1.0"
rand = "0.8"
reqwest = { version = "0.11", default-features = false }
reqwest = { version = "0.12", default-features = false }
ringbuffer = "0.15"
schnellru = { version = "0.2", default-features = false }
serde = { version = "1.0", features = ["derive"] }
Expand Down
2 changes: 1 addition & 1 deletion ic-crypto-getrandom-for-wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ edition.workspace = true
getrandom = { version = "0.2", features = ["custom"] }

[target.'cfg(not(target_family = "wasm"))'.dependencies]
getrandom = { version = "0.2" }
getrandom = { version = "0.2" }
28 changes: 22 additions & 6 deletions ic-crypto-getrandom-for-wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//! canister code.
//!
//! Depending on this crate converts the compile time error into a runtime error, by
//! registering a custom `getrandom` implementation which always fails. This matches the
//! registering a custom `getrandom` implementation. This matches the
//! behavior of `getrandom` 0.1. For code that is not being compiled to
//! `wasm32-unknown-unknown`, this crate has no effect whatsoever.
//!
Expand All @@ -19,20 +19,36 @@
//! See the [getrandom
//! documentation](https://docs.rs/getrandom/latest/getrandom/macro.register_custom_getrandom.html)
//! for more details on custom implementations.
//!
//! To register this custom implementation, call the function inside of your canister's `init` method
//! or wherever canister initialization happens, with conditional compilation, like this:
//!
//! ```ignore
//! #[init]
//! pub fn init(&mut self) {
//! #[cfg(target_family = "wasm")]
//! ic_crypto_getrandom_for_wasm::register_custom_getrandom();
//! }
//! ```

#[cfg(all(
target_family = "wasm",
target_vendor = "unknown",
target_os = "unknown"
))]
/// A getrandom implementation that always fails
pub fn always_fail(_buf: &mut [u8]) -> Result<(), getrandom::Error> {
Err(getrandom::Error::UNSUPPORTED)
}
pub use custom_getrandom_impl::register_custom_getrandom;

#[cfg(all(
target_family = "wasm",
target_vendor = "unknown",
target_os = "unknown"
))]
getrandom::register_custom_getrandom!(always_fail);
mod custom_getrandom_impl {
pub fn register_custom_getrandom() {
kobby-pentangeli marked this conversation as resolved.
Show resolved Hide resolved
getrandom::register_custom_getrandom!(always_fail);
}
/// A getrandom implementation that always fails
fn always_fail(_buf: &mut [u8]) -> Result<(), getrandom::Error> {
Err(getrandom::Error::UNSUPPORTED)
}
}