stellar_xdr/curr/tx_hash.rs
1#![cfg(feature = "std")]
2use super::{
3 FeeBumpTransaction, FeeBumpTransactionEnvelope, FeeBumpTransactionInnerTx, Hash, Limits,
4 Transaction, TransactionEnvelope, TransactionSignaturePayload,
5 TransactionSignaturePayloadTaggedTransaction, TransactionV0, TransactionV0Envelope,
6 TransactionV1Envelope, WriteXdr,
7};
8
9use sha2::{Digest, Sha256};
10
11impl TransactionEnvelope {
12 /// Computes the hash of the transaction envelope.
13 ///
14 /// # Arguments
15 ///
16 /// * `network_id` - The network ID to include in the hash computation.
17 ///
18 /// # Returns
19 ///
20 /// The transaction hash.
21 ///
22 /// # Errors
23 ///
24 /// If there is any issue serializing to XDR.
25 pub fn hash(&self, network_id: [u8; 32]) -> Result<[u8; 32], super::Error> {
26 match self {
27 TransactionEnvelope::TxV0(e) => e.hash(network_id),
28 TransactionEnvelope::Tx(e) => e.hash(network_id),
29 TransactionEnvelope::TxFeeBump(e) => e.hash(network_id),
30 }
31 }
32}
33
34impl TransactionV0Envelope {
35 /// Computes the hash of the V0 transaction envelope.
36 ///
37 /// # Arguments
38 ///
39 /// * `network_id` - The network ID to include in the hash computation.
40 ///
41 /// # Returns
42 ///
43 /// The transaction hash.
44 ///
45 /// # Errors
46 ///
47 /// If there is any issue serializing to XDR.
48 pub fn hash(&self, network_id: [u8; 32]) -> Result<[u8; 32], super::Error> {
49 self.tx.hash(network_id)
50 }
51}
52
53impl TransactionV1Envelope {
54 /// Computes the hash of the V1 transaction envelope.
55 ///
56 /// # Arguments
57 ///
58 /// * `network_id` - The network ID to include in the hash computation.
59 ///
60 /// # Returns
61 ///
62 /// The transaction hash.
63 ///
64 /// # Errors
65 ///
66 /// If there is any issue serializing to XDR.
67 pub fn hash(&self, network_id: [u8; 32]) -> Result<[u8; 32], super::Error> {
68 self.tx.hash(network_id)
69 }
70}
71
72impl TransactionV0 {
73 /// Computes the hash of the V0 transaction.
74 ///
75 /// # Arguments
76 ///
77 /// * `network_id` - The network ID to include in the hash computation.
78 ///
79 /// # Returns
80 ///
81 /// The transaction hash.
82 ///
83 /// # Errors
84 ///
85 /// If there is any issue serializing to XDR.
86 pub fn hash(&self, network_id: [u8; 32]) -> Result<[u8; 32], super::Error> {
87 <_ as Into<Transaction>>::into(self).hash(network_id)
88 }
89}
90
91impl FeeBumpTransactionEnvelope {
92 /// Computes the hash of the fee bump transaction envelope.
93 ///
94 /// # Arguments
95 ///
96 /// * `network_id` - The network ID to include in the hash computation.
97 ///
98 /// # Returns
99 ///
100 /// The transaction hash.
101 ///
102 /// # Errors
103 ///
104 /// If there is any issue serializing to XDR.
105 pub fn hash(&self, network_id: [u8; 32]) -> Result<[u8; 32], super::Error> {
106 self.tx.hash(network_id)
107 }
108}
109
110impl FeeBumpTransactionInnerTx {
111 /// Computes the hash of the fee bump transaction inner tx.
112 ///
113 /// # Arguments
114 ///
115 /// * `network_id` - The network ID to include in the hash computation.
116 ///
117 /// # Returns
118 ///
119 /// The transaction hash.
120 ///
121 /// # Errors
122 ///
123 /// If there is any issue serializing to XDR.
124 pub fn hash(&self, network_id: [u8; 32]) -> Result<[u8; 32], super::Error> {
125 match self {
126 Self::Tx(inner_tx) => inner_tx.hash(network_id),
127 }
128 }
129}
130
131impl Transaction {
132 /// Computes the hash of the transaction.
133 ///
134 /// # Arguments
135 ///
136 /// * `network_id` - The network ID to include in the hash computation.
137 ///
138 /// # Returns
139 ///
140 /// The transaction hash.
141 ///
142 /// # Errors
143 ///
144 /// If there is any issue serializing to XDR.
145 pub fn hash(&self, network_id: [u8; 32]) -> Result<[u8; 32], super::Error> {
146 let payload = TransactionSignaturePayload {
147 network_id: Hash(network_id),
148 // TODO: Add support for borrowing the self here instead of cloning it.
149 tagged_transaction: TransactionSignaturePayloadTaggedTransaction::Tx(self.clone()),
150 };
151 let payload = payload.to_xdr(Limits::none())?;
152 let hash = Sha256::digest(payload);
153 Ok(hash.into())
154 }
155}
156
157impl FeeBumpTransaction {
158 /// Computes the hash of the fee bump transaction.
159 ///
160 /// # Arguments
161 ///
162 /// * `network_id` - The network ID to include in the hash computation.
163 ///
164 /// # Returns
165 ///
166 /// The transaction hash.
167 ///
168 /// # Errors
169 ///
170 /// If there is any issue serializing to XDR.
171 pub fn hash(&self, network_id: [u8; 32]) -> Result<[u8; 32], super::Error> {
172 let payload = TransactionSignaturePayload {
173 network_id: Hash(network_id),
174 tagged_transaction: TransactionSignaturePayloadTaggedTransaction::TxFeeBump(
175 self.clone(),
176 ),
177 };
178 let payload = payload.to_xdr(Limits::none())?;
179 let hash = Sha256::digest(payload);
180 Ok(hash.into())
181 }
182}