Skip to content

Commit cf8ea17

Browse files
committed
Add Hash256 type
Implement a new hash type that does not displat/fromstr in backwards direction. We need support for support for double hashing, but if we use upstream sha256d::Hash, our FromString/Display requirements break
1 parent c80aa7c commit cf8ea17

File tree

3 files changed

+134
-1
lines changed

3 files changed

+134
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub use crate::interpreter::Interpreter;
135135
pub use crate::miniscript::context::{BareCtx, Legacy, ScriptContext, Segwitv0, Tap};
136136
pub use crate::miniscript::decode::Terminal;
137137
pub use crate::miniscript::satisfy::{Preimage32, Satisfier};
138-
pub use crate::miniscript::Miniscript;
138+
pub use crate::miniscript::{hash256, Miniscript};
139139
use crate::prelude::*;
140140

141141
///Public key trait which can be converted to Hash type

src/miniscript/hash256.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
//! Functionality similar to `sha256d` Hash type in bitcoin, but the FromStr
18+
//! `Display` are *NOT* REVERSE.
19+
//!
20+
21+
// use bitcoin::hashes::sha256d;
22+
23+
// /// Wrapper for [`sha256d::Hash`]. The only difference between [`sha256d::Hash`]
24+
// /// and [`Hash256`] is that this does not do a reverse serialization from string.
25+
// #[derive(Debug, Copy, Clone, Ord, PartialEq, PartialOrd, Eq, Hash, Default)]
26+
// pub struct Hash256(sha256d::Hash);
27+
28+
// impl Hash256 {
29+
// /// Obtains [`Hash256`] from inner [`sha256d::Hash`]
30+
// pub fn from_inner(h: sha256d::Hash) -> sha256d::Hash {
31+
// Self(h)
32+
// }
33+
34+
// /// Obtains the inner [`sha256d::Hash`]
35+
// pub fn into_inner(self) -> sha256d::Hash {
36+
// self.0
37+
// }
38+
39+
// /// Obtains a reference to the inner [`sha256d::Hash`]
40+
// pub fn as_inner(&self) -> &sha256d::Hash {
41+
// &self.0
42+
// }
43+
// }
44+
45+
// impl From<sha256d::Hash> for Hash256 {
46+
// fn from(hash: sha256d::Hash) -> Self {
47+
// Self(hash)
48+
// }
49+
// }
50+
51+
// impl FromStr for Hash256 {
52+
// type Err;
53+
54+
// fn from_str(s: &str) -> Result<Self, Self::Err> {
55+
// todo!()
56+
// }
57+
// }
58+
use core::str;
59+
60+
use bitcoin::hashes::{
61+
self, borrow_slice_impl, hex, hex_fmt_impl, index_impl, serde_impl, sha256, Hash as HashTrait,
62+
};
63+
64+
/// Output of the SHA256d hash function
65+
#[derive(Copy, Clone, PartialEq, Eq, Default, PartialOrd, Ord, Hash)]
66+
#[repr(transparent)]
67+
pub struct Hash(
68+
#[cfg_attr(
69+
feature = "schemars",
70+
schemars(schema_with = "crate::util::json_hex_string::len_32")
71+
)]
72+
[u8; 32],
73+
);
74+
75+
hex_fmt_impl!(Debug, Hash);
76+
hex_fmt_impl!(Display, Hash);
77+
hex_fmt_impl!(LowerHex, Hash);
78+
index_impl!(Hash);
79+
serde_impl!(Hash, 32);
80+
borrow_slice_impl!(Hash);
81+
82+
impl str::FromStr for Hash {
83+
type Err = hex::Error;
84+
fn from_str(s: &str) -> Result<Self, Self::Err> {
85+
hex::FromHex::from_hex(s)
86+
}
87+
}
88+
89+
impl HashTrait for Hash {
90+
type Engine = sha256::HashEngine;
91+
type Inner = [u8; 32];
92+
93+
fn engine() -> sha256::HashEngine {
94+
sha256::Hash::engine()
95+
}
96+
97+
fn from_engine(e: sha256::HashEngine) -> Hash {
98+
let sha2 = sha256::Hash::from_engine(e);
99+
let sha2d = sha256::Hash::hash(&sha2[..]);
100+
101+
let mut ret = [0; 32];
102+
ret.copy_from_slice(&sha2d[..]);
103+
Hash(ret)
104+
}
105+
106+
const LEN: usize = 32;
107+
108+
fn from_slice(sl: &[u8]) -> Result<Hash, hashes::Error> {
109+
if sl.len() != 32 {
110+
Err(hashes::Error::InvalidLength(Self::LEN, sl.len()))
111+
} else {
112+
let mut ret = [0; 32];
113+
ret.copy_from_slice(sl);
114+
Ok(Hash(ret))
115+
}
116+
}
117+
118+
/// sha256d has DISPLAY_BACKWARD as true
119+
const DISPLAY_BACKWARD: bool = false;
120+
121+
fn into_inner(self) -> Self::Inner {
122+
self.0
123+
}
124+
125+
fn as_inner(&self) -> &Self::Inner {
126+
&self.0
127+
}
128+
129+
fn from_inner(inner: Self::Inner) -> Self {
130+
Hash(inner)
131+
}
132+
}

src/miniscript/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub mod analyzable;
3737
pub mod astelem;
3838
pub(crate) mod context;
3939
pub mod decode;
40+
pub mod hash256;
4041
pub mod iter;
4142
pub mod lex;
4243
pub mod limits;

0 commit comments

Comments
 (0)