Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitry Murzin <[email protected]>
  • Loading branch information
dima74 committed Sep 11, 2024
1 parent 5daec36 commit b81435c
Showing 1 changed file with 57 additions and 88 deletions.
145 changes: 57 additions & 88 deletions crates/iroha_crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<Self, Self::Err> {
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<Self, ParseError> {
match algorithm {
Expand All @@ -380,6 +337,7 @@ impl PublicKeyInner {
}
}

#[cfg(not(target_family = "wasm"))]
fn to_raw(&self) -> (Algorithm, Vec<u8>) {
(self.algorithm(), self.payload())
}
Expand Down Expand Up @@ -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};

Expand Down Expand Up @@ -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<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let inner = PublicKeyInner::deserialize(deserializer)?;
Ok(PublicKeyLazy::new(inner))
}
}

impl Serialize for PublicKeyLazy {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<H: core::hash::Hasher>(&self, state: &mut H) {
Expand All @@ -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<Self, Self::Err> {
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 {
Expand Down Expand Up @@ -665,14 +642,6 @@ impl IntoSchema for PublicKey {
}
}

impl FromStr for PublicKey {
type Err = ParseError;

fn from_str(key: &str) -> Result<Self, Self::Err> {
Ok(Self::new(key.parse()?))
}
}

// TODO: Enable in ffi_import
#[cfg(not(feature = "ffi_import"))]
impl From<PrivateKey> for PublicKey {
Expand Down

0 comments on commit b81435c

Please sign in to comment.