dubp_documents/
identity.rs

1//  Copyright (C) 2020  Éloïs SANCHEZ.
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//! Wrappers around Identity documents.
17
18pub mod v10;
19
20pub use v10::{IdentityDocumentV10, IdentityDocumentV10Builder, IdentityDocumentV10Stringified};
21
22use crate::*;
23
24/// Identity document
25#[derive(Clone, Debug, Deserialize, Hash, Serialize, PartialEq, Eq)]
26pub enum IdentityDocument {
27    /// Identity document V10
28    V10(IdentityDocumentV10),
29}
30
31impl Document for IdentityDocument {
32    type PublicKey = PubKeyEnum;
33
34    #[inline]
35    fn version(&self) -> usize {
36        match self {
37            IdentityDocument::V10(idty_v10) => idty_v10.version(),
38        }
39    }
40
41    #[inline]
42    fn currency(&self) -> &str {
43        match self {
44            IdentityDocument::V10(idty_v10) => idty_v10.currency(),
45        }
46    }
47
48    #[inline]
49    fn blockstamp(&self) -> Blockstamp {
50        match self {
51            IdentityDocument::V10(idty_v10) => idty_v10.blockstamp(),
52        }
53    }
54
55    #[inline]
56    fn issuers(&self) -> SmallVec<[Self::PublicKey; 1]> {
57        match self {
58            IdentityDocument::V10(idty_v10) => svec![PubKeyEnum::Ed25519(idty_v10.issuers()[0])],
59        }
60    }
61
62    #[inline]
63    fn signatures(&self) -> SmallVec<[<Self::PublicKey as PublicKey>::Signature; 1]> {
64        match self {
65            IdentityDocument::V10(idty_v10) => svec![Sig::Ed25519(idty_v10.signatures()[0])],
66        }
67    }
68
69    #[inline]
70    fn as_bytes(&self) -> BeefCow<[u8]> {
71        match self {
72            IdentityDocument::V10(idty_v10) => idty_v10.as_bytes(),
73        }
74    }
75}
76
77#[derive(Clone, Debug, Deserialize, Serialize)]
78pub enum IdentityDocumentStringified {
79    V10(IdentityDocumentV10Stringified),
80}
81
82impl ToStringObject for IdentityDocument {
83    type StringObject = IdentityDocumentStringified;
84
85    fn to_string_object(&self) -> Self::StringObject {
86        match self {
87            IdentityDocument::V10(idty) => {
88                IdentityDocumentStringified::V10(idty.to_string_object())
89            }
90        }
91    }
92}