aptos-sdk 0.4.1

A user-friendly, idiomatic Rust SDK for the Aptos blockchain
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//! Hash value type.
//!
//! A 32-byte cryptographic hash value used throughout Aptos for
//! transaction hashes, state roots, and other cryptographic commitments.

use crate::error::{AptosError, AptosResult};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha3::{Digest, Sha3_256};
use std::fmt;
use std::str::FromStr;

/// The length of a hash value in bytes.
pub const HASH_LENGTH: usize = 32;

/// A 32-byte cryptographic hash value.
///
/// Hash values are used throughout Aptos for:
/// - Transaction hashes
/// - State roots
/// - Event keys
/// - Merkle tree nodes
///
/// # Example
///
/// ```rust
/// use aptos_sdk::HashValue;
///
/// // Compute a hash
/// let hash = HashValue::sha3_256(b"hello world");
/// assert_eq!(hash.to_hex().len(), 66); // "0x" + 64 hex chars
///
/// // Parse from hex (64 hex characters)
/// let hex = "0x0000000000000000000000000000000000000000000000000000000000000001";
/// let hash = HashValue::from_hex(hex).unwrap();
/// ```
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct HashValue([u8; HASH_LENGTH]);

impl HashValue {
    /// The "zero" hash (all zeros).
    pub const ZERO: Self = Self([0u8; HASH_LENGTH]);

    /// Creates a hash from a byte array.
    pub const fn new(bytes: [u8; HASH_LENGTH]) -> Self {
        Self(bytes)
    }

    /// Computes the SHA3-256 hash of the given data.
    pub fn sha3_256<T: AsRef<[u8]>>(data: T) -> Self {
        let mut hasher = Sha3_256::new();
        hasher.update(data.as_ref());
        let result = hasher.finalize();
        let mut bytes = [0u8; HASH_LENGTH];
        bytes.copy_from_slice(&result);
        Self(bytes)
    }

    /// Computes the SHA3-256 hash of multiple byte slices.
    pub fn sha3_256_of<I, T>(items: I) -> Self
    where
        I: IntoIterator<Item = T>,
        T: AsRef<[u8]>,
    {
        let mut hasher = Sha3_256::new();
        for item in items {
            hasher.update(item.as_ref());
        }
        let result = hasher.finalize();
        let mut bytes = [0u8; HASH_LENGTH];
        bytes.copy_from_slice(&result);
        Self(bytes)
    }

    /// Creates a hash from a hex string (with or without `0x` prefix).
    ///
    /// # Errors
    ///
    /// Returns an error if the hex string is not exactly 64 hex characters
    /// (excluding the optional `0x` prefix) or contains invalid hex characters.
    pub fn from_hex<T: AsRef<[u8]>>(hex_str: T) -> AptosResult<Self> {
        let hex_str = hex_str.as_ref();
        let hex_str = if hex_str.starts_with(b"0x") || hex_str.starts_with(b"0X") {
            &hex_str[2..]
        } else {
            hex_str
        };

        let mut hash = [0u8; HASH_LENGTH];
        const_hex::decode_to_slice(hex_str, &mut hash)?;
        Ok(Self(hash))
    }

    /// Creates a hash from a byte slice.
    ///
    /// # Errors
    ///
    /// Returns an error if the byte slice is not exactly 32 bytes long.
    pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> AptosResult<Self> {
        let bytes = bytes.as_ref();
        if bytes.len() != HASH_LENGTH {
            return Err(AptosError::Internal(format!(
                "Invalid hash length: expected {} bytes, got {}",
                HASH_LENGTH,
                bytes.len()
            )));
        }
        let mut hash = [0u8; HASH_LENGTH];
        hash.copy_from_slice(bytes);
        Ok(Self(hash))
    }

    /// Returns the hash as a byte slice.
    pub fn as_bytes(&self) -> &[u8] {
        &self.0
    }

    /// Returns the hash as a byte array.
    pub fn to_bytes(&self) -> [u8; HASH_LENGTH] {
        self.0
    }

    /// Returns the hash as a hex string with `0x` prefix.
    pub fn to_hex(&self) -> String {
        const_hex::encode_prefixed(self.0)
    }

    /// Returns true if this is the zero hash.
    pub fn is_zero(&self) -> bool {
        self == &Self::ZERO
    }
}

impl Default for HashValue {
    fn default() -> Self {
        Self::ZERO
    }
}

impl fmt::Debug for HashValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "HashValue({})", self.to_hex())
    }
}

impl fmt::Display for HashValue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.to_hex())
    }
}

impl FromStr for HashValue {
    type Err = AptosError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_hex(s)
    }
}

