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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
use bs58;
use multibase::Base;
use rand::Rng;
use std::{fmt, hash};
use crate::{
identity::PublicKey,
multibase::Multibase,
multicodec::{self, Multicodec},
multihash::Multihash,
Error, Result,
};
const MAX_INLINE_KEY_LENGTH: usize = 42;
#[derive(Clone, Eq)]
pub struct PeerId {
mh: Multihash,
}
impl fmt::Debug for PeerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("PeerId").field(&self.to_base58btc()).finish()
}
}
impl fmt::Display for PeerId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.to_base58btc() {
Ok(val) => val.fmt(f),
Err(_) => Err(fmt::Error),
}
}
}
impl hash::Hash for PeerId {
fn hash<H>(&self, state: &mut H)
where
H: hash::Hasher,
{
hash::Hash::hash(&self.mh.encode().unwrap(), state)
}
}
impl PartialEq<PeerId> for PeerId {
fn eq(&self, other: &PeerId) -> bool {
self.mh == other.mh
}
}
impl From<Multihash> for PeerId {
fn from(mh: Multihash) -> Self {
PeerId { mh }
}
}
impl From<PeerId> for Multihash {
fn from(peer_id: PeerId) -> Self {
peer_id.mh
}
}
impl PeerId {
pub fn from_public_key(key: PublicKey) -> Result<PeerId> {
let enc_buf = key.into_protobuf_encoding()?;
let codec: Multicodec = match enc_buf.len() <= MAX_INLINE_KEY_LENGTH {
true => multicodec::IDENTITY.into(),
false => multicodec::SHA2_256.into(),
};
let mh = Multihash::new(codec, &enc_buf)?;
Ok(PeerId { mh })
}
pub fn generate() -> Result<PeerId> {
let (bytes, codec) = match rand::thread_rng().gen::<bool>() {
true => {
let codec: Multicodec = multicodec::IDENTITY.into();
let bytes = rand::thread_rng().gen::<[u8; 32]>().to_vec();
(bytes, codec)
}
false => {
let codec: Multicodec = multicodec::SHA2_256.into();
let bytes = {
let mut data = vec![];
data.extend(&rand::thread_rng().gen::<[u8; 32]>());
data.extend(&rand::thread_rng().gen::<[u8; 32]>());
data
};
(bytes, codec)
}
};
let mh = Multihash::new(codec, &bytes)?;
Ok(PeerId { mh })
}
pub fn from_text(text: &str) -> Result<PeerId> {
let mut chars = text.chars();
let peer_id = match (chars.next(), chars.next()) {
(Some('Q'), Some('m')) | (Some('1'), Some(_)) => {
let bytes = {
let res = bs58::decode(text.as_bytes()).into_vec();
err_at!(BadInput, res)?
};
let (mh, _) = Multihash::decode(&bytes)?;
PeerId { mh }
}
_ => {
let bytes = {
let mb = Multibase::decode(text)?;
match mb.to_bytes() {
Some(bytes) => bytes,
None => err_at!(BadInput, msg: format!("{}", text))?,
}
};
let (codec, bytes) = Multicodec::decode(&bytes)?;
match codec.to_code() {
multicodec::CID_V1 => (),
_ => err_at!(BadInput, msg: format!("CID {}", codec))?,
}
let (codec, bytes) = Multicodec::decode(bytes)?;
match codec.to_code() {
multicodec::LIBP2P_KEY => (),
_ => err_at!(BadInput, msg: format!("codec {}", codec))?,
}
let (mh, _) = Multihash::decode(bytes)?;
PeerId { mh }
}
};
Ok(peer_id)
}
pub fn to_base58btc(&self) -> Result<String> {
Ok(bs58::encode(self.mh.encode()?).into_string())
}
pub fn to_base_text(&self, base: Base) -> Result<String> {
let mut data = {
let codec = Multicodec::from_code(multicodec::CID_V1)?;
codec.encode()?
};
{
let codec = Multicodec::from_code(multicodec::LIBP2P_KEY)?;
data.extend(codec.encode()?);
};
data.extend(self.mh.encode()?);
Ok(Multibase::from_base(base.clone(), &data)?.encode()?)
}
pub fn encode(&self) -> Result<Vec<u8>> {
self.mh.encode()
}
pub fn decode(buf: &[u8]) -> Result<(PeerId, &[u8])> {
let (mh, rem) = Multihash::decode(buf)?;
Ok((PeerId { mh }, rem))
}
pub fn is_public_key(&self, public_key: &PublicKey) -> Option<bool> {
let other = PeerId::from_public_key(public_key.clone()).ok()?;
Some(self.mh == other.mh)
}
pub fn to_short_string(&self) -> String {
use std::iter::FromIterator;
let s = self.to_string();
let chars: Vec<char> = s.chars().collect();
if chars.len() <= 10 {
String::from_iter(chars.into_iter())
} else {
let mut short = chars[..2].to_vec();
short.push('*');
short.extend_from_slice(&chars[(chars.len() - 6)..]);
String::from_iter(short.into_iter())
}
}
pub fn to_public_key(&self) -> Result<Option<PublicKey>> {
let (codec, digest) = self.mh.clone().unwrap()?;
let public_key = match codec.to_code() {
multicodec::IDENTITY => {
let public_key = PublicKey::from_protobuf_encoding(&digest)?;
Some(public_key)
}
_ => None,
};
Ok(public_key)
}
}
#[cfg(test)]
#[path = "peer_id_test.rs"]
mod peer_id_test;