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
No known key found for this signature in database
GPG Key ID: D362883A9218FCC6
6 changed files with 61 additions and 4 deletions

View File

@ -9,4 +9,5 @@ bls-aggregates = { git = "https://github.com/sigp/signature-schemes", tag = "0.5
hashing = { path = "../hashing" } hashing = { path = "../hashing" }
hex = "0.3" hex = "0.3"
serde = "1.0" serde = "1.0"
serde_derive = "1.0"
ssz = { path = "../ssz" } ssz = { path = "../ssz" }

View File

@ -1,6 +1,7 @@
use super::{PublicKey, SecretKey}; use super::{PublicKey, SecretKey};
use serde_derive::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Keypair { pub struct Keypair {
pub sk: SecretKey, pub sk: SecretKey,
pub pk: PublicKey, pub pk: PublicKey,

View File

@ -6,6 +6,7 @@ mod aggregate_signature;
mod keypair; mod keypair;
mod public_key; mod public_key;
mod secret_key; mod secret_key;
mod serde_vistors;
mod signature; mod signature;
pub use crate::aggregate_public_key::AggregatePublicKey; pub use crate::aggregate_public_key::AggregatePublicKey;

View File

@ -1,6 +1,8 @@
use super::serde_vistors::HexVisitor;
use super::SecretKey; use super::SecretKey;
use bls_aggregates::PublicKey as RawPublicKey; use bls_aggregates::PublicKey as RawPublicKey;
use hex::encode as hex_encode; use hex::encode as hex_encode;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use ssz::{ use ssz::{
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash, decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
@ -61,7 +63,19 @@ impl Serialize for PublicKey {
where where
S: Serializer, S: Serializer,
{ {
serializer.serialize_bytes(&ssz_encode(self)) serializer.serialize_str(&hex_encode(ssz_encode(self)))
}
}
impl<'de> Deserialize<'de> for PublicKey {
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)
} }
} }

View File

@ -1,5 +1,9 @@
use super::serde_vistors::HexVisitor;
use bls_aggregates::{DecodeError as BlsDecodeError, SecretKey as RawSecretKey}; use bls_aggregates::{DecodeError as BlsDecodeError, SecretKey as RawSecretKey};
use ssz::{decode_ssz_list, Decodable, DecodeError, Encodable, SszStream, TreeHash}; use hex::encode as hex_encode;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer};
use ssz::{decode_ssz_list, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash};
/// A single BLS signature. /// A single BLS signature.
/// ///
@ -40,6 +44,27 @@ impl Decodable for SecretKey {
} }
} }
impl Serialize for SecretKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&hex_encode(ssz_encode(self)))
}
}
impl<'de> Deserialize<'de> for SecretKey {
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)
}
}
impl TreeHash for SecretKey { impl TreeHash for SecretKey {
fn hash_tree_root_internal(&self) -> Vec<u8> { fn hash_tree_root_internal(&self) -> Vec<u8> {
self.0.as_bytes().clone() self.0.as_bytes().clone()

View File

@ -1,5 +1,8 @@
use super::serde_vistors::HexVisitor;
use super::{PublicKey, SecretKey}; use super::{PublicKey, SecretKey};
use bls_aggregates::Signature as RawSignature; use bls_aggregates::Signature as RawSignature;
use hex::encode as hex_encode;
use serde::de::{Deserialize, Deserializer};
use serde::ser::{Serialize, Serializer}; use serde::ser::{Serialize, Serializer};
use ssz::{ use ssz::{
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash, decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
@ -83,7 +86,19 @@ impl Serialize for Signature {
where where
S: Serializer, 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)
} }
} }