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
use crate::cardano_cli::mock::command::write_to_file_or_println;
use crate::cardano_cli::mock::fake;
use clap::Parser;
use std::fs::File;
use std::io;
use std::io::Write;
use std::path::PathBuf;

#[derive(Parser, Debug)]
pub enum Transaction {
    Id(IdCommand),
    Sign(SignCommand),
    Submit(SubmitCommand),
    Build(BuildCommand),
}

impl Transaction {
    pub fn exec(self) -> Result<(), io::Error> {
        match self {
            Self::Id(_id) => {
                IdCommand::exec();
                Ok(())
            }
            Self::Sign(sign) => sign.exec(),
            Self::Submit(_submit) => {
                SubmitCommand::exec();
                Ok(())
            }
            Self::Build(build) => build.exec(),
        }
    }
}

#[derive(Parser, Debug)]
pub struct IdCommand {
    #[clap(long = "tx-file")]
    pub tx_file: PathBuf,
}

impl IdCommand {
    pub fn exec() {
        println!("{}", fake::hash());
    }
}

#[derive(Parser, Debug)]
pub struct SignCommand {
    #[clap(long = "tx-body-file")]
    pub tx_body_file: PathBuf,

    #[clap(long = "signing-key-file")]
    pub signing_key_file: PathBuf,

    #[clap(long = "testnet-magic")]
    pub testnet_magic: Option<u32>,

    #[clap(long = "mainnet")]
    pub mainnet: bool,

    #[clap(long = "out-file")]
    pub out_file: Option<PathBuf>,
}

impl SignCommand {
    pub fn exec(self) -> Result<(), io::Error> {
        write_to_file_or_println(self.out_file, &fake::sign())
    }
}

#[derive(Parser, Debug)]
pub struct SubmitCommand {
    #[clap(long = "tx-file")]
    pub tx_body_file: PathBuf,

    #[clap(long = "testnet-magic")]
    pub testnet_magic: Option<u32>,

    #[clap(long = "mainnet")]
    pub mainnet: bool,
}

impl SubmitCommand {
    pub fn exec() {
        println!("{}", fake::submit());
    }
}

#[derive(Parser, Debug)]
pub struct BuildCommand {
    #[clap(long = "mainnet")]
    pub mainnet: bool,

    #[clap(long = "testnet-magic")]
    pub testnet_magic: Option<u32>,

    #[clap(long = "tx-in")]
    pub tx_in: String,

    #[clap(long = "change-address")]
    pub change_address: String,

    #[clap(long = "certificate-file")]
    pub certificate_file: PathBuf,

    #[clap(long = "protocol-params-file")]
    pub protocol_params_file: PathBuf,

    #[clap(long = "out-file")]
    pub out_file: PathBuf,

    #[clap(long = "witness-override")]
    pub witness_override: u32,
}

impl BuildCommand {
    pub fn exec(self) -> std::io::Result<()> {
        let mut file = File::create(self.out_file)?;
        file.write_all(fake::transaction().as_bytes())
    }
}