1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//  Copyright (C) 2017-2019  The AXIOM TEAM Association.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.

//! Implements the Dunitrust Documents Protocol.

#![deny(
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features,
    unused_import_braces
)]

#[macro_use]
extern crate log;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate pest_derive;
#[cfg(test)]
#[macro_use]
extern crate pretty_assertions;
#[macro_use]
extern crate serde_derive;

pub mod blockstamp;
pub mod documents;
pub mod parsers;
pub mod text_document_traits;

use dup_crypto::hashs::Hash;
use dup_crypto::keys::*;
use pest::iterators::Pair;
use pest::RuleType;
use serde::Serialize;
use std::cmp::Ordering;
use std::fmt::{Debug, Display, Error, Formatter};
use std::net::AddrParseError;

pub use crate::blockstamp::{Blockstamp, PreviousBlockstamp};

#[derive(Parser)]
#[grammar = "documents_grammar.pest"]
/// Parser for Documents
struct DocumentsParser;

pub trait TextDocumentParser<R: RuleType> {
    /// Type of document generated by the parser
    type DocumentType;

    /// Parse text document from raw format
    fn parse(doc: &str) -> Result<Self::DocumentType, TextDocumentParseError>;
    /// Parse text document from pest pairs
    fn from_pest_pair(pairs: Pair<R>) -> Result<Self::DocumentType, TextDocumentParseError>;
    /// Parse text document from versioned pest pairs
    fn from_versioned_pest_pair(
        version: u16,
        pairs: Pair<R>,
    ) -> Result<Self::DocumentType, TextDocumentParseError>;
}

/// Error with pest parser (grammar)
#[derive(Debug, Clone, Eq, Fail, PartialEq)]
#[fail(display = "Grammar error: {}", _0)]
pub struct PestError(pub String);

impl<T: pest::RuleType> From<pest::error::Error<T>> for PestError {
    fn from(e: pest::error::Error<T>) -> Self {
        PestError(format!("{}", e))
    }
}

/// List of possible errors while parsing a text document.
#[derive(Debug, Clone, Eq, Fail, PartialEq)]
pub enum TextDocumentParseError {
    /// The given source don't have a valid specific document format (document type).
    #[fail(display = "TextDocumentParseError: Invalid inner format: {}", _0)]
    InvalidInnerFormat(String),
    /// Ip address parse error
    #[fail(display = "TextDocumentParseError: invalid ip: {}", _0)]
    IpAddrError(AddrParseError),
    /// Error with pest parser
    #[fail(display = "TextDocumentParseError: {}", _0)]
    PestError(PestError),
    /// Unexpected rule
    #[fail(display = "TextDocumentParseError: Unexpected rule: '{}'", _0)]
    UnexpectedRule(String),
    /// Unexpected version
    #[fail(display = "TextDocumentParseError: Unexpected version: '{}'", _0)]
    UnexpectedVersion(String),
    /// Unknown type
    #[fail(display = "TextDocumentParseError: UnknownType.")]
    UnknownType,
}

impl From<AddrParseError> for TextDocumentParseError {
    fn from(e: AddrParseError) -> Self {
        TextDocumentParseError::IpAddrError(e)
    }
}

impl From<PestError> for TextDocumentParseError {
    fn from(e: PestError) -> Self {
        TextDocumentParseError::PestError(e)
    }
}

impl<T: pest::RuleType> From<pest::error::Error<T>> for TextDocumentParseError {
    fn from(e: pest::error::Error<T>) -> Self {
        TextDocumentParseError::PestError(e.into())
    }
}

/// A block Id.
#[derive(Copy, Clone, Debug, Deserialize, Ord, PartialEq, PartialOrd, Eq, Hash, Serialize)]
pub struct BlockNumber(pub u32);

impl Display for BlockNumber {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}", self.0)
    }
}

/// Wrapper of a block hash.
#[derive(Copy, Clone, Default, Deserialize, Eq, Ord, PartialEq, PartialOrd, Hash, Serialize)]
pub struct BlockHash(pub Hash);

impl Display for BlockHash {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}", self.0.to_hex())
    }
}

impl Debug for BlockHash {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "BlockHash({})", self)
    }
}

/// trait providing commun methods for any documents of any protocol version.
///
/// # Design choice
///
/// Allow only ed25519 for protocol 10 and many differents
/// schemes for protocol 11 through a proxy type.
pub trait Document: Debug + Clone + PartialEq + Eq {
    /// Type of the `PublicKey` used by the document.
    type PublicKey: PublicKey;

    /// Get document as bytes for signature verification.
    fn as_bytes(&self) -> &[u8];

    /// Get document blockstamp
    fn blockstamp(&self) -> Blockstamp;

    /// Get document currency name.
    fn currency(&self) -> &str;

    /// Iterate over document issuers.
    fn issuers(&self) -> &Vec<Self::PublicKey>;

