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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use management_canister;
use ;
/// Initializes a [`StdRng`] using 32 bytes of randomness retrieved from the IC management canister.
///
/// # Behavior
///
/// This function internally calls [`raw_rand()`] to obtain 32 bytes of randomness
/// from the management canister and uses them as a seed to create a deterministic
/// random number generator (`StdRng`).
///
/// If randomness cannot be retrieved (e.g., due to a call failure), the function
/// returns `None` instead of blocking initialization. This allows the caller to
/// proceed with limited functionality.
///
/// # Returns
///
/// - `Some(StdRng)` if randomness was successfully fetched.
/// - `None` if the management canister call failed.
///
/// # Example
///
/// ```ignore
/// if let Some(mut rng) = get_random_seed().await {
/// let value: u64 = rng.gen();
/// println!("Random value: {}", value);
/// }
/// ```
pub async
/// Retrieves 32 bytes of randomness from the Internet Computer management canister.
///
/// # Behavior
///
/// Calls the management canister's [`raw_rand`](https://internetcomputer.org/docs/current/references/ic-interface-spec#ic-management-canister)
/// method, which returns 32 random bytes (256 bits) of cryptographically secure randomness.
/// These bytes can be used as a seed for random number generators or as a unique salt.
///
/// The function performs length validation to ensure the result is exactly 32 bytes.
///
/// # Returns
///
/// - `Ok([u8; 32])` containing the random seed bytes on success.
/// - `Err(String)` with an error message if the call failed or returned an unexpected length.
///
/// # Example
///
/// ```ignore
/// let seed = raw_rand().await?;
/// println!("Received 32 bytes of randomness: {:?}", seed);
/// ```
pub async