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
use crate::{
    jcli::{command::TransactionCommand, data::Witness, WitnessData},
    testing::process::ProcessOutput,
};
use assert_cmd::assert::OutputAssertExt;
use assert_fs::TempDir;
use chain_core::{packer::Codec, property::DeserializeFromSlice};
use chain_impl_mockchain::{fee::LinearFee, fragment::Fragment};
use jormungandr_lib::{
    crypto::hash::Hash,
    interfaces::{BlockDate, LegacyUTxO, UTxOInfo, Value},
};
use jortestkit::process::output_extensions::ProcessOutput as _;
use std::path::Path;

pub struct Transaction {
    command: TransactionCommand,
}

impl Transaction {
    pub fn new(command: TransactionCommand) -> Self {
        Self { command }
    }

    pub fn new_transaction<P: AsRef<Path>>(self, staging_file: P) {
        self.command
            .new_transaction(staging_file)
            .build()
            .assert()
            .success();
    }

    pub fn add_input<P: AsRef<Path>>(
        self,
        tx_id: &Hash,
        tx_index: u8,
        amount: &str,
        staging_file: P,
    ) {
        self.command
            .add_input(&tx_id.to_hex(), tx_index, amount, staging_file)
            .build()
            .assert()
            .success();
    }

    pub fn add_input_expect_fail<P: AsRef<Path>>(
        self,
        tx_id: &Hash,
        tx_index: u8,
        amount: &str,
        staging_file: P,
        expected_part: &str,
    ) {
        self.command
            .add_input(&tx_id.to_hex(), tx_index, amount, staging_file)
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_part));
    }

    pub fn add_input_from_utxo_with_value<P: AsRef<Path>>(
        self,
        utxo: &UTxOInfo,
        amount: Value,
        staging_file: P,
    ) {
        self.add_input(
            utxo.transaction_id(),
            utxo.index_in_transaction(),
            &amount.to_string(),
            staging_file,
        )
    }

    pub fn add_input_from_utxo<P: AsRef<Path>>(self, utxo: &UTxOInfo, staging_file: P) {
        self.add_input(
            utxo.transaction_id(),
            utxo.index_in_transaction(),
            &utxo.associated_fund().to_string(),
            staging_file,
        );
    }

    pub fn add_certificate<S: Into<String>, P: AsRef<Path>>(self, certificate: S, staging_file: P) {
        self.command
            .add_certificate(certificate, staging_file)
            .build()
            .assert()
            .success();
    }

    pub fn add_account<P: AsRef<Path>>(self, account_addr: &str, amount: &str, staging_file: P) {
        self.command
            .add_account(account_addr, amount, staging_file)
            .build()
            .assert()
            .success();
    }

    pub fn add_account_expect_fail<P: AsRef<Path>>(
        self,
        account_addr: &str,
        amount: &str,
        staging_file: P,
        expected_msg: &str,
    ) {
        self.command
            .add_account(account_addr, amount, staging_file)
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_msg));
    }

    pub fn add_account_from_legacy<P: AsRef<Path>>(self, fund: &LegacyUTxO, staging_file: P) {
        self.add_account(
            &fund.address.to_string(),
            &fund.value.to_string(),
            staging_file,
        )
    }

    pub fn add_output<P: AsRef<Path>>(self, addr: &str, amount: Value, staging_file: P) {
        self.command
            .add_output(addr, &amount.to_string(), staging_file)
            .build()
            .assert()
            .success();
    }

    pub fn finalize<P: AsRef<Path>>(self, staging_file: P) {
        self.command
            .finalize(staging_file)
            .build()
            .assert()
            .success();
    }

    pub fn finalize_with_fee<P: AsRef<Path>>(
        self,
        address: &str,
        linear_fee: &LinearFee,
        staging_file: P,
    ) {
        self.command
            .finalize_with_fee(address, linear_fee, staging_file)
            .build()
            .assert()
            .success();
    }

    pub fn finalize_expect_fail<P: AsRef<Path>>(self, staging_file: P, expected_part: &str) {
        self.command
            .finalize(staging_file)
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_part));
    }

    pub fn auth<P: AsRef<Path>, Q: AsRef<Path>>(self, key: P, staging_file: Q) {
        self.command
            .auth(key, staging_file)
            .build()
            .assert()
            .success();
    }

    pub fn make_witness(self, witness: &Witness) {
        self.command
            .make_witness(
                &witness.block_hash.to_hex(),
                &witness.transaction_id.to_hex(),
                witness.addr_type,
                witness.account_spending_counter,
                &witness.file,
                &witness.private_key_path,
            )
            .build()
            .assert()
            .success();
    }

    pub fn make_witness_expect_fail(self, witness: &Witness, expected_msg: &str) {
        self.command
            .make_witness(
                &witness.block_hash.to_hex(),
                &witness.transaction_id.to_hex(),
                witness.addr_type,
                witness.account_spending_counter,
                &witness.file,
                &witness.private_key_path,
            )
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_msg));
    }

    pub fn create_witness<P: AsRef<Path>>(
        self,
        staging_dir: &TempDir,
        genesis_hash: Hash,
        witness_data: WitnessData,
        staging_file: P,
    ) -> Witness {
        let transaction_id = self.id(staging_file);
        witness_data.into_witness(staging_dir, &genesis_hash, &transaction_id)
    }

    pub fn add_witness_expect_fail<P: AsRef<Path>>(
        self,
        witness: &Witness,
        staging_file: P,
        expected_part: &str,
    ) {
        self.command
            .add_witness(&witness.file, staging_file)
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_part));
    }

    pub fn add_witness<P: AsRef<Path>>(self, witness: &Witness, staging_file: P) {
        self.command
            .add_witness(&witness.file, staging_file)
            .build()
            .assert()
            .success();
    }

    pub fn seal<P: AsRef<Path>>(self, staging_file: P) {
        self.command.seal(staging_file).build().assert().success();
    }

    #[allow(clippy::too_many_arguments)]
    pub fn make_transaction(
        self,
        host: String,
        sender: jormungandr_lib::interfaces::Address,
        receiver: Option<jormungandr_lib::interfaces::Address>,
        value: jormungandr_lib::interfaces::Value,
        block0_hash: String,
        valid_until: BlockDate,
        secret: impl AsRef<Path>,
        staging_file: impl AsRef<Path>,
        post: bool,
    ) {
        self.command
            .make_transaction(
                host,
                sender,
                receiver,
                value,
                block0_hash,
                valid_until,
                secret,
                staging_file,
                post,
            )
            .build()
            .assert()
            .success();
    }

    pub fn convert_to_message<P: AsRef<Path>>(self, staging_file: P) -> String {
        self.command
            .to_message(staging_file)
            .build()
            .assert()
            .success()
            .get_output()
            .as_single_line()
    }

    pub fn convert_to_message_expect_fail<P: AsRef<Path>>(
        self,
        staging_file: P,
        expected_msg: &str,
    ) {
        self.command
            .to_message(staging_file)
            .build()
            .assert()
            .failure()
            .stderr(predicates::str::contains(expected_msg));
    }

    pub fn id<P: AsRef<Path>>(self, staging_file: P) -> Hash {
        self.command
            .id(staging_file)
            .build()
            .assert()
            .success()
            .get_output()
            .as_hash()
    }

    pub fn info<P: AsRef<Path>>(self, format: &str, staging_file: P) -> String {
        self.command
            .info(format, staging_file)
            .build()
            .assert()
            .success()
            .get_output()
            .as_single_line()
    }

    pub fn fragment_id<P: AsRef<Path>>(self, staging_file: P) -> Hash {
        let fragment_hex = self.convert_to_message(staging_file);
        let fragment_bytes = hex::decode(fragment_hex).expect("Failed to parse message hex");
        Fragment::deserialize_from_slice(&mut Codec::new(fragment_bytes.as_slice()))
            .expect("Failed to parse message")
            .hash()
            .into()
    }

    pub fn set_expiry_date<P: AsRef<Path>>(self, valid_until: BlockDate, staging_file: P) {
        self.command
            .set_expiry_date(&valid_until.to_string(), staging_file)
            .build()
            .assert()
            .success();
    }
}