Impl serde ser and deser for bls keypairs

This commit is contained in:
Paul Hauner
2019-03-08 13:15:41 +11:00
parent b98f514d68
commit 3b6431b4b4
6 changed files with 61 additions and 4 deletions

View File

@@ -1,5 +1,8 @@
use super::serde_vistors::HexVisitor;
use super::{PublicKey, SecretKey};
use bls_aggregates::Signature as RawSignature;
use hex::encode as hex_encode;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use ssz::{
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
@@ -83,7 +86,19 @@ impl Serialize for Signature {
where
S: Serializer,
{
serializer.serialize_bytes(&ssz_encode(self))
serializer.serialize_str(&hex_encode(ssz_encode(self)))
}
}
impl<'de> Deserialize<'de> for Signature {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
let bytes = deserializer.deserialize_str(HexVisitor)?;
let (pubkey, _) = <_>::ssz_decode(&bytes[..], 0)
.map_err(|e| serde::de::Error::custom(format!("invalid ssz ({:?})", e)))?;
Ok(pubkey)
}
}