key_resolver/
ed25519_verification_key2018.rs

1use fi_common::error::Error;
2
3use crate::common::{KeyPair, VerificationKey};
4
5pub(crate) const SUITE_ID: &str = "Ed25519VerificationKey2018";
6pub(crate) const SUITE_CONTEXT: &str = "https://w3id.org/security/suites/ed25519-2018/v1";
7
8pub struct Ed25519VerificationKey2018 {
9    _type: String,
10    id: Option<String>,
11    controller: Option<String>,
12    public_key_base58: String,
13    private_key_base58: Option<String>,
14    revoked: bool,
15}
16
17impl Ed25519VerificationKey2018 {
18    pub fn new(
19        controller: Option<String>,
20        public_key_base58: String,
21        private_key_base58: Option<String>,
22        fingerprint: Option<String>,
23    ) -> Self {
24        let mut id: Option<String> = None;
25        if controller.is_some() && fingerprint.is_some() {
26            let ctrler = controller.clone().unwrap();
27            let fprint = fingerprint.clone().unwrap();
28            id = Some(format!("{}#{}", ctrler, fprint));
29        }
30
31        return Ed25519VerificationKey2018 {
32            _type: String::from(SUITE_ID),
33            id,
34            controller,
35            private_key_base58,
36            public_key_base58,
37            revoked: false,
38        };
39    }
40}
41
42impl VerificationKey for Ed25519VerificationKey2018 {
43    fn from_fingerprint(fingerprint: &str) -> Result<Self, Error> {
44        if !fingerprint[0..1].eq("z") {
45            return Err(Error::new(
46                "`fingerprint` must be a multibase encoded string.",
47            ));
48        }
49
50        let buffer = bs58::decode(&fingerprint[1..]);
51        let decoded = match buffer.into_vec() {
52            Ok(val) => val,
53            Err(_error) => {
54                return Err(Error::new(
55                    "Couldn't convert decoded bs58 values into a vec",
56                ));
57            }
58        };
59
60        if decoded[0] == 0xed && decoded[1] == 0x01 {
61            let encoded_public_key_builder = bs58::encode(&decoded[2..]);
62            let encoded_public_key = encoded_public_key_builder.into_string();
63            return Ok(Ed25519VerificationKey2018::new(
64                None,
65                encoded_public_key,
66                None,
67                Some(String::from(fingerprint)),
68            ));
69        }
70
71        return Err(Error::new(
72            format!("Unsupported fingerprint {}", fingerprint).as_str(),
73        ));
74    }
75
76    fn get_suite_id() -> &'static str
77    where
78        Self: Sized,
79    {
80        SUITE_ID
81    }
82
83    fn get_private_key_content(&self) -> &Option<String> {
84        &self.private_key_base58
85    }
86    fn get_public_key_content(&self) -> &String {
87        &self.public_key_base58
88    }
89
90    fn get_current_suite_id(&self) -> &'static str
91    where
92        Self: Sized,
93    {
94        SUITE_ID
95    }
96
97    fn get_suite_context() -> &'static str
98    where
99        Self: Sized,
100    {
101        SUITE_CONTEXT
102    }
103
104    fn get_current_suite_context(&self) -> &'static str
105    where
106        Self: Sized,
107    {
108        SUITE_CONTEXT
109    }
110
111    fn get_controller(&self) -> &Option<String> {
112        &self.controller
113    }
114
115    fn export(&self, public_key: bool, private_key: bool, include_context: bool) -> KeyPair {
116        KeyPair {
117            id: self.id.clone(),
118            _type: self._type.clone(),
119            context: match include_context {
120                true => Some(String::from(SUITE_CONTEXT)),
121                false => None,
122            },
123            public_key_base58: match public_key {
124                true => Some(self.public_key_base58.clone()),
125                false => None,
126            },
127            private_key_base58: match private_key {
128                true => self.private_key_base58.clone(),
129                false => None,
130            },
131            private_key_multibase: None,
132            public_key_multibase: None,
133            revoked: self.revoked,
134            controller: self.controller.clone(),
135        }
136    }
137
138    fn get_type(&self) -> String {
139        self._type.clone()
140    }
141}