File size: 995 Bytes
224e773
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! Seed chain: cryptographic state derivation

pub struct SeedChain {
    seeds: Vec<Vec<u8>>,
}

impl SeedChain {
    pub fn new(initial_seed: &[u8]) -> Self {
        SeedChain {
            seeds: vec![initial_seed.to_vec()],
        }
    }

    pub fn derive_next(&mut self) {
        let last = &self.seeds[self.seeds.len() - 1];
        let mut next = last.clone();
        // Simple derivation: increment first byte
        if let Some(first) = next.first_mut() {
            *first = first.wrapping_add(1);
        }
        self.seeds.push(next);
    }

    pub fn current_seed(&self) -> Vec<u8> {
        self.seeds.last().cloned().unwrap_or_default()
    }

    pub fn len(&self) -> usize {
        self.seeds.len()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn seed_chain_derivation() {
        let mut chain = SeedChain::new(&[1, 2, 3]);
        chain.derive_next();
        assert_eq!(chain.len(), 2);
    }
}