Skip to main content

hiero_sdk/
transaction_hash.rs

1// SPDX-License-Identifier: Apache-2.0
2
3use std::fmt::{
4    self,
5    Debug,
6    Display,
7    Formatter,
8};
9
10use sha2::{
11    Digest,
12    Sha384,
13};
14
15/// The client-generated SHA-384 hash of a transaction that was submitted.
16///
17/// This can be used to lookup the transaction in an explorer.
18#[derive(Copy, Clone, Hash)]
19pub struct TransactionHash(pub [u8; 48]);
20
21impl TransactionHash {
22    #[must_use]
23    pub(crate) fn new(bytes: &[u8]) -> Self {
24        Self(Sha384::digest(bytes).into())
25    }
26}
27
28impl Debug for TransactionHash {
29    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
30        write!(f, "\"{self}\"")
31    }
32}
33
34impl Display for TransactionHash {
35    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
36        f.pad(&hex::encode(self.0))
37    }
38}