Skip to content

Commit 5d66803

Browse files
Add tx decode/encode subcommands (#1785)
1 parent eb0e31e commit 5d66803

4 files changed

Lines changed: 201 additions & 0 deletions

File tree

FULL_HELP_DOCS.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,8 @@ Sign, Simulate, and Send transactions
16081608
* `sign` — Sign a transaction envelope appending the signature to the envelope
16091609
* `simulate` — Simulate a transaction envelope from stdin
16101610
* `fetch` — Fetch a transaction from the network by hash If no subcommand is passed in, the transaction envelope will be returned
1611+
* `decode` — Decode a transaction envelope from XDR to JSON
1612+
* `encode` — Encode a transaction envelope from JSON to XDR
16111613

16121614

16131615

@@ -3023,6 +3025,60 @@ Fetch the transaction fee information
30233025

30243026

30253027

3028+
## `stellar tx decode`
3029+
3030+
Decode a transaction envelope from XDR to JSON
3031+
3032+
**Usage:** `stellar tx decode [OPTIONS] [INPUT]...`
3033+
3034+
###### **Arguments:**
3035+
3036+
* `<INPUT>` — XDR or files containing XDR to decode, or stdin if empty
3037+
3038+
###### **Options:**
3039+
3040+
* `--input <INPUT_FORMAT>`
3041+
3042+
Default value: `single-base64`
3043+
3044+
Possible values: `single-base64`, `single`
3045+
3046+
* `--output <OUTPUT_FORMAT>`
3047+
3048+
Default value: `json`
3049+
3050+
Possible values: `json`, `json-formatted`
3051+
3052+
3053+
3054+
3055+
## `stellar tx encode`
3056+
3057+
Encode a transaction envelope from JSON to XDR
3058+
3059+
**Usage:** `stellar tx encode [OPTIONS] [INPUT]...`
3060+
3061+
###### **Arguments:**
3062+
3063+
* `<INPUT>` — XDR or files containing XDR to decode, or stdin if empty
3064+
3065+
###### **Options:**
3066+
3067+
* `--input <INPUT_FORMAT>`
3068+
3069+
Default value: `json`
3070+
3071+
Possible values: `json`
3072+
3073+
* `--output <OUTPUT_FORMAT>`
3074+
3075+
Default value: `single-base64`
3076+
3077+
Possible values: `single-base64`, `single`
3078+
3079+
3080+
3081+
30263082
## `stellar xdr`
30273083

30283084
Decode and encode XDR
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
use clap::ValueEnum;
2+
use std::ffi::OsString;
3+
use stellar_xdr::{cli::Channel, curr::TypeVariant};
4+
5+
#[derive(thiserror::Error, Debug)]
6+
pub enum Error {
7+
#[error(transparent)]
8+
Cli(#[from] stellar_xdr::cli::decode::Error),
9+
}
10+
11+
/// Decode a transaction envelope from XDR to JSON
12+
#[derive(Debug, clap::Parser, Clone, Default)]
13+
pub 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)]
26+
pub enum InputFormat {
27+
#[default]
28+
SingleBase64,
29+
Single,
30+
}
31+
32+
impl From<InputFormat> for stellar_xdr::cli::decode::InputFormat {
33+
fn from(v: InputFormat) -> Self {
34+
match v {
35+
InputFormat::SingleBase64 => Self::SingleBase64,
36+
InputFormat::Single => Self::Single,
37+
}
38+
}
39+
}
40+
41+
#[derive(Default, Clone, Copy, Debug, Eq, Hash, PartialEq, ValueEnum)]
42+
pub enum OutputFormat {
43+
#[default]
44+
Json,
45+
JsonFormatted,
46+
}
47+
48+
impl From<OutputFormat> for stellar_xdr::cli::decode::OutputFormat {
49+
fn from(v: OutputFormat) -> Self {
50+
match v {
51+
OutputFormat::Json => Self::Json,
52+
OutputFormat::JsonFormatted => Self::JsonFormatted,
53+
}
54+
}
55+
}
56+
57+
impl Cmd {
58+
pub fn run(&self) -> Result<(), Error> {
59+
let cmd = stellar_xdr::cli::decode::Cmd {
60+
input: self.input.clone(),
61+
r#type: TypeVariant::TransactionEnvelope.name().to_string(),
62+
input_format: self.input_format.into(),
63+
output_format: self.output_format.into(),
64+
};
65+
cmd.run(&Channel::Curr)?;
66+
Ok(())
67+
}
68+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
use clap::ValueEnum;
2+
use std::ffi::OsString;
3+
use stellar_xdr::{cli::Channel, curr::TypeVariant};
4+
5+
#[derive(thiserror::Error, Debug)]
6+
pub 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)]
13+
pub 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)]
26+
pub enum InputFormat {
27+
#[default]
28+
Json,
29+
}
30+
31+
impl 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)]
40+
pub enum OutputFormat {
41+
#[default]
42+
SingleBase64,
43+
Single,
44+
}
45+
46+
impl 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+
55+
impl 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+
}

cmd/soroban-cli/src/commands/tx/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use super::global;
22

33
pub mod args;
4+
5+
pub mod decode;
46
pub mod edit;
7+
pub mod encode;
58
pub mod fetch;
69
pub mod hash;
710
pub mod help;
@@ -49,6 +52,8 @@ pub enum Cmd {
4952
/// Fetch a transaction from the network by hash
5053
/// If no subcommand is passed in, the transaction envelope will be returned
5154
Fetch(fetch::Cmd),
55+
Decode(decode::Cmd),
56+
Encode(encode::Cmd),
5257
}
5358

5459
#[derive(thiserror::Error, Debug)]
@@ -73,6 +78,10 @@ pub enum Error {
7378
Update(#[from] update::Error),
7479
#[error(transparent)]
7580
Fetch(#[from] fetch::Error),
81+
#[error(transparent)]
82+
Decode(#[from] decode::Error),
83+
#[error(transparent)]
84+
Encode(#[from] encode::Error),
7685
}
7786

7887
impl Cmd {
@@ -87,6 +96,8 @@ impl Cmd {
8796
Cmd::Simulate(cmd) => cmd.run(global_args).await?,
8897
Cmd::Update(cmd) => cmd.run(global_args).await?,
8998
Cmd::Fetch(cmd) => cmd.run(global_args).await?,
99+
Cmd::Decode(cmd) => cmd.run()?,
100+
Cmd::Encode(cmd) => cmd.run()?,
90101
}
91102
Ok(())
92103
}

0 commit comments

Comments
 (0)