celestia_generators/
generator.rs

1use std::time::SystemTime;
2
3use hex::encode;
4use rand::{Rng, SeedableRng};
5use rand_chacha::ChaCha8Rng;
6
7pub struct PayForBlobGen {
8    pub rand: ChaCha8Rng,
9}
10
11impl PayForBlobGen {
12    pub fn new() -> Self {
13        PayForBlobGen::from_seed(1000)
14    }
15
16    pub fn from_seed(seed: u64) -> Self {
17        let rand_seed = SystemTime::now()
18            .duration_since(SystemTime::UNIX_EPOCH)
19            .expect("System time is before UNIX_EPOCH")
20            .as_nanos() as u64;
21        let rand = ChaCha8Rng::seed_from_u64(seed + rand_seed);
22
23        Self { rand }
24    }
25
26    fn hexer(&self, bytes: &[u8]) -> String {
27        encode(bytes)
28    }
29
30    pub fn namespace_id(&mut self) -> String {
31        let mut random_bytes = [0u8; 8];
32        self.rand.fill(&mut random_bytes); // Generate 8 random bytes
33        self.hexer(&random_bytes) // Encode the bytes as a hex string and return it
34    }
35
36    pub fn message(&mut self, length: usize) -> String {
37        assert!(length <= 100, "Message length should be up to 100 bytes");
38
39        let mut random_bytes = vec![0u8; length];
40        self.rand.fill(&mut random_bytes[..]); // Generate random bytes with the given length
41        self.hexer(&random_bytes) // Encode the bytes as a hex string and return it
42    }
43}
44
45impl Default for PayForBlobGen {
46    fn default() -> Self {
47        Self::new()
48    }
49}