decanter_crypto/keys/
keypair.rs

1/*
2    Appellation: keypair <module>
3    Contributors: FL03 <jo3mccain@icloud.com>
4    Description:
5        ... Summary ...
6*/
7use crate::generate_random_pkcs8;
8use ring::{pkcs8, rand::SystemRandom, signature::Ed25519KeyPair};
9
10#[derive(Debug)]
11pub struct Keypair {
12    keypair: Ed25519KeyPair,
13}
14
15impl Keypair {
16    pub fn new(keypair: Ed25519KeyPair) -> Self {
17        Self { keypair }
18    }
19    pub fn generate() -> Self {
20        Self::from(generate_random_pkcs8())
21    }
22    pub fn keypair(&self) -> &Ed25519KeyPair {
23        &self.keypair
24    }
25}
26
27impl From<&[u8]> for Keypair {
28    fn from(data: &[u8]) -> Self {
29        Self::new(Ed25519KeyPair::from_pkcs8(data).expect(""))
30    }
31}
32
33impl From<pkcs8::Document> for Keypair {
34    fn from(data: pkcs8::Document) -> Self {
35        Self::from(data.as_ref())
36    }
37}
38
39#[derive(Debug)]
40pub enum Keys {
41    ED25519(Ed25519KeyPair),
42}
43
44impl Keys {
45    pub fn new() -> Self {
46        let rng = SystemRandom::new();
47        let pkcs8_bytes = Ed25519KeyPair::generate_pkcs8(&rng).ok().unwrap();
48        let keypair = Ed25519KeyPair::from_pkcs8(pkcs8_bytes.as_ref())
49            .ok()
50            .unwrap();
51        Self::ED25519(keypair)
52    }
53}
54
55impl Default for Keys {
56    fn default() -> Self {
57        Self::new()
58    }
59}