Skip to content

Commit

Permalink
add tx hash command (#1430)
Browse files Browse the repository at this point in the history
* add tx hash command

* docs

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

more detailed doc

Co-authored-by: Willem Wyndham <[email protected]>

* docs update

* fix: use network only

---------

Co-authored-by: Willem Wyndham <[email protected]>
  • Loading branch information
BlaineHeffron and willemneal authored Jul 23, 2024
1 parent 3fbf979 commit 2b19b7a
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
15 changes: 15 additions & 0 deletions FULL_HELP_DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1197,6 +1197,7 @@ Sign, Simulate, and Send transactions
###### **Subcommands:**

* `simulate` — Simulate a transaction envelope from stdin
* `hash` — Calculate the hash of a transaction envelope from stdin



Expand All @@ -1218,6 +1219,20 @@ Simulate a transaction envelope from stdin



## `stellar tx hash`

Calculate the hash of a transaction envelope from stdin

**Usage:** `stellar tx hash [OPTIONS]`

###### **Options:**

* `--rpc-url <RPC_URL>` — RPC server endpoint
* `--network-passphrase <NETWORK_PASSPHRASE>` — Network passphrase to sign the transaction sent to the rpc server
* `--network <NETWORK>` — Name of network to use from config



## `stellar cache`

Cache for transactions and contract specs
Expand Down
19 changes: 19 additions & 0 deletions cmd/crates/soroban-test/tests/it/integration/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,22 @@ async fn txn_simulate() {
assembled_str
);
}

#[tokio::test]
async fn txn_hash() {
let sandbox = &TestEnv::new();

let xdr_base64 = "AAAAAgAAAACVk/0xt9tV/cUbF53iwQ3tkKLlq9zG2wV5qd9lRjZjlQAHt/sAFsKTAAAABAAAAAEAAAAAAAAAAAAAAABmOg6nAAAAAAAAAAEAAAAAAAAAGAAAAAAAAAABfcHs35M1GZ/JkY2+DHMs4dEUaqjynMnDYK/Gp0eulN8AAAAIdHJhbnNmZXIAAAADAAAAEgAAAAEFO1FR2Wg49QFY5KPOFAQ0bV5fN+7LD2GSQvOaHSH44QAAABIAAAAAAAAAA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hbmNlAAAAABIAAAABBTtRUdloOPUBWOSjzhQENG1eXzfuyw9hkkLzmh0h+OEAAAABAAAAAAAAAACVk/0xt9tV/cUbF53iwQ3tkKLlq9zG2wV5qd9lRjZjlQAAAAYAAAABBTtRUdloOPUBWOSjzhQENG1eXzfuyw9hkkLzmh0h+OEAAAAVAAABj1LItJUAAAAAAEyTowAAGMgAAAG4AAAAAAADJBsAAAABRjZjlQAAAEASFnAIzNqpfdzv6yT0rSLMUDFgt7a/inCHurNCG55Jp8Imho04qRH+JNdkq0BgMC7yAJqH4N6Y2iGflFt3Lp4L";

let expected_hash = "bcc9fa60c8f6607c981d6e1c65d77ae07617720113f9080fe5883d8e4a331a68";

let hash = sandbox
.new_assert_cmd("tx")
.arg("hash")
.write_stdin(xdr_base64.as_bytes())
.assert()
.success()
.stdout_as_str();

assert_eq!(hash.trim(), expected_hash);
}
33 changes: 33 additions & 0 deletions cmd/soroban-cli/src/commands/tx/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::{commands::global, utils::transaction_hash};
use hex;

#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error(transparent)]
TxEnvelopeFromStdin(#[from] super::xdr::Error),
#[error(transparent)]
XdrToBase64(#[from] soroban_env_host::xdr::Error),
#[error(transparent)]
Config(#[from] super::super::network::Error),
}

// Command to return the transaction hash submitted to a network
/// e.g. `cat file.txt | soroban tx hash`
#[derive(Debug, clap::Parser, Clone, Default)]
#[group(skip)]
pub struct Cmd {
#[clap(flatten)]
pub network: super::super::network::Args,
}

impl Cmd {
pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
let tx = super::xdr::unwrap_envelope_v1(super::xdr::tx_envelope_from_stdin()?)?;
let network = &self.network.get(&global_args.locator)?;
println!(
"{}",
hex::encode(transaction_hash(&tx, &network.network_passphrase)?)
);
Ok(())
}
}
7 changes: 7 additions & 0 deletions cmd/soroban-cli/src/commands/tx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,33 @@ use clap::Parser;

use super::global;

pub mod hash;
pub mod simulate;
pub mod xdr;

#[derive(Debug, Parser)]
pub enum Cmd {
/// Simulate a transaction envelope from stdin
Simulate(simulate::Cmd),
/// Calculate the hash of a transaction envelope from stdin
Hash(hash::Cmd),
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
/// An error during the simulation
#[error(transparent)]
Simulate(#[from] simulate::Error),
/// An error during hash calculation
#[error(transparent)]
Hash(#[from] hash::Error),
}

impl Cmd {
pub async fn run(&self, global_args: &global::Args) -> Result<(), Error> {
match self {
Cmd::Simulate(cmd) => cmd.run(global_args).await?,
Cmd::Hash(cmd) => cmd.run(global_args)?,
};
Ok(())
}
Expand Down

0 comments on commit 2b19b7a

Please sign in to comment.