trust_graph/
public_key_hashable.rs

1/*
2 * Copyright 2020 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16use fluence_keypair::PublicKey;
17
18use core::fmt;
19use ref_cast::RefCast;
20use serde::ser::Serializer;
21use std::str::FromStr;
22use std::{
23    fmt::{Display, Formatter},
24    hash::{Hash, Hasher},
25};
26
27/// Wrapper to use PublicKey in HashMap
28#[derive(PartialEq, Eq, Debug, Clone, RefCast)]
29#[repr(transparent)]
30pub struct PublicKeyHashable(PublicKey);
31
32#[allow(clippy::derived_hash_with_manual_eq)]
33impl Hash for PublicKeyHashable {
34    fn hash<H: Hasher>(&self, state: &mut H) {
35        state.write(&self.0.encode());
36        state.finish();
37    }
38
39    fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
40    where
41        Self: Sized,
42    {
43        // TODO check for overflow
44        let mut bytes: Vec<u8> = Vec::with_capacity(data.len() * 32);
45        for d in data {
46            bytes.extend_from_slice(&d.0.encode())
47        }
48        state.write(bytes.as_slice());
49        state.finish();
50    }
51}
52
53impl From<PublicKey> for PublicKeyHashable {
54    fn from(pk: PublicKey) -> Self {
55        Self(pk)
56    }
57}
58
59impl From<PublicKeyHashable> for PublicKey {
60    fn from(pk: PublicKeyHashable) -> PublicKey {
61        pk.0
62    }
63}
64
65impl AsRef<PublicKey> for PublicKeyHashable {
66    fn as_ref(&self) -> &PublicKey {
67        &self.0
68    }
69}
70
71impl AsRef<PublicKeyHashable> for PublicKey {
72    fn as_ref(&self) -> &PublicKeyHashable {
73        PublicKeyHashable::ref_cast(self)
74    }
75}
76
77impl Display for PublicKeyHashable {
78    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
79        write!(f, "{}", bs58::encode(self.0.encode()).into_string())
80    }
81}
82
83impl FromStr for PublicKeyHashable {
84    type Err = fluence_keypair::error::DecodingError;
85
86    fn from_str(s: &str) -> Result<Self, Self::Err> {
87        let pk = PublicKey::from_base58(s)?;
88        Ok(PublicKeyHashable::from(pk))
89    }
90}
91
92impl serde::Serialize for PublicKeyHashable {
93    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
94    where
95        S: Serializer,
96    {
97        serializer.serialize_bytes(&self.0.encode())
98    }
99}
100
101impl<'de> serde::Deserialize<'de> for PublicKeyHashable {
102    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
103    where
104        D: serde::Deserializer<'de>,
105    {
106        use serde::de::{Error, Visitor};
107
108        struct PKVisitor;
109
110        impl<'de> Visitor<'de> for PKVisitor {
111            type Value = PublicKeyHashable;
112
113            fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
114                formatter.write_str("byte array or base58 string")
115            }
116
117            fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
118            where
119                E: Error,
120            {
121                bs58::decode(s)
122                    .into_vec()
123                    .map_err(|err| Error::custom(format!("Invalid string '{s}': {err}")))
124                    .and_then(|v| self.visit_bytes(v.as_slice()))
125                    .map_err(|err: E| {
126                        Error::custom(format!("Parsed string '{s}' as base58, but {err}"))
127                    })
128            }
129
130            fn visit_bytes<E>(self, b: &[u8]) -> Result<Self::Value, E>
131            where
132                E: Error,
133            {
134                let pk = PublicKey::decode(b)
135                    .map_err(|err| Error::custom(format!("Invalid bytes {b:?}: {err}")))?;
136                Ok(PublicKeyHashable::from(pk))
137            }
138        }
139
140        deserializer.deserialize_str(PKVisitor)
141    }
142}