diff --git a/documents/blockchain/mod.rs b/documents/blockchain/mod.rs index dee4005a95245c05687b01dc9189c7c10ab5a20d..ff070ebabeaab0aa48fb8feec7057bde65684026 100644 --- a/documents/blockchain/mod.rs +++ b/documents/blockchain/mod.rs @@ -19,6 +19,8 @@ use std::fmt::Debug; use duniter_crypto::keys::{PrivateKey, PublicKey}; +use super::Blockstamp; + pub mod v10; /// List of blockchain protocol versions. @@ -48,6 +50,9 @@ pub trait Document: Debug + Clone { /// Get document currency. fn currency(&self) -> &Self::CurrencyType; + /// Get document blockstamp + fn blockstamp(&self) -> Blockstamp; + /// Iterate over document issuers. fn issuers(&self) -> &Vec<Self::PublicKey>; @@ -160,6 +165,10 @@ mod tests { unimplemented!() } + fn blockstamp(&self) -> Blockstamp { + unimplemented!() + } + fn issuers(&self) -> &Vec<ed25519::PublicKey> { &self.issuers } diff --git a/documents/blockchain/v10/documents/block.rs b/documents/blockchain/v10/documents/block.rs index 2ce24373b83eade972dcc01bc382d2810042f703..6b2b9113c6571f9820bbc53f65b22ded48d0bb40 100644 --- a/documents/blockchain/v10/documents/block.rs +++ b/documents/blockchain/v10/documents/block.rs @@ -114,7 +114,7 @@ pub struct BlockDocument { /// Currency parameters (only in genesis block) pub parameters: Option<BlockParameters>, /// Hash of the previous block - pub previous_hash: Option<Hash>, + pub previous_hash: Hash, /// Issuer of the previous block pub previous_issuer: Option<ed25519::PublicKey>, /// Hash of the deterministic content of the block @@ -142,13 +142,6 @@ pub struct BlockDocument { } impl BlockDocument { - /// Return blockstamp - pub fn blockstamp(&self) -> Blockstamp { - Blockstamp { - id: self.number, - hash: self.hash.unwrap(), - } - } /// Compute inner hash pub fn compute_inner_hash(&mut self) { let mut sha256 = Sha256::new(); @@ -269,7 +262,7 @@ Transactions:{transactions} issuers_frame = self.issuers_frame, issuers_frame_var = self.issuers_frame_var, issuers_count = self.issuers_count, - previous_hash = self.previous_hash.unwrap(), + previous_hash = self.previous_hash, previous_issuer = self.previous_issuer.unwrap(), members_count = self.members_count, identities = identities_str, @@ -296,6 +289,13 @@ impl Document for BlockDocument { &self.currency } + fn blockstamp(&self) -> Blockstamp { + Blockstamp { + id: self.number, + hash: self.hash.unwrap(), + } + } + fn issuers(&self) -> &Vec<ed25519::PublicKey> { &self.issuers } @@ -357,7 +357,7 @@ mod tests { signatures: vec![ed25519::Signature::from_base64("FsRxB+NOiL+8zTr2d3B2j2KBItDuCa0KjFMF6hXmdQzfqXAs9g3m7DlGgYLcqzqe6JXjx/Lyzqze1HBR4cS0Aw==").unwrap()], hash: None, parameters: None, - previous_hash: Some(Hash::from_hex("0000001F8AACF6764135F3E5D0D4E8358A3CBE537A4BF71152A00CC442EFD136").expect("fail to parse previous_hash")), + previous_hash: Hash::from_hex("0000001F8AACF6764135F3E5D0D4E8358A3CBE537A4BF71152A00CC442EFD136").expect("fail to parse previous_hash"), previous_issuer: Some(ed25519::PublicKey::from_base58("38MEAZN68Pz1DTvT3tqgxx4yQP6snJCQhPqEFxbDk4aE").unwrap()), inner_hash: None, dividend: None, @@ -492,7 +492,7 @@ a9PHPuSfw7jW8FRQHXFsGi/bnLjbtDnTYvEVgUC9u0WlR7GVofa+Xb+l5iy6NwuEXiwvueAkf08wPVY8 signatures: vec![ed25519::Signature::from_base64("92id58VmkhgVNee4LDqBGSm8u/ooHzAD67JM6fhAE/CV8LCz7XrMF1DvRl+eRpmlaVkp6I+Iy8gmZ1WUM5C8BA==").unwrap()], hash: None, parameters: None, - previous_hash: Some(Hash::from_hex("000001144968D0C3516BE6225E4662F182E28956AF46DD7FB228E3D0F9413FEB").expect("fail to parse previous_hash")), + previous_hash: Hash::from_hex("000001144968D0C3516BE6225E4662F182E28956AF46DD7FB228E3D0F9413FEB").expect("fail to parse previous_hash"), previous_issuer: Some(ed25519::PublicKey::from_base58("D3krfq6J9AmfpKnS3gQVYoy7NzGCc61vokteTS8LJ4YH").unwrap()), inner_hash: None, dividend: None, diff --git a/documents/blockchain/v10/documents/certification.rs b/documents/blockchain/v10/documents/certification.rs index b615ee41150d51cb66cded403dba5c1b5c4bece3..8dd7fe0e10b7ef3f9cace4d48958202d5191ff6b 100644 --- a/documents/blockchain/v10/documents/certification.rs +++ b/documents/blockchain/v10/documents/certification.rs @@ -89,6 +89,10 @@ impl Document for CertificationDocument { &self.currency } + fn blockstamp(&self) -> Blockstamp { + self.blockstamp + } + fn issuers(&self) -> &Vec<ed25519::PublicKey> { &self.issuers } diff --git a/documents/blockchain/v10/documents/identity.rs b/documents/blockchain/v10/documents/identity.rs index 8dccf472179f9c5355cfb17e5854e22d75a6d452..c78e6f053448b00042e4366f9e777c5d10188c9f 100644 --- a/documents/blockchain/v10/documents/identity.rs +++ b/documents/blockchain/v10/documents/identity.rs @@ -74,6 +74,10 @@ impl Document for IdentityDocument { &self.currency } + fn blockstamp(&self) -> Blockstamp { + self.blockstamp + } + fn issuers(&self) -> &Vec<ed25519::PublicKey> { &self.issuers } diff --git a/documents/blockchain/v10/documents/membership.rs b/documents/blockchain/v10/documents/membership.rs index c6f92fd793599def5ec2ae57000942ea4af515dc..666b2ceab4db0da67632af92a4fea258199eaa62 100644 --- a/documents/blockchain/v10/documents/membership.rs +++ b/documents/blockchain/v10/documents/membership.rs @@ -95,6 +95,10 @@ impl Document for MembershipDocument { &self.currency } + fn blockstamp(&self) -> Blockstamp { + self.blockstamp + } + fn issuers(&self) -> &Vec<ed25519::PublicKey> { &self.issuers } diff --git a/documents/blockchain/v10/documents/revocation.rs b/documents/blockchain/v10/documents/revocation.rs index fc2b347d3a906b37fed7a69d88d3c429d76d710d..20595c762fb9196ca80346024d8542b43b5787b5 100644 --- a/documents/blockchain/v10/documents/revocation.rs +++ b/documents/blockchain/v10/documents/revocation.rs @@ -78,6 +78,10 @@ impl Document for RevocationDocument { &self.currency } + fn blockstamp(&self) -> Blockstamp { + unimplemented!() + } + fn issuers(&self) -> &Vec<ed25519::PublicKey> { &self.issuers } diff --git a/documents/blockchain/v10/documents/transaction.rs b/documents/blockchain/v10/documents/transaction.rs index 5e0a117b638a68ff43772a87ebb38540d3995763..421d018f912bd9a089c78813fe31a4be8b58fe41 100644 --- a/documents/blockchain/v10/documents/transaction.rs +++ b/documents/blockchain/v10/documents/transaction.rs @@ -15,12 +15,16 @@ //! Wrappers around Transaction documents. +extern crate serde; + use std::ops::Deref; use duniter_crypto::keys::{ed25519, PublicKey}; use regex::Regex; use regex::RegexBuilder; +use self::serde::ser::{Serialize, Serializer}; + use blockchain::v10::documents::{StandardTextDocumentParser, TextDocument, TextDocumentBuilder, V10Document, V10DocumentParsingError}; use blockchain::{BlockchainProtocol, Document, DocumentBuilder, IntoSpecializedDocument}; @@ -419,6 +423,10 @@ impl Document for TransactionDocument { &self.currency } + fn blockstamp(&self) -> Blockstamp { + self.blockstamp + } + fn issuers(&self) -> &Vec<ed25519::PublicKey> { &self.issuers } @@ -483,6 +491,16 @@ impl TextDocument for TransactionDocument { } } +impl Serialize for TransactionDocument { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + let compact_text = self.generate_compact_text(); + serializer.serialize_str(&compact_text.replace("\n", "$")) + } +} + impl IntoSpecializedDocument<BlockchainProtocol> for TransactionDocument { fn into_specialized(self) -> BlockchainProtocol { BlockchainProtocol::V10(Box::new(V10Document::Transaction(Box::new(self)))) diff --git a/documents/lib.rs b/documents/lib.rs index d0cd06724feef8216b1d990da4b019c8717c2d5d..bd373bd57e5d03563092be024c0aa40e3273b476 100644 --- a/documents/lib.rs +++ b/documents/lib.rs @@ -30,15 +30,17 @@ extern crate duniter_crypto; extern crate lazy_static; extern crate linked_hash_map; extern crate regex; +extern crate serde; use std::fmt::{Debug, Display, Error, Formatter}; use duniter_crypto::keys::BaseConvertionError; +use self::serde::ser::{Serialize, Serializer}; pub mod blockchain; /// A block Id. -#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] +#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Hash)] pub struct BlockId(pub u32); impl Display for BlockId { @@ -186,6 +188,15 @@ impl Default for Blockstamp { } } +impl Serialize for Blockstamp { + fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> + where + S: Serializer, + { + serializer.serialize_str(&format!("{}-{}", self.id, self.hash)) + } +} + impl Blockstamp { /// Create a `BlockUId` from a text. pub fn from_string(src: &str) -> Result<Blockstamp, BlockUIdParseError> {