simple_crypto/
traits.rs

1use super::structs::{Hash, BHash};
2
3pub trait Hashable: erased_serde::Serialize {
4    fn hash(&self) -> Hash {
5        let mut ser = std::io::BufWriter::new(Vec::<u8>::new());
6        let serializer = &mut serde_json::ser::Serializer::new(&mut ser);
7        self.erased_serialize(&mut Box::new(<dyn erased_serde::Serializer>::erase(
8            serializer
9        ))).unwrap();
10        Hash::new(<BHash as bitcoin_hashes::Hash>::hash(ser.buffer()))
11    }
12    fn hash_bytes(&self) -> Vec<u8> {self.hash().to_vec()}
13}
14
15impl Hashable for &[u8] {
16    fn hash(&self) -> Hash {
17        Hash::new(<BHash as bitcoin_hashes::Hash>::hash(self))
18    }
19}
20
21impl Hashable for Vec<u8> {
22    fn hash(&self) -> Hash {
23        Hash::new(<BHash as bitcoin_hashes::Hash>::hash(self))
24    }
25}