#getrandom #rand #wasm32-unknown-unknown #target #workaround #problem #fail

ic-dummy-getrandom-for-wasm

Workaround a problem with rand on wasm32-unknown-unknown targets

1 unstable release

0.1.0 Feb 14, 2025

#335 in WebAssembly

Download history 93/week @ 2025-02-11

93 downloads per month

Apache-2.0

5KB

ic-dummy-getrandom-for-wasm

The rand crate is widely used in the Rust ecosystem. The rand crate in turn relies on getrandom to acquire cryptographic seed material. For policy reasons, getrandom refuses to compile on the wasm32-unknown-unknown target used by the Internet Computer. This prevents using rand without workarounds.

This crate implements such a workaround; on wasm32-unknown-unknown target, it registers a custom getrandom implementation which just returns an error at runtime. On any other target, it does nothing.


lib.rs:

This crate exists to work around a problem with getrandom 0.2, which is a dependency of rand 0.8

For the wasm32-unknown-unknown target, getrandom 0.2 will refuse to compile. This is an intentional policy decision on the part of the getrandom developers. As a consequence, it would not be possible to compile anything which depends on rand 0.8 to wasm for use in 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 behavior of getrandom 0.1. For code that is not being compiled to wasm32-unknown-unknown, this crate has no effect whatsoever.

The reason for placing this function into its own dedicated crate is that it not possible to register more than one getrandom implementation. If more than one custom getrandom implementation existed within the source tree, then a canister which depended on two different crates which included the workaround would fail to build due to the conflict.

See the getrandom documentation for more details on custom implementations.

Dependencies

~235KB