1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::profile::Profile;
use crate::EntityError;
use ockam_core::hex::encode;
use ockam_core::Result;
use ockam_vault_core::{Hasher, KeyId};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize, Default)]
pub struct ProfileIdentifier(KeyId);
impl ProfileIdentifier {
pub fn from_key_id(key_id: KeyId) -> Self {
Self { 0: key_id }
}
pub fn to_external(&self) -> String {
format!("P_ID.{}", &self.0)
}
pub fn from_external(str: &str) -> Result<Self> {
if let Some(str) = str.strip_prefix("P_ID.") {
Ok(Self::from(str))
} else {
Err(EntityError::InvalidProfileId.into())
}
}
pub fn key_id(&self) -> &KeyId {
&self.0
}
}
impl From<&str> for ProfileIdentifier {
fn from(s: &str) -> Self {
ProfileIdentifier::from_key_id(s.into())
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq, Hash)]
pub struct EventIdentifier([u8; 32]);
impl AsRef<[u8]> for EventIdentifier {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
impl EventIdentifier {
pub fn initial<H: Hasher>(mut hasher: H) -> Self {
let h = match hasher.sha256(Profile::NO_EVENT) {
Ok(hash) => hash,
Err(_) => panic!("failed to hash initial event"),
};
EventIdentifier::from_hash(h)
}
pub fn from_hash(hash: [u8; 32]) -> Self {
Self { 0: hash }
}
pub fn to_string_representation(&self) -> String {
format!("E_ID.{}", encode(&self.0))
}
}
#[cfg(test)]
mod test {
use super::*;
use rand::{thread_rng, RngCore};
impl ProfileIdentifier {
pub fn random() -> ProfileIdentifier {
ProfileIdentifier(format!("{:x}", thread_rng().next_u64()))
}
}
#[test]
fn test_new() {
let _identifier = ProfileIdentifier::from_key_id("test".to_string());
}
}