Impl serde::Serialize for all types

This commit is contained in:
Paul Hauner
2019-01-26 08:25:31 +11:00
parent dbd5e850fe
commit 90ae2298ab
31 changed files with 101 additions and 27 deletions

View File

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

View File

@@ -1,6 +1,9 @@
use super::{AggregatePublicKey, Signature};
use bls_aggregates::AggregateSignature as RawAggregateSignature;
use ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use serde::ser::{Serialize, Serializer};
use ssz::{
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
};
/// A BLS aggregate signature.
///
@@ -44,6 +47,15 @@ impl Decodable for AggregateSignature {
}
}
impl Serialize for AggregateSignature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(&ssz_encode(self))
}
}
impl TreeHash for AggregateSignature {
fn hash_tree_root(&self) -> Vec<u8> {
hash(&self.0.as_bytes())

View File

@@ -1,6 +1,7 @@
use super::SecretKey;
use bls_aggregates::PublicKey as RawPublicKey;
use hex::encode as hex_encode;
use serde::ser::{Serialize, Serializer};
use ssz::{
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
};
@@ -55,6 +56,15 @@ impl Decodable for PublicKey {
}
}
impl Serialize for PublicKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(&ssz_encode(self))
}
}
impl TreeHash for PublicKey {
fn hash_tree_root(&self) -> Vec<u8> {
hash(&self.0.as_bytes())

View File

@@ -1,6 +1,9 @@
use super::{PublicKey, SecretKey};
use bls_aggregates::Signature as RawSignature;
use ssz::{decode_ssz_list, hash, Decodable, DecodeError, Encodable, SszStream, TreeHash};
use serde::ser::{Serialize, Serializer};
use ssz::{
decode_ssz_list, hash, ssz_encode, Decodable, DecodeError, Encodable, SszStream, TreeHash,
};
/// A single BLS signature.
///
@@ -63,6 +66,15 @@ impl TreeHash for Signature {
}
}
impl Serialize for Signature {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(&ssz_encode(self))
}
}
#[cfg(test)]
mod tests {
use super::super::Keypair;

View File

@@ -7,3 +7,5 @@ edition = "2018"
[dependencies]
ssz = { path = "../ssz" }
bit-vec = "0.5.0"
serde = "1.0"
serde_derive = "1.0"

View File

@@ -3,6 +3,7 @@ extern crate ssz;
use bit_vec::BitVec;
use serde::ser::{Serialize, Serializer};
use std::cmp;
use std::default;
@@ -149,6 +150,15 @@ impl ssz::Decodable for BooleanBitfield {
}
}
impl Serialize for BooleanBitfield {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(&ssz::ssz_encode(self))
}
}
impl ssz::TreeHash for BooleanBitfield {
fn hash_tree_root(&self) -> Vec<u8> {
self.to_bytes().hash_tree_root()