Use hex encoding for Debug fmt of PublicKey

This commit is contained in:
Paul Hauner 2019-05-24 14:07:26 +10:00
parent a92c209787
commit f9d48dee68
No known key found for this signature in database
GPG Key ID: 5E2CFF9B75FA63DF

View File

@ -4,7 +4,7 @@ use cached_tree_hash::cached_tree_hash_ssz_encoding_as_vector;
use serde::de::{Deserialize, Deserializer}; use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use serde_hex::{encode as hex_encode, HexVisitor}; use serde_hex::{encode as hex_encode, HexVisitor};
use ssz::{ssz_encode, Decode, DecodeError}; use ssz::{Decode, DecodeError, Encode};
use std::default; use std::default;
use std::fmt; use std::fmt;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
@ -14,7 +14,7 @@ use tree_hash::tree_hash_ssz_encoding_as_vector;
/// ///
/// This struct is a wrapper upon a base type and provides helper functions (e.g., SSZ /// This struct is a wrapper upon a base type and provides helper functions (e.g., SSZ
/// serialization). /// serialization).
#[derive(Debug, Clone, Eq)] #[derive(Clone, Eq)]
pub struct PublicKey(RawPublicKey); pub struct PublicKey(RawPublicKey);
impl PublicKey { impl PublicKey {
@ -60,9 +60,14 @@ impl PublicKey {
/// ///
/// Useful for providing a short identifier to the user. /// Useful for providing a short identifier to the user.
pub fn concatenated_hex_id(&self) -> String { pub fn concatenated_hex_id(&self) -> String {
let bytes = ssz_encode(self); self.as_hex_string()[0..6].to_string()
let end_bytes = &bytes[bytes.len().saturating_sub(6)..bytes.len()]; }
hex_encode(end_bytes)
/// Returns the point as a hex string of the SSZ encoding.
///
/// Note: the string is prefixed with `0x`.
pub fn as_hex_string(&self) -> String {
hex_encode(self.as_ssz_bytes())
} }
} }
@ -72,6 +77,12 @@ impl fmt::Display for PublicKey {
} }
} }
impl fmt::Debug for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.as_hex_string())
}
}
impl default::Default for PublicKey { impl default::Default for PublicKey {
fn default() -> Self { fn default() -> Self {
let secret_key = SecretKey::random(); let secret_key = SecretKey::random();
@ -107,7 +118,7 @@ cached_tree_hash_ssz_encoding_as_vector!(PublicKey, 48);
impl PartialEq for PublicKey { impl PartialEq for PublicKey {
fn eq(&self, other: &PublicKey) -> bool { fn eq(&self, other: &PublicKey) -> bool {
ssz_encode(self) == ssz_encode(other) self.as_ssz_bytes() == other.as_ssz_bytes()
} }
} }