Initializes the random number generator if it has not been initialized yet. This function is async because it may need to call into the management canister to get a seed with `raw_rand``.
()
| 44 | /// |
| 45 | /// This function is async because it may need to call into the management canister to get a seed with `raw_rand``. |
| 46 | pub async fn initialize_rng() -> Result<(), String> { |
| 47 | ic_cdk::print("started to initialize rng"); |
| 48 | |
| 49 | let (created_seed,) = management_canister::main::raw_rand() |
| 50 | .await |
| 51 | .map_err(|e| e.1)?; |
| 52 | |
| 53 | let seed = created_seed |
| 54 | .try_into() |
| 55 | .map_err(|_| "raw_rand not 32 bytes".to_string())?; |
| 56 | |
| 57 | initialize_rng_from_seed(seed); |
| 58 | |
| 59 | RAW_RAND_SUCCESSFUL.with(|b| *b.borrow_mut() = true); |
| 60 | |
| 61 | ic_cdk::print("rng succesfully initialized"); |
| 62 | Ok(()) |
| 63 | } |
| 64 | |
| 65 | /// Initializes the random number generator with the given seed. |
| 66 | pub fn initialize_rng_from_seed(seed: [u8; 32]) { |
no test coverage detected