|
| 1 | +// Miniscript |
| 2 | +// Written in 2022 by |
| 3 | +// Sanket Kanjalkar <[email protected]> |
| 4 | +// |
| 5 | +// To the extent possible under law, the author(s) have dedicated all |
| 6 | +// copyright and related and neighboring rights to this software to |
| 7 | +// the public domain worldwide. This software is distributed without |
| 8 | +// any warranty. |
| 9 | +// |
| 10 | +// You should have received a copy of the CC0 Public Domain Dedication |
| 11 | +// along with this software. |
| 12 | +// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. |
| 13 | +// |
| 14 | + |
| 15 | +//! Hash256 type |
| 16 | +//! |
| 17 | +//! This module is _identical_ in functionality to the `bitcoin_hashes::sha256d` hash type |
| 18 | +//! but the `FromHex/FromStr` and `ToHex/Display` implementations use `DISPLAY_BACKWARDS = false`. |
| 19 | +//! |
| 20 | +use core::str; |
| 21 | + |
| 22 | +use bitcoin::hashes::{ |
| 23 | + self, borrow_slice_impl, hex, hex_fmt_impl, index_impl, serde_impl, sha256, Hash as HashTrait, |
| 24 | +}; |
| 25 | + |
| 26 | +/// Output of the SHA256d hash function |
| 27 | +#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)] |
| 28 | +#[repr(transparent)] |
| 29 | +pub struct Hash([u8; 32]); |
| 30 | + |
| 31 | +hex_fmt_impl!(Debug, Hash); |
| 32 | +hex_fmt_impl!(Display, Hash); |
| 33 | +hex_fmt_impl!(LowerHex, Hash); |
| 34 | +index_impl!(Hash); |
| 35 | +serde_impl!(Hash, 32); |
| 36 | +borrow_slice_impl!(Hash); |
| 37 | + |
| 38 | +impl str::FromStr for Hash { |
| 39 | + type Err = hex::Error; |
| 40 | + fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 41 | + hex::FromHex::from_hex(s) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl HashTrait for Hash { |
| 46 | + type Engine = sha256::HashEngine; |
| 47 | + type Inner = [u8; 32]; |
| 48 | + |
| 49 | + fn engine() -> sha256::HashEngine { |
| 50 | + sha256::Hash::engine() |
| 51 | + } |
| 52 | + |
| 53 | + fn from_engine(e: sha256::HashEngine) -> Hash { |
| 54 | + let sha2 = sha256::Hash::from_engine(e); |
| 55 | + let sha2d = sha256::Hash::hash(&sha2[..]); |
| 56 | + |
| 57 | + let mut ret = [0; 32]; |
| 58 | + ret.copy_from_slice(&sha2d[..]); |
| 59 | + Hash(ret) |
| 60 | + } |
| 61 | + |
| 62 | + const LEN: usize = 32; |
| 63 | + |
| 64 | + fn from_slice(sl: &[u8]) -> Result<Hash, hashes::Error> { |
| 65 | + if sl.len() != 32 { |
| 66 | + Err(hashes::Error::InvalidLength(Self::LEN, sl.len())) |
| 67 | + } else { |
| 68 | + let mut ret = [0; 32]; |
| 69 | + ret.copy_from_slice(sl); |
| 70 | + Ok(Hash(ret)) |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// sha256d has DISPLAY_BACKWARD as true |
| 75 | + const DISPLAY_BACKWARD: bool = false; |
| 76 | + |
| 77 | + fn into_inner(self) -> Self::Inner { |
| 78 | + self.0 |
| 79 | + } |
| 80 | + |
| 81 | + fn as_inner(&self) -> &Self::Inner { |
| 82 | + &self.0 |
| 83 | + } |
| 84 | + |
| 85 | + fn from_inner(inner: Self::Inner) -> Self { |
| 86 | + Hash(inner) |
| 87 | + } |
| 88 | +} |
0 commit comments