Skip to content

Commit

Permalink
Impl display for bls
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 5, 2020
1 parent e76d476 commit b1b364c
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 4 deletions.
6 changes: 3 additions & 3 deletions common/eth2/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,13 @@ impl FromStr for ValidatorId {

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with("0x") {
serde_json::from_str(&format!("\"{}\"", s))
PublicKeyBytes::from_str(s)
.map(ValidatorId::PublicKey)
.map_err(|_| format!("{} cannot be parsed as a public key", s))
.map_err(|e| format!("{} cannot be parsed as a public key: {}", s, e))
} else {
u64::from_str(s)
.map(ValidatorId::Index)
.map_err(|_| format!("{} cannot be parsed as a slot", s))
.map_err(|e| format!("{} cannot be parsed as a slot: {}", s, e))
}
}
}
Expand Down
17 changes: 17 additions & 0 deletions crypto/bls/src/generic_aggregate_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,23 @@ where
impl_tree_hash!(SIGNATURE_BYTES_LEN);
}

impl<Pub, AggPub, Sig, AggSig> fmt::Display for GenericAggregateSignature<Pub, AggPub, Sig, AggSig>
where
Sig: TSignature<Pub>,
AggSig: TAggregateSignature<Pub, AggPub, Sig>,
{
impl_display!();
}

impl<Pub, AggPub, Sig, AggSig> std::str::FromStr
for GenericAggregateSignature<Pub, AggPub, Sig, AggSig>
where
Sig: TSignature<Pub>,
AggSig: TAggregateSignature<Pub, AggPub, Sig>,
{
impl_from_str!();
}

impl<Pub, AggPub, Sig, AggSig> Serialize for GenericAggregateSignature<Pub, AggPub, Sig, AggSig>
where
Sig: TSignature<Pub>,
Expand Down
8 changes: 8 additions & 0 deletions crypto/bls/src/generic_public_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@ impl<Pub: TPublicKey> TreeHash for GenericPublicKey<Pub> {
impl_tree_hash!(PUBLIC_KEY_BYTES_LEN);
}

impl<Pub: TPublicKey> fmt::Display for GenericPublicKey<Pub> {
impl_display!();
}

impl<Pub: TPublicKey> std::str::FromStr for GenericPublicKey<Pub> {
impl_from_str!();
}

impl<Pub: TPublicKey> Serialize for GenericPublicKey<Pub> {
impl_serde_serialize!();
}
Expand Down
8 changes: 8 additions & 0 deletions crypto/bls/src/generic_public_key_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ impl<Pub> TreeHash for GenericPublicKeyBytes<Pub> {
impl_tree_hash!(PUBLIC_KEY_BYTES_LEN);
}

impl<Pub> fmt::Display for GenericPublicKeyBytes<Pub> {
impl_display!();
}

impl<Pub> std::str::FromStr for GenericPublicKeyBytes<Pub> {
impl_from_str!();
}

impl<Pub> Serialize for GenericPublicKeyBytes<Pub> {
impl_serde_serialize!();
}
Expand Down
8 changes: 8 additions & 0 deletions crypto/bls/src/generic_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,14 @@ impl<PublicKey, T: TSignature<PublicKey>> TreeHash for GenericSignature<PublicKe
impl_tree_hash!(SIGNATURE_BYTES_LEN);
}

impl<PublicKey, T: TSignature<PublicKey>> fmt::Display for GenericSignature<PublicKey, T> {
impl_display!();
}

impl<PublicKey, T: TSignature<PublicKey>> std::str::FromStr for GenericSignature<PublicKey, T> {
impl_from_str!();
}

impl<PublicKey, T: TSignature<PublicKey>> Serialize for GenericSignature<PublicKey, T> {
impl_serde_serialize!();
}
Expand Down
8 changes: 8 additions & 0 deletions crypto/bls/src/generic_signature_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ impl<Pub, Sig> TreeHash for GenericSignatureBytes<Pub, Sig> {
impl_tree_hash!(SIGNATURE_BYTES_LEN);
}

impl<Pub, Sig> fmt::Display for GenericSignatureBytes<Pub, Sig> {
impl_display!();
}

impl<Pub, Sig> std::str::FromStr for GenericSignatureBytes<Pub, Sig> {
impl_from_str!();
}

impl<Pub, Sig> Serialize for GenericSignatureBytes<Pub, Sig> {
impl_serde_serialize!();
}
Expand Down
31 changes: 30 additions & 1 deletion crypto/bls/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,35 @@ macro_rules! impl_ssz_decode {
};
}

/// Contains the functions required for a `fmt::Display` implementation.
///
/// Does not include the `Impl` section since it gets very complicated when it comes to generics.
macro_rules! impl_display {
() => {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", hex_encode(self.serialize().to_vec()))
}
};
}

/// Contains the functions required for a `fmt::Display` implementation.
///
/// Does not include the `Impl` section since it gets very complicated when it comes to generics.
macro_rules! impl_from_str {
() => {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.starts_with("0x") {
let bytes = hex::decode(&s[2..]).map_err(|e| e.to_string())?;
Self::deserialize(&bytes[..]).map_err(|e| format!("{:?}", e))
} else {
Err("must start with 0x".to_string())
}
}
};
}

/// Contains the functions required for a `serde::Serialize` implementation.
///
/// Does not include the `Impl` section since it gets very complicated when it comes to generics.
Expand All @@ -85,7 +114,7 @@ macro_rules! impl_serde_serialize {
where
S: Serializer,
{
serializer.serialize_str(&hex_encode(self.serialize().to_vec()))
serializer.serialize_str(&self.to_string())
}
};
}
Expand Down

0 comments on commit b1b364c

Please sign in to comment.