impl Serialize for HashValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        if serializer.is_human_readable() {
            serializer.serialize_str(&self.to_hex())
        } else {
            // Use fixed-size array serialization to match deserialize
            self.0.serialize(serializer)
        }
    }
}

impl<'de> Deserialize<'de> for HashValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        if deserializer.is_human_readable() {
            let s = String::deserialize(deserializer)?;
            Self::from_hex(&s).map_err(serde::de::Error::custom)
        } else {
            // Use fixed-size array deserialization to match serialize
            let bytes = <[u8; HASH_LENGTH]>::deserialize(deserializer)?;
            Ok(Self(bytes))
        }
    }
}

impl From<[u8; HASH_LENGTH]> for HashValue {
    fn from(bytes: [u8; HASH_LENGTH]) -> Self {
        Self(bytes)
    }
}

impl From<HashValue> for [u8; HASH_LENGTH] {
    fn from(hash: HashValue) -> Self {
        hash.0
    }
}

impl AsRef<[u8]> for HashValue {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_sha3_256() {
        let hash = HashValue::sha3_256(b"hello world");
        assert!(!hash.is_zero());

        // Same input should produce same hash
        let hash2 = HashValue::sha3_256(b"hello world");
        assert_eq!(hash, hash2);

        // Different input should produce different hash
        let hash3 = HashValue::sha3_256(b"hello world!");
        assert_ne!(hash, hash3);
    }

    #[test]
    fn test_sha3_256_of_multiple() {
        let hash1 = HashValue::sha3_256_of([b"hello" as &[u8], b" " as &[u8], b"world" as &[u8]]);
        let hash2 = HashValue::sha3_256(b"hello world");
        assert_eq!(hash1, hash2);
    }

    #[test]
    fn test_from_hex() {
        let hash = HashValue::sha3_256(b"test");
        let hex = hash.to_hex();
        let parsed = HashValue::from_hex(&hex).unwrap();
        assert_eq!(hash, parsed);

        // Without prefix
        let parsed2 = HashValue::from_hex(&hex[2..]).unwrap();
        assert_eq!(hash, parsed2);

        // Uppercase 0X prefix
        let hex_upper = hex.replace("0x", "0X");
        let parsed3 = HashValue::from_hex(&hex_upper).unwrap();
        assert_eq!(hash, parsed3);
    }

    #[test]
    fn test_from_hex_invalid_length() {
        let result = HashValue::from_hex("0x1234");
        assert!(result.is_err());
    }

    #[test]
    fn test_from_hex_invalid_chars() {
        let result = HashValue::from_hex("0xZZZZ");
        assert!(result.is_err());
    }

    #[test]
    fn test_from_bytes() {
        let bytes = [1u8; HASH_LENGTH];
        let hash = HashValue::new(bytes);
        assert_eq!(hash.as_bytes(), &bytes);
    }

    #[test]
    fn test_json_serialization() {
        let hash = HashValue::sha3_256(b"test");
        let json = serde_json::to_string(&hash).unwrap();
        let parsed: HashValue = serde_json::from_str(&json).unwrap();
        assert_eq!(hash, parsed);
    }

    #[test]
    fn test_zero_hash() {
        assert!(HashValue::ZERO.is_zero());
        assert!(!HashValue::sha3_256(b"").is_zero());
    }

    #[test]
    fn test_new() {
        let bytes = [42u8; HASH_LENGTH];
        let hash = HashValue::new(bytes);
        assert_eq!(hash.as_bytes(), &bytes);
    }

    #[test]
    fn test_display() {
        let hash = HashValue::ZERO;
        let display = format!("{hash}");
        assert!(display.starts_with("0x"));
        assert_eq!(display.len(), 66); // 0x + 64 hex chars
    }

    #[test]
    fn test_debug() {
        let hash = HashValue::ZERO;
        let debug = format!("{hash:?}");
        assert!(debug.contains("HashValue"));
    }

    #[test]
    fn test_from_str() {
        let hash = HashValue::sha3_256(b"test");
        let hex = hash.to_hex();
        let parsed: HashValue = hex.parse().unwrap();
        assert_eq!(hash, parsed);
    }

    #[test]
    fn test_ordering() {
        let hash1 = HashValue::new([0u8; HASH_LENGTH]);
        let hash2 = HashValue::new([1u8; HASH_LENGTH]);
        assert!(hash1 < hash2);
    }

    #[test]
    fn test_as_ref() {
        let hash = HashValue::sha3_256(b"test");
        let slice: &[u8] = hash.as_ref();
        assert_eq!(slice.len(), HASH_LENGTH);
    }

