1#![deny(missing_docs)]
5#![deny(unsafe_code)]
6
7use essential_types::{convert::bytes_from_word, ContentAddress, Hash, Word};
8use serde::Serialize;
9use sha2::Digest;
10
11mod address_impl;
12pub mod contract_addr;
13pub mod solution_set_addr;
14
15pub trait Address {
18 fn content_address(&self) -> ContentAddress;
20}
21
22pub fn serialize<T: Serialize>(t: &T) -> Vec<u8> {
28 postcard::to_allocvec(t).expect("`postcard`'s `Serializer` implementation should never fail")
29}
30
31pub fn hash<T: Serialize>(t: &T) -> Hash {
36 let data = serialize(t);
37 let mut hasher = <sha2::Sha256 as sha2::Digest>::new();
38 hasher.update(&data);
39 hasher.finalize().into()
40}
41
42pub fn content_addr<T: Address>(t: &T) -> ContentAddress {
46 t.content_address()
47}
48
49pub fn hash_words(words: &[Word]) -> Hash {
51 let data = words
52 .iter()
53 .copied()
54 .flat_map(bytes_from_word)
55 .collect::<Vec<_>>();
56 let mut hasher = <sha2::Sha256 as sha2::Digest>::new();
57 hasher.update(&data);
58 hasher.finalize().into()
59}
60
61pub fn hash_bytes(bytes: &[u8]) -> Hash {
63 let mut hasher = <sha2::Sha256 as sha2::Digest>::new();
64 hasher.update(bytes);
65 hasher.finalize().into()
66}
67
68pub fn hash_bytes_iter<'i>(iter: impl IntoIterator<Item = &'i [u8]>) -> Hash {
70 let mut hasher = <sha2::Sha256 as sha2::Digest>::new();
71 for bytes in iter {
72 hasher.update(bytes);
73 }
74 hasher.finalize().into()
75}