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
use std::iter::{FusedIterator, Map, Zip};
use std::slice;
use crate::algorithms::{FlowSigner, Signature};
use crate::transaction::SignatureE;
#[derive(Clone)]
pub struct Multi<SecretKey> {
pub(crate) primary_key_idx: usize,
pub(crate) keys: Box<[One<SecretKey>]>,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct One<SecretKey> {
pub(crate) key_id: u32,
pub(crate) key: SecretKey,
}
pub type KeyIdIter<'a, T> = Map<slice::Iter<'a, One<T>>, fn(&'a One<T>) -> u32>;
pub type KeyIter<'a, T> = Map<slice::Iter<'a, One<T>>, fn(&'a One<T>) -> &'a T>;
impl<SecretKey> Multi<SecretKey> {
pub fn primary_key(&self) -> &SecretKey {
&self.keys[self.primary_key_idx].key
}
pub fn primary_key_id(&self) -> u32 {
self.keys[self.primary_key_idx].key_id
}
pub fn key_ids(&self) -> KeyIdIter<SecretKey> {
self.keys.iter().map(key_id)
}
pub fn keys(&self) -> KeyIter<SecretKey> {
self.keys.iter().map(key)
}
}
#[derive(Clone)]
pub enum SignMethod<SecretKey> {
One(One<SecretKey>),
Multi(Multi<SecretKey>),
}
impl<SecretKey> SignMethod<SecretKey> {
pub fn primary_secret_key(&self) -> &SecretKey {
match self {
Self::One(one) => &one.key,
Self::Multi(multi) => multi.primary_key(),
}
}
pub fn primary_key_id(&self) -> u32 {
match self {
Self::One(one) => one.key_id,
Self::Multi(multi) => multi.primary_key_id(),
}
}
pub fn key_ids(&self) -> KeyIdIter<SecretKey> {
let keys = match self {
Self::One(one) => unsafe { std::slice::from_raw_parts(one, 1) },
Self::Multi(multi) => &multi.keys,
};
keys.iter().map(key_id)
}
}
#[derive(Clone)]
pub struct MkSigIter<'a, KeyIdIter, SigIter> {
address: &'a [u8],
iter: Zip<KeyIdIter, SigIter>,
}
impl<'a, KeyIdIter: Iterator<Item = u32>, SigIter: Iterator> MkSigIter<'a, KeyIdIter, SigIter>
where
SigIter::Item: Signature,
{
pub fn new(address: &'a [u8], key_ids: KeyIdIter, signatures: SigIter) -> Self {
Self {
address,
iter: key_ids.zip(signatures),
}
}
}
impl<'a, KeyIdIter: Iterator<Item = u32>, SigIter: Iterator> Iterator
for MkSigIter<'a, KeyIdIter, SigIter>
where
SigIter::Item: Signature,
{
type Item = SignatureE<&'a [u8], <SigIter::Item as Signature>::Serialized>;
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|(key_id, sig)| SignatureE {
address: self.address,
signature: sig.serialize(),
key_id,
})
}
}
#[derive(Clone)]
pub struct SignIter<'a, Signer: FlowSigner> {
data: [u8; 32],
signer: &'a Signer,
idx: usize,
method: &'a SignMethod<Signer::SecretKey>,
}
impl<'a, Signer: FlowSigner> SignIter<'a, Signer> {
pub fn new(
data: [u8; 32],
signer: &'a Signer,
method: &'a SignMethod<Signer::SecretKey>,
) -> Self {
Self {
data,
signer,
idx: 0,
method,
}
}
pub fn remaining(&self) -> usize {
let count = match self.method {
SignMethod::One(_) => 1,
SignMethod::Multi(multi) => multi.keys.len(),
};
count - self.idx
}
#[inline]
pub fn has_remaining(&self) -> bool {
self.remaining() > 0
}
unsafe fn next_unchecked(&mut self) -> Signer::Signature {
let secret_key = match self.method {
SignMethod::One(sec) => &sec.key,
SignMethod::Multi(multi) => &multi.keys.get_unchecked(self.idx).key,
};
self.idx += 1;
self.signer.sign_populated(self.data, secret_key)
}
}
impl<Signer: FlowSigner> Iterator for SignIter<'_, Signer> {
type Item = Signer::Signature;
fn next(&mut self) -> Option<Self::Item> {
self.has_remaining()
.then(|| unsafe { self.next_unchecked() })
}
fn size_hint(&self) -> (usize, Option<usize>) {
let len = self.remaining();
(len, Some(len))
}
}
impl<Signer: FlowSigner> FusedIterator for SignIter<'_, Signer> {}
impl<Signer: FlowSigner> ExactSizeIterator for SignIter<'_, Signer> {
fn len(&self) -> usize {
self.remaining()
}
}
fn key_id<T>(one: &One<T>) -> u32 {
one.key_id
}
fn key<T>(one: &One<T>) -> &T {
&one.key
}