indy_data_types/keys/
types.rs

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