soroban_cli/commands/tx/
hash.rs

1use hex;
2use std::ffi::OsString;
3
4use crate::{commands::global, config::network, utils::transaction_hash};
5
6#[derive(thiserror::Error, Debug)]
7pub enum Error {
8    #[error(transparent)]
9    TxEnvelopeFromStdin(#[from] super::xdr::Error),
10    #[error(transparent)]
11    XdrToBase64(#[from] crate::xdr::Error),
12    #[error(transparent)]
13    Config(#[from] network::Error),
14}
15
16// Command to return the transaction hash submitted to a network
17/// e.g. `stellar tx hash file.txt` or `cat file.txt | stellar tx hash`
18#[derive(Debug, clap::Parser, Clone, Default)]
19#[group(skip)]
20pub struct Cmd {
21    /// Base-64 transaction envelope XDR or file containing XDR to decode, or stdin if empty
22    #[arg()]
23    pub tx_xdr: Option<OsString>,
24
25    #[clap(flatten)]
26    pub network: network::Args,
27}
28
29impl Cmd {
30    pub fn run(&self, global_args: &global::Args) -> Result<(), Error> {
31        let tx = super::xdr::unwrap_envelope_v1(super::xdr::tx_envelope_from_input(&self.tx_xdr)?)?;
32        let network = &self.network.get(&global_args.locator)?;
33        println!(
34            "{}",
35            hex::encode(transaction_hash(&tx, &network.network_passphrase)?)
36        );
37        Ok(())
38    }
39}