Skip to content

Commit

Permalink
Change serde deser for bls
Browse files Browse the repository at this point in the history
  • Loading branch information
paulhauner committed Sep 5, 2020
1 parent b1b364c commit 925722c
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crypto/bls/src/generic_aggregate_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde_utils::hex::{encode as hex_encode, PrefixedHexVisitor};
use serde_utils::hex::encode as hex_encode;
use ssz::{Decode, Encode};
use std::fmt;
use std::marker::PhantomData;
Expand Down
2 changes: 1 addition & 1 deletion crypto/bls/src/generic_public_key.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::Error;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde_utils::hex::{encode as hex_encode, PrefixedHexVisitor};
use serde_utils::hex::encode as hex_encode;
use ssz::{Decode, Encode};
use std::fmt;
use std::hash::{Hash, Hasher};
Expand Down
2 changes: 1 addition & 1 deletion crypto/bls/src/generic_public_key_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde_utils::hex::{encode as hex_encode, PrefixedHexVisitor};
use serde_utils::hex::encode as hex_encode;
use ssz::{Decode, Encode};
use std::convert::TryInto;
use std::fmt;
Expand Down
2 changes: 1 addition & 1 deletion crypto/bls/src/generic_signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
};
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde_utils::hex::{encode as hex_encode, PrefixedHexVisitor};
use serde_utils::hex::encode as hex_encode;
use ssz::{Decode, Encode};
use std::fmt;
use std::marker::PhantomData;
Expand Down
2 changes: 1 addition & 1 deletion crypto/bls/src/generic_signature_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
};
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use serde_utils::hex::{encode as hex_encode, PrefixedHexVisitor};
use serde_utils::hex::encode as hex_encode;
use ssz::{Decode, Encode};
use std::convert::TryInto;
use std::fmt;
Expand Down
22 changes: 19 additions & 3 deletions crypto/bls/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,25 @@ macro_rules! impl_serde_deserialize {
where
D: Deserializer<'de>,
{
let bytes = deserializer.deserialize_str(PrefixedHexVisitor)?;
Self::deserialize(&bytes[..])
.map_err(|e| serde::de::Error::custom(format!("invalid pubkey ({:?})", e)))
pub struct StringVisitor;

impl<'de> serde::de::Visitor<'de> for StringVisitor {
type Value = String;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a hex string with 0x prefix")
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(value.to_string())
}
}

let string = deserializer.deserialize_str(StringVisitor)?;
<Self as std::str::FromStr>::from_str(&string).map_err(serde::de::Error::custom)
}
};
}
Expand Down

0 comments on commit 925722c

Please sign in to comment.