dpp/state_transition/state_transitions/
signable_bytes_hasher.rs

1use crate::util::hash::hash_double;
2use platform_value::Bytes32;
3
4/// This is a structure to hash signable bytes when we are not sure if we will need the hashing
5#[derive(Debug, Clone)]
6pub enum SignableBytesHasher {
7    Bytes(Vec<u8>),
8    PreHashed(Bytes32),
9}
10
11impl SignableBytesHasher {
12    pub fn into_hashed_bytes(self) -> Bytes32 {
13        match self {
14            SignableBytesHasher::Bytes(signable_bytes) => hash_double(signable_bytes).into(),
15            SignableBytesHasher::PreHashed(pre_hashed) => pre_hashed,
16        }
17    }
18
19    pub fn to_hashed_bytes(&self) -> Bytes32 {
20        match self {
21            SignableBytesHasher::Bytes(signable_bytes) => hash_double(signable_bytes).into(),
22            SignableBytesHasher::PreHashed(pre_hashed) => *pre_hashed,
23        }
24    }
25
26    pub fn hash_bytes(&mut self) -> Bytes32 {
27        match self {
28            SignableBytesHasher::Bytes(signable_bytes) => {
29                let bytes_32: Bytes32 = hash_double(signable_bytes).into();
30                *self = SignableBytesHasher::PreHashed(bytes_32);
31                bytes_32
32            }
33            SignableBytesHasher::PreHashed(pre_hashed) => *pre_hashed,
34        }
35    }
36
37    pub fn hash_bytes_and_check_if_vec_contains(&mut self, vec: &[Bytes32]) -> bool {
38        match self {
39            SignableBytesHasher::Bytes(signable_bytes) => {
40                let bytes_32: Bytes32 = hash_double(signable_bytes).into();
41                let contains = vec.contains(&bytes_32);
42                *self = SignableBytesHasher::PreHashed(bytes_32);
43                contains
44            }
45            SignableBytesHasher::PreHashed(pre_hashed) => vec.contains(pre_hashed),
46        }
47    }
48}