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
289
290
291
292
293
294
use blsttc::{PublicKeySet, SignatureShare};
use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::iter::FromIterator;
use curve25519_dalek_ng::scalar::Scalar;
use crate::{
Amount, AmountSecrets, Dbc, DbcContent, Error, NodeSignature, PublicKey, ReissueRequest,
ReissueShare, ReissueTransaction, Result, SpendKey, SpentProof, SpentProofShare,
};
pub struct Output {
pub amount: Amount,
pub owner: PublicKey,
}
#[derive(Default)]
pub struct TransactionBuilder {
pub inputs: HashMap<Dbc, AmountSecrets>,
pub outputs: Vec<Output>,
}
impl TransactionBuilder {
pub fn add_input(mut self, dbc: Dbc, amount_secrets: AmountSecrets) -> Self {
self.inputs.insert(dbc, amount_secrets);
self
}
pub fn add_inputs(mut self, inputs: impl IntoIterator<Item = (Dbc, AmountSecrets)>) -> Self {
self.inputs.extend(inputs);
self
}
pub fn add_output(mut self, output: Output) -> Self {
self.outputs.push(output);
self
}
pub fn add_outputs(mut self, outputs: impl IntoIterator<Item = Output>) -> Self {
self.outputs.extend(outputs);
self
}
pub fn input_owners(&self) -> BTreeSet<PublicKey> {
BTreeSet::from_iter(self.inputs.keys().map(Dbc::owner))
}
pub fn input_spend_keys(&self) -> BTreeSet<SpendKey> {
BTreeSet::from_iter(self.inputs.keys().map(Dbc::spend_key))
}
pub fn inputs_amount_sum(&self) -> Amount {
self.inputs.iter().map(|(_, s)| s.amount).sum()
}
pub fn outputs_amount_sum(&self) -> Amount {
self.outputs.iter().map(|o| o.amount).sum()
}
pub fn build(self) -> Result<ReissueTransaction> {
let parents = BTreeSet::from_iter(self.inputs.keys().map(Dbc::spend_key));
let inputs_bf_sum = self
.inputs
.values()
.map(|amount_secrets| amount_secrets.blinding_factor)
.sum();
let mut outputs_bf_sum: Scalar = Default::default();
let outputs_and_owners = self
.outputs
.iter()
.enumerate()
.map(|(out_idx, output)| {
let blinding_factor = DbcContent::calc_blinding_factor(
out_idx == self.outputs.len() - 1,
inputs_bf_sum,
outputs_bf_sum,
);
outputs_bf_sum += blinding_factor;
let dbc_content = DbcContent::new(
parents.clone(),
output.amount,
output.owner,
blinding_factor,
)?;
Ok((dbc_content, output.owner))
})
.collect::<Result<Vec<_>>>()?;
let inputs = HashSet::from_iter(self.inputs.into_keys());
let outputs = HashSet::from_iter(outputs_and_owners.into_iter().map(|(o, _)| o));
Ok(ReissueTransaction { inputs, outputs })
}
}
#[derive(Debug)]
pub struct ReissueRequestBuilder {
pub reissue_transaction: ReissueTransaction,
pub spent_proof_shares: BTreeMap<SpendKey, HashSet<SpentProofShare>>,
}
impl ReissueRequestBuilder {
pub fn new(reissue_transaction: ReissueTransaction) -> Self {
Self {
reissue_transaction,
spent_proof_shares: Default::default(),
}
}
pub fn add_spent_proof_share(mut self, spend_key: SpendKey, share: SpentProofShare) -> Self {
let shares = self.spent_proof_shares.entry(spend_key).or_default();
shares.insert(share);
self
}
pub fn build(&self) -> Result<ReissueRequest> {
let spent_proofs: BTreeMap<SpendKey, SpentProof> = self
.spent_proof_shares
.iter()
.map(|(spend_key, shares)| {
let any_share = shares
.iter()
.next()
.ok_or(Error::ReissueRequestMissingSpentProofShare(*spend_key))?;
if shares
.iter()
.map(SpentProofShare::spentbook_pks)
.any(|pks| pks != any_share.spentbook_pks())
{
return Err(Error::ReissueRequestPublicKeySetMismatch);
}
let spent_sig = any_share.spent_sig.clone();
let spentbook_pub_key = any_share.spentbook_public_key();
let spentbook_sig = any_share.spentbook_pks.combine_signatures(
shares
.iter()
.map(SpentProofShare::spentbook_sig_share)
.map(NodeSignature::threshold_crypto),
)?;
let spent_proof = SpentProof {
spent_sig,
spentbook_pub_key,
spentbook_sig,
};
Ok((*spend_key, spent_proof))
})
.collect::<Result<_>>()?;
let transaction = self.reissue_transaction.clone();
let rr = ReissueRequest {
transaction,
spent_proofs,
};
Ok(rr)
}
}
#[derive(Debug)]
pub struct DbcBuilder {
pub reissue_transaction: ReissueTransaction,
pub reissue_shares: Vec<ReissueShare>,
}
impl DbcBuilder {
pub fn new(reissue_transaction: ReissueTransaction) -> Self {
Self {
reissue_transaction,
reissue_shares: Default::default(),
}
}
pub fn add_reissue_share(mut self, reissue_share: ReissueShare) -> Self {
self.reissue_shares.push(reissue_share);
self
}
pub fn add_reissue_shares(mut self, shares: impl IntoIterator<Item = ReissueShare>) -> Self {
self.reissue_shares.extend(shares);
self
}
pub fn build(self) -> Result<Vec<Dbc>> {
if self.reissue_shares.is_empty() {
return Err(Error::NoReissueShares);
}
let mut mint_sig_shares: Vec<NodeSignature> = Default::default();
let mut pk_set: HashSet<PublicKeySet> = Default::default();
for rs in self.reissue_shares.iter() {
let mut node_shares: Vec<NodeSignature> = rs
.mint_node_signatures
.iter()
.map(|e| e.1 .1.clone())
.collect();
mint_sig_shares.append(&mut node_shares);
let pub_key_sets: HashSet<PublicKeySet> = rs
.mint_node_signatures
.iter()
.map(|e| e.1 .0.clone())
.collect();
pk_set = &pk_set | &pub_key_sets;
if self.reissue_transaction.blinded() != rs.dbc_transaction {
return Err(Error::ReissueShareDbcTransactionMismatch);
}
if rs.mint_node_signatures.len() != self.reissue_transaction.inputs.len() {
return Err(Error::ReissueShareMintNodeSignaturesLenMismatch);
}
for input in self.reissue_transaction.inputs.iter() {
if rs.mint_node_signatures.get(&input.spend_key()).is_none() {
return Err(Error::ReissueShareMintNodeSignatureNotFoundForInput);
}
}
}
if pk_set.len() != 1 {
return Err(Error::ReissueSharePublicKeySetMismatch);
}
let mint_public_key_set = match pk_set.iter().next() {
Some(pks) => pks,
None => return Err(Error::ReissueSharePublicKeySetMismatch),
};
let mint_sig_shares_ref: Vec<(u64, &SignatureShare)> = mint_sig_shares
.iter()
.map(|e| e.threshold_crypto())
.collect();
let dbc_transaction = &self.reissue_shares[0].dbc_transaction;
let mint_sig = mint_public_key_set.combine_signatures(mint_sig_shares_ref)?;
let mut output_dbcs: Vec<Dbc> = self
.reissue_transaction
.outputs
.iter()
.map(|content| Dbc {
content: content.clone(),
transaction: dbc_transaction.clone(),
transaction_sigs: self
.reissue_transaction
.inputs
.iter()
.map(|input| {
(
input.spend_key(),
(mint_public_key_set.public_key(), mint_sig.clone()),
)
})
.collect(),
})
.collect();
output_dbcs.sort_by_key(Dbc::owner);
Ok(output_dbcs)
}
}