    /// Some documents do not directly store the sequence of bytes that will be signed but generate
    // it on request, so these types of documents cannot provide a reference to the signed bytes.
    fn no_as_bytes(&self) -> bool {
        false
    }

    /// Get document to bytes for signature verification.
    fn to_bytes(&self) -> Vec<u8> {
        self.as_bytes().to_vec()
    }

    /// Iterate over document signatures.
    fn signatures(&self) -> &Vec<<Self::PublicKey as PublicKey>::Signature>;

    /// Verify one signature
    #[inline]
    fn verify_one_signature(
        &self,
        public_key: &Self::PublicKey,
        signature: &<Self::PublicKey as PublicKey>::Signature,
    ) -> Result<(), SigError> {
        if self.no_as_bytes() {
            public_key.verify(&self.to_bytes(), signature)
        } else {
            public_key.verify(self.as_bytes(), signature)
        }
    }

    /// Verify signatures of document content (as text format)
    fn verify_signatures(&self) -> Result<(), DocumentSigsErr> {
        let issuers_count = self.issuers().len();
        let signatures_count = self.signatures().len();

        if issuers_count != signatures_count {
            Err(DocumentSigsErr::IncompletePairs(
                issuers_count,
                signatures_count,
            ))
        } else {
            let issuers = self.issuers();
            let signatures = self.signatures();
            let mismatches: HashMap<usize, SigError> = issuers
                .iter()
                .zip(signatures)
                .enumerate()
                .filter_map(|(i, (key, signature))| {
                    if let Err(e) = self.verify_one_signature(key, signature) {
                        Some((i, e))
                    } else {
                        None
                    }
                })
                .collect();

            if mismatches.is_empty() {
                Ok(())
            } else {
                Err(DocumentSigsErr::Invalid(mismatches))
            }
        }
    }

    /// Get document version.
    fn version(&self) -> u16;
}

use std::collections::HashMap;

// todo: à mon avis faudrait pas que y ait de Valid() dans cette enum
// et du coup faudrait que les fonctions qui renvoient un DocumentSigsErr renvoie Result<(), DocumentSigsErr>
// du coup SignatureError dans la local verif sert plus à rien.

/// List of possible errors for document signatures verification.
#[derive(Debug, Eq, PartialEq)]
pub enum DocumentSigsErr {
    /// Not same amount of issuers and signatures.
    /// (issuers count, signatures count)
    IncompletePairs(usize, usize),
    /// Signatures don't match.
    /// List of mismatching pairs indexes.
    Invalid(HashMap<usize, SigError>),
}

/// Trait helper for building new documents.
pub trait DocumentBuilder {
    /// Type of the builded document.
    type Document: Document;

    /// Type of the private keys signing the documents.
    type PrivateKey: PrivateKey<
        Signature = <<Self::Document as Document>::PublicKey as PublicKey>::Signature,
    >;

    /// Build a document with provided signatures.
    fn build_with_signature(
        &self,
        signatures: Vec<<<Self::Document as Document>::PublicKey as PublicKey>::Signature>,
    ) -> Self::Document;

    /// Build a document and sign it with the private key.
    fn build_and_sign(&self, private_keys: Vec<Self::PrivateKey>) -> Self::Document;
}

/// Trait for a document parser from a `S` source
/// format to a `D` document. Will return the
/// parsed document or an `E` error.
pub trait DocumentParser<S, D, E> {
    /// Parse a source and return a document or an error.
    fn parse(source: S) -> Result<D, E>;
}

/// Stringify a document
pub trait ToStringObject {
    type StringObject: Serialize;
    /// Transforms object fields into string
    fn to_string_object(&self) -> Self::StringObject;
}

/// Jsonify a document
pub trait ToJsonObject: ToStringObject {
    /// Convert to JSON String
    fn to_json_string(&self) -> Result<String, serde_json::Error> {
        Ok(serde_json::to_string(&self.to_string_object())?)
    }
    /// Convert to JSON String pretty
    fn to_json_string_pretty(&self) -> Result<String, serde_json::Error> {
        Ok(serde_json::to_string_pretty(&self.to_string_object())?)
    }
}

impl<T: ToStringObject> ToJsonObject for T {}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::documents::UserDocumentDUBP;

    #[test]
    fn parse_dubp_document() {
        let text = "Version: 10
Type: Identity
Currency: g1
Issuer: D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx
UniqueID: elois
Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
Ydnclvw76/JHcKSmU9kl9Ie0ne5/X8NYOqPqbGnufIK3eEPRYYdEYaQh+zffuFhbtIRjv6m/DkVLH5cLy/IyAg==";

        let doc = UserDocumentDUBP::parse(text).expect("Fail to parse UserDocumentDUBP !");
        println!("Doc : {:?}", doc);
    }
}