1#![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#[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#[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 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 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#[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#[derive(Debug, Copy, Clone, PartialEq, Eq)]
139pub enum BlockUIdParseError {
140 InvalidFormat(),
142 InvalidBlockId(),
144 InvalidBlockHash(),
146}
147
148#[derive(Copy, Clone, PartialEq, Eq, Hash)]
159pub struct Blockstamp {
160 pub id: BlockId,
162 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 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 pub fn to_string(&self) -> String {
213 format!("{}", self)
214 }
215}