1use std::{
4 borrow::Borrow,
5 cmp::{Ord, PartialOrd},
6 fmt::{Debug, Display},
7 hash::Hash,
8 str::FromStr,
9};
10
11use curve25519_dalek::edwards::CompressedEdwardsY;
12pub use ed25519_dalek::{Signature, SignatureError};
13use ed25519_dalek::{SigningKey, VerifyingKey};
14use nested_enum_utils::common_fields;
15use rand_core::CryptoRngCore;
16use serde::{Deserialize, Serialize};
17use snafu::{Backtrace, Snafu};
18
19#[derive(Clone, Copy, PartialEq, Eq)]
24#[repr(transparent)]
25pub struct PublicKey(CompressedEdwardsY);
26
27impl Borrow<[u8; 32]> for PublicKey {
28 fn borrow(&self) -> &[u8; 32] {
29 self.as_bytes()
30 }
31}
32
33impl PartialOrd for PublicKey {
34 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
35 Some(self.cmp(other))
36 }
37}
38
39impl Ord for PublicKey {
40 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
41 self.0.as_bytes().cmp(other.0.as_bytes())
42 }
43}
44
45pub type NodeId = PublicKey;
58
59impl Hash for PublicKey {
60 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
61 self.0.hash(state);
62 }
63}
64
65impl Serialize for PublicKey {
66 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
67 where
68 S: serde::Serializer,
69 {
70 if serializer.is_human_readable() {
71 serializer.serialize_str(&self.to_string())
72 } else {
73 self.0.as_bytes().serialize(serializer)
74 }
75 }
76}
77
78impl<'de> Deserialize<'de> for PublicKey {
79 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
80 where
81 D: serde::Deserializer<'de>,
82 {
83 if deserializer.is_human_readable() {
84 let s = String::deserialize(deserializer)?;
85 Self::from_str(&s).map_err(serde::de::Error::custom)
86 } else {
87 let data: [u8; 32] = serde::Deserialize::deserialize(deserializer)?;
88 Self::try_from(data.as_ref()).map_err(serde::de::Error::custom)
89 }
90 }
91}
92
93impl PublicKey {
94 pub fn as_bytes(&self) -> &[u8; 32] {
96 self.0.as_bytes()
97 }
98
99 pub fn public(&self) -> VerifyingKey {
101 VerifyingKey::from_bytes(self.0.as_bytes()).expect("already verified")
102 }
103
104 pub fn from_bytes(bytes: &[u8; 32]) -> Result<Self, SignatureError> {
112 let key = VerifyingKey::from_bytes(bytes)?;
113 let y = CompressedEdwardsY(key.to_bytes());
114 Ok(Self(y))
115 }
116
117 pub fn verify(&self, message: &[u8], signature: &Signature) -> Result<(), SignatureError> {
123 self.public().verify_strict(message, signature)
124 }
125
126 pub fn fmt_short(&self) -> String {
129 data_encoding::HEXLOWER.encode(&self.as_bytes()[..5])
130 }
131
132 pub const LENGTH: usize = ed25519_dalek::PUBLIC_KEY_LENGTH;
134}
135
136impl TryFrom<&[u8]> for PublicKey {
137 type Error = SignatureError;
138
139 #[inline]
140 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
141 let vk = VerifyingKey::try_from(bytes)?;
142 Ok(Self(CompressedEdwardsY(vk.to_bytes())))
143 }
144}
145
146impl TryFrom<&[u8; 32]> for PublicKey {
147 type Error = SignatureError;
148
149 #[inline]
150 fn try_from(bytes: &[u8; 32]) -> Result<Self, Self::Error> {
151 Self::from_bytes(bytes)
152 }
153}
154
155impl AsRef<[u8]> for PublicKey {
156 fn as_ref(&self) -> &[u8] {
157 self.as_bytes()
158 }
159}
160
161impl From<VerifyingKey> for PublicKey {
162 fn from(verifying_key: VerifyingKey) -> Self {
163 let key = verifying_key.to_bytes();
164 PublicKey(CompressedEdwardsY(key))
165 }
166}
167
168impl Debug for PublicKey {
169 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
170 write!(
171 f,
172 "PublicKey({})",
173 data_encoding::HEXLOWER.encode(self.as_bytes())
174 )
175 }
176}
177
178impl Display for PublicKey {
179 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
180 write!(f, "{}", data_encoding::HEXLOWER.encode(self.as_bytes()))
181 }
182}
183
184#[common_fields({
186 backtrace: Option<Backtrace>,
187 #[snafu(implicit)]
188 span_trace: n0_snafu::SpanTrace,
189})]
190#[derive(Snafu, Debug)]
191#[allow(missing_docs)]
192#[snafu(visibility(pub(crate)))]
193pub enum KeyParsingError {
194 #[snafu(transparent)]
196 Decode { source: data_encoding::DecodeError },
197 #[snafu(transparent)]
199 Key {
200 source: ed25519_dalek::SignatureError,
201 },
202 #[snafu(display("invalid length"))]
204 DecodeInvalidLength {},
205}
206
207impl FromStr for PublicKey {
211 type Err = KeyParsingError;
212
213 fn from_str(s: &str) -> Result<Self, Self::Err> {
214 let bytes = decode_base32_hex(s)?;
215
216 Ok(Self::from_bytes(&bytes)?)
217 }
218}
219
220#[derive(Clone)]
222pub struct SecretKey {
223 secret: SigningKey,
224}
225
226impl Debug for SecretKey {
227 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
228 write!(f, "SecretKey(..)")
229 }
230}
231
232impl Display for SecretKey {
233 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
234 write!(
236 f,
237 "{}",
238 data_encoding::HEXLOWER.encode(self.secret.as_bytes())
239 )
240 }
241}
242
243impl FromStr for SecretKey {
244 type Err = KeyParsingError;
245
246 fn from_str(s: &str) -> Result<Self, Self::Err> {
247 let bytes = decode_base32_hex(s)?;
248 Ok(SecretKey::from(bytes))
249 }
250}
251
252impl Serialize for SecretKey {
253 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
254 where
255 S: serde::Serializer,
256 {
257 self.secret.serialize(serializer)
258 }
259}
260
261impl<'de> Deserialize<'de> for SecretKey {
262 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
263 where
264 D: serde::Deserializer<'de>,
265 {
266 let secret = SigningKey::deserialize(deserializer)?;
267 Ok(secret.into())
268 }
269}
270
271impl SecretKey {
272 pub fn public(&self) -> PublicKey {
274 self.secret.verifying_key().into()
275 }
276
277 pub fn generate<R: CryptoRngCore>(mut csprng: R) -> Self {
285 let secret = SigningKey::generate(&mut csprng);
286
287 Self { secret }
288 }
289
290 pub fn sign(&self, msg: &[u8]) -> Signature {
292 use ed25519_dalek::Signer;
293
294 self.secret.sign(msg)
295 }
296
297 pub fn to_bytes(&self) -> [u8; 32] {
300 self.secret.to_bytes()
301 }
302
303 pub fn from_bytes(bytes: &[u8; 32]) -> Self {
305 let secret = SigningKey::from_bytes(bytes);
306 secret.into()
307 }
308
309 pub fn secret(&self) -> &SigningKey {
311 &self.secret
312 }
313}
314
315impl From<SigningKey> for SecretKey {
316 fn from(secret: SigningKey) -> Self {
317 SecretKey { secret }
318 }
319}
320
321impl From<[u8; 32]> for SecretKey {
322 fn from(value: [u8; 32]) -> Self {
323 Self::from_bytes(&value)
324 }
325}
326
327impl TryFrom<&[u8]> for SecretKey {
328 type Error = SignatureError;
329
330 #[inline]
331 fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
332 let secret = SigningKey::try_from(bytes)?;
333 Ok(secret.into())
334 }
335}
336
337fn decode_base32_hex(s: &str) -> Result<[u8; 32], KeyParsingError> {
338 let mut bytes = [0u8; 32];
339
340 let res = if s.len() == PublicKey::LENGTH * 2 {
341 data_encoding::HEXLOWER.decode_mut(s.as_bytes(), &mut bytes)
343 } else {
344 let input = s.to_ascii_uppercase();
345 let input = input.as_bytes();
346 if data_encoding::BASE32_NOPAD.decode_len(input.len())? != bytes.len() {
347 return Err(DecodeInvalidLengthSnafu.build());
348 }
349 data_encoding::BASE32_NOPAD.decode_mut(input, &mut bytes)
350 };
351 match res {
352 Ok(len) => {
353 if len != PublicKey::LENGTH {
354 return Err(DecodeInvalidLengthSnafu.build());
355 }
356 }
357 Err(partial) => return Err(partial.error.into()),
358 }
359 Ok(bytes)
360}
361
362#[cfg(test)]
363mod tests {
364 use data_encoding::HEXLOWER;
365
366 use super::*;
367
368 #[test]
369 fn test_public_key_postcard() {
370 let public_key =
371 PublicKey::from_str("ae58ff8833241ac82d6ff7611046ed67b5072d142c588d0063e942d9a75502b6")
372 .unwrap();
373 let bytes = postcard::to_stdvec(&public_key).unwrap();
374 let expected = HEXLOWER
375 .decode(b"ae58ff8833241ac82d6ff7611046ed67b5072d142c588d0063e942d9a75502b6")
376 .unwrap();
377 assert_eq!(bytes, expected);
378 }
379
380 #[test]
381 fn public_key_postcard() {
382 let key = PublicKey::from_bytes(&[0; 32]).unwrap();
383 let bytes = postcard::to_stdvec(&key).unwrap();
384 let key2: PublicKey = postcard::from_bytes(&bytes).unwrap();
385 assert_eq!(key, key2);
386 }
387
388 #[test]
389 fn public_key_json() {
390 let key = PublicKey::from_bytes(&[0; 32]).unwrap();
391 let bytes = serde_json::to_string(&key).unwrap();
392 let key2: PublicKey = serde_json::from_str(&bytes).unwrap();
393 assert_eq!(key, key2);
394 }
395
396 #[test]
397 fn test_display_from_str() {
398 let key = SecretKey::generate(&mut rand::thread_rng());
399 assert_eq!(
400 SecretKey::from_str(&key.to_string()).unwrap().to_bytes(),
401 key.to_bytes()
402 );
403
404 assert_eq!(
405 PublicKey::from_str(&key.public().to_string()).unwrap(),
406 key.public()
407 );
408 }
409
410 #[test]
411 fn test_regression_parse_node_id_panic() {
412 let not_a_node_id = "foobarbaz";
413 assert!(PublicKey::from_str(not_a_node_id).is_err());
414 }
415}