    #[test]
    fn test_from_bytes_valid() {
        let bytes = [42u8; HASH_LENGTH];
        let hash = HashValue::from_bytes(bytes).unwrap();
        assert_eq!(hash.as_bytes(), &bytes);
    }

    #[test]
    fn test_from_bytes_invalid_length() {
        let bytes = vec![0u8; 16]; // Wrong length
        let result = HashValue::from_bytes(&bytes);
        assert!(result.is_err());
    }

    #[test]
    fn test_to_bytes() {
        let bytes = [42u8; HASH_LENGTH];
        let hash = HashValue::new(bytes);
        assert_eq!(hash.to_bytes(), bytes);
    }

    #[test]
    fn test_default() {
        let hash = HashValue::default();
        assert!(hash.is_zero());
        assert_eq!(hash, HashValue::ZERO);
    }

    #[test]
    fn test_from_array() {
        let bytes = [1u8; HASH_LENGTH];
        let hash: HashValue = bytes.into();
        assert_eq!(hash.as_bytes(), &bytes);
    }

    #[test]
    fn test_into_array() {
        let bytes = [1u8; HASH_LENGTH];
        let hash = HashValue::new(bytes);
        let extracted: [u8; HASH_LENGTH] = hash.into();
        assert_eq!(extracted, bytes);
    }

    #[test]
    fn test_hash_equality() {
        let hash1 = HashValue::sha3_256(b"test");
        let hash2 = HashValue::sha3_256(b"test");
        let hash3 = HashValue::sha3_256(b"different");

        assert_eq!(hash1, hash2);
        assert_ne!(hash1, hash3);
    }

    #[test]
    fn test_hash_clone() {
        let hash = HashValue::sha3_256(b"test");
        let cloned = hash;
        assert_eq!(hash, cloned);
    }

    #[test]
    fn test_hash_copy() {
        let hash = HashValue::sha3_256(b"test");
        let copied = hash;
        assert_eq!(hash, copied);
    }

    #[test]
    fn test_hash_in_hashmap() {
        use std::collections::HashMap;

        let hash1 = HashValue::sha3_256(b"key1");
        let hash2 = HashValue::sha3_256(b"key2");

        let mut map = HashMap::new();
        map.insert(hash1, "value1");
        map.insert(hash2, "value2");

        assert_eq!(map.get(&hash1), Some(&"value1"));
        assert_eq!(map.get(&hash2), Some(&"value2"));
    }

    #[test]
    fn test_from_hex_with_bytes() {
        let hash = HashValue::sha3_256(b"test");
        let hex_bytes = hash.to_hex().into_bytes();
        let parsed = HashValue::from_hex(&hex_bytes).unwrap();
        assert_eq!(hash, parsed);
    }

    #[test]
    fn test_sha3_256_empty() {
        let hash = HashValue::sha3_256(b"");
        assert!(!hash.is_zero()); // Empty string still has a hash
    }

    #[test]
    fn test_sha3_256_of_empty() {
        let hash = HashValue::sha3_256_of::<[&[u8]; 0], &[u8]>([]);
        assert!(!hash.is_zero());
    }

    #[test]
    fn test_sha3_256_of_single() {
        let hash1 = HashValue::sha3_256_of([b"test" as &[u8]]);
        let hash2 = HashValue::sha3_256(b"test");
        assert_eq!(hash1, hash2);
    }

    #[test]
    fn test_json_human_readable_serialization_roundtrip() {
        // JSON uses human-readable format (hex string)
        let hash = HashValue::sha3_256(b"test");
        let json = serde_json::to_string(&hash).unwrap();

        // Should be a quoted hex string
        assert!(json.starts_with('"'));
        assert!(json.contains("0x"));

        let deserialized: HashValue = serde_json::from_str(&json).unwrap();
        assert_eq!(hash, deserialized);
    }

    #[test]
    fn test_bcs_binary_serialization_roundtrip() {
        // BCS uses non-human-readable format (binary)
        let hash = HashValue::sha3_256(b"test");
        let serialized = aptos_bcs::to_bytes(&hash).unwrap();

        // Fixed-size array serialization should be exactly HASH_LENGTH bytes
        assert_eq!(serialized.len(), HASH_LENGTH);

        let deserialized: HashValue = aptos_bcs::from_bytes(&serialized).unwrap();
        assert_eq!(hash, deserialized);
    }

    #[test]
    fn test_bcs_binary_serialization_zero_hash() {
        // Test with zero hash
        let hash = HashValue::ZERO;
        let serialized = aptos_bcs::to_bytes(&hash).unwrap();
        let deserialized: HashValue = aptos_bcs::from_bytes(&serialized).unwrap();
        assert_eq!(hash, deserialized);
        assert!(deserialized.is_zero());
    }
}