2021-01-15 19:37:06 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2020 Fluence Labs Limited
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-01-19 21:55:28 +03:00
|
|
|
use fluence_identity::public_key::PublicKey;
|
2021-01-15 19:37:06 +03:00
|
|
|
|
2021-01-19 17:25:28 +03:00
|
|
|
use core::fmt;
|
2021-01-15 19:37:06 +03:00
|
|
|
use ref_cast::RefCast;
|
|
|
|
use std::{
|
|
|
|
fmt::{Display, Formatter},
|
|
|
|
hash::{Hash, Hasher},
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Wrapper to use PublicKey in HashMap
|
2021-01-18 20:08:05 +03:00
|
|
|
#[derive(PartialEq, Eq, Debug, Clone, RefCast)]
|
2021-01-15 19:37:06 +03:00
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct PublicKeyHashable(PublicKey);
|
|
|
|
|
|
|
|
#[allow(clippy::derive_hash_xor_eq)]
|
|
|
|
impl Hash for PublicKeyHashable {
|
|
|
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
2021-01-18 20:08:05 +03:00
|
|
|
state.write(&self.0.to_bytes());
|
2021-01-15 19:37:06 +03:00
|
|
|
state.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_slice<H: Hasher>(data: &[Self], state: &mut H)
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
// TODO check for overflow
|
|
|
|
let mut bytes: Vec<u8> = Vec::with_capacity(data.len() * 32);
|
|
|
|
for d in data {
|
2021-01-18 20:08:05 +03:00
|
|
|
bytes.extend_from_slice(&d.0.to_bytes())
|
2021-01-15 19:37:06 +03:00
|
|
|
}
|
|
|
|
state.write(bytes.as_slice());
|
|
|
|
state.finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<PublicKey> for PublicKeyHashable {
|
|
|
|
fn from(pk: PublicKey) -> Self {
|
|
|
|
Self(pk)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Into<PublicKey> for PublicKeyHashable {
|
|
|
|
fn into(self) -> PublicKey {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<PublicKey> for PublicKeyHashable {
|
|
|
|
fn as_ref(&self) -> &PublicKey {
|
|
|
|
&self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsRef<PublicKeyHashable> for PublicKey {
|
|
|
|
fn as_ref(&self) -> &PublicKeyHashable {
|
|
|
|
PublicKeyHashable::ref_cast(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for PublicKeyHashable {
|
|
|
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
2021-01-18 20:08:05 +03:00
|
|
|
write!(f, "{}", bs58::encode(self.0.to_bytes()).into_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-19 17:25:28 +03:00
|
|
|
impl<'de> serde::Deserialize<'de> for PublicKeyHashable {
|
2021-01-18 20:08:05 +03:00
|
|
|
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
2021-01-19 17:25:28 +03:00
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
2021-01-18 20:08:05 +03:00
|
|
|
use serde::de::{Error, Visitor};
|
|
|
|
|
|
|
|
struct PKVisitor;
|
|
|
|
|
|
|
|
impl<'de> Visitor<'de> for PKVisitor {
|
|
|
|
type Value = PublicKeyHashable;
|
|
|
|
|
2021-01-18 20:14:40 +03:00
|
|
|
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
2021-01-18 20:08:05 +03:00
|
|
|
formatter.write_str("byte array or base58 string")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
|
2021-01-19 17:25:28 +03:00
|
|
|
where
|
|
|
|
E: Error,
|
2021-01-18 20:08:05 +03:00
|
|
|
{
|
|
|
|
bs58::decode(s)
|
|
|
|
.into_vec()
|
|
|
|
.map_err(|err| Error::custom(format!("Invalid string '{}': {}", s, err)))
|
|
|
|
.and_then(|v| self.visit_bytes(v.as_slice()))
|
2021-01-19 17:25:28 +03:00
|
|
|
.map_err(|err: E| {
|
|
|
|
Error::custom(format!("Parsed string '{}' as base58, but {}", s, err))
|
|
|
|
})
|
2021-01-18 20:08:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_bytes<E>(self, b: &[u8]) -> Result<Self::Value, E>
|
2021-01-19 17:25:28 +03:00
|
|
|
where
|
|
|
|
E: Error,
|
2021-01-18 20:08:05 +03:00
|
|
|
{
|
|
|
|
let pk = PublicKey::from_bytes(b)
|
|
|
|
.map_err(|err| Error::custom(format!("Invalid bytes {:?}: {}", b, err)))?;
|
|
|
|
Ok(PublicKeyHashable::from(pk))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deserializer.deserialize_str(PKVisitor)
|
2021-01-15 19:37:06 +03:00
|
|
|
}
|
|
|
|
}
|