/
transaction_initializer.rs
1137 lines (1059 loc) · 47.5 KB
/
transaction_initializer.rs
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019. The Tari Project
//
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
// following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
// disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
// following disclaimer in the documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
use std::{
collections::HashMap,
fmt::{Debug, Error, Formatter},
};
use log::*;
use rand::rngs::OsRng;
use tari_common_types::{
transaction::TxId,
types::{BlindingFactor, Commitment, HashOutput, PrivateKey, PublicKey},
};
use tari_crypto::{
commitment::HomomorphicCommitmentFactory,
keys::{PublicKey as PublicKeyTrait, SecretKey},
tari_utilities::fixed_set::FixedSet,
};
use tari_script::{ExecutionStack, TariScript};
use crate::{
borsh::SerializedSize,
consensus::ConsensusConstants,
covenants::Covenant,
transactions::{
crypto_factories::CryptoFactories,
fee::Fee,
tari_amount::*,
transaction_components::{
EncryptedValue,
OutputFeatures,
TransactionInput,
TransactionOutput,
TransactionOutputVersion,
UnblindedOutput,
MAX_TRANSACTION_INPUTS,
MAX_TRANSACTION_OUTPUTS,
},
transaction_protocol::{
recipient::RecipientInfo,
sender::{calculate_tx_id, RawTransactionInfo, SenderState, SenderTransactionProtocol},
KernelFeatures,
RewindData,
TransactionMetadata,
},
},
};
pub const LOG_TARGET: &str = "c::tx::tx_protocol::tx_initializer";
/// The SenderTransactionProtocolBuilder is a Builder that helps set up the initial state for the Sender party of a new
/// transaction Typically you don't instantiate this object directly. Rather use
/// ```ignore
/// # use crate::SenderTransactionProtocol;
/// SenderTransactionProtocol::new(1);
/// ```
/// which returns an instance of this builder. Once all the sender's information has been added via the builder
/// methods, you can call `build()` which will return a
#[derive(Debug, Clone)]
pub struct SenderTransactionInitializer {
num_recipients: usize,
amounts: FixedSet<MicroTari>,
lock_height: Option<u64>,
fee_per_gram: Option<MicroTari>,
inputs: Vec<TransactionInput>,
unblinded_inputs: Vec<UnblindedOutput>,
sender_custom_outputs: Vec<UnblindedOutput>,
sender_offset_private_keys: Vec<PrivateKey>,
change_secret: Option<BlindingFactor>,
change_script: Option<TariScript>,
change_input_data: Option<ExecutionStack>,
change_script_private_key: Option<PrivateKey>,
change_sender_offset_private_key: Option<PrivateKey>,
change_covenant: Covenant,
rewind_data: Option<RewindData>,
offset: Option<BlindingFactor>,
excess_blinding_factor: BlindingFactor,
private_nonce: Option<PrivateKey>,
message: Option<String>,
prevent_fee_gt_amount: bool,
recipient_output_features: FixedSet<OutputFeatures>,
recipient_scripts: FixedSet<TariScript>,
recipient_sender_offset_private_keys: FixedSet<PrivateKey>,
recipient_covenants: FixedSet<Covenant>,
recipient_minimum_value_promise: FixedSet<MicroTari>,
private_commitment_nonces: FixedSet<PrivateKey>,
tx_id: Option<TxId>,
kernel_features: KernelFeatures,
burn_commitment: Option<Commitment>,
fee: Fee,
}
pub struct BuildError {
pub builder: SenderTransactionInitializer,
pub message: String,
}
impl Debug for BuildError {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
f.write_str(&self.message)
}
}
impl SenderTransactionInitializer {
pub fn new(num_recipients: usize, consensus_constants: &ConsensusConstants) -> Self {
Self {
fee: Fee::new(*consensus_constants.transaction_weight()),
num_recipients,
amounts: FixedSet::new(num_recipients),
lock_height: None,
fee_per_gram: None,
inputs: Vec::new(),
unblinded_inputs: Vec::new(),
sender_custom_outputs: Vec::new(),
sender_offset_private_keys: vec![],
change_secret: None,
change_script: None,
change_input_data: None,
change_script_private_key: None,
change_sender_offset_private_key: None,
change_covenant: Covenant::default(),
rewind_data: None,
offset: None,
private_nonce: None,
excess_blinding_factor: BlindingFactor::default(),
message: None,
prevent_fee_gt_amount: true,
recipient_output_features: FixedSet::new(num_recipients),
recipient_scripts: FixedSet::new(num_recipients),
recipient_sender_offset_private_keys: FixedSet::new(num_recipients),
recipient_covenants: FixedSet::new(num_recipients),
recipient_minimum_value_promise: FixedSet::new(num_recipients),
private_commitment_nonces: FixedSet::new(num_recipients),
kernel_features: KernelFeatures::empty(),
burn_commitment: None,
tx_id: None,
}
}
/// Set the fee per weight for the transaction. See (Fee::calculate)[Struct.Fee.html#calculate] for how the
/// absolute fee is calculated from the fee-per-gram value.
pub fn with_fee_per_gram(&mut self, fee_per_gram: MicroTari) -> &mut Self {
self.fee_per_gram = Some(fee_per_gram);
self
}
/// Set the amount to pay to the ith recipient. This method will silently fail if `receiver_index` >= num_receivers.
pub fn with_amount(&mut self, receiver_index: usize, amount: MicroTari) -> &mut Self {
self.amounts.set_item(receiver_index, amount);
self
}
/// Set the spending script of the ith recipient's output, a script offset will be generated for this recipient at
/// the same time. This method will silently fail if `receiver_index` >= num_receivers.
pub fn with_recipient_data(
&mut self,
receiver_index: usize,
script: TariScript,
recipient_sender_offset_private_key: PrivateKey,
recipient_output_features: OutputFeatures,
private_commitment_nonce: PrivateKey,
covenant: Covenant,
minimum_value_promise: MicroTari,
) -> &mut Self {
self.recipient_output_features
.set_item(receiver_index, recipient_output_features);
self.recipient_scripts.set_item(receiver_index, script);
self.recipient_sender_offset_private_keys
.set_item(receiver_index, recipient_sender_offset_private_key);
self.private_commitment_nonces
.set_item(receiver_index, private_commitment_nonce);
self.recipient_covenants.set_item(receiver_index, covenant);
self.recipient_minimum_value_promise
.set_item(receiver_index, minimum_value_promise);
self
}
/// Sets the minimum block height that this transaction will be mined.
pub fn with_lock_height(&mut self, lock_height: u64) -> &mut Self {
self.lock_height = Some(lock_height);
self
}
/// Manually sets the offset value. If this is not called, a random offset will be used when `build()` is called.
pub fn with_offset(&mut self, offset: BlindingFactor) -> &mut Self {
self.offset = Some(offset);
self
}
/// Adds an input to the transaction. The sender must provide the blinding factor that was used when the input
/// was first set as an output. We don't check that the input and commitments match at this point.
pub fn with_input(&mut self, utxo: TransactionInput, input: UnblindedOutput) -> &mut Self {
self.inputs.push(utxo);
self.excess_blinding_factor = &self.excess_blinding_factor - &input.spending_key;
self.unblinded_inputs.push(input);
self
}
/// As the Sender adds an output to the transaction. Because we are adding this output as the sender a
/// sender_offset_private_key needs to be provided with the output. This can be called multiple times
pub fn with_output(
&mut self,
output: UnblindedOutput,
sender_offset_private_key: PrivateKey,
) -> Result<&mut Self, BuildError> {
self.excess_blinding_factor = &self.excess_blinding_factor + &output.spending_key;
self.sender_custom_outputs.push(output);
self.sender_offset_private_keys.push(sender_offset_private_key);
Ok(self)
}
/// Provide a blinding factor for the change output. The amount of change will automatically be calculated when
/// the transaction is built.
pub fn with_change_secret(&mut self, blinding_factor: BlindingFactor) -> &mut Self {
self.change_secret = Some(blinding_factor);
self
}
/// Provide the script data that will be used to spend the change output
pub fn with_change_script(
&mut self,
script: TariScript,
input_data: ExecutionStack,
script_private_key: PrivateKey,
) -> &mut Self {
self.change_script = Some(script);
self.change_input_data = Some(input_data);
self.change_script_private_key = Some(script_private_key);
self
}
/// Provide the rewind data required for outputs (change and manually added sender outputs) to be rewindable.
pub fn with_rewindable_outputs(&mut self, rewind_data: RewindData) -> &mut Self {
self.rewind_data = Some(rewind_data);
self
}
/// Provide the private nonce that will be used for the sender's partial signature for the transaction.
pub fn with_private_nonce(&mut self, nonce: PrivateKey) -> &mut Self {
self.private_nonce = Some(nonce);
self
}
/// Provide a text message for receiver
pub fn with_message(&mut self, message: String) -> &mut Self {
self.message = Some(message);
self
}
/// This will select the desired kernel features to be signed by the receiver
pub fn with_kernel_features(&mut self, features: KernelFeatures) -> &mut Self {
self.kernel_features = features;
self
}
/// This will allow the receipient to sign the burn commitment
pub fn with_burn_commitment(&mut self, commitment: Option<Commitment>) -> &mut Self {
self.burn_commitment = commitment;
self
}
/// Enable or disable spending of an amount less than the fee
pub fn with_prevent_fee_gt_amount(&mut self, prevent_fee_gt_amount: bool) -> &mut Self {
self.prevent_fee_gt_amount = prevent_fee_gt_amount;
self
}
fn get_total_metadata_size_for_outputs(&self) -> usize {
let mut size = 0;
size += self
.sender_custom_outputs
.iter()
.map(|o| self.fee.weighting().round_up_metadata_size(o.metadata_byte_size()))
.sum::<usize>();
size += self
.recipient_scripts
.iter()
.filter_map(|script| {
script.map(|s| {
self.fee.weighting().round_up_metadata_size(
self.get_recipient_output_features().get_serialized_size() + s.get_serialized_size(),
)
})
})
.sum::<usize>();
size
}
fn get_recipient_output_features(&self) -> OutputFeatures {
Default::default()
}
/// Tries to make a change output with the given transaction parameters and add it to the set of outputs. The total
/// fee, including the additional change output (if any) is returned along with the amount of change.
/// The change output **always has default output features**.
fn add_change_if_required(
&mut self,
factories: &CryptoFactories,
) -> Result<(MicroTari, MicroTari, Option<UnblindedOutput>), String> {
// The number of outputs excluding a possible residual change output
let num_outputs = self.sender_custom_outputs.len() + self.num_recipients;
let num_inputs = self.inputs.len();
let total_being_spent = self.unblinded_inputs.iter().map(|i| i.value).sum::<MicroTari>();
let total_to_self = self.sender_custom_outputs.iter().map(|o| o.value).sum::<MicroTari>();
let total_amount = self.amounts.sum().ok_or("Not all amounts have been provided")?;
let fee_per_gram = self.fee_per_gram.ok_or("Fee per gram was not provided")?;
// We require scripts for each recipient to be set to calculate the fee
if !self.recipient_scripts.is_full() {
return Err(format!(
"{} recipient script(s) are required",
self.recipient_scripts.size()
));
}
let metadata_size_without_change = self.get_total_metadata_size_for_outputs();
let fee_without_change =
self.fee()
.calculate(fee_per_gram, 1, num_inputs, num_outputs, metadata_size_without_change);
let output_features = self.get_recipient_output_features();
let change_metadata_size = self
.change_script
.as_ref()
.map(|script| script.get_serialized_size())
.unwrap_or(0) +
output_features.get_serialized_size();
let change_metadata_size = self.fee().weighting().round_up_metadata_size(change_metadata_size);
let change_fee = self.fee().calculate(fee_per_gram, 0, 0, 1, change_metadata_size);
// Subtract with a check on going negative
let total_input_value = total_to_self + total_amount + fee_without_change;
let change_amount = total_being_spent.checked_sub(total_input_value);
match change_amount {
None => Err(format!(
"You are spending ({}) more than you're providing ({}).",
total_input_value, total_being_spent
)),
Some(MicroTari(0)) => Ok((fee_without_change, MicroTari(0), None)),
Some(v) => {
let change_amount = v.checked_sub(change_fee);
let change_sender_offset_private_key = PrivateKey::random(&mut OsRng);
self.change_sender_offset_private_key = Some(change_sender_offset_private_key.clone());
match change_amount {
// You can't win. Just add the change to the fee (which is less than the cost of adding another
// output and go without a change output
None => Ok((fee_without_change + v, MicroTari(0), None)),
Some(MicroTari(0)) => Ok((fee_without_change + v, MicroTari(0), None)),
Some(v) => {
let change_script = self
.change_script
.as_ref()
.ok_or("Change script was not provided")?
.clone();
let change_key = self
.change_secret
.as_ref()
.ok_or("Change spending key was not provided")?;
let commitment = factories.commitment.commit_value(&change_key.clone(), v.as_u64());
let encrypted_value = self
.rewind_data
.as_ref()
.map(|rd| EncryptedValue::encrypt_value(&rd.encryption_key, &commitment, v))
.transpose()
.map_err(|e| e.to_string())?
.unwrap_or_default();
let minimum_value_promise = MicroTari::zero();
let metadata_signature = TransactionOutput::create_metadata_signature(
TransactionOutputVersion::get_current_version(),
v,
change_key,
&change_script,
&output_features,
&change_sender_offset_private_key,
&self.change_covenant,
&encrypted_value,
minimum_value_promise,
)
.map_err(|e| e.to_string())?;
let change_unblinded_output = UnblindedOutput::new_current_version(
v,
change_key.clone(),
output_features,
change_script,
self.change_input_data
.as_ref()
.ok_or("Change script was not provided")?
.clone(),
self.change_script_private_key
.as_ref()
.ok_or("Change script private key was not provided")?
.clone(),
PublicKey::from_secret_key(&change_sender_offset_private_key),
metadata_signature,
0,
self.change_covenant.clone(),
encrypted_value,
minimum_value_promise,
);
Ok((fee_without_change + change_fee, v, Some(change_unblinded_output)))
},
}
},
}
}
/// Specify the tx_id of this transaction, if not provided it will be calculated on build
pub fn with_tx_id(&mut self, tx_id: TxId) -> &mut Self {
self.tx_id = Some(tx_id);
self
}
fn check_value<T>(name: &str, val: &Option<T>, vec: &mut Vec<String>) {
if val.is_none() {
vec.push(name.to_string());
}
}
fn build_err<T>(self, msg: &str) -> Result<T, BuildError> {
Err(BuildError {
builder: self,
message: msg.to_string(),
})
}
fn calculate_amount_to_others(&self) -> MicroTari {
self.amounts.clone().into_vec().iter().sum()
}
pub(super) fn fee(&self) -> &Fee {
&self.fee
}
/// Construct a `SenderTransactionProtocol` instance in and appropriate state. The data stored
/// in the struct is _moved_ into the new struct. If any data is missing, the `self` instance is returned in the
/// error (so that you can continue building) along with a string listing the missing fields.
/// If all the input data is present, but one or more fields are invalid, the function will return a
/// `SenderTransactionProtocol` instance in the Failed state.
#[allow(clippy::too_many_lines)]
pub fn build(
mut self,
factories: &CryptoFactories,
prev_header: Option<HashOutput>,
height: u64,
) -> Result<SenderTransactionProtocol, BuildError> {
// Compile a list of all data that is missing
let mut message = Vec::new();
Self::check_value("Missing Lock Height", &self.lock_height, &mut message);
Self::check_value("Missing Fee per gram", &self.fee_per_gram, &mut message);
Self::check_value("Missing Offset", &self.offset, &mut message);
Self::check_value("Change script", &self.private_nonce, &mut message);
Self::check_value("Change input data", &self.private_nonce, &mut message);
Self::check_value("Change script private key", &self.private_nonce, &mut message);
if !message.is_empty() {
return self.build_err(&message.join(","));
}
if !self.amounts.is_full() {
let size = self.amounts.size();
return self.build_err(&*format!("Missing all {} amounts", size));
}
if !self.recipient_sender_offset_private_keys.is_full() {
let size = self.recipient_sender_offset_private_keys.size();
return self.build_err(&*format!("Missing {} recipient script offset private key/s", size));
}
if !self.private_commitment_nonces.is_full() {
let size = self.private_commitment_nonces.size();
return self.build_err(&*format!("Missing {} private commitment nonce/s", size));
}
if !self.recipient_scripts.is_full() {
let size = self.recipient_scripts.size();
return self.build_err(&*format!("Missing all {} recipient scripts", size));
}
if self.inputs.is_empty() {
return self.build_err("A transaction cannot have zero inputs");
}
// Prevent overflow attacks by imposing sane limits on inputs
if self.inputs.len() > MAX_TRANSACTION_INPUTS {
return self.build_err("Too many inputs in transaction");
}
// Calculate the fee based on whether we need to add a residual change output or not
let (total_fee, change, change_output) = match self.add_change_if_required(factories) {
Ok((fee, change, output)) => (fee, change, output),
Err(e) => return self.build_err(&e),
};
debug!(
target: LOG_TARGET,
"Build transaction with Fee: {}. Change: {}. Output: {:?}", total_fee, change, change_output,
);
// Some checks on the fee
if total_fee < Fee::MINIMUM_TRANSACTION_FEE {
return self.build_err("Fee is less than the minimum");
}
// Create transaction outputs
let mut outputs = match self
.sender_custom_outputs
.iter()
.map(|o| {
if let Some(rewind_data) = self.rewind_data.as_ref() {
o.as_rewindable_transaction_output(factories, rewind_data, None)
} else {
o.as_transaction_output(factories)
}
})
.collect::<Result<Vec<TransactionOutput>, _>>()
{
Ok(o) => o,
Err(e) => {
return self.build_err(&e.to_string());
},
};
if let Some(change_unblinded_output) = change_output.clone() {
let change_output_sender_offset_private_key = match self.change_sender_offset_private_key {
None => return self.build_err("A change output script offset was not provided"),
Some(ref pk) => pk.clone(),
};
self.excess_blinding_factor = self.excess_blinding_factor + change_unblinded_output.spending_key.clone();
// If rewind data is present we produce a rewindable output, else a standard output
let change_output = if let Some(rewind_data) = self.rewind_data.as_ref() {
match change_unblinded_output.as_rewindable_transaction_output(factories, rewind_data, None) {
Ok(o) => o,
Err(e) => {
return self.build_err(e.to_string().as_str());
},
}
} else {
match change_unblinded_output.as_transaction_output(factories) {
Ok(o) => o,
Err(e) => {
return self.build_err(e.to_string().as_str());
},
}
};
self.sender_custom_outputs.push(change_unblinded_output);
self.sender_offset_private_keys
.push(change_output_sender_offset_private_key);
outputs.push(change_output);
}
// Prevent overflow attacks by imposing sane limits on outputs
if outputs.len() > MAX_TRANSACTION_OUTPUTS {
return self.build_err("Too many outputs in transaction");
}
// Calculate the Inputs portion of Gamma so we don't have to store the individual script private keys in
// RawTransactionInfo while we wait for the recipients reply
let mut gamma = PrivateKey::default();
for uo in &self.unblinded_inputs {
gamma = gamma + uo.script_private_key.clone();
}
if outputs.len() != self.sender_offset_private_keys.len() {
return self
.build_err("There should be the same number of sender added outputs as script offset private keys");
}
for sender_offset_private_key in &self.sender_offset_private_keys {
gamma = gamma - sender_offset_private_key.clone();
}
let nonce = self.private_nonce.clone().unwrap();
let public_nonce = PublicKey::from_secret_key(&nonce);
let offset = self.offset.clone().unwrap();
let excess_blinding_factor = self.excess_blinding_factor.clone();
let offset_blinding_factor = &excess_blinding_factor - &offset;
let excess = PublicKey::from_secret_key(&offset_blinding_factor);
let amount_to_self = self
.sender_custom_outputs
.iter()
.fold(MicroTari::from(0), |sum, o| sum + o.value);
let recipient_info = match self.num_recipients {
0 => RecipientInfo::None,
1 => RecipientInfo::Single(None),
_ => RecipientInfo::Multiple(HashMap::new()),
};
let tx_id = match self.tx_id {
Some(id) => id,
None => calculate_tx_id(&public_nonce, 0),
};
let recipient_output_features = self.recipient_output_features.clone().into_vec();
// The fee should be less than the amount being sent. This isn't a protocol requirement, but it's what you want
// 99.999% of the time, however, always preventing this will also prevent spending dust in some edge
// cases.
// Don't care about the fees when we are sending token.
if self.amounts.size() > 0 && total_fee > self.calculate_amount_to_others() {
warn!(
target: LOG_TARGET,
"Fee ({}) is greater than amount ({}) being sent for Transaction (TxId: {}).",
total_fee,
self.calculate_amount_to_others(),
tx_id
);
if self.prevent_fee_gt_amount {
return self.build_err("Fee is greater than amount");
}
}
let change_output_metadata_signature = change_output.as_ref().map(|v| v.metadata_signature.clone());
// Everything is here. Let's send some Tari!
let sender_info = RawTransactionInfo {
num_recipients: self.num_recipients,
amount_to_self,
tx_id,
amounts: self.amounts.into_vec(),
recipient_output_features,
recipient_scripts: self.recipient_scripts.into_vec(),
recipient_sender_offset_private_keys: self.recipient_sender_offset_private_keys.into_vec(),
recipient_covenants: self.recipient_covenants.into_vec(),
recipient_minimum_value_promise: self.recipient_minimum_value_promise.into_vec(),
private_commitment_nonces: self.private_commitment_nonces.into_vec(),
change,
unblinded_change_output: change_output,
change_output_metadata_signature,
change_sender_offset_public_key: self
.change_sender_offset_private_key
.map(|pk| PublicKey::from_secret_key(&pk)),
metadata: TransactionMetadata {
fee: total_fee,
lock_height: self.lock_height.unwrap(),
kernel_features: self.kernel_features,
burn_commitment: self.burn_commitment.clone(),
},
inputs: self.inputs,
outputs,
offset,
offset_blinding_factor,
gamma,
public_excess: excess,
private_nonce: nonce,
public_nonce: public_nonce.clone(),
public_nonce_sum: public_nonce,
recipient_info,
signatures: Vec::new(),
message: self.message.unwrap_or_default(),
prev_header,
height,
};
let state = SenderState::Initializing(Box::new(sender_info));
let state = state
.initialize()
.expect("It should be possible to call initialize from Initializing state");
Ok(state.into())
}
}
//---------------------------------------- Tests ----------------------------------------------------//
#[cfg(test)]
mod test {
use rand::rngs::OsRng;
use tari_common_types::types::PrivateKey;
use tari_crypto::keys::SecretKey;
use tari_script::{script, ExecutionStack, TariScript};
use crate::{
covenants::Covenant,
test_helpers::create_consensus_constants,
transactions::{
crypto_factories::CryptoFactories,
fee::Fee,
tari_amount::*,
test_helpers::{create_test_input, create_unblinded_output, TestParams, UtxoTestParams},
transaction_components::{OutputFeatures, MAX_TRANSACTION_INPUTS},
transaction_protocol::{
sender::SenderState,
transaction_initializer::SenderTransactionInitializer,
TransactionProtocolError,
},
},
};
/// One input, 2 outputs
#[test]
fn no_receivers() {
// Create some inputs
let factories = CryptoFactories::default();
let p = TestParams::new();
// Start the builder
let builder = SenderTransactionInitializer::new(0, &create_consensus_constants(0));
let err = builder.build(&factories, None, u64::MAX).unwrap_err();
let script = script!(Nop);
// We should have a bunch of fields missing still, but we can recover and continue
assert_eq!(
err.message,
"Missing Lock Height,Missing Fee per gram,Missing Offset,Change script,Change input data,Change script \
private key"
);
let mut builder = err.builder;
builder
.with_lock_height(100)
.with_offset(p.offset.clone())
.with_private_nonce(p.nonce.clone());
builder
.with_output(
create_unblinded_output(script.clone(), OutputFeatures::default(), &p, MicroTari(100)),
PrivateKey::random(&mut OsRng),
)
.unwrap();
let (utxo, input) = TestParams::new().create_input(UtxoTestParams {
value: MicroTari(5_000),
..Default::default()
});
builder.with_input(utxo, input);
builder
.with_fee_per_gram(MicroTari(20))
.with_recipient_data(
0,
script.clone(),
PrivateKey::random(&mut OsRng),
Default::default(),
PrivateKey::random(&mut OsRng),
Covenant::default(),
MicroTari::zero(),
)
.with_change_script(script, ExecutionStack::default(), PrivateKey::default());
let expected_fee = builder
.fee()
.calculate(MicroTari(20), 1, 1, 2, p.get_size_for_default_metadata(2));
// We needed a change input, so this should fail
let err = builder.build(&factories, None, u64::MAX).unwrap_err();
assert_eq!(err.message, "Change spending key was not provided");
// Ok, give them a change output
let mut builder = err.builder;
builder.with_change_secret(p.change_spend_key);
let result = builder.build(&factories, None, u64::MAX).unwrap();
// Peek inside and check the results
if let SenderState::Finalizing(info) = result.into_state() {
assert_eq!(info.num_recipients, 0, "Number of receivers");
assert_eq!(info.signatures.len(), 0, "Number of signatures");
assert_eq!(info.amounts.len(), 0, "Number of external payment amounts");
assert_eq!(info.metadata.lock_height, 100, "Lock height");
assert_eq!(info.metadata.fee, expected_fee, "Fee");
assert_eq!(info.outputs.len(), 2, "There should be 2 outputs");
assert_eq!(info.inputs.len(), 1, "There should be 1 input");
} else {
panic!("There were no recipients, so we should be finalizing");
}
}
/// One output, one input
#[test]
fn no_change_or_receivers() {
// Create some inputs
let factories = CryptoFactories::default();
let p = TestParams::new();
let (utxo, input) = create_test_input(MicroTari(5000), 0, &factories.commitment);
let constants = create_consensus_constants(0);
let expected_fee = Fee::from(*constants.transaction_weight()).calculate(
MicroTari(4),
1,
1,
1,
p.get_size_for_default_metadata(1),
);
let output = create_unblinded_output(
TariScript::default(),
OutputFeatures::default(),
&p,
MicroTari(5000) - expected_fee,
);
// Start the builder
let mut builder = SenderTransactionInitializer::new(0, &constants);
builder
.with_lock_height(0)
.with_offset(p.offset)
.with_private_nonce(p.nonce)
.with_output(output, p.sender_offset_private_key)
.unwrap()
.with_input(utxo, input)
.with_fee_per_gram(MicroTari(4))
.with_prevent_fee_gt_amount(false);
let result = builder.build(&factories, None, u64::MAX).unwrap();
// Peek inside and check the results
if let SenderState::Finalizing(info) = result.into_state() {
assert_eq!(info.num_recipients, 0, "Number of receivers");
assert_eq!(info.signatures.len(), 0, "Number of signatures");
assert_eq!(info.amounts.len(), 0, "Number of external payment amounts");
assert_eq!(info.metadata.lock_height, 0, "Lock height");
assert_eq!(info.metadata.fee, expected_fee, "Fee");
assert_eq!(info.outputs.len(), 1, "There should be 1 output");
assert_eq!(info.inputs.len(), 1, "There should be 1 input");
} else {
panic!("There were no recipients, so we should be finalizing");
}
}
/// Hit the edge case where our change isn't enough to cover the cost of an extra output
#[test]
#[allow(clippy::identity_op)]
fn change_edge_case() {
// Create some inputs
let factories = CryptoFactories::default();
let p = TestParams::new();
let constants = create_consensus_constants(0);
let weighting = constants.transaction_weight();
let tx_fee = Fee::new(*weighting).calculate(1.into(), 1, 1, 1, 0);
let fee_for_change_output = weighting.params().output_weight * uT;
// fee == 340, output = 80
// outputs weight: 1060, kernel weight: 10, input weight: 9, output weight: 53,
// Pay out so that I should get change, but not enough to pay for the output
let (utxo, input) = create_test_input(
// one under the amount required to pay the fee for a change output
2000 * uT + tx_fee + fee_for_change_output - 1 * uT,
0,
&factories.commitment,
);
let output = p.create_unblinded_output(UtxoTestParams {
value: 2000 * uT,
..Default::default()
});
// Start the builder
let mut builder = SenderTransactionInitializer::new(0, &constants);
builder
.with_lock_height(0)
.with_offset(p.offset)
.with_private_nonce(p.nonce)
.with_output(output, p.sender_offset_private_key)
.unwrap()
.with_input(utxo, input)
.with_fee_per_gram(MicroTari(1))
.with_prevent_fee_gt_amount(false);
let result = builder.build(&factories, None, u64::MAX).unwrap();
// Peek inside and check the results
if let SenderState::Finalizing(info) = result.into_state() {
assert_eq!(info.num_recipients, 0, "Number of receivers");
assert_eq!(info.signatures.len(), 0, "Number of signatures");
assert_eq!(info.amounts.len(), 0, "Number of external payment amounts");
assert_eq!(info.metadata.lock_height, 0, "Lock height");
assert_eq!(info.metadata.fee, tx_fee + fee_for_change_output - 1 * uT, "Fee");
assert_eq!(info.outputs.len(), 1, "There should be 1 output");
assert_eq!(info.inputs.len(), 1, "There should be 1 input");
} else {
panic!("There were no recipients, so we should be finalizing");
}
}
#[test]
fn too_many_inputs() {
// Create some inputs
let factories = CryptoFactories::default();
let p = TestParams::new();
let output = create_unblinded_output(TariScript::default(), OutputFeatures::default(), &p, MicroTari(500));
let constants = create_consensus_constants(0);
// Start the builder
let mut builder = SenderTransactionInitializer::new(0, &constants);
builder
.with_lock_height(0)
.with_offset(p.offset)
.with_private_nonce(p.nonce)
.with_output(output, p.sender_offset_private_key)
.unwrap()
.with_fee_per_gram(MicroTari(2));
for _ in 0..=MAX_TRANSACTION_INPUTS {
let (utxo, input) = create_test_input(MicroTari(50), 0, &factories.commitment);
builder.with_input(utxo, input);
}
let err = builder.build(&factories, None, u64::MAX).unwrap_err();
assert_eq!(err.message, "Too many inputs in transaction");
}
#[test]
fn fee_too_low() {
// Create some inputs
let factories = CryptoFactories::default();
let p = TestParams::new();
let tx_fee = p
.fee()
.calculate(MicroTari(1), 1, 1, 1, p.get_size_for_default_metadata(1));
let (utxo, input) = create_test_input(500 * uT + tx_fee, 0, &factories.commitment);
let script = script!(Nop);
let output = create_unblinded_output(script.clone(), OutputFeatures::default(), &p, MicroTari(500));
// Start the builder
let constants = create_consensus_constants(0);
let mut builder = SenderTransactionInitializer::new(0, &constants);
builder
.with_lock_height(0)
.with_offset(p.offset)
.with_private_nonce(p.nonce)
.with_input(utxo, input)
.with_output(output, p.sender_offset_private_key)
.unwrap()
.with_change_secret(p.change_spend_key)
.with_fee_per_gram(MicroTari(1))
.with_recipient_data(
0,
script,
PrivateKey::random(&mut OsRng),
Default::default(),
PrivateKey::random(&mut OsRng),
Covenant::default(),
MicroTari::zero(),
);
// .with_change_script(script, ExecutionStack::default(), PrivateKey::default());
let err = builder.build(&factories, None, u64::MAX).unwrap_err();
assert_eq!(err.message, "Fee is less than the minimum");
}
#[test]
fn not_enough_funds() {
// Create some inputs
let factories = CryptoFactories::default();
let p = TestParams::new();
let (utxo, input) = create_test_input(MicroTari(400), 0, &factories.commitment);
let script = script!(Nop);
let output = create_unblinded_output(script.clone(), OutputFeatures::default(), &p, MicroTari(400));
// Start the builder
let constants = create_consensus_constants(0);
let mut builder = SenderTransactionInitializer::new(0, &constants);
builder
.with_lock_height(0)
.with_offset(p.offset)
.with_private_nonce(p.nonce)
.with_input(utxo, input)
.with_output(output, p.sender_offset_private_key.clone())
.unwrap()
.with_change_secret(p.change_spend_key)
.with_fee_per_gram(MicroTari(1))
.with_recipient_data(
0,
script.clone(),
PrivateKey::random(&mut OsRng),
Default::default(),
PrivateKey::random(&mut OsRng),
Covenant::default(),
MicroTari::zero(),
)
.with_change_script(script, ExecutionStack::default(), PrivateKey::default());
let err = builder.build(&factories, None, u64::MAX).unwrap_err();
assert_eq!(
err.message,
"You are spending (473 µT) more than you're providing (400 µT)."
);
}
#[test]
fn multi_recipients() {
// Create some inputs
let factories = CryptoFactories::default();
let p = TestParams::new();
let (utxo, input) = create_test_input(MicroTari(100_000), 0, &factories.commitment);
let script = script!(Nop);
let output = create_unblinded_output(script.clone(), OutputFeatures::default(), &p, MicroTari(15000));
// Start the builder
let constants = create_consensus_constants(0);
let mut builder = SenderTransactionInitializer::new(2, &constants);
builder
.with_lock_height(0)
.with_offset(p.offset)
.with_amount(0, MicroTari(1200))
.with_amount(1, MicroTari(1100))
.with_private_nonce(p.nonce)
.with_input(utxo, input)
.with_output(output, p.sender_offset_private_key.clone())
.unwrap()
.with_change_secret(p.change_spend_key)
.with_fee_per_gram(MicroTari(4))
.with_recipient_data(
0,
script.clone(),
PrivateKey::random(&mut OsRng),