From 8d1c1c0ae95ab005d20cb96d026c80b68f95cf14 Mon Sep 17 00:00:00 2001 From: librelois <elois@ifee.fr> Date: Sat, 7 Apr 2018 00:12:18 +0200 Subject: [PATCH] [fix] #55 impl Serialize for WOT Documents & pubkey --- crypto/keys/ed25519.rs | 12 ++++++++++++ documents/blockchain/v10/documents/certification.rs | 12 ++++++++++++ documents/blockchain/v10/documents/identity.rs | 12 ++++++++++++ documents/blockchain/v10/documents/membership.rs | 12 ++++++++++++ documents/blockchain/v10/documents/revocation.rs | 12 ++++++++++++ documents/lib.rs | 2 +- 6 files changed, 61 insertions(+), 1 deletion(-) diff --git a/crypto/keys/ed25519.rs b/crypto/keys/ed25519.rs index 2ed60366..f239e51b 100644 --- a/crypto/keys/ed25519.rs +++ b/crypto/keys/ed25519.rs @@ -19,6 +19,8 @@ //! //! [`KeyPairGenerator`]: struct.KeyPairGenerator.html +extern crate serde; + use std::collections::hash_map::DefaultHasher; use std::fmt::Display; use std::fmt::Error; @@ -30,6 +32,7 @@ use base58::{FromBase58, FromBase58Error, ToBase58}; use base64; use base64::DecodeError; use crypto; +use self::serde::ser::{Serialize, Serializer}; use super::{BaseConvertionError, PrivateKey as PrivateKeyMethods, PublicKey as PublicKeyMethods}; @@ -123,6 +126,15 @@ impl Debug for PublicKey { } } +impl Serialize for PublicKey { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(&format!("{}", self)) + } +} + impl super::PublicKey for PublicKey { type Signature = Signature; diff --git a/documents/blockchain/v10/documents/certification.rs b/documents/blockchain/v10/documents/certification.rs index 6c0e3c0f..cc841f9d 100644 --- a/documents/blockchain/v10/documents/certification.rs +++ b/documents/blockchain/v10/documents/certification.rs @@ -15,8 +15,11 @@ //! Wrappers around Certification documents. +extern crate serde; + use duniter_crypto::keys::{PublicKey, Signature, ed25519}; use regex::Regex; +use self::serde::ser::{Serialize, Serializer}; use Blockstamp; use blockchain::{BlockchainProtocol, Document, DocumentBuilder, IntoSpecializedDocument}; @@ -120,6 +123,15 @@ impl TextDocument for CertificationDocument { } } +impl Serialize for CertificationDocument { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(&self.generate_compact_text()) + } +} + impl IntoSpecializedDocument<BlockchainProtocol> for CertificationDocument { fn into_specialized(self) -> BlockchainProtocol { BlockchainProtocol::V10(Box::new(V10Document::Certification(Box::new(self)))) diff --git a/documents/blockchain/v10/documents/identity.rs b/documents/blockchain/v10/documents/identity.rs index cf40450c..ed9de160 100644 --- a/documents/blockchain/v10/documents/identity.rs +++ b/documents/blockchain/v10/documents/identity.rs @@ -15,8 +15,11 @@ //! Wrappers around Identity documents. +extern crate serde; + use duniter_crypto::keys::{PublicKey, ed25519}; use regex::Regex; +use self::serde::ser::{Serialize, Serializer}; use Blockstamp; use blockchain::{BlockchainProtocol, Document, DocumentBuilder, IntoSpecializedDocument}; @@ -100,6 +103,15 @@ impl TextDocument for IdentityDocument { } } +impl Serialize for IdentityDocument { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(&self.generate_compact_text()) + } +} + impl IntoSpecializedDocument<BlockchainProtocol> for IdentityDocument { fn into_specialized(self) -> BlockchainProtocol { BlockchainProtocol::V10(Box::new(V10Document::Identity(self))) diff --git a/documents/blockchain/v10/documents/membership.rs b/documents/blockchain/v10/documents/membership.rs index b8e5652b..061dc808 100644 --- a/documents/blockchain/v10/documents/membership.rs +++ b/documents/blockchain/v10/documents/membership.rs @@ -15,8 +15,11 @@ //! Wrappers around Membership documents. +extern crate serde; + use duniter_crypto::keys::{PublicKey, ed25519}; use regex::Regex; +use self::serde::ser::{Serialize, Serializer}; use Blockstamp; use blockchain::{BlockchainProtocol, Document, DocumentBuilder, IntoSpecializedDocument}; @@ -122,6 +125,15 @@ impl TextDocument for MembershipDocument { } } +impl Serialize for MembershipDocument { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(&self.generate_compact_text()) + } +} + impl IntoSpecializedDocument<BlockchainProtocol> for MembershipDocument { fn into_specialized(self) -> BlockchainProtocol { BlockchainProtocol::V10(Box::new(V10Document::Membership(self))) diff --git a/documents/blockchain/v10/documents/revocation.rs b/documents/blockchain/v10/documents/revocation.rs index 43b58913..4ba5f777 100644 --- a/documents/blockchain/v10/documents/revocation.rs +++ b/documents/blockchain/v10/documents/revocation.rs @@ -15,8 +15,11 @@ //! Wrappers around Revocation documents. +extern crate serde; + use duniter_crypto::keys::{PublicKey, Signature, ed25519}; use regex::Regex; +use self::serde::ser::{Serialize, Serializer}; use Blockstamp; use blockchain::{BlockchainProtocol, Document, DocumentBuilder, IntoSpecializedDocument}; @@ -102,6 +105,15 @@ impl TextDocument for RevocationDocument { } } +impl Serialize for RevocationDocument { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(&self.generate_compact_text()) + } +} + impl IntoSpecializedDocument<BlockchainProtocol> for RevocationDocument { fn into_specialized(self) -> BlockchainProtocol { BlockchainProtocol::V10(Box::new(V10Document::Revocation(Box::new(self)))) diff --git a/documents/lib.rs b/documents/lib.rs index 702879a4..f89b5acc 100644 --- a/documents/lib.rs +++ b/documents/lib.rs @@ -118,7 +118,7 @@ impl Hash { /// Wrapper of a block hash. #[derive(Copy, Clone, PartialEq, Eq, Hash)] -pub struct BlockHash(Hash); +pub struct BlockHash(pub Hash); impl Display for BlockHash { fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { -- GitLab