diff --git a/apps/src/lib/config/genesis.rs b/apps/src/lib/config/genesis.rs index 1e2d75853ce..5a25d759b9b 100644 --- a/apps/src/lib/config/genesis.rs +++ b/apps/src/lib/config/genesis.rs @@ -77,29 +77,11 @@ pub mod genesis_config { #[derive(Error, Debug)] pub enum HexKeyError { #[error("Invalid hex string: {0:?}")] - InvalidHexString(data_encoding::DecodeError), + InvalidHexString(#[from] data_encoding::DecodeError), #[error("Invalid sha256 checksum: {0}")] - InvalidSha256(TryFromSliceError), + InvalidSha256(#[from] TryFromSliceError), #[error("Invalid public key: {0}")] - InvalidPublicKey(ParsePublicKeyError), - } - - impl From for HexKeyError { - fn from(err: data_encoding::DecodeError) -> Self { - Self::InvalidHexString(err) - } - } - - impl From for HexKeyError { - fn from(err: ParsePublicKeyError) -> Self { - Self::InvalidPublicKey(err) - } - } - - impl From for HexKeyError { - fn from(err: TryFromSliceError) -> Self { - Self::InvalidSha256(err) - } + InvalidPublicKey(#[from] common::DecodeError), } #[derive(Clone, Debug, Deserialize, Serialize)] diff --git a/core/src/types/key/common.rs b/core/src/types/key/common.rs index e928579367b..07d3f1831a6 100644 --- a/core/src/types/key/common.rs +++ b/core/src/types/key/common.rs @@ -14,6 +14,8 @@ use super::{ ParseSignatureError, RefTo, SchemeType, SigScheme as SigSchemeTrait, VerifySigError, }; +use crate::impl_display_and_from_str_via_format; +use crate::types::string_encoding; /// Public key #[derive( @@ -66,24 +68,23 @@ impl super::PublicKey for PublicKey { } } -impl Display for PublicKey { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{}", HEXLOWER.encode(&self.try_to_vec().unwrap())) - } -} +/// String decoding error +pub type DecodeError = string_encoding::DecodeError; -impl FromStr for PublicKey { - type Err = ParsePublicKeyError; +impl string_encoding::Format for PublicKey { + const HRP: &'static str = string_encoding::COMMON_PK_HRP; - fn from_str(str: &str) -> Result { - let vec = HEXLOWER - .decode(str.as_ref()) - .map_err(ParsePublicKeyError::InvalidHex)?; - Self::try_from_slice(vec.as_slice()) - .map_err(ParsePublicKeyError::InvalidEncoding) + fn to_bytes(&self) -> Vec { + BorshSerialize::try_to_vec(self).unwrap() + } + + fn decode_bytes(bytes: &[u8]) -> Result { + BorshDeserialize::try_from_slice(bytes) } } +impl_display_and_from_str_via_format!(PublicKey); + /// Secret key #[derive(Debug, Clone, BorshSerialize, BorshDeserialize, BorshSchema)] #[allow(clippy::large_enum_variant)] diff --git a/core/src/types/key/dkg_session_keys.rs b/core/src/types/key/dkg_session_keys.rs index f2cafb639ca..15f299cf42b 100644 --- a/core/src/types/key/dkg_session_keys.rs +++ b/core/src/types/key/dkg_session_keys.rs @@ -1,18 +1,16 @@ //! Utilities around the DKG session keys use std::cmp::Ordering; -use std::fmt::Display; use std::io::{Error, ErrorKind}; -use std::str::FromStr; use ark_serialize::{CanonicalDeserialize, CanonicalSerialize}; use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; -use data_encoding::HEXLOWER; use serde::{Deserialize, Serialize}; +use crate::impl_display_and_from_str_via_format; use crate::types::address::Address; -use crate::types::key::ParsePublicKeyError; use crate::types::storage::{DbKeySeg, Key, KeySeg}; +use crate::types::string_encoding; use crate::types::transaction::EllipticCurve; /// A keypair used in the DKG protocol @@ -138,27 +136,21 @@ impl BorshSchema for DkgPublicKey { } } -impl Display for DkgPublicKey { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let vec = self - .try_to_vec() - .expect("Encoding public key shouldn't fail"); - write!(f, "{}", HEXLOWER.encode(&vec)) - } -} +impl string_encoding::Format for DkgPublicKey { + const HRP: &'static str = string_encoding::DKG_PK_HRP; -impl FromStr for DkgPublicKey { - type Err = ParsePublicKeyError; + fn to_bytes(&self) -> Vec { + self.try_to_vec() + .expect("Encoding public key shouldn't fail") + } - fn from_str(s: &str) -> Result { - let vec = HEXLOWER - .decode(s.as_ref()) - .map_err(ParsePublicKeyError::InvalidHex)?; - BorshDeserialize::try_from_slice(&vec) - .map_err(ParsePublicKeyError::InvalidEncoding) + fn decode_bytes(bytes: &[u8]) -> Result { + BorshDeserialize::try_from_slice(bytes) } } +impl_display_and_from_str_via_format!(DkgPublicKey); + /// Obtain a storage key for user's public dkg session key. pub fn dkg_pk_key(owner: &Address) -> Key { Key::from(owner.to_db_key()) diff --git a/core/src/types/key/mod.rs b/core/src/types/key/mod.rs index 4e533d411f4..9b37a33ddd0 100644 --- a/core/src/types/key/mod.rs +++ b/core/src/types/key/mod.rs @@ -186,7 +186,6 @@ pub trait PublicKey: + Display + Debug + PartialOrd - + FromStr + Hash + Send + Sync diff --git a/core/src/types/string_encoding.rs b/core/src/types/string_encoding.rs index afa6594e2a3..2f12ccf3730 100644 --- a/core/src/types/string_encoding.rs +++ b/core/src/types/string_encoding.rs @@ -28,6 +28,10 @@ pub const MASP_PAYMENT_ADDRESS_HRP: &str = "patest"; pub const MASP_PINNED_PAYMENT_ADDRESS_HRP: &str = "ppatest"; /// MASP extended spending key human-readable part pub const MASP_EXT_SPENDING_KEY_HRP: &str = "xsktest"; +/// `common::PublicKey` human-readable part +pub const COMMON_PK_HRP: &str = "pktest"; +/// `DkgPublicKey` human-readable part +pub const DKG_PK_HRP: &str = "dpktest"; #[allow(missing_docs)] #[derive(Error, Debug)]