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
use diem_crypto::{
ed25519::{Ed25519PrivateKey, Ed25519PublicKey},
PrivateKey, Uniform,
};
use rand::{
rngs::{OsRng, StdRng},
Rng, SeedableRng,
};
#[derive(Debug)]
pub struct KeyGen(StdRng);
impl KeyGen {
pub fn from_seed(seed: [u8; 32]) -> Self {
Self(StdRng::from_seed(seed))
}
pub fn from_os_rng() -> Self {
let mut seed_rng = OsRng;
let seed: [u8; 32] = seed_rng.gen();
Self::from_seed(seed)
}
pub fn generate_keypair(&mut self) -> (Ed25519PrivateKey, Ed25519PublicKey) {
let private_key = Ed25519PrivateKey::generate(&mut self.0);
let public_key = private_key.public_key();
(private_key, public_key)
}
}