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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
pub mod app;
pub mod client;
pub mod node;
use crate::{utils, PublicKey, Result, XorName};
use multibase::Decodable;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Debug, Display, Formatter};
#[derive(Clone, Eq, PartialEq, Serialize, Deserialize, PartialOrd, Ord, Hash)]
pub enum PublicId {
Node(node::PublicId),
Client(client::PublicId),
App(app::PublicId),
}
impl PublicId {
pub fn name(&self) -> &XorName {
match self {
Self::Node(pub_id) => pub_id.name(),
Self::Client(pub_id) => pub_id.name(),
Self::App(pub_id) => pub_id.owner_name(),
}
}
pub fn node_public_id(&self) -> Option<&node::PublicId> {
if let Self::Node(id) = self {
Some(id)
} else {
None
}
}
pub fn client_public_id(&self) -> Option<&client::PublicId> {
if let Self::Client(id) = self {
Some(id)
} else {
None
}
}
pub fn app_public_id(&self) -> Option<&app::PublicId> {
if let Self::App(id) = self {
Some(id)
} else {
None
}
}
pub fn public_key(&self) -> PublicKey {
match self {
Self::Node(pub_id) => (*pub_id.ed25519_public_key()).into(),
Self::Client(pub_id) => *pub_id.public_key(),
Self::App(pub_id) => *pub_id.public_key(),
}
}
pub fn encode_to_zbase32(&self) -> String {
utils::encode(&self)
}
pub fn decode_from_zbase32<T: Decodable>(encoded: T) -> Result<Self> {
utils::decode(encoded)
}
}
impl Debug for PublicId {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
Self::Node(pub_id) => write!(formatter, "{:?}", pub_id),
Self::Client(pub_id) => write!(formatter, "{:?}", pub_id),
Self::App(pub_id) => write!(formatter, "{:?}", pub_id),
}
}
}
impl Display for PublicId {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
Debug::fmt(self, formatter)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{ClientFullId, Error};
use unwrap::unwrap;
#[test]
fn zbase32_encode_decode_client_public_id() {
let mut rng = rand::thread_rng();
let id = client::FullId::new_ed25519(&mut rng);
assert_eq!(
unwrap!(client::PublicId::decode_from_zbase32(
&id.public_id().encode_to_zbase32()
)),
*id.public_id()
);
let node_id = node::FullId::new(&mut rng);
assert!(match client::PublicId::decode_from_zbase32(
&node_id.public_id().encode_to_zbase32()
) {
Err(Error::FailedToParse(_)) => true,
_ => false,
});
assert!(client::PublicId::decode_from_zbase32("sdkjf832939fjs").is_err());
}
#[test]
fn zbase32_encode_decode_node_public_id() {
let mut rng = rand::thread_rng();
let mut id = node::FullId::new(&mut rng);
let bls_secret_key = threshold_crypto::SecretKeySet::random(1, &mut rng);
id.set_bls_keys(bls_secret_key.secret_key_share(0));
assert_eq!(
unwrap!(node::PublicId::decode_from_zbase32(
&id.public_id().encode_to_zbase32()
)),
*id.public_id()
);
assert!(node::PublicId::decode_from_zbase32("7djsk38").is_err());
}
#[test]
fn zbase32_encode_decode_app_public_id() {
let mut rng = rand::thread_rng();
let owner = ClientFullId::new_ed25519(&mut rng);
let id = app::FullId::new_ed25519(&mut rng, owner.public_id().clone());
assert_eq!(
unwrap!(app::PublicId::decode_from_zbase32(
&id.public_id().encode_to_zbase32()
)),
*id.public_id()
);
assert!(app::PublicId::decode_from_zbase32("7od8fh2").is_err());
}
#[test]
fn zbase32_encode_decode_enum_public_id() {
let mut rng = rand::thread_rng();
let id = PublicId::Client(client::FullId::new_ed25519(&mut rng).public_id().clone());
assert_eq!(
id,
unwrap!(PublicId::decode_from_zbase32(&id.encode_to_zbase32()))
);
assert!(PublicId::decode_from_zbase32("c419cxim9").is_err());
}
}