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
//  Copyright (C) 2020  Éloïs SANCHEZ.
//
// 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/>.

//! Define DUBP Documents Traits.

pub mod text;

use crate::*;

/// trait providing commun methods for any documents of any protocol version.
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.
    ///
    /// 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.
    /// This is why this method must return a `Cow<[u8]>` (we use the beef implementation instead of the std implementation).
    fn as_bytes(&self) -> BeefCow<[u8]>;

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

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

    /// Iterate over document issuers.
    fn issuers(&self) -> SmallVec<[Self::PublicKey; 1]>;

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

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

    /// Verify signatures of document content
    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) -> usize;
}

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

    /// Type of the signator signing the documents.
    type Signator: Signator<
        Signature = <<Self::Document as Document>::PublicKey as PublicKey>::Signature,
    >;

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

    /// Build a document with provided signatures.
    fn build_with_signature(
        self,
        signatures: SmallVec<
            [<<Self::Document as Document>::PublicKey as PublicKey>::Signature; 1],
        >,
    ) -> Self::Document;
}

/// Stringify a document
pub trait ToStringObject {
    /// Generated string object
    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 {}