#![deny(missing_docs)]
#![deny(unsafe_code)]
use essential_types::{convert::bytes_from_word, ContentAddress, Hash, Word};
use serde::Serialize;
use sha2::Digest;
mod address_impl;
pub mod contract_addr;
pub mod solution_set_addr;
pub trait Address {
fn content_address(&self) -> ContentAddress;
}
pub fn serialize<T: Serialize>(t: &T) -> Vec<u8> {
postcard::to_allocvec(t).expect("`postcard`'s `Serializer` implementation should never fail")
}
pub fn hash<T: Serialize>(t: &T) -> Hash {
let data = serialize(t);
let mut hasher = <sha2::Sha256 as sha2::Digest>::new();
hasher.update(&data);
hasher.finalize().into()
}
pub fn content_addr<T: Address>(t: &T) -> ContentAddress {
t.content_address()
}
pub fn hash_words(words: &[Word]) -> Hash {
let data = words
.iter()
.copied()
.flat_map(bytes_from_word)
.collect::<Vec<_>>();
let mut hasher = <sha2::Sha256 as sha2::Digest>::new();
hasher.update(&data);
hasher.finalize().into()
}
pub fn hash_bytes(bytes: &[u8]) -> Hash {
let mut hasher = <sha2::Sha256 as sha2::Digest>::new();
hasher.update(bytes);
hasher.finalize().into()
}
pub fn hash_bytes_iter<'i>(iter: impl IntoIterator<Item = &'i [u8]>) -> Hash {
let mut hasher = <sha2::Sha256 as sha2::Digest>::new();
for bytes in iter {
hasher.update(bytes);
}
hasher.finalize().into()
}