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
use crate::cardano_cli::wrapper::cli::command;
use crate::cardano_cli::wrapper::Error;
use jortestkit::prelude::ProcessOutput;
use snapshot_trigger_service::config::NetworkType;
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
use tracing::debug;
use uuid::Uuid;

pub struct Transaction {
    transaction_command: command::Transaction,
}

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

    pub fn sign<P: AsRef<Path>, Q: AsRef<Path>>(
        self,
        tx_raw: P,
        payment_skey_file: P,
        output_file: Q,
    ) -> Result<ExitStatus, Error> {
        let mut command = self
            .transaction_command
            .sign()
            .tx_body_file(tx_raw)
            .signing_key_file(payment_skey_file)
            .out_file(output_file)
            .build();
        let output = command.output().map_err(|e| Error::Io(e.to_string()))?;

        std::io::stdout()
            .write_all(&output.stdout)
            .map_err(|e| Error::Io(e.to_string()))?;
        std::io::stderr()
            .write_all(&output.stderr)
            .map_err(|e| Error::Io(e.to_string()))?;
        Ok(output.status)
    }

    pub fn submit_from_bytes<P: AsRef<Path>>(
        self,
        raw: &[u8],
        working_dir: P,
    ) -> Result<String, Error> {
        let id = Uuid::new_v4();
        let mut tx_signed_file = working_dir.as_ref().to_path_buf();
        tx_signed_file.push(id.to_string());
        tx_signed_file.push("tx.signed");

        std::fs::create_dir_all(
            tx_signed_file
                .parent()
                .ok_or_else(|| Error::CannotGetParentDirectory(tx_signed_file.clone()))?,
        )
        .map_err(|x| Error::CannotCreateParentDirectory(x.to_string()))?;

        let mut file =
            File::create(&tx_signed_file).map_err(|x| Error::CannotCreateAFile(x.to_string()))?;
        file.write_all(raw)
            .map_err(|x| Error::CannotWriteAFile(x.to_string()))?;

        self.submit(&tx_signed_file)
    }

    pub fn submit<P: AsRef<Path>>(self, tx_signed: P) -> Result<String, Error> {
        let mut command = self.transaction_command.submit().tx_file(tx_signed).build();
        let output = command
            .output()
            .map_err(|_| Error::CannotGetOutputFromCommand(format!("{command:?}")))?;

        debug!("status: {}", output.status);
        std::io::stdout()
            .write_all(&output.stdout)
            .map_err(|e| Error::Io(e.to_string()))?;
        std::io::stderr()
            .write_all(&output.stderr)
            .map_err(|e| Error::Io(e.to_string()))?;

        Ok(output.as_lossy_string())
    }

    pub fn id<P: AsRef<Path>>(self, tx_signed: P) -> Result<String, Error> {
        let mut command = self.transaction_command.id().tx_file(tx_signed).build();
        let output = command
            .output()
            .map_err(|_| Error::CannotGetOutputFromCommand(format!("{command:?}")))?;

        println!("status: {}", output.status);
        std::io::stdout()
            .write_all(&output.stdout)
            .map_err(|e| Error::Io(e.to_string()))?;
        std::io::stderr()
            .write_all(&output.stderr)
            .map_err(|e| Error::Io(e.to_string()))?;
        Ok(output.as_lossy_string())
    }

    #[allow(clippy::too_many_arguments)]
    pub fn build(
        self,
        network: NetworkType,
        tx_in: String,
        change_address: String,
        certificate_file: PathBuf,
        protocol_params_file: PathBuf,
        out_file: PathBuf,
        witness_override: u32,
    ) -> Result<ExitStatus, Error> {
        let mut command = self
            .transaction_command
            .build()
            .network(network)
            .tx_in(tx_in)
            .change_address(change_address)
            .certificate_file(certificate_file)
            .protocol_params_file(protocol_params_file)
            .out_file(out_file)
            .witness_override(witness_override)
            .build();

        let output = command
            .output()
            .map_err(|_| Error::CannotGetOutputFromCommand(format!("{command:?}")))?;

        std::io::stdout()
            .write_all(&output.stdout)
            .map_err(|e| Error::Io(e.to_string()))?;
        std::io::stderr()
            .write_all(&output.stderr)
            .map_err(|e| Error::Io(e.to_string()))?;

        Ok(output.status)
    }
}