forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentity.rs
More file actions
47 lines (38 loc) · 1.21 KB
/
Copy pathidentity.rs
File metadata and controls
47 lines (38 loc) · 1.21 KB
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
use ed25519_dalek::{SigningKey, VerifyingKey};
use rand_core::OsRng;
use std::hash::{Hash, Hasher};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct ActorId(pub Vec<u8>);
impl ActorId {
pub fn new() -> Self {
let mut csprng = OsRng;
let signing_key = SigningKey::generate(&mut csprng);
let verifying_key = signing_key.verifying_key();
let pub_bytes = verifying_key.as_bytes();
let hash = blake3::hash(pub_bytes);
let mut envelope = Vec::new();
// multihash prefix for blake3 (0x1e)
envelope.push(0x1e);
envelope.push(32); // length
envelope.extend_from_slice(hash.as_bytes());
ActorId(envelope)
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
}
impl std::fmt::Debug for ActorId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "ActorId({})", hex::encode(&self.0))
}
}
impl std::fmt::Display for ActorId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", hex::encode(&self.0))
}
}
impl Hash for ActorId {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}