From 1fe38b1b9c8a6fc0b9df22953bcf5ab94aa7119b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Wed, 19 May 2021 15:33:01 +0100 Subject: [PATCH] Implemented display traits for identity and encryption keys (#607) --- common/crypto/src/asymmetric/encryption/mod.rs | 12 ++++++++++++ common/crypto/src/asymmetric/identity/mod.rs | 14 +++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/common/crypto/src/asymmetric/encryption/mod.rs b/common/crypto/src/asymmetric/encryption/mod.rs index 595daa8713..1e4dd1f030 100644 --- a/common/crypto/src/asymmetric/encryption/mod.rs +++ b/common/crypto/src/asymmetric/encryption/mod.rs @@ -106,6 +106,12 @@ impl PemStorableKeyPair for KeyPair { #[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)] pub struct PublicKey(x25519_dalek::PublicKey); +impl Display for PublicKey { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.to_base58_string()) + } +} + impl PublicKey { pub fn to_bytes(self) -> [u8; PUBLIC_KEY_SIZE] { *self.0.as_bytes() @@ -149,6 +155,12 @@ impl PemStorableKey for PublicKey { #[derive(Clone)] pub struct PrivateKey(x25519_dalek::StaticSecret); +impl Display for PrivateKey { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.to_base58_string()) + } +} + impl<'a> From<&'a PrivateKey> for PublicKey { fn from(pk: &'a PrivateKey) -> Self { PublicKey((&pk.0).into()) diff --git a/common/crypto/src/asymmetric/identity/mod.rs b/common/crypto/src/asymmetric/identity/mod.rs index b31ec8fd06..d50b31b5a7 100644 --- a/common/crypto/src/asymmetric/identity/mod.rs +++ b/common/crypto/src/asymmetric/identity/mod.rs @@ -18,7 +18,7 @@ pub use ed25519_dalek::{Verifier, PUBLIC_KEY_LENGTH, SECRET_KEY_LENGTH, SIGNATUR use nymsphinx_types::{DestinationAddressBytes, DESTINATION_ADDRESS_LENGTH}; use pemstore::traits::{PemStorableKey, PemStorableKeyPair}; use rand::{CryptoRng, RngCore}; -use std::fmt::{self, Formatter}; +use std::fmt::{self, Display, Formatter}; #[derive(Debug)] pub enum KeyRecoveryError { @@ -104,6 +104,12 @@ impl PemStorableKeyPair for KeyPair { #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub struct PublicKey(ed25519_dalek::PublicKey); +impl Display for PublicKey { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.to_base58_string()) + } +} + impl PublicKey { pub fn derive_destination_address(&self) -> DestinationAddressBytes { let mut temporary_address = [0u8; DESTINATION_ADDRESS_LENGTH]; @@ -158,6 +164,12 @@ impl PemStorableKey for PublicKey { #[derive(Debug)] pub struct PrivateKey(ed25519_dalek::SecretKey); +impl Display for PrivateKey { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.to_base58_string()) + } +} + impl<'a> From<&'a PrivateKey> for PublicKey { fn from(pk: &'a PrivateKey) -> Self { PublicKey((&pk.0).into())