Skip to content

Commit

Permalink
add encode too
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch committed Dec 10, 2024
1 parent e46db6d commit 829a0e9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
10 changes: 6 additions & 4 deletions cmd/soroban-cli/src/commands/tx/decode.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use clap::ValueEnum;
use stellar_xdr::cli::{decode::InputFormat, Channel};
use stellar_xdr::{
cli::{decode::InputFormat, Channel},
curr::TypeVariant,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Cli(#[from] stellar_xdr::cli::decode::Error),
}

/// Decode a transaction envelope to JSON
/// Decode a transaction envelope from XDR to JSON
#[derive(Debug, clap::Parser, Clone, Default)]
#[group(skip)]
pub struct Cmd {
// Output format
#[arg(long, value_enum, default_value_t)]
Expand All @@ -36,7 +38,7 @@ impl Cmd {
pub fn run(&self) -> Result<(), Error> {
let cmd = stellar_xdr::cli::decode::Cmd {
files: vec![],
r#type: "TransactionEnvelope".to_string(),
r#type: TypeVariant::TransactionEnvelope.to_string(),
input: InputFormat::SingleBase64,
output: self.output.into(),
};
Expand Down
30 changes: 30 additions & 0 deletions cmd/soroban-cli/src/commands/tx/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use stellar_xdr::{
cli::{
encode::{InputFormat, OutputFormat},
Channel,
},
curr::TypeVariant,
};

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
Cli(#[from] stellar_xdr::cli::encode::Error),
}

/// Encode a transaction envelope from JSON to XDR
#[derive(Debug, clap::Parser, Clone, Default)]
pub struct Cmd;

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
let cmd = stellar_xdr::cli::encode::Cmd {
files: vec![],
r#type: TypeVariant::TransactionEnvelope.to_string(),
input: InputFormat::Json,
output: OutputFormat::SingleBase64,
};
cmd.run(&Channel::Curr)?;
Ok(())
}
}
6 changes: 5 additions & 1 deletion cmd/soroban-cli/src/commands/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod send;
pub mod sign;
pub mod simulate;
pub mod xdr;
pub mod encode;

pub use args::Args;

Expand All @@ -29,8 +30,8 @@ pub enum Cmd {
Sign(sign::Cmd),
/// Simulate a transaction envelope from stdin
Simulate(simulate::Cmd),
/// Decode a transaction envelope to JSON
Decode(decode::Cmd),
Encode(encode::Cmd),
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -49,6 +50,8 @@ pub enum Error {
Simulate(#[from] simulate::Error),
#[error(transparent)]
Decode(#[from] decode::Error),
#[error(transparent)]
Encode(#[from] encode::Error),
}

impl Cmd {
Expand All @@ -61,6 +64,7 @@ impl Cmd {
Cmd::Sign(cmd) => cmd.run(global_args).await?,
Cmd::Simulate(cmd) => cmd.run(global_args).await?,
Cmd::Decode(cmd) => cmd.run()?,
Cmd::Encode(cmd) => cmd.run()?,
};
Ok(())
}
Expand Down

0 comments on commit 829a0e9

Please sign in to comment.