essential_hash/
address_impl.rs

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
use essential_types::{
    contract::Contract, predicate::Predicate, solution::Solution, Block, ContentAddress,
};
use sha2::Digest;

use crate::{hash, Address};

impl Address for Block {
    fn content_address(&self) -> ContentAddress {
        crate::block_addr::from_block(self)
    }
}

impl Address for Predicate {
    fn content_address(&self) -> ContentAddress {
        let Ok(header) = self.encoded_header() else {
            // Invalid predicates can't be hashed.
            return ContentAddress([0; 32]);
        };
        let mut hasher = <sha2::Sha256 as sha2::Digest>::new();
        hasher.update(header.fixed_size_header.0);
        hasher.update(header.lens);
        for item in self.programs() {
            hasher.update(item);
        }
        ContentAddress(hasher.finalize().into())
    }
}

impl Address for Contract {
    fn content_address(&self) -> ContentAddress {
        crate::contract_addr::from_contract(self)
    }
}

impl Address for Solution {
    fn content_address(&self) -> ContentAddress {
        ContentAddress(hash(self))
    }
}