ockam_entity/
identifiers.rs

1use crate::profile::Profile;
2use crate::EntityError;
3use core::convert::TryFrom;
4use core::fmt::{Display, Formatter};
5use ockam_core::compat::string::String;
6use ockam_core::hex::encode;
7use ockam_core::vault::{Hasher, KeyId};
8use ockam_core::{Error, Result};
9use serde::{Deserialize, Serialize};
10
11/// An identifier of a Profile.
12#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize, Default)]
13pub struct EntityIdentifier(KeyId);
14
15pub type ProfileIdentifier = EntityIdentifier;
16
17/// Unique [`crate::Profile`] identifier, computed as SHA256 of root public key
18impl EntityIdentifier {
19    pub const PREFIX: &'static str = "P";
20    /// Create a EntityIdentifier from a KeyId
21    pub fn from_key_id(key_id: KeyId) -> Self {
22        Self { 0: key_id }
23    }
24    /// Return the wrapped KeyId
25    pub fn key_id(&self) -> &KeyId {
26        &self.0
27    }
28}
29
30impl Display for EntityIdentifier {
31    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
32        let str: String = self.clone().into();
33        write!(f, "{}", &str)
34    }
35}
36
37impl From<EntityIdentifier> for String {
38    fn from(id: EntityIdentifier) -> Self {
39        format!("{}{}", EntityIdentifier::PREFIX, &id.0)
40    }
41}
42
43impl TryFrom<&str> for EntityIdentifier {
44    type Error = Error;
45
46    fn try_from(value: &str) -> Result<Self> {
47        if let Some(str) = value.strip_prefix(Self::PREFIX) {
48            Ok(Self::from_key_id(str.into()))
49        } else {
50            Err(EntityError::InvalidProfileId.into())
51        }
52    }
53}
54
55impl TryFrom<String> for EntityIdentifier {
56    type Error = Error;
57
58    fn try_from(value: String) -> Result<Self> {
59        Self::try_from(value.as_str())
60    }
61}
62
63/// Unique [`crate::ProfileChangeEvent`] identifier, computed as SHA256 of the event data
64#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
65pub struct EventIdentifier([u8; 32]);
66
67impl AsRef<[u8]> for EventIdentifier {
68    fn as_ref(&self) -> &[u8] {
69        &self.0
70    }
71}
72
73impl EventIdentifier {
74    pub async fn initial(hasher: &mut (impl Hasher + Sync)) -> Self {
75        let h = match hasher.sha256(Profile::NO_EVENT).await {
76            Ok(hash) => hash,
77            Err(_) => panic!("failed to hash initial event"),
78        };
79        EventIdentifier::from_hash(h)
80    }
81    /// Create identifier from public key hash
82    pub fn from_hash(hash: [u8; 32]) -> Self {
83        Self { 0: hash }
84    }
85    /// Human-readable form of the id
86    pub fn to_string_representation(&self) -> String {
87        format!("E_ID.{}", encode(&self.0))
88    }
89}
90
91#[cfg(test)]
92mod test {
93    use super::*;
94    use core::convert::TryInto;
95    use rand::{thread_rng, RngCore};
96
97    impl EntityIdentifier {
98        pub fn random() -> EntityIdentifier {
99            EntityIdentifier(format!("{:x}", thread_rng().next_u64()))
100        }
101    }
102
103    #[test]
104    fn test_new() {
105        let _identifier = EntityIdentifier::from_key_id("test".to_string());
106    }
107
108    #[test]
109    fn test_into() {
110        let id1 = EntityIdentifier::random();
111
112        let str: String = id1.clone().into();
113        assert!(str.starts_with('P'));
114
115        let id2: EntityIdentifier = str.try_into().unwrap();
116        assert_eq!(id1, id2);
117    }
118}