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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// Wildland Project
//
// Copyright © 2022 Golem Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License version 3 as published by
// the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

use crate::WildlandIdentity::{Device, Forest};
use std::fmt::{self, Display, Formatter};
use wildland_crypto::identity::{
    signing_keypair::{PubKey, SecKey},
    SigningKeypair,
};

#[derive(Debug, PartialEq)]
pub enum WildlandIdentity {
    Forest(u64, SigningKeypair),
    Device(String, SigningKeypair),
}

impl Display for WildlandIdentity {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Forest(index, _) => write!(f, "wildland.forest.{index}"),
            Device(name, _) => write!(f, "wildland.device.{name}"),
        }
    }
}

impl WildlandIdentity {
    #[tracing::instrument(level = "debug", skip(self))]
    pub fn get_identifier(&self) -> String {
        match self {
            Forest(index, _) => index.to_string(),
            Device(name, _) => name.to_string(),
        }
    }

    #[tracing::instrument(level = "debug", skip(self))]
    pub fn get_public_key(&self) -> PubKey {
        match self {
            Forest(_, keypair) | Device(_, keypair) => keypair.public(),
        }
    }

    #[tracing::instrument(level = "debug", skip(self))]
    pub fn get_private_key(&self) -> SecKey {
        match self {
            Forest(_, keypair) | Device(_, keypair) => keypair.secret(),
        }
    }

    #[tracing::instrument(level = "debug", skip(self))]
    pub fn get_keypair_bytes(&self) -> Vec<u8> {
        match self {
            Forest(_, keypair) | Device(_, keypair) => keypair.to_bytes(),
        }
    }

    #[tracing::instrument(level = "debug", skip(self))]
    pub fn get_keypair(&self) -> SigningKeypair {
        match self {
            Forest(_, keypair) | Device(_, keypair) => SigningKeypair::from(keypair),
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::test_utilities::{SIGNING_PUBLIC_KEY, SIGNING_SECRET_KEY};
    use crate::WildlandIdentity;
    use wildland_crypto::identity::SigningKeypair;

    #[test]
    fn should_get_correct_fingerprint() {
        let keypair = SigningKeypair::try_from_str(SIGNING_PUBLIC_KEY, SIGNING_SECRET_KEY).unwrap();
        let wildland_identity = WildlandIdentity::Device("Device 1".to_string(), keypair);

        assert_eq!(
            wildland_identity.to_string(),
            "wildland.device.Device 1".to_string()
        )
    }
}