Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tx decode/encode subcommands #1785

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions cmd/soroban-cli/src/commands/tx/decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use clap::ValueEnum;
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 from XDR to JSON
#[derive(Debug, clap::Parser, Clone, Default)]
pub struct Cmd {
// Output format
#[arg(long, value_enum, default_value_t)]
pub output: OutputFormat,
}

#[derive(Default, Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)]
pub enum OutputFormat {
#[default]
Json,
JsonFormatted,
}

impl From<OutputFormat> for stellar_xdr::cli::decode::OutputFormat {
fn from(v: OutputFormat) -> Self {
match v {
OutputFormat::Json => Self::Json,
OutputFormat::JsonFormatted => Self::JsonFormatted,
}
}
}

impl Cmd {
pub fn run(&self) -> Result<(), Error> {
let cmd = stellar_xdr::cli::decode::Cmd {
files: vec![],
r#type: TypeVariant::TransactionEnvelope.to_string(),
input: InputFormat::SingleBase64,
output: self.output.into(),
};
cmd.run(&Channel::Curr)?;
Ok(())
}
}
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(())
}
}
10 changes: 10 additions & 0 deletions cmd/soroban-cli/src/commands/tx/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use super::global;

pub mod args;
pub mod decode;
pub mod encode;
pub mod hash;
pub mod help;
pub mod new;
Expand Down Expand Up @@ -28,6 +30,8 @@ pub enum Cmd {
Sign(sign::Cmd),
/// Simulate a transaction envelope from stdin
Simulate(simulate::Cmd),
Decode(decode::Cmd),
Encode(encode::Cmd),
}

#[derive(thiserror::Error, Debug)]
Expand All @@ -44,6 +48,10 @@ pub enum Error {
Sign(#[from] sign::Error),
#[error(transparent)]
Simulate(#[from] simulate::Error),
#[error(transparent)]
Decode(#[from] decode::Error),
#[error(transparent)]
Encode(#[from] encode::Error),
}

impl Cmd {
Expand All @@ -55,6 +63,8 @@ impl Cmd {
Cmd::Send(cmd) => cmd.run(global_args).await?,
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
Loading