mithril_common/crypto_helper/cardano/
cold_key.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
use ed25519_dalek::SigningKey as ColdSecretKey;
use rand_chacha::ChaCha20Rng;
use rand_core::SeedableRng;

/// A cold key generator / test only
#[doc(hidden)]
#[derive(Debug)]
pub struct ColdKeyGenerator();

impl ColdKeyGenerator {
    #[doc(hidden)]
    /// Create a deterministic secret key using the given seed. (test only)
    pub fn create_deterministic_keypair(seed: [u8; 32]) -> ColdSecretKey {
        let mut rng = ChaCha20Rng::from_seed(seed);
        ColdSecretKey::generate(&mut rng)
    }
}

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

    #[test]
    fn test_generate_deterministic_genesis_keypair() {
        let cold_keypair1 = ColdKeyGenerator::create_deterministic_keypair([0u8; 32]);
        let cold_keypair2 = ColdKeyGenerator::create_deterministic_keypair([0u8; 32]);
        let cold_keypair3 = ColdKeyGenerator::create_deterministic_keypair([1u8; 32]);
        assert_eq!(cold_keypair1.to_bytes(), cold_keypair2.to_bytes());
        assert_ne!(cold_keypair1.to_bytes(), cold_keypair3.to_bytes());
    }
}