Skip to main content

ossa_core/
auth.rs

1use std::fmt::Display;
2
3use rand_core::OsRng;
4use serde::{Deserialize, Serialize};
5
6#[derive(Debug, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
7pub(crate) struct DeviceId {
8    auth_key: ed25519_dalek::VerifyingKey,
9}
10
11impl PartialOrd for DeviceId {
12    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
13        Some(self.cmp(&other))
14    }
15}
16
17impl Ord for DeviceId {
18    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
19        self.auth_key.as_bytes().cmp(other.auth_key.as_bytes())
20    }
21}
22
23impl Display for DeviceId {
24    // Display devide ids as base 58.
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        let base58 = bs58::encode(self.auth_key.as_bytes()).into_string();
27        write!(f, "{base58}")?;
28        Ok(())
29    }
30}
31
32impl DeviceId {
33    pub(crate) fn new(auth_key: ed25519_dalek::VerifyingKey) -> Self {
34        Self { auth_key }
35    }
36}
37
38#[derive(Debug, Clone)]
39pub(crate) struct Identity {
40    // Authentication key.
41    auth_key: ed25519_dalek::SigningKey,
42    // Signing key.
43    // Encryption key.
44}
45
46impl Identity {
47    pub(crate) fn auth_key(&self) -> &ed25519_dalek::SigningKey {
48        &self.auth_key
49    }
50}
51
52pub fn generate_identity() -> Identity {
53    let mut rng = OsRng;
54    let auth_key = ed25519_dalek::SigningKey::generate(&mut rng);
55
56    Identity { auth_key }
57}