From b81435cb5d6c3978fd64a9e14d8b856f5f493470 Mon Sep 17 00:00:00 2001 From: Dmitry Murzin Date: Tue, 10 Sep 2024 16:54:38 +0300 Subject: [PATCH] Review fixes Signed-off-by: Dmitry Murzin --- crates/iroha_crypto/src/lib.rs | 145 +++++++++++++-------------------- 1 file changed, 57 insertions(+), 88 deletions(-) diff --git a/crates/iroha_crypto/src/lib.rs b/crates/iroha_crypto/src/lib.rs index 87f1787491b..6c15aa8ad2e 100755 --- a/crates/iroha_crypto/src/lib.rs +++ b/crates/iroha_crypto/src/lib.rs @@ -312,10 +312,6 @@ impl<'de> Deserialize<'de> for KeyPair { } #[derive(Clone, PartialEq, Eq)] -#[cfg_attr( - not(feature = "ffi_import"), - derive(DeserializeFromStr, SerializeDisplay) -)] #[allow(missing_docs, variant_size_differences)] enum PublicKeyInner { Ed25519(ed25519::PublicKey), @@ -324,45 +320,6 @@ enum PublicKeyInner { BlsSmall(bls::BlsSmallPublicKey), } -#[cfg(not(feature = "ffi_import"))] -impl fmt::Debug for PublicKeyInner { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_tuple(self.algorithm().as_static_str()) - .field(&self.normalize()) - .finish() - } -} - -#[cfg(not(feature = "ffi_import"))] -impl fmt::Display for PublicKeyInner { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.write_str(&self.normalize()) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl FromStr for PublicKeyInner { - type Err = ParseError; - - fn from_str(key: &str) -> Result { - let bytes = hex_decode(key)?; - - let (algorithm, payload) = multihash::decode_public_key(&bytes)?; - PublicKeyInner::from_bytes(algorithm, &payload) - } -} - -#[cfg(not(feature = "ffi_import"))] -impl PublicKeyInner { - fn normalize(&self) -> String { - let (algorithm, payload) = self.to_raw(); - let bytes = multihash::encode_public_key(algorithm, &payload) - .expect("Failed to convert multihash to bytes."); - - multihash::multihash_to_hex_string(&bytes) - } -} - impl PublicKeyInner { fn from_bytes(algorithm: Algorithm, payload: &[u8]) -> Result { match algorithm { @@ -380,6 +337,7 @@ impl PublicKeyInner { } } + #[cfg(not(target_family = "wasm"))] fn to_raw(&self) -> (Algorithm, Vec) { (self.algorithm(), self.payload()) } @@ -413,9 +371,7 @@ impl PublicKeyInner { #[cfg(target_family = "wasm")] mod lazy { use alloc::{boxed::Box, vec::Vec}; - use core::{borrow::Borrow, cell::OnceCell, fmt}; - - use serde::{Deserialize, Deserializer, Serialize, Serializer}; + use core::{borrow::Borrow, cell::OnceCell}; use crate::{Algorithm, PublicKeyInner}; @@ -466,37 +422,6 @@ mod lazy { } } - impl fmt::Display for PublicKeyLazy { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Display::fmt(self.get_inner(), f) - } - } - - impl fmt::Debug for PublicKeyLazy { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - fmt::Debug::fmt(self.get_inner(), f) - } - } - - impl<'de> Deserialize<'de> for PublicKeyLazy { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - let inner = PublicKeyInner::deserialize(deserializer)?; - Ok(PublicKeyLazy::new(inner)) - } - } - - impl Serialize for PublicKeyLazy { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - self.get_inner().serialize(serializer) - } - } - impl PartialEq for PublicKeyLazy { fn eq(&self, other: &Self) -> bool { self.algorithm == other.algorithm && self.payload == other.payload @@ -530,9 +455,8 @@ ffi::ffi_item! { /// "ed01201509A611AD6D97B01D871E58ED00C8FD7C3917B6CA61A8C2833A19E000AAC2E4" /// ); /// ``` - #[derive(Debug, Clone, PartialEq, Eq, TypeId)] - #[cfg_attr(not(feature="ffi_import"), derive(Deserialize, Serialize, derive_more::Display))] - #[cfg_attr(not(feature="ffi_import"), display(fmt = "{_0}"))] + #[derive(Clone, PartialEq, Eq, TypeId)] + #[cfg_attr(not(feature="ffi_import"), derive(DeserializeFromStr, SerializeDisplay))] #[cfg_attr(all(feature = "ffi_export", not(feature = "ffi_import")), ffi_type(opaque))] #[allow(missing_docs)] pub struct PublicKey(PublicKeyInnerType); @@ -589,6 +513,17 @@ impl PublicKey { } } +#[cfg(not(feature = "ffi_import"))] +impl PublicKey { + fn normalize(&self) -> String { + let (algorithm, payload) = self.to_bytes(); + let bytes = multihash::encode_public_key(algorithm, &payload) + .expect("Failed to convert multihash to bytes."); + + multihash::multihash_to_hex_string(&bytes) + } +} + #[cfg(not(feature = "ffi_import"))] impl core::hash::Hash for PublicKey { fn hash(&self, state: &mut H) { @@ -608,6 +543,48 @@ impl Ord for PublicKey { } } +#[cfg(not(feature = "ffi_import"))] +impl fmt::Debug for PublicKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + // This could be simplified using `f.field_with` when `debug_closure_helpers` feature become stable + struct Helper { + algorithm: Algorithm, + normalized: String, + } + impl fmt::Debug for Helper { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple(self.algorithm.as_static_str()) + .field(&self.normalized) + .finish() + } + } + + let helper = Helper { + algorithm: self.algorithm(), + normalized: self.normalize(), + }; + f.debug_tuple("PublicKey").field(&helper).finish() + } +} + +#[cfg(not(feature = "ffi_import"))] +impl fmt::Display for PublicKey { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.normalize()) + } +} + +#[cfg(not(feature = "ffi_import"))] +impl FromStr for PublicKey { + type Err = ParseError; + + fn from_str(key: &str) -> Result { + let bytes = hex_decode(key)?; + let (algorithm, payload) = multihash::decode_public_key(&bytes)?; + Self::from_bytes(algorithm, &payload) + } +} + #[cfg(not(feature = "ffi_import"))] impl Encode for PublicKey { fn size_hint(&self) -> usize { @@ -665,14 +642,6 @@ impl IntoSchema for PublicKey { } } -impl FromStr for PublicKey { - type Err = ParseError; - - fn from_str(key: &str) -> Result { - Ok(Self::new(key.parse()?)) - } -} - // TODO: Enable in ffi_import #[cfg(not(feature = "ffi_import"))] impl From for PublicKey {