-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: txn commands to sign, simulate, and send txns
- Loading branch information
1 parent
6ce8006
commit 0443ef2
Showing
6 changed files
with
167 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use clap::Parser; | ||
|
||
pub mod send; | ||
pub mod sign; | ||
pub mod simulate; | ||
pub mod xdr; | ||
|
||
use stellar_xdr::cli as xdr_cli; | ||
|
||
#[derive(Debug, Parser)] | ||
pub enum Cmd { | ||
/// Add a new identity (keypair, ledger, macOS keychain) | ||
Inspect(xdr_cli::Root), | ||
/// Given an identity return its address (public key) | ||
Sign(sign::Cmd), | ||
/// Submit a transaction to the network | ||
Send(send::Cmd), | ||
/// Simulate a transaction | ||
Simulate(simulate::Cmd), | ||
} | ||
|
||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
/// An error during the simulation | ||
#[error(transparent)] | ||
Simulate(#[from] simulate::Error), | ||
/// An error during the inspect | ||
#[error(transparent)] | ||
Inspect(#[from] xdr_cli::Error), | ||
/// An error during the sign | ||
#[error(transparent)] | ||
Sign(#[from] sign::Error), | ||
/// An error during the send | ||
#[error(transparent)] | ||
Send(#[from] send::Error), | ||
} | ||
|
||
impl Cmd { | ||
pub async fn run(&self) -> Result<(), Error> { | ||
match self { | ||
Cmd::Inspect(cmd) => cmd.run()?, | ||
Cmd::Sign(cmd) => cmd.run().await?, | ||
Cmd::Send(cmd) => cmd.run().await?, | ||
Cmd::Simulate(cmd) => cmd.run().await?, | ||
}; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
XdrArgs(#[from] super::xdr::Error), | ||
} | ||
|
||
#[derive(Debug, clap::Parser, Clone)] | ||
#[group(skip)] | ||
pub struct Cmd { | ||
#[clap(flatten)] | ||
pub xdr_args: super::xdr::Args, | ||
} | ||
|
||
impl Cmd { | ||
pub async fn run(&self) -> Result<(), Error> { | ||
println!("{:#?}", self.xdr_args.txn()?); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
XdrArgs(#[from] super::xdr::Error), | ||
} | ||
|
||
#[derive(Debug, clap::Parser, Clone)] | ||
#[group(skip)] | ||
pub struct Cmd { | ||
#[clap(flatten)] | ||
pub xdr_args: super::xdr::Args, | ||
} | ||
|
||
impl Cmd { | ||
pub async fn run(&self) -> Result<(), Error> { | ||
println!("{:#?}", self.xdr_args.txn()?); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#[derive(thiserror::Error, Debug)] | ||
pub enum Error { | ||
#[error(transparent)] | ||
XdrArgs(#[from] super::xdr::Error), | ||
} | ||
|
||
#[derive(Debug, clap::Parser, Clone)] | ||
#[group(skip)] | ||
pub struct Cmd { | ||
#[clap(flatten)] | ||
pub xdr_args: super::xdr::Args, | ||
} | ||
|
||
impl Cmd { | ||
pub async fn run(&self) -> Result<(), Error> { | ||
println!("{:#?}", self.xdr_args.txn()?); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use std::{io::stdin, path::PathBuf}; | ||
|
||
use soroban_env_host::xdr::ReadXdr; | ||
use soroban_sdk::xdr::{Limited, Limits, Transaction}; | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum Error { | ||
#[error("failed to decode XDR from base64")] | ||
Base64Decode, | ||
#[error("failed to decode XDR from file: {0}")] | ||
FileDecode(PathBuf), | ||
#[error("failed to decode XDR from stdin")] | ||
StdinDecode, | ||
#[error(transparent)] | ||
Io(#[from] std::io::Error), | ||
} | ||
|
||
/// XDR input, either base64 encoded or file path and stdin if neither is provided | ||
#[derive(Debug, clap::Args, Clone)] | ||
#[group(skip)] | ||
pub struct Args { | ||
/// Base64 encoded XDR transaction | ||
#[arg( | ||
long = "xdr-base64", | ||
env = "STELLAR_TXN_XDR_BASE64", | ||
conflicts_with = "xdr_file" | ||
)] | ||
pub xdr_base64: Option<String>, | ||
//// File containing Binary encoded data | ||
#[arg( | ||
long = "xdr-file", | ||
env = "STELLAR_TXN_XDR_FILE", | ||
conflicts_with = "xdr_base64" | ||
)] | ||
pub xdr_file: Option<PathBuf>, | ||
} | ||
|
||
impl Args { | ||
pub fn xdr<T: ReadXdr>(&self) -> Result<T, Error> { | ||
match (self.xdr_base64.as_ref(), self.xdr_file.as_ref()) { | ||
(Some(xdr_base64), None) => { | ||
T::from_xdr_base64(xdr_base64, Limits::none()).map_err(|_| Error::Base64Decode) | ||
} | ||
(_, Some(xdr_file)) => T::from_xdr(std::fs::read(xdr_file)?, Limits::none()) | ||
.map_err(|_| Error::FileDecode(xdr_file.clone())), | ||
|
||
_ => T::read_xdr_base64_to_end(&mut Limited::new(stdin(), Limits::none())) | ||
.map_err(|_| Error::StdinDecode), | ||
} | ||
} | ||
|
||
pub fn txn(&self) -> Result<Transaction, Error> { | ||
self.xdr::<Transaction>() | ||
} | ||
} |