forked from fedimint/fedimint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.rs
57 lines (50 loc) · 1.42 KB
/
transaction.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
use bitcoin::KeyPair;
use minimint_api::Amount;
use minimint_core::config::FeeConsensus;
use minimint_core::transaction::{Input, Output, Transaction};
use rand::{CryptoRng, RngCore};
use bitcoin::secp256k1::{All, Secp256k1};
pub struct TransactionBuilder {
keys: Vec<KeyPair>,
pub tx: Transaction,
}
impl Default for TransactionBuilder {
fn default() -> Self {
TransactionBuilder {
keys: vec![],
tx: Transaction {
inputs: vec![],
outputs: vec![],
signature: None,
},
}
}
}
impl TransactionBuilder {
pub fn input(&mut self, key: &mut Vec<KeyPair>, input: Input) {
self.keys.append(key);
self.tx.inputs.push(input);
}
pub fn output(&mut self, output: Output) {
self.tx.outputs.push(output);
}
pub fn change_required(&self, fees: &FeeConsensus) -> Amount {
self.tx.in_amount() - self.tx.out_amount() - self.tx.fee_amount(fees)
}
pub fn build<R: RngCore + CryptoRng>(
mut self,
secp: &Secp256k1<All>,
mut rng: R,
) -> Transaction {
if !self.keys.is_empty() {
let signature = minimint_core::transaction::agg_sign(
&self.keys,
self.tx.tx_hash().as_hash(),
secp,
&mut rng,
);
self.tx.signature = Some(signature);
}
self.tx
}
}