snowstorm/identity/
mod.rs1use std::{collections::HashSet, fmt::Display};
2
3use libp2p::identity::SigningError;
4use serde::{Deserialize, Serialize};
5
6use crate::config::Config;
7
8#[derive(Debug, Clone, Serialize, Deserialize, Copy, PartialEq, Eq, Hash)]
9pub enum Role {
10 Client,
11 Volunteer,
12 Exit,
13}
14
15impl Display for Role {
16 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
17 let s = match self {
18 Role::Client => "client",
19 Role::Volunteer => "volunteer",
20 Role::Exit => "exit",
21 };
22
23 write!(f, "{}", s)
24 }
25}
26
27impl From<&str> for Role {
28 fn from(s: &str) -> Self {
29 match s {
30 "client" => Role::Client,
31 "volunteer" => Role::Volunteer,
32 "exit" => Role::Exit,
33 _ => panic!("invalid role {:?}", s),
34 }
35 }
36}
37
38#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
39pub struct Identity {
40 pub pid: String,
42
43 pub roles: HashSet<Role>,
45
46 pub location: Option<String>,
48}
49
50impl Identity {
51 pub fn sign(&self, config: &Config) -> Result<Vec<u8>, SigningError> {
52 let payload = serde_cbor::to_vec(&self).unwrap();
53 config.keypair.sign(&payload)
54 }
55}
56
57impl From<&Config> for Identity {
58 fn from(config: &Config) -> Self {
59 Identity {
60 pid: config.keypair.public().to_peer_id().to_base58().to_string(),
61 roles: config.roles.clone(),
62 location: config.location.clone(),
63 }
64 }
65}