3838/// - Logged in event data for recovery
3939/// - Computed deterministically from stable inputs
4040/// - Documented in event sequences for human auditors
41- use soroban_sdk:: { crypto:: Hash , Address , Bytes , Env , Symbol } ;
41+ extern crate alloc;
42+
43+ use alloc:: { vec, vec:: Vec } ;
44+ use soroban_sdk:: { xdr:: ToXdr , Address , Bytes , Env , Symbol } ;
4245
4346/// Correlation ID uniquely identifying a causal workflow
4447/// This is a 32-byte hash computed from workflow parameters
@@ -60,12 +63,16 @@ impl EventCausality {
6063 asset_pair : & Symbol ,
6164 timestamp : u64 ,
6265 ) -> CorrelationId {
63- Self :: hash_bytes ( & [
64- b"score_submit" . as_slice ( ) ,
65- wallet. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
66- asset_pair. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
67- timestamp. to_le_bytes ( ) . as_slice ( ) ,
68- ] )
66+ let env = Env :: default ( ) ;
67+ Self :: hash_bytes (
68+ & env,
69+ & [
70+ Bytes :: from_slice ( & env, b"score_submit" ) ,
71+ wallet. to_xdr ( & env) ,
72+ asset_pair. to_xdr ( & env) ,
73+ Bytes :: from_array ( & env, & timestamp. to_le_bytes ( ) ) ,
74+ ] ,
75+ )
6976 }
7077
7178 /// Generate correlation ID for admin transfer workflow
@@ -80,12 +87,16 @@ impl EventCausality {
8087 to : & Address ,
8188 timestamp : u64 ,
8289 ) -> CorrelationId {
83- Self :: hash_bytes ( & [
84- b"admin_xfer" . as_slice ( ) ,
85- from. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
86- to. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
87- timestamp. to_le_bytes ( ) . as_slice ( ) ,
88- ] )
90+ let env = Env :: default ( ) ;
91+ Self :: hash_bytes (
92+ & env,
93+ & [
94+ Bytes :: from_slice ( & env, b"admin_xfer" ) ,
95+ from. to_xdr ( & env) ,
96+ to. to_xdr ( & env) ,
97+ Bytes :: from_array ( & env, & timestamp. to_le_bytes ( ) ) ,
98+ ] ,
99+ )
89100 }
90101
91102 /// Generate correlation ID for upgrade workflow
@@ -95,11 +106,15 @@ impl EventCausality {
95106 /// - `new_wasm_hash`: The new contract WASM hash being proposed
96107 /// - `timestamp`: The proposal timestamp
97108 pub fn upgrade_correlation_id ( new_wasm_hash : & [ u8 ; 32 ] , timestamp : u64 ) -> CorrelationId {
98- Self :: hash_bytes ( & [
99- b"upgrade_prop" . as_slice ( ) ,
100- new_wasm_hash. as_slice ( ) ,
101- timestamp. to_le_bytes ( ) . as_slice ( ) ,
102- ] )
109+ let env = Env :: default ( ) ;
110+ Self :: hash_bytes (
111+ & env,
112+ & [
113+ Bytes :: from_slice ( & env, b"upgrade_prop" ) ,
114+ Bytes :: from_array ( & env, new_wasm_hash) ,
115+ Bytes :: from_array ( & env, & timestamp. to_le_bytes ( ) ) ,
116+ ] ,
117+ )
103118 }
104119
105120 /// Generate correlation ID for parameter change workflow
@@ -114,12 +129,16 @@ impl EventCausality {
114129 param_key : & Symbol ,
115130 timestamp : u64 ,
116131 ) -> CorrelationId {
117- Self :: hash_bytes ( & [
118- b"param_change" . as_slice ( ) ,
119- proposal_id. to_le_bytes ( ) . as_slice ( ) ,
120- param_key. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
121- timestamp. to_le_bytes ( ) . as_slice ( ) ,
122- ] )
132+ let env = Env :: default ( ) ;
133+ Self :: hash_bytes (
134+ & env,
135+ & [
136+ Bytes :: from_slice ( & env, b"param_change" ) ,
137+ Bytes :: from_array ( & env, & proposal_id. to_le_bytes ( ) ) ,
138+ param_key. to_xdr ( & env) ,
139+ Bytes :: from_array ( & env, & timestamp. to_le_bytes ( ) ) ,
140+ ] ,
141+ )
123142 }
124143
125144 /// Generate correlation ID for dispute workflow
@@ -134,12 +153,16 @@ impl EventCausality {
134153 asset_pair : & Symbol ,
135154 timestamp : u64 ,
136155 ) -> CorrelationId {
137- Self :: hash_bytes ( & [
138- b"dispute_open" . as_slice ( ) ,
139- challenger. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
140- asset_pair. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
141- timestamp. to_le_bytes ( ) . as_slice ( ) ,
142- ] )
156+ let env = Env :: default ( ) ;
157+ Self :: hash_bytes (
158+ & env,
159+ & [
160+ Bytes :: from_slice ( & env, b"dispute_open" ) ,
161+ challenger. to_xdr ( & env) ,
162+ asset_pair. to_xdr ( & env) ,
163+ Bytes :: from_array ( & env, & timestamp. to_le_bytes ( ) ) ,
164+ ] ,
165+ )
143166 }
144167
145168 /// Generate correlation ID for governance action chain
@@ -149,11 +172,15 @@ impl EventCausality {
149172 /// - `action_index`: Sequential index of this action in the governance chain
150173 /// - `timestamp`: The action timestamp
151174 pub fn governance_chain_correlation_id ( action_index : u64 , timestamp : u64 ) -> CorrelationId {
152- Self :: hash_bytes ( & [
153- b"gov_chain" . as_slice ( ) ,
154- action_index. to_le_bytes ( ) . as_slice ( ) ,
155- timestamp. to_le_bytes ( ) . as_slice ( ) ,
156- ] )
175+ let env = Env :: default ( ) ;
176+ Self :: hash_bytes (
177+ & env,
178+ & [
179+ Bytes :: from_slice ( & env, b"gov_chain" ) ,
180+ Bytes :: from_array ( & env, & action_index. to_le_bytes ( ) ) ,
181+ Bytes :: from_array ( & env, & timestamp. to_le_bytes ( ) ) ,
182+ ] ,
183+ )
157184 }
158185
159186 /// Generate correlation ID for consensus score workflow
@@ -168,12 +195,16 @@ impl EventCausality {
168195 asset_pair : & Symbol ,
169196 round_id : u64 ,
170197 ) -> CorrelationId {
171- Self :: hash_bytes ( & [
172- b"consensus_round" . as_slice ( ) ,
173- wallet. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
174- asset_pair. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
175- round_id. to_le_bytes ( ) . as_slice ( ) ,
176- ] )
198+ let env = Env :: default ( ) ;
199+ Self :: hash_bytes (
200+ & env,
201+ & [
202+ Bytes :: from_slice ( & env, b"consensus_round" ) ,
203+ wallet. to_xdr ( & env) ,
204+ asset_pair. to_xdr ( & env) ,
205+ Bytes :: from_array ( & env, & round_id. to_le_bytes ( ) ) ,
206+ ] ,
207+ )
177208 }
178209
179210 /// Generate correlation ID for escalation workflow
@@ -188,25 +219,25 @@ impl EventCausality {
188219 asset_pair : & Symbol ,
189220 timestamp : u64 ,
190221 ) -> CorrelationId {
191- Self :: hash_bytes ( & [
192- b"escalation" . as_slice ( ) ,
193- wallet. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
194- asset_pair. to_xdr ( & Env :: default ( ) ) . to_bytes ( ) . as_slice ( ) ,
195- timestamp. to_le_bytes ( ) . as_slice ( ) ,
196- ] )
222+ let env = Env :: default ( ) ;
223+ Self :: hash_bytes (
224+ & env,
225+ & [
226+ Bytes :: from_slice ( & env, b"escalation" ) ,
227+ wallet. to_xdr ( & env) ,
228+ asset_pair. to_xdr ( & env) ,
229+ Bytes :: from_array ( & env, & timestamp. to_le_bytes ( ) ) ,
230+ ] ,
231+ )
197232 }
198233
199234 /// Internal: Hash multiple byte sequences to produce a correlation ID
200- fn hash_bytes ( parts : & [ & [ u8 ] ] ) -> CorrelationId {
201- let mut combined = Vec :: new ( ) ;
235+ fn hash_bytes ( env : & Env , parts : & [ Bytes ] ) -> CorrelationId {
236+ let mut combined = Bytes :: new ( env ) ;
202237 for part in parts {
203- combined. extend_from_slice ( part) ;
238+ combined. append ( part) ;
204239 }
205- // Use SHA-256 to produce a deterministic 32-byte ID
206- let hash_result = Hash :: sha256 ( & Bytes :: from_slice ( & Env :: default ( ) , & combined) ) ;
207- let mut result = [ 0u8 ; 32 ] ;
208- result. copy_from_slice ( hash_result. as_slice ( ) ) ;
209- result
240+ env. crypto ( ) . sha256 ( & combined) . to_array ( )
210241 }
211242}
212243
0 commit comments