1use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
6pub struct PeerId(pub [u8; 32]);
7
8impl PeerId {
9 pub fn to_hex(&self) -> String {
11 hex::encode(self.0)
12 }
13}
14
15impl std::fmt::Display for PeerId {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 write!(f, "{}", self.to_hex())
18 }
19}
20
21#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
22pub struct ChannelId(pub [u8; 32]);
23
24impl ChannelId {
25 pub fn from_channel(channel: &str, password: Option<&str>) -> Self {
27 let mut hasher = blake3::Hasher::new();
28 hasher.update(channel.as_bytes());
29 hasher.update(b":");
30 if let Some(password) = password {
31 hasher.update(password.as_bytes());
32 }
33 let hash = hasher.finalize();
34 ChannelId(*hash.as_bytes())
35 }
36
37 pub fn to_hex(&self) -> String {
39 hex::encode(self.0)
40 }
41}
42
43#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
44pub struct MessageId(pub [u8; 32]);
45
46impl MessageId {
47 pub fn new(from: PeerId, timestamp: u64, text: &str) -> Self {
50 let mut hasher = blake3::Hasher::new();
51 hasher.update(&from.0);
52 hasher.update(×tamp.to_le_bytes());
53 hasher.update(text.as_bytes());
54 let hash = hasher.finalize();
55 MessageId(*hash.as_bytes())
56 }
57}