From 483d4376270f8011f244b6a2307e3e609cb0cad6 Mon Sep 17 00:00:00 2001 From: Blaine Heffron Date: Wed, 3 Jul 2024 18:32:00 -0400 Subject: [PATCH] add tx hash command --- .../soroban-test/tests/it/integration/tx.rs | 19 +++++++++++ cmd/soroban-cli/src/commands/tx/hash.rs | 33 +++++++++++++++++++ cmd/soroban-cli/src/commands/tx/mod.rs | 7 ++++ 3 files changed, 59 insertions(+) create mode 100644 cmd/soroban-cli/src/commands/tx/hash.rs diff --git a/cmd/crates/soroban-test/tests/it/integration/tx.rs b/cmd/crates/soroban-test/tests/it/integration/tx.rs index 9f5204a8db..bcb880b18c 100644 --- a/cmd/crates/soroban-test/tests/it/integration/tx.rs +++ b/cmd/crates/soroban-test/tests/it/integration/tx.rs @@ -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+7LD2GSQvOaHSH44QAAABIAAAAAAAAAAJWT/TG321X9xRsXneLBDe2QouWr3MbbBXmp32VGNmOVAAAACgAAAAAAAAAAAAAAADuaygAAAAABAAAAAQAAAAEFO1FR2Wg49QFY5KPOFAQ0bV5fN+7LD2GSQvOaHSH44QAAAY9SyLSVABbC/QAAABEAAAABAAAAAwAAAA8AAAASYXV0aGVudGljYXRvcl9kYXRhAAAAAAANAAAAJUmWDeWIDoxodDQXD2R2YFuP5K65ooYyx5lc87qDHZdjHQAAAAAAAAAAAAAPAAAAEGNsaWVudF9kYXRhX2pzb24AAAANAAAAcnsidHlwZSI6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iGflFt3Lp4L"; + + 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); +} diff --git a/cmd/soroban-cli/src/commands/tx/hash.rs b/cmd/soroban-cli/src/commands/tx/hash.rs new file mode 100644 index 0000000000..f8d0d7a5ac --- /dev/null +++ b/cmd/soroban-cli/src/commands/tx/hash.rs @@ -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::config::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 config: super::super::config::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.config.get_network()?; + println!( + "{}", + hex::encode(transaction_hash(&tx, &network.network_passphrase)?) + ); + Ok(()) + } +} diff --git a/cmd/soroban-cli/src/commands/tx/mod.rs b/cmd/soroban-cli/src/commands/tx/mod.rs index 48161e3c76..59f07228ad 100644 --- a/cmd/soroban-cli/src/commands/tx/mod.rs +++ b/cmd/soroban-cli/src/commands/tx/mod.rs @@ -2,6 +2,7 @@ use clap::Parser; use super::global; +pub mod hash; pub mod simulate; pub mod xdr; @@ -9,6 +10,8 @@ pub mod xdr; 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)] @@ -16,12 +19,16 @@ 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(()) }