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
use amount::Stroops;
use time_bounds::TimeBounds;
use memo::Memo;
use network::Network;
use keypair::{KeyPair, PublicKey};
use signature::DecoratedSignature;
use operation::Operation;
use error::Result;
use xdr::ToXdr;
use crypto;
const BASE_FEE: Stroops = Stroops(100);
#[derive(Debug, Clone)]
pub struct Transaction {
pub source: PublicKey,
pub sequence: u64,
pub fee: Stroops,
pub time_bounds: Option<TimeBounds>,
pub memo: Memo,
pub operations: Vec<Operation>,
}
impl Transaction {
pub fn new(
source: PublicKey,
sequence: u64,
time_bounds: Option<TimeBounds>,
memo: Memo,
operations: Vec<Operation>,
) -> Transaction {
let fee = BASE_FEE * operations.len();
Transaction {
source,
sequence,
fee,
time_bounds,
memo,
operations,
}
}
pub fn source(&self) -> &PublicKey {
&self.source
}
pub fn base_fee(&self) -> &Stroops {
&self.fee
}
pub fn time_bounds(&self) -> &Option<TimeBounds> {
&self.time_bounds
}
pub fn memo(&self) -> &Memo {
&self.memo
}
pub fn sequence(&self) -> u64 {
self.sequence
}
pub fn operations(&self) -> &Vec<Operation> {
&self.operations
}
pub fn sign(self, keypair: &KeyPair, network: &Network) -> Result<SignedTransaction> {
let mut sig = SignedTransaction::new(self, network)?;
sig.sign(keypair)?;
Ok(sig)
}
}
#[derive(Debug, Clone)]
pub struct SignedTransaction {
network_id: Vec<u8>,
transaction: Transaction,
signatures: Vec<DecoratedSignature>,
}
impl SignedTransaction {
pub fn new(transaction: Transaction, network: &Network) -> Result<SignedTransaction> {
Ok(SignedTransaction {
network_id: network.network_id().clone(),
transaction,
signatures: Vec::new(),
})
}
pub fn new_without_network(
transaction: Transaction,
signatures: Vec<DecoratedSignature>,
) -> SignedTransaction {
SignedTransaction {
network_id: Vec::new(),
transaction,
signatures,
}
}
pub fn sign(&mut self, keypair: &KeyPair) -> Result<()> {
let payload = self.hash()?;
let new_signature = keypair.sign_decorated(&payload);
self.signatures.push(new_signature);
Ok(())
}
pub fn hash(&self) -> Result<Vec<u8>> {
let payload = self.signature_base()?;
Ok(crypto::hash(&payload))
}
pub fn signature_base(&self) -> Result<Vec<u8>> {
let sig_payload = TransactionSignaturePayload {
network_id: &self.network_id,
transaction: &self.transaction,
};
let mut payload = Vec::new();
sig_payload.to_writer(&mut payload)?;
Ok(payload)
}
pub fn transaction(&self) -> &Transaction {
&self.transaction
}
pub fn signatures(&self) -> &Vec<DecoratedSignature> {
&self.signatures
}
}
#[derive(Debug)]
pub struct TransactionSignaturePayload<'a> {
pub network_id: &'a Vec<u8>,
pub transaction: &'a Transaction,
}