indy_utils/keys/
types.rs

1pub const KEY_ENC_BASE58: &'static str = "base58";
2
3pub const KEY_TYPE_ED25519: &'static str = "ed25519";
4pub const KEY_TYPE_X25519: &'static str = "x25519";
5
6/// Enum of known and unknown key types
7#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub enum KeyType {
9    ED25519,
10    X25519,
11    Other(String),
12}
13
14impl KeyType {
15    pub fn from_str(keytype: &str) -> KeyType {
16        match keytype.to_ascii_lowercase().as_str() {
17            KEY_TYPE_ED25519 => KeyType::ED25519,
18            KEY_TYPE_X25519 => KeyType::X25519,
19            _ => KeyType::Other(keytype.to_owned()),
20        }
21    }
22
23    pub fn is_known(&self) -> bool {
24        match self {
25            Self::Other(_) => false,
26            _ => true,
27        }
28    }
29
30    pub fn as_str(&self) -> &str {
31        match self {
32            Self::ED25519 => KEY_TYPE_ED25519,
33            Self::X25519 => KEY_TYPE_X25519,
34            Self::Other(t) => t.as_str(),
35        }
36    }
37}
38
39impl std::string::ToString for KeyType {
40    fn to_string(&self) -> String {
41        self.as_str().to_owned()
42    }
43}
44
45impl Default for KeyType {
46    fn default() -> Self {
47        KeyType::ED25519
48    }
49}
50
51impl std::ops::Deref for KeyType {
52    type Target = str;
53    fn deref(&self) -> &str {
54        self.as_str()
55    }
56}
57
58impl From<&str> for KeyType {
59    fn from(value: &str) -> Self {
60        Self::from_str(value)
61    }
62}
63
64impl From<String> for KeyType {
65    fn from(value: String) -> Self {
66        Self::from_str(&value)
67    }
68}
69
70/// Enum of known and unknown key encodings
71#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
72pub enum KeyEncoding {
73    BASE58,
74    Other(String),
75}
76
77impl KeyEncoding {
78    pub fn from_str(keyenc: &str) -> KeyEncoding {
79        match keyenc.to_ascii_lowercase().as_str() {
80            KEY_ENC_BASE58 => KeyEncoding::BASE58,
81            _ => KeyEncoding::Other(keyenc.to_owned()),
82        }
83    }
84
85    pub fn as_str(&self) -> &str {
86        match self {
87            Self::BASE58 => KEY_ENC_BASE58,
88            Self::Other(e) => e.as_str(),
89        }
90    }
91}
92
93impl std::string::ToString for KeyEncoding {
94    fn to_string(&self) -> String {
95        self.as_str().to_owned()
96    }
97}
98
99impl Default for KeyEncoding {
100    fn default() -> Self {
101        KeyEncoding::BASE58
102    }
103}
104
105impl std::ops::Deref for KeyEncoding {
106    type Target = str;
107    fn deref(&self) -> &str {
108        self.as_str()
109    }
110}
111
112impl From<&str> for KeyEncoding {
113    fn from(value: &str) -> Self {
114        Self::from_str(value)
115    }
116}
117
118impl From<String> for KeyEncoding {
119    fn from(value: String) -> Self {
120        Self::from_str(&value)
121    }
122}