prople_did_core/keys/
secure.rs

1use prople_crypto::types::VectorValue;
2use rst_common::standard::serde::{self, Deserialize, Serialize};
3use rst_common::standard::serde_json;
4use rst_common::with_errors::thiserror::{self, Error as ThisError};
5
6use prople_crypto::keysecure::KeySecure;
7
8use crate::types::{DIDError, JSONValue, ToJSON};
9
10/// `Error` defined for specific this module error types
11#[derive(Debug, ThisError)]
12pub enum Error {
13    #[error("keysecure error")]
14    BuildKeySecureError,
15
16    #[error("keysecure error: unable to build identity private keys")]
17    BuildIdentityPrivateKeysError,
18
19    #[error("keysecure error: unable to build JSON")]
20    BuildJSONError,
21
22    #[error("keysecure error: storage error")]
23    StorageError,
24
25    #[error("keysecure error: decrypt error: {0}")]
26    DecryptError(String),
27}
28
29/// `KeySecureBuilder` is a trait that should be implemented by any objects
30/// that need to save it's properties and structure into [`KeySecure`] format
31pub trait KeySecureBuilder {
32    fn build_keysecure(&self, password: String) -> Result<KeySecure, Error>;
33}
34
35/// `IdentityPrivateKeyPairsBuilder` is a trait that implemented to build privat ekeys
36pub trait IdentityPrivateKeyPairsBuilder {
37    fn build_private_keys(&self, password: String) -> Result<IdentityPrivateKeyPairs, Error>;
38}
39
40/// `PrivateKeyPairs` used to generate the `verification` and `aggrement`
41#[derive(Debug, Clone, Serialize, Deserialize)]
42#[serde(crate = "self::serde")]
43pub struct PrivateKeyPairs {
44    pub verification: KeySecure,
45    pub aggrement: KeySecure,
46}
47
48impl PrivateKeyPairs {
49    pub fn decrypt_verification(&self, password: String) -> Result<Vec<u8>, Error> {
50        let decrypted = self
51            .verification
52            .decrypt(password)
53            .map_err(|err| Error::DecryptError(err.to_string()))?;
54
55        Ok(decrypted.vec())
56    }
57
58    pub fn decrypt_agreement(&self, password: String) -> Result<Vec<u8>, Error> {
59        let decrypted = self
60            .aggrement
61            .decrypt(password)
62            .map_err(|err| Error::DecryptError(err.to_string()))?;
63
64        Ok(decrypted.vec())
65    }
66}
67
68/// `IdentityPrivateKeyPairs` used to specific identity object, it is different with the `Identity` object
69///
70/// This object used to as an object that will be saved to [`KeySecure`] format
71#[derive(Debug, Clone, Serialize, Deserialize)]
72#[serde(crate = "self::serde")]
73pub struct IdentityPrivateKeyPairs {
74    pub identity: String,
75
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub authentication: Option<PrivateKeyPairs>,
78
79    #[serde(skip_serializing_if = "Option::is_none")]
80    pub assertion: Option<PrivateKeyPairs>,
81}
82
83impl IdentityPrivateKeyPairs {
84    pub fn new(identity: String) -> Self {
85        Self {
86            identity,
87            authentication: None,
88            assertion: None,
89        }
90    }
91}
92
93impl ToJSON for IdentityPrivateKeyPairs {
94    fn to_json(&self) -> Result<JSONValue, DIDError> {
95        let jsonstr = serde_json::to_string(self)
96            .map_err(|err| DIDError::GenerateJSONError(err.to_string()))?;
97        Ok(JSONValue::from(jsonstr))
98    }
99}