duniter_documents/
lib.rs

1//  Copyright (C) 2018  The Duniter Project Developers.
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU Affero General Public License as
5// published by the Free Software Foundation, either version 3 of the
6// License, or (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU Affero General Public License for more details.
12//
13// You should have received a copy of the GNU Affero General Public License
14// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16//! Implements the Duniter Documents Protocol.
17
18#![cfg_attr(feature = "strict", deny(warnings))]
19#![deny(missing_docs, missing_debug_implementations, missing_copy_implementations, trivial_casts,
20        trivial_numeric_casts, unsafe_code, unstable_features, unused_import_braces,
21        unused_qualifications)]
22
23extern crate base58;
24extern crate base64;
25extern crate crypto;
26extern crate duniter_crypto;
27#[macro_use]
28extern crate lazy_static;
29extern crate linked_hash_map;
30extern crate regex;
31
32use std::fmt::{Debug, Display, Error, Formatter};
33
34use duniter_crypto::keys::BaseConvertionError;
35
36pub mod blockchain;
37
38/// A block Id.
39#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
40pub struct BlockId(pub u32);
41
42impl Display for BlockId {
43    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
44        write!(f, "{}", self.0)
45    }
46}
47
48/// A hash wrapper.
49///
50/// A hash is often provided as string composed of 64 hexadecimal character (0 to 9 then A to F).
51#[derive(Copy, Clone, PartialEq, Eq, Hash)]
52pub struct Hash(pub [u8; 32]);
53
54impl Display for Hash {
55    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
56        write!(f, "{}", self.to_hex())
57    }
58}
59
60impl Debug for Hash {
61    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
62        write!(f, "Hash({})", self)
63    }
64}
65
66impl Default for Hash {
67    fn default() -> Hash {
68        let default: [u8; 32] = [0; 32];
69        Hash(default)
70    }
71}
72
73impl Hash {
74    /// Convert a `Hash` to an hex string.
75    pub fn to_hex(&self) -> String {
76        let strings: Vec<String> = self.0.iter().map(|b| format!("{:02X}", b)).collect();
77
78        strings.join("")
79    }
80
81    /// Convert a hex string in a `Hash`.
82    ///
83    /// The hex string must only contains hex characters
84    /// and produce a 32 bytes value.
85    pub fn from_hex(text: &str) -> Result<Hash, BaseConvertionError> {
86        if text.len() != 64 {
87            Err(BaseConvertionError::InvalidKeyLendth(text.len(), 64))
88        } else {
89            let mut hash = Hash([0u8; 32]);
90
91            let chars: Vec<char> = text.chars().collect();
92
93            for i in 0..64 {
94                if i % 2 != 0 {
95                    continue;
96                }
97
98                let byte1 = chars[i].to_digit(16);
99                let byte2 = chars[i + 1].to_digit(16);
100
101                if byte1.is_none() {
102                    return Err(BaseConvertionError::InvalidCharacter(chars[i], i));
103                } else if byte2.is_none() {
104                    return Err(BaseConvertionError::InvalidCharacter(chars[i + 1], i + 1));
105                }
106
107                let byte1 = byte1.unwrap() as u8;
108                let byte2 = byte2.unwrap() as u8;
109
110                let byte = (byte1 << 4) | byte2;
111                hash.0[i / 2] = byte;
112            }
113
114            Ok(hash)
115        }
116    }
117}
118
119/// Wrapper of a block hash.
120#[derive(Copy, Clone, PartialEq, Eq, Hash)]
121pub struct BlockHash(Hash);
122
123impl Display for BlockHash {
124    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
125        write!(f, "{}", self.0.to_hex())
126    }
127}
128
129impl Debug for BlockHash {
130    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
131        write!(f, "BlockHash({})", self)
132    }
133}
134
135/// Type of errors for [`BlockUId`] parsing.
136///
137/// [`BlockUId`]: struct.BlockUId.html
138#[derive(Debug, Copy, Clone, PartialEq, Eq)]
139pub enum BlockUIdParseError {
140    /// Given string have invalid format
141    InvalidFormat(),
142    /// [`BlockId`](struct.BlockHash.html) part is not a valid number.
143    InvalidBlockId(),
144    /// [`BlockHash`](struct.BlockHash.html) part is not a valid hex number.
145    InvalidBlockHash(),
146}
147
148/// A blockstamp (Unique ID).
149///
150/// It's composed of the [`BlockId`] and
151/// the [`BlockHash`] of the block.
152///
153/// Thanks to blockchain immutability and frequent block production, it can
154/// be used to date information.
155///
156/// [`BlockId`]: struct.BlockId.html
157/// [`BlockHash`]: struct.BlockHash.html
158#[derive(Copy, Clone, PartialEq, Eq, Hash)]
159pub struct Blockstamp {
160    /// Block Id.
161    pub id: BlockId,
162    /// Block hash.
163    pub hash: BlockHash,
164}
165
166impl Display for Blockstamp {
167    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
168        write!(f, "{}-{}", self.id, self.hash)
169    }
170}
171
172impl Debug for Blockstamp {
173    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
174        write!(f, "BlockUId({})", self)
175    }
176}
177
178impl Default for Blockstamp {
179    fn default() -> Blockstamp {
180        Blockstamp {
181            id: BlockId(0),
182            hash: BlockHash(Hash::default()),
183        }
184    }
185}
186
187impl Blockstamp {
188    /// Create a `BlockUId` from a text.
189    pub fn from_string(src: &str) -> Result<Blockstamp, BlockUIdParseError> {
190        let mut split = src.split('-');
191
192        if split.clone().count() != 2 {
193            Err(BlockUIdParseError::InvalidFormat())
194        } else {
195            let id = split.next().unwrap().parse::<u32>();
196            let hash = Hash::from_hex(split.next().unwrap());
197
198            if id.is_err() {
199                Err(BlockUIdParseError::InvalidBlockId())
200            } else if hash.is_err() {
201                Err(BlockUIdParseError::InvalidBlockHash())
202            } else {
203                Ok(Blockstamp {
204                    id: BlockId(id.unwrap()),
205                    hash: BlockHash(hash.unwrap()),
206                })
207            }
208        }
209    }
210
211    /// Convert a `BlockUId` to its text format.
212    pub fn to_string(&self) -> String {
213        format!("{}", self)
214    }
215}