This fork introduces a noop
(short for "no operation") feature which, when enabled and used with a build target of wasm32-unknown-unknown
does... NOTHING! Well, not exactly. Actually, what it does is stub out the low level getrandom_inner
function to panic when called with a helpful message to that effect. Why is this? Well, the only other way for the getrandom
to compile into WASM is to set the js
flag instead which makes the crate rely on browser or Node.js Javascript calls to implement getrandom_inner
. However, this code
Line 26 in 4b15c00
{
"ActionError": {
"index": 3,
"kind": {
"FunctionCallError": {
"CompilationError": {
"PrepareError": "Instantiate"
}
}
}
}
}
This means that if any crate you are pulling into your project relies on rand
or getrandom
it will blow up your contract. You will have to find a new crate to use or fork the crate and cut out the reliant code.
Instead, just path getrandom
and turn on the noop
feature in your Cargo.toml
file:
[patch.crates-io]
getrandom = { git = "https://github.com/peteoleary/getrandom" }
[dependencies]
getrandom = { version="0.2.6", features = ["noop"] }
Your contract will (hopefully) instantiate without problem and your code won't blow up until it actually tries to get a random number at which point you will get an informative error message.
NOTE: this fork has a branch specifically for use in NEAR smart contracts. Please read https://github.com/peteoleary/getrandom/tree/2022-may-31-add-noop-feature#readme
A Rust library for retrieving random data from (operating) system source. It is
assumed that system always provides high-quality cryptographically secure random
data, ideally backed by hardware entropy sources. This crate derives its name
from Linux's getrandom
function, but is cross platform, roughly supporting
the same set of platforms as Rust's std
lib.
This is a low-level API. Most users should prefer using high-level random-number
library like rand
.
Add this to your Cargo.toml
:
[dependencies]
getrandom = "0.2"
Then invoke the getrandom
function:
fn get_random_buf() -> Result<[u8; 32], getrandom::Error> {
let mut buf = [0u8; 32];
getrandom::getrandom(&mut buf)?;
Ok(buf)
}
For more information about supported targets, entropy sources, no_std
targets,
crate features, WASM support and Custom RNGs see the
getrandom
documentation and
getrandom::Error
documentation.
This crate requires Rust 1.34.0 or later.
The getrandom
library is distributed under either of
at your option.