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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::fmt::{Debug, Formatter, Result as FmtResult};
use crate::message::RecvType;
use crate::primitive::{HandleResult, PeerId, Result};
pub const GROUP_LENGTH: usize = 32;
#[derive(Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
pub struct GroupId(pub [u8; GROUP_LENGTH]);
impl GroupId {
pub fn short_show(&self) -> String {
let mut hex = String::new();
hex.extend(self.0.iter().map(|byte| format!("{:02x?}", byte)));
let mut new_hex = String::new();
new_hex.push_str("0x");
new_hex.push_str(&hex[0..4]);
new_hex.push_str("...");
new_hex.push_str(&hex[hex.len() - 5..]);
new_hex
}
pub fn from_symbol(s: impl ToString) -> GroupId {
let s = s.to_string();
let bytes = blake3::hash(s.as_bytes());
GroupId(*bytes.as_bytes())
}
pub fn from_hex(s: impl ToString) -> Result<GroupId> {
let s = s.to_string();
if s.len() != GROUP_LENGTH * 2 {
return Err(anyhow::anyhow!("Hex is invalid"));
}
let mut value = [0u8; GROUP_LENGTH];
for i in 0..(s.len() / 2) {
let res = u8::from_str_radix(&s[2 * i..2 * i + 2], 16)?;
value[i] = res;
}
Ok(GroupId(value))
}
pub fn to_hex(&self) -> String {
let mut hex = String::new();
hex.extend(self.0.iter().map(|byte| format!("{:02x?}", byte)));
hex
}
}
impl Debug for GroupId {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
let mut hex = String::new();
hex.extend(self.0.iter().map(|byte| format!("{:02x?}", byte)));
write!(f, "0x{}", hex)
}
}
impl Into<PeerId> for GroupId {
fn into(self) -> PeerId {
chamomile_types::types::PeerId(self.0)
}
}
pub trait Group {
fn id(&self) -> &GroupId;
fn guard(&self, addr: &PeerId) -> bool;
fn handle(&mut self, msg: RecvType) -> Result<HandleResult>;
}
pub trait Peer {
type PublicKey: Serialize + DeserializeOwned + Eq + std::hash::Hash + Clone;
type SecretKey: Serialize + DeserializeOwned;
type Signature: Serialize + DeserializeOwned;
fn sign(sk: &Self::SecretKey, msg: &Vec<u8>) -> Result<Self::Signature>;
fn verify(pk: &Self::PublicKey, msg: &Vec<u8>, sign: &Self::Signature) -> bool;
}
#[derive(Copy, Clone, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
pub struct EventId(pub [u8; 32]);
impl EventId {
pub fn from_hex(s: impl ToString) -> Result<EventId> {
let s = s.to_string();
if s.len() != 64 {
return Err(anyhow::anyhow!("Hex is invalid"));
}
let mut value = [0u8; 32];
for i in 0..(s.len() / 2) {
let res = u8::from_str_radix(&s[2 * i..2 * i + 2], 16)?;
value[i] = res;
}
Ok(EventId(value))
}
pub fn to_hex(&self) -> String {
let mut hex = String::new();
hex.extend(self.0.iter().map(|byte| format!("{:02x?}", byte)));
hex
}
}
pub trait Event: Clone + Send + Debug + Eq + Ord + Serialize + DeserializeOwned {
fn id(&self) -> &EventId;
}