-
Notifications
You must be signed in to change notification settings - Fork 2
/
random.rs
40 lines (33 loc) · 1.31 KB
/
random.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use proc_macro2::TokenStream;
use quote::quote;
pub fn generate_randomness_implementation() -> TokenStream {
quote! {
thread_local! {
static _CDK_RNG_REF_CELL: std::cell::RefCell<rand::rngs::StdRng> = std::cell::RefCell::new(rand::SeedableRng::from_seed([0u8; 32]));
}
fn custom_getrandom(_buf: &mut [u8]) -> Result<(), getrandom::Error> {
_CDK_RNG_REF_CELL.with(|rng_ref_cell| {
let mut rng = rng_ref_cell.borrow_mut();
rng.fill(_buf);
});
Ok(())
}
getrandom::register_custom_getrandom!(custom_getrandom);
fn rng_seed() {
ic_cdk::spawn(async move {
let result: ic_cdk::api::call::CallResult<(Vec<u8>,)> = ic_cdk::api::call::call(
candid::Principal::from_text("aaaaa-aa").unwrap(),
"raw_rand",
()
).await;
_CDK_RNG_REF_CELL.with(|rng_ref_cell| {
let mut rng = rng_ref_cell.borrow_mut();
match result {
Ok(randomness) => *rng = rand::SeedableRng::from_seed(randomness.0[..].try_into().unwrap()),
Err(err) => panic!(err)
};
});
});
}
}
}