soroban_cli/commands/tx/
encode.rs

1use clap::ValueEnum;
2use std::ffi::OsString;
3use stellar_xdr::{cli::Channel, curr::TypeVariant};
4
5#[derive(thiserror::Error, Debug)]
6pub enum Error {
7    #[error(transparent)]
8    Cli(#[from] stellar_xdr::cli::encode::Error),
9}
10
11/// Encode a transaction envelope from JSON to XDR
12#[derive(Debug, clap::Parser, Clone, Default)]
13pub struct Cmd {
14    /// XDR or files containing XDR to decode, or stdin if empty
15    #[arg()]
16    pub input: Vec<OsString>,
17    // Input format
18    #[arg(long = "input", value_enum, default_value_t)]
19    pub input_format: InputFormat,
20    // Output format
21    #[arg(long = "output", value_enum, default_value_t)]
22    pub output_format: OutputFormat,
23}
24
25#[derive(Default, Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)]
26pub enum InputFormat {
27    #[default]
28    Json,
29}
30
31impl From<InputFormat> for stellar_xdr::cli::encode::InputFormat {
32    fn from(v: InputFormat) -> Self {
33        match v {
34            InputFormat::Json => Self::Json,
35        }
36    }
37}
38
39#[derive(Default, Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)]
40pub enum OutputFormat {
41    #[default]
42    SingleBase64,
43    Single,
44}
45
46impl From<OutputFormat> for stellar_xdr::cli::encode::OutputFormat {
47    fn from(v: OutputFormat) -> Self {
48        match v {
49            OutputFormat::SingleBase64 => Self::SingleBase64,
50            OutputFormat::Single => Self::Single,
51        }
52    }
53}
54
55impl Cmd {
56    pub fn run(&self) -> Result<(), Error> {
57        let cmd = stellar_xdr::cli::encode::Cmd {
58            input: self.input.clone(),
59            r#type: TypeVariant::TransactionEnvelope.name().to_string(),
60            input_format: self.input_format.into(),
61            output_format: self.output_format.into(),
62        };
63        cmd.run(&Channel::Curr)?;
64        Ok(())
65    }
66}