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
pub mod text;
use crate::*;
pub trait Document: Debug + Clone + PartialEq + Eq {
type PublicKey: PublicKey;
fn as_bytes(&self) -> BeefCow<[u8]>;
fn blockstamp(&self) -> Blockstamp;
fn currency(&self) -> &str;
fn issuers(&self) -> SmallVec<[Self::PublicKey; 1]>;
fn signatures(&self) -> SmallVec<[<Self::PublicKey as PublicKey>::Signature; 1]>;
#[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)
}
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))
}
}
}
fn version(&self) -> usize;
}
pub trait DocumentBuilder {
type Document: Document;
type Signator: Signator<
Signature = <<Self::Document as Document>::PublicKey as PublicKey>::Signature,
>;
fn build_and_sign(self, signators: Vec<Self::Signator>) -> Self::Document;
fn build_with_signature(
self,
signatures: SmallVec<
[<<Self::Document as Document>::PublicKey as PublicKey>::Signature; 1],
>,
) -> Self::Document;
}
pub trait ToStringObject {
type StringObject: Serialize;
fn to_string_object(&self) -> Self::StringObject;
}
pub trait ToJsonObject: ToStringObject {
fn to_json_string(&self) -> Result<String, serde_json::Error> {
Ok(serde_json::to_string(&self.to_string_object())?)
}
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 {}