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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//  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 blockchain Documents.

use crate::documents::block::*;
use crate::documents::certification::*;
use crate::documents::identity::*;
use crate::documents::membership::*;
use crate::documents::revocation::*;
use crate::documents::transaction::*;
use crate::Rule;
use crate::*;

use durs_common_tools::fatal_error;
use pest::iterators::Pair;
use pest::Parser;

pub mod block;
pub mod certification;
pub mod identity;
pub mod membership;
pub mod revocation;
pub mod transaction;

/// Document of DUBP (DUniter Blockhain Protocol)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DocumentDUBP {
    /// Block document.
    Block(Box<BlockDocument>),
    /// User document of DUBP (DUniter Blockhain Protocol)
    UserDocument(UserDocumentDUBP),
}

/// User document of DUBP (DUniter Blockhain Protocol)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UserDocumentDUBP {
    /// Transaction document.
    Transaction(Box<TransactionDocument>),

    /// Identity document.
    Identity(IdentityDocument),

    /// Membership document.
    Membership(MembershipDocument),

    /// Certification document.
    Certification(Box<CertificationDocument>),

    /// Revocation document.
    Revocation(Box<RevocationDocument>),
}

/// List of stringified document types.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DocumentDUBPStr {
    /// Block document (not yet implemented)
    Block(Box<BlockDocumentStringified>),
    /// Stringified user document.
    UserDocument(UserDocumentDUBPStr),
}

/// List of stringified user document types.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UserDocumentDUBPStr {
    /// Transaction document.
    Transaction(Box<TransactionDocumentStringified>),

    /// Identity document.
    Identity(IdentityDocumentStringified),

    /// Membership document.
    Membership(MembershipDocumentStringified),

    /// Certification document.
    Certification(Box<CertificationDocumentStringified>),

    /// Revocation document.
    Revocation(Box<RevocationDocumentStringified>),
}

impl ToStringObject for DocumentDUBP {
    type StringObject = DocumentDUBPStr;

    fn to_string_object(&self) -> Self::StringObject {
        match *self {
            DocumentDUBP::Block(ref doc) => {
                DocumentDUBPStr::Block(Box::new(doc.to_string_object()))
            }
            DocumentDUBP::UserDocument(ref user_doc) => {
                DocumentDUBPStr::UserDocument(user_doc.to_string_object())
            }
        }
    }
}

impl ToStringObject for UserDocumentDUBP {
    type StringObject = UserDocumentDUBPStr;

    fn to_string_object(&self) -> Self::StringObject {
        match *self {
            UserDocumentDUBP::Identity(ref doc) => {
                UserDocumentDUBPStr::Identity(doc.to_string_object())
            }
            UserDocumentDUBP::Membership(ref doc) => {
                UserDocumentDUBPStr::Membership(doc.to_string_object())
            }
            UserDocumentDUBP::Certification(ref doc) => {
                UserDocumentDUBPStr::Certification(Box::new(doc.to_string_object()))
            }
            UserDocumentDUBP::Revocation(ref doc) => {
                UserDocumentDUBPStr::Revocation(Box::new(doc.to_string_object()))
            }
            UserDocumentDUBP::Transaction(ref doc) => {
                UserDocumentDUBPStr::Transaction(Box::new(doc.to_string_object()))
            }
        }
    }
}

impl TextDocumentParser<Rule> for UserDocumentDUBP {
    type DocumentType = UserDocumentDUBP;

    fn parse(doc: &str) -> Result<UserDocumentDUBP, TextDocumentParseError> {
        match DocumentsParser::parse(Rule::document, doc) {
            Ok(mut doc_pairs) => Ok(UserDocumentDUBP::from_pest_pair(doc_pairs.next().unwrap())?), // get and unwrap the `document` rule; never fails
            Err(pest_error) => Err(pest_error.into()),
        }
    }
    #[inline]
    fn from_pest_pair(pair: Pair<Rule>) -> Result<Self::DocumentType, TextDocumentParseError> {
        let doc_vx_pair = pair.into_inner().next().unwrap(); // get and unwrap the `document_vX` rule; never fails

        match doc_vx_pair.as_rule() {
            Rule::document_v10 => Ok(UserDocumentDUBP::from_versioned_pest_pair(10, doc_vx_pair)?),
            _ => fatal_error!("unexpected rule: {:?}", doc_vx_pair.as_rule()), // Grammar ensures that we never reach this line
        }
    }
    #[inline]
    fn from_versioned_pest_pair(
        version: u16,
        pair: Pair<Rule>,
    ) -> Result<Self::DocumentType, TextDocumentParseError> {
        match version {
            10 => Ok(UserDocumentDUBP::from_pest_pair_v10(pair)?),
            v => Err(TextDocumentParseError::UnexpectedVersion(format!(
                "Unsupported version: {}",
                v
            ))),
        }
    }
}

