thruput_native_exec/test_helpers.rs
1use sha2::{Digest, Sha256};
2use thruput_xdr::types::ContractAddress;
3
4/// Create deterministic test addresses from human-readable names.
5///
6/// ```
7/// use thruput_native_exec::test_helpers::Named;
8/// use thruput_xdr::types::ContractAddress;
9///
10/// let alice = ContractAddress::named("alice");
11/// assert_eq!(alice, ContractAddress::named("alice")); // deterministic
12/// assert_ne!(alice, ContractAddress::named("bob")); // distinct
13/// ```
14pub trait Named {
15 /// The deterministic address derived from `name`.
16 fn named(name: &str) -> Self;
17}
18
19impl Named for ContractAddress {
20 fn named(name: &str) -> Self {
21 let mut hasher = Sha256::new();
22 hasher.update(b"thruput-test-address:");
23 hasher.update(name.as_bytes());
24 let result: [u8; 32] = hasher.finalize().into();
25 result.into()
26 }
27}