From a86618faf3da60c65fe2180478540c6f2eabb4a1 Mon Sep 17 00:00:00 2001 From: Tyera Eulberg Date: Tue, 31 Jul 2018 11:26:26 -0600 Subject: [PATCH] Add PublicKey wrapper Add custom formatting for PublicKey display and debug --- src/lib.rs | 1 + src/signature.rs | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0ec9cf6301..f81ce846c4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,6 +56,7 @@ pub mod wallet; pub mod window_stage; pub mod write_stage; extern crate bincode; +extern crate bs58; extern crate byteorder; extern crate chrono; extern crate generic_array; diff --git a/src/signature.rs b/src/signature.rs index 431f0d9540..45ec93c52e 100644 --- a/src/signature.rs +++ b/src/signature.rs @@ -1,5 +1,6 @@ //! The `signature` module provides functionality for public, and private keys. +use bs58; use generic_array::typenum::{U32, U64}; use generic_array::GenericArray; use rand::{ChaChaRng, Rng, SeedableRng}; @@ -11,11 +12,26 @@ use ring::{rand, signature}; use serde_json; use std::cell::RefCell; use std::error; +use std::fmt; use std::fs::File; use untrusted::Input; pub type KeyPair = Ed25519KeyPair; -pub type PublicKey = GenericArray; +#[derive(Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub struct PublicKey(pub GenericArray); + +impl fmt::Debug for PublicKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", bs58::encode(self.0).into_string()) + } +} + +impl fmt::Display for PublicKey { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", bs58::encode(self.0).into_string()) + } +} + pub type Signature = GenericArray; pub trait KeyPairUtil { @@ -33,7 +49,7 @@ impl KeyPairUtil for Ed25519KeyPair { /// Return the public key for the given keypair fn pubkey(&self) -> PublicKey { - GenericArray::clone_from_slice(self.public_key_bytes()) + PublicKey(GenericArray::clone_from_slice(self.public_key_bytes())) } }