prople_did_core/identity/
mod.rs

1//! `identity` is a module used to generate an entity [`types::Identity`]
2use crate::doc::{Doc, Primary, ToDoc};
3use crate::keys::{
4    AgreementKey, AgreementPairs, IdentityPrivateKeyPairs, IdentityPrivateKeyPairsBuilder,
5    KeySecureBuilder, KeySecureError, PrivateKeyPairs, VerificationKey, VerificationPairs,
6};
7
8use crate::types::*;
9
10/// `VerificationMethod` is an object used to generate two important keys, a `verification_pairs`
11/// and an `aggreement_pairs`
12///
13/// A `verification_pairs` is a public and private keys used to verify a context based on some signature
14/// An `aggreement_pairs` is a public and private keys used to generate the shared secret through `ECDH` algorithm
15#[derive(Debug, Clone)]
16pub struct VerificationMethod {
17    pub verification_pairs: VerificationPairs,
18    pub agreement_pairs: AgreementPairs,
19}
20
21/// `Identity` is an object that hold a `DID Syntax` with specific `Prople DID Method` which contains
22/// `authentication` and `assertion`
23///
24/// The `authentication` used to authenticate an access request.
25/// The  `assertion` used to assert some given resources
26#[derive(Debug, Clone)]
27pub struct Identity {
28    identity: String,
29    authentication: Option<VerificationMethod>,
30    assertion: Option<VerificationMethod>,
31}
32
33impl Identity {
34    pub fn new(val: String) -> Self {
35        Self {
36            identity: val,
37            authentication: None,
38            assertion: None,
39        }
40    }
41
42    pub fn value(&self) -> String {
43        self.identity.clone()
44    }
45
46    pub fn account(&self) -> Result<String, DIDError> {
47        let identity = self.identity.clone();
48        let value = identity.as_str().split(DID_SYNTAX_MARK);
49        if value.clone().count() < 3 {
50            return Err(DIDError::InvalidDID);
51        }
52
53        value
54            .clone()
55            .last()
56            .map(|val| val.to_string())
57            .ok_or(DIDError::InvalidDID)
58    }
59
60    pub fn get_authentication_method(&self) -> Option<VerificationMethod> {
61        self.authentication.to_owned()
62    }
63
64    pub fn get_assertion_method(&self) -> Option<VerificationMethod> {
65        self.assertion.to_owned()
66    }
67
68    pub fn build_auth_method(&mut self) -> &mut Self {
69        if self.authentication.is_none() {
70            let verification_method = self.build_verification_method();
71            self.authentication = Some(verification_method);
72        }
73
74        self
75    }
76
77    pub fn build_assertion_method(&mut self) -> &mut Self {
78        if self.assertion.is_none() {
79            let verification_method = self.build_verification_method();
80            self.assertion = Some(verification_method);
81        }
82
83        self
84    }
85
86    fn build_verification_method(&self) -> VerificationMethod {
87        let verification_key = VerificationKey::new();
88        let verification_pairs = verification_key.generate();
89
90        let agreement_key = AgreementKey::new();
91        let agreement_pairs = agreement_key.generate();
92
93        VerificationMethod {
94            verification_pairs,
95            agreement_pairs,
96        }
97    }
98}
99
100impl ToDoc for Identity {
101    fn to_doc(&self) -> Doc {
102        let auth_verification_id = format!("{}#key-auth-verification", self.identity);
103        let auth_aggreement_id = format!("{}#key-auth-aggrement", self.identity);
104        let assertion_verification_id = format!("{}#key-assertion-verification", self.identity);
105        let assertion_aggreement_id = format!("{}#key-assertion-aggrement", self.identity);
106
107        let mut doc = Doc::generate(self.identity.clone());
108        doc.add_context(CONTEXT_ED25519.to_string())
109            .add_context(CONTEXT_X25519.to_string());
110
111        if let Some(auth) = &self.authentication {
112            let auth_verification_primary = Primary {
113                id: auth_verification_id,
114                controller: self.identity.clone(),
115                verification_type: VERIFICATION_TYPE_ED25519.to_string(),
116                multibase: auth.verification_pairs.clone().pub_key,
117            };
118
119            let auth_aggreement_primary = Primary {
120                id: auth_aggreement_id,
121                controller: self.identity.clone(),
122                verification_type: VERIFICATION_TYPE_X25519.to_string(),
123                multibase: auth.agreement_pairs.clone().pub_key,
124            };
125
126            doc.add_authentication(auth_verification_primary)
127                .add_authentication(auth_aggreement_primary);
128        }
129
130        if let Some(assertion) = &self.assertion {
131            let assertion_verification_primary = Primary {
132                id: assertion_verification_id,
133                controller: self.identity.clone(),
134                verification_type: VERIFICATION_TYPE_ED25519.to_string(),
135                multibase: assertion.verification_pairs.clone().pub_key,
136            };
137
138            let assertion_aggreement_primary = Primary {
139                id: assertion_aggreement_id,
140                controller: self.identity.clone(),
141                verification_type: VERIFICATION_TYPE_X25519.to_string(),
142                multibase: assertion.agreement_pairs.clone().pub_key,
143            };
144
145            doc.add_assertion(assertion_verification_primary)
146                .add_assertion(assertion_aggreement_primary);
147        }
148
149        doc
150    }
151}
152
153impl IdentityPrivateKeyPairsBuilder for Identity {
154    fn build_private_keys(
155        &self,
156        password: String,
157    ) -> Result<IdentityPrivateKeyPairs, KeySecureError> {
158        let mut pairs = IdentityPrivateKeyPairs::new(self.value());
159
160        if let Some(authentication) = &self.authentication {
161            let auth_verification_keysecure = authentication
162                .verification_pairs
163                .clone()
164                .build_keysecure(password.clone())
165                .map_err(|_| KeySecureError::BuildIdentityPrivateKeysError)?;
166
167            let auth_aggrement_keysecure = authentication
168                .agreement_pairs
169                .clone()
170                .build_keysecure(password.clone())
171                .map_err(|_| KeySecureError::BuildIdentityPrivateKeysError)?;
172
173            pairs.authentication = Some(PrivateKeyPairs {
174                verification: auth_verification_keysecure,
175                aggrement: auth_aggrement_keysecure,
176            })
177        }
178
179        if let Some(assertion) = &self.assertion {
180            let assertion_verification_keysecure = assertion
181                .verification_pairs
182                .clone()
183                .build_keysecure(password.clone())
184                .map_err(|_| KeySecureError::BuildIdentityPrivateKeysError)?;
185
186            let assertion_aggrement_keysecure = assertion
187                .agreement_pairs
188                .clone()
189                .build_keysecure(password.clone())
190                .map_err(|_| KeySecureError::BuildIdentityPrivateKeysError)?;
191
192            pairs.assertion = Some(PrivateKeyPairs {
193                verification: assertion_verification_keysecure,
194                aggrement: assertion_aggrement_keysecure,
195            })
196        }
197
198        Ok(pairs)
199    }
200}