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
#![deny(
clippy::expect_used,
clippy::unwrap_used,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unsafe_code,
unstable_features,
unused_import_braces
)]
pub mod certification;
pub mod identity;
pub mod membership;
pub mod revocation;
mod traits;
pub mod transaction;
pub use dubp_wallet;
pub use dubp_wallet::dubp_common;
pub use dubp_wallet::smallvec;
pub mod prelude {
pub use crate::traits::{
text::{CompactTextDocument, TextDocument, TextDocumentBuilder, TextDocumentFormat},
Document, DocumentBuilder, ToJsonObject, ToStringObject,
};
pub use crate::{DubpDocument, DubpDocumentStr};
}
pub(crate) use crate::prelude::*;
pub(crate) use crate::transaction::{TransactionDocumentTrait, UTXOConditions};
pub(crate) use beef::lean::Cow as BeefCow;
pub(crate) use dubp_common::crypto::bases::b58::ToBase58;
pub(crate) use dubp_common::crypto::hashs::Hash;
pub(crate) use dubp_common::crypto::keys::*;
pub(crate) use dubp_common::prelude::*;
pub(crate) use dubp_wallet::prelude::*;
pub(crate) use serde::{Deserialize, Serialize};
pub(crate) use smallvec::{smallvec as svec, SmallVec, ToSmallVec};
pub(crate) use std::{
borrow::Cow,
collections::{BTreeMap, BTreeSet, HashMap},
fmt::Debug,
};
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum DubpDocument {
Transaction(transaction::TransactionDocument),
Identity(identity::IdentityDocument),
Membership(membership::MembershipDocument),
Certification(certification::CertificationDocument),
Revocation(revocation::RevocationDocument),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum DubpDocumentStr {
Transaction(Box<transaction::TransactionDocumentStringified>),
Identity(identity::IdentityDocumentStringified),
Membership(membership::MembershipDocumentStringified),
Certification(Box<certification::CertificationDocumentStringified>),
Revocation(Box<revocation::RevocationDocumentStringified>),
}
impl ToStringObject for DubpDocument {
type StringObject = DubpDocumentStr;
fn to_string_object(&self) -> Self::StringObject {
match *self {
DubpDocument::Identity(ref doc) => DubpDocumentStr::Identity(doc.to_string_object()),
DubpDocument::Membership(ref doc) => {
DubpDocumentStr::Membership(doc.to_string_object())
}
DubpDocument::Certification(ref doc) => {
DubpDocumentStr::Certification(Box::new(doc.to_string_object()))
}
DubpDocument::Revocation(ref doc) => {
DubpDocumentStr::Revocation(Box::new(doc.to_string_object()))
}
DubpDocument::Transaction(ref doc) => {
DubpDocumentStr::Transaction(Box::new(doc.to_string_object()))
}
}
}
}
macro_rules! dubp_document_fn {
($fn_name:ident, $return_type:ty) => {
fn $fn_name(&self) -> $return_type {
match self {
Self::Certification(doc) => doc.$fn_name(),
Self::Identity(doc) => doc.$fn_name(),
Self::Membership(doc) => doc.$fn_name(),
Self::Revocation(doc) => doc.$fn_name(),
Self::Transaction(doc) => doc.$fn_name(),
}
}
};
}
impl Document for DubpDocument {
type PublicKey = PubKeyEnum;
dubp_document_fn!(as_bytes, BeefCow<[u8]>);
dubp_document_fn!(blockstamp, Blockstamp);
dubp_document_fn!(currency, &str);
dubp_document_fn!(issuers, SmallVec<[Self::PublicKey; 1]>);
dubp_document_fn!(
signatures,
SmallVec<[<Self::PublicKey as PublicKey>::Signature; 1]>
);
dubp_document_fn!(version, usize);
}
#[cfg(test)]
mod tests {
use super::*;
use unwrap::unwrap;
#[derive(Debug, Clone, PartialEq, Eq)]
struct PlainTextDocument {
pub text: &'static str,
pub issuers: SmallVec<[PubKeyEnum; 1]>,
pub signatures: SmallVec<[Sig; 1]>,
}
impl Document for PlainTextDocument {
type PublicKey = PubKeyEnum;
fn version(&self) -> usize {
unimplemented!()
}
fn currency(&self) -> &str {
unimplemented!()
}
fn blockstamp(&self) -> Blockstamp {
unimplemented!()
}
fn issuers(&self) -> SmallVec<[Self::PublicKey; 1]> {
self.issuers.iter().copied().collect()
}
fn signatures(&self) -> SmallVec<[<Self::PublicKey as PublicKey>::Signature; 1]> {
self.signatures.iter().copied().collect()
}
fn as_bytes(&self) -> BeefCow<[u8]> {
BeefCow::borrowed(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
";
let issuer1 = PubKeyEnum::Ed25519(unwrap!(
ed25519::PublicKey::from_base58("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"),
"Fail to parse PublicKey from base58"
));
let sig1 = Sig::Ed25519(unwrap!(
ed25519::Signature::from_base64(
"1eubHHbuNfilHMM0G2bI30iZzebQ2cQ1PC7uPAw08FGMM\
mQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==",
),
"Fail to parse Signature from base64"
));
let issuer2 = PubKeyEnum::Ed25519(unwrap!(
ed25519::PublicKey::from_base58("DNann1Lh55eZMEDXeYt32bzHbA3NJR46DeQYCS2qQdLV"),
"Fail to parse PublicKey from base58"
));
let sig2 = Sig::Ed25519(unwrap!(
ed25519::Signature::from_base64(
"1eubHHbuNfilHHH0G2bI30iZzebQ2cQ1PC7uPAw08FGMM\
mQCRerlF/3pc4sAcsnexsxBseA/3lY03KlONqJBAg==",
),
"Fail to parse Signature from base64"
));
{
let doc = PlainTextDocument {
text,
issuers: svec![issuer1],
signatures: svec![sig1],
};
if let Err(e) = doc.verify_signatures() {
panic!("DocumentSigsErr: {:?}", e)
}
}
{
let doc = PlainTextDocument {
text,
issuers: svec![issuer1],
signatures: svec![sig2],
};
assert_eq!(
doc.verify_signatures(),
Err(DocumentSigsErr::Invalid(
maplit::hashmap![0 => SigError::InvalidSig]
))
);
}
{
let doc = PlainTextDocument {
text,
issuers: svec![issuer1, issuer2],
signatures: svec![sig1],
};
assert_eq!(
doc.verify_signatures(),
Err(DocumentSigsErr::IncompletePairs(2, 1))
);
}
{
let doc = PlainTextDocument {
text,
issuers: svec![issuer1],
signatures: svec![sig1, sig2],
};
assert_eq!(
doc.verify_signatures(),
Err(DocumentSigsErr::IncompletePairs(1, 2))
);
}
}
}