impl UserDocumentDUBP {
    pub fn from_pest_pair_v10(
        pair: Pair<Rule>,
    ) -> Result<UserDocumentDUBP, TextDocumentParseError> {
        let doc_type_v10_pair = pair.into_inner().next().unwrap(); // get and unwrap the `{DOC_TYPE}_v10` rule; never fails

        match doc_type_v10_pair.as_rule() {
            Rule::idty_v10 => Ok(UserDocumentDUBP::Identity(IdentityDocument::V10(
                IdentityDocumentV10::from_pest_pair(doc_type_v10_pair)?,
            ))),
            Rule::membership_v10 => Ok(UserDocumentDUBP::Membership(MembershipDocument::V10(
                MembershipDocumentV10::from_pest_pair(doc_type_v10_pair)?,
            ))),
            Rule::cert_v10 => Ok(UserDocumentDUBP::Certification(Box::new(
                CertificationDocument::V10(CertificationDocumentV10::from_pest_pair(
                    doc_type_v10_pair,
                )?),
            ))),
            Rule::revoc_v10 => Ok(UserDocumentDUBP::Revocation(Box::new(
                RevocationDocumentParser::from_pest_pair(doc_type_v10_pair)?,
            ))),
            Rule::tx_v10 => Ok(UserDocumentDUBP::Transaction(Box::new(
                transaction::TransactionDocumentParser::from_pest_pair(doc_type_v10_pair)?,
            ))),
            _ => fatal_error!("unexpected rule: {:?}", doc_type_v10_pair.as_rule()), // Grammar ensures that we never reach this line
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::blockstamp::Blockstamp;
    use crate::*;

    use super::certification::CertificationDocumentParser;
    use super::identity::IdentityDocumentParser;
    use super::membership::MembershipDocumentParser;
    use super::revocation::RevocationDocumentParser;
    use super::transaction::TransactionDocumentParser;

    use dup_crypto::keys::*;

    // simple text document for signature testing
    #[derive(Debug, Clone, PartialEq, Eq)]
    struct PlainTextDocument {
        pub text: &'static str,
        pub issuers: Vec<PubKey>,
        pub signatures: Vec<Sig>,
    }

    impl Document for PlainTextDocument {
        type PublicKey = PubKey;

        fn version(&self) -> u16 {
            unimplemented!()
        }

        fn currency(&self) -> &str {
            unimplemented!()
        }

        fn blockstamp(&self) -> Blockstamp {
            unimplemented!()
        }

        fn issuers(&self) -> &Vec<PubKey> {
            &self.issuers
        }

        fn signatures(&self) -> &Vec<Sig> {
            &self.signatures
        }

        fn as_bytes(&self) -> &[u8] {
            self.text.as_bytes()
        }
    }

    #[test]
    fn verify_signatures() {
        let text = "Version: 10
Type: Identity
Currency: duniter_unit_test_currency
Issuer: DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV
UniqueID: tic
Timestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
";

        // good pair
        let issuer1 = PubKey::Ed25519(
            ed25519::PublicKey::from_base58("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV")
                .unwrap(),
        );

        let sig1 = Sig::Ed25519(
            ed25519::Signature::from_base64(
                "1eubHHbuNfilHMM0G2bI30iZzebQ2cQ1PC7uPAw08FGMM\
                 mQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==",
            )
            .unwrap(),
        );

        // incorrect pair
        let issuer2 = PubKey::Ed25519(
            ed25519::PublicKey::from_base58("DNann1Lh55eZMEDXeYt32bzHbA3NJR46DeQYCS2qQdLV")
                .unwrap(),
        );

        let sig2 = Sig::Ed25519(
            ed25519::Signature::from_base64(
                "1eubHHbuNfilHHH0G2bI30iZzebQ2cQ1PC7uPAw08FGMM\
                 mQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==",
            )
            .unwrap(),
        );

        {
            let doc = PlainTextDocument {
                text,
                issuers: vec![issuer1],
                signatures: vec![sig1],
            };

            if let Err(e) = doc.verify_signatures() {
                panic!("DocumentSigsErr: {:?}", e)
            }
        }

        {
            let doc = PlainTextDocument {
                text,
                issuers: vec![issuer1],
                signatures: vec![sig2],
            };
            // todo: gérer l'erreur avec PartialEq
            /*
            assert_eq!(
                doc.verify_signatures(),
                Err(DocumentSigsErr::Invalid(vec![0]))
            );
            */
            assert!(doc.verify_signatures().is_err());
        }

        {
            let doc = PlainTextDocument {
                text,
                issuers: vec![issuer1, issuer2],
                signatures: vec![sig1],
            };

            // todo: gérer l'erreur avec PartialEq
            /*
            assert_eq!(
                doc.verify_signatures(),
                Err(DocumentSigsErr::IncompletePairs(2, 1))
            );
            */
            assert!(doc.verify_signatures().is_err());
        }

        {
            let doc = PlainTextDocument {
                text,
                issuers: vec![issuer1],
                signatures: vec![sig1, sig2],
            };

            // todo: gérer l'erreur avec PartialEq
            /*
            assert_eq!(
                doc.verify_signatures(),
                Err(DocumentSigsErr::IncompletePairs(1, 2))
            );
            */
            assert!(doc.verify_signatures().is_err());
        }
    }

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

        let doc = IdentityDocumentParser::parse(text).unwrap();
        println!("Doc : {:?}", doc);
        assert!(doc.verify_signatures().is_ok());
    }

    #[test]
    fn parse_membership_document() {
        let text = "Version: 10
Type: Membership
Currency: g1
Issuer: D9D2zaJoWYWveii1JRYLVK3J4Z7ZH3QczoKrnQeiM6mx
Block: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
Membership: IN
UserID: elois
CertTS: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
FFeyrvYio9uYwY5aMcDGswZPNjGLrl8THn9l3EPKSNySD3SDSHjCljSfFEwb87sroyzJQoVzPwER0sW/cbZMDg==";

        let doc = MembershipDocumentParser::parse(text).unwrap();
        println!("Doc : {:?}", doc);
        assert!(doc.verify_signatures().is_ok());
    }

    #[test]
    fn parse_certification_document() {
        let text = "Version: 10
Type: Certification
Currency: g1
Issuer: 2sZF6j2PkxBDNAqUde7Dgo5x3crkerZpQ4rBqqJGn8QT
IdtyIssuer: 7jzkd8GiFnpys4X7mP78w2Y3y3kwdK6fVSLEaojd3aH9
IdtyUniqueID: fbarbut
IdtyTimestamp: 98221-000000575AC04F5164F7A307CDB766139EA47DD249E4A2444F292BC8AAB408B3
IdtySignature: DjeipIeb/RF0tpVCnVnuw6mH1iLJHIsDfPGLR90Twy3PeoaDz6Yzhc/UjLWqHCi5Y6wYajV0dNg4jQRUneVBCQ==
CertTimestamp: 99956-00000472758331FDA8388E30E50CA04736CBFD3B7C21F34E74707107794B56DD
Hkps1QU4HxIcNXKT8YmprYTVByBhPP1U2tIM7Z8wENzLKIWAvQClkAvBE7pW9dnVa18sJIJhVZUcRrPAZfmjBA==";

        let doc = CertificationDocumentParser::parse(text).unwrap();
        println!("Doc : {:?}", doc);
        assert!(doc.verify_signatures().is_ok());
    }

    #[test]
    fn parse_revocation_document() {
        let text = "Version: 10
Type: Revocation
Currency: g1
Issuer: DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV
IdtyUniqueID: tic
IdtyTimestamp: 0-E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855
IdtySignature: 1eubHHbuNfilHMM0G2bI30iZzebQ2cQ1PC7uPAw08FGMMmQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==
XXOgI++6qpY9O31ml/FcfbXCE6aixIrgkT5jL7kBle3YOMr+8wrp7Rt+z9hDVjrNfYX2gpeJsuMNfG4T/fzVDQ==";

        let doc = RevocationDocumentParser::parse(text).unwrap();
        println!("Doc : {:?}", doc);
        assert!(doc.verify_signatures().is_ok());
    }

    #[test]
    fn parse_transaction_document() {
        let text = "Version: 10
Type: Transaction
Currency: g1
Blockstamp: 107702-0000017CDBE974DC9A46B89EE7DC2BEE4017C43A005359E0853026C21FB6A084
Locktime: 0
Issuers:
Do6Y6nQ2KTo5fB4MXbSwabXVmXHxYRB9UUAaTPKn1XqC
Inputs:
1002:0:D:Do6Y6nQ2KTo5fB4MXbSwabXVmXHxYRB9UUAaTPKn1XqC:104937
1002:0:D:Do6Y6nQ2KTo5fB4MXbSwabXVmXHxYRB9UUAaTPKn1XqC:105214
Unlocks:
0:SIG(0)
1:SIG(0)
Outputs:
2004:0:SIG(DTgQ97AuJ8UgVXcxmNtULAs8Fg1kKC1Wr9SAS96Br9NG)
Comment: c est pour 2 mois d adhesion ressourcerie
lnpuFsIymgz7qhKF/GsZ3n3W8ZauAAfWmT4W0iJQBLKJK2GFkesLWeMj/+GBfjD6kdkjreg9M6VfkwIZH+hCCQ==";

        let doc = TransactionDocumentParser::parse(text).unwrap();
        println!("Doc : {:?}", doc);
        assert!(doc.verify_signatures().is_ok());
    }
}