Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bitcoin::util::address::WitnessVersion;
use bitcoin::Network;
use miniscript::descriptor::DescriptorType;
use miniscript::policy::Concrete;
use miniscript::{Descriptor, Miniscript, Tap, TranslatePk, Translator};
use miniscript::{hash256, Descriptor, Miniscript, Tap, TranslatePk, Translator};
use secp256k1::{rand, KeyPair};

// Refer to https://github.com/sanket1729/adv_btc_workshop/blob/master/workshop.md#creating-a-taproot-descriptor
Expand All @@ -28,6 +28,10 @@ impl Translator<String, bitcoin::XOnlyPublicKey, ()> for StrPkTranslator {
fn sha256(&mut self, _sha256: &String) -> Result<sha256::Hash, ()> {
unreachable!("Policy does not contain any sha256 fragment");
}

fn hash256(&mut self, _sha256: &String) -> Result<hash256::Hash, ()> {
unreachable!("Policy does not contain any hash256 fragment");
}
}

fn main() {
Expand Down
4 changes: 2 additions & 2 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
Pk::RawPkHash: 'a,
{
self.ms.for_each_key(pred)
}
Expand Down Expand Up @@ -329,7 +329,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
Pk::RawPkHash: 'a,
{
pred(&self.pk)
}
Expand Down
16 changes: 11 additions & 5 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bitcoin::util::bip32;
use bitcoin::{self, XOnlyPublicKey, XpubIdentifier};

use crate::prelude::*;
use crate::{MiniscriptKey, ToPublicKey};
use crate::{hash256, MiniscriptKey, ToPublicKey};

/// The descriptor pubkey, either a single pubkey or an xpub.
#[derive(Debug, Eq, PartialEq, Clone, Ord, PartialOrd, Hash)]
Expand Down Expand Up @@ -736,8 +736,9 @@ impl<K: InnerXKey> DescriptorXKey<K> {

impl MiniscriptKey for DescriptorPublicKey {
// This allows us to be able to derive public keys even for PkH s
type Hash = Self;
type Sha256 = bitcoin::hashes::sha256::Hash;
type RawPkHash = Self;
type Sha256 = sha256::Hash;
type Hash256 = hash256::Hash;

fn is_uncompressed(&self) -> bool {
match self {
Expand Down Expand Up @@ -802,8 +803,9 @@ impl fmt::Display for DerivedDescriptorKey {

impl MiniscriptKey for DerivedDescriptorKey {
// This allows us to be able to derive public keys even for PkH s
type Hash = Self;
type Sha256 = bitcoin::hashes::sha256::Hash;
type RawPkHash = Self;
type Sha256 = sha256::Hash;
type Hash256 = hash256::Hash;

fn is_uncompressed(&self) -> bool {
self.key.is_uncompressed()
Expand Down Expand Up @@ -831,6 +833,10 @@ impl ToPublicKey for DerivedDescriptorKey {
fn to_sha256(hash: &sha256::Hash) -> sha256::Hash {
*hash
}

fn to_hash256(hash: &hash256::Hash) -> hash256::Hash {
*hash
}
}

#[cfg(test)]
Expand Down
16 changes: 13 additions & 3 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use self::checksum::verify_checksum;
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
use crate::prelude::*;
use crate::{
expression, miniscript, BareCtx, Error, ForEachKey, MiniscriptKey, PkTranslator, Satisfier,
ToPublicKey, TranslatePk, Translator,
expression, hash256, miniscript, BareCtx, Error, ForEachKey, MiniscriptKey, PkTranslator,
Satisfier, ToPublicKey, TranslatePk, Translator,
};

mod bare;
Expand Down Expand Up @@ -498,7 +498,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
Pk::RawPkHash: 'a,
{
match *self {
Descriptor::Bare(ref bare) => bare.for_each_key(pred),
Expand Down Expand Up @@ -646,6 +646,12 @@ impl Descriptor<DescriptorPublicKey> {
sha256::Hash::from_str(sha256).map_err(|e| Error::Unexpected(e.to_string()))?;
Ok(hash)
}

fn hash256(&mut self, hash256: &String) -> Result<hash256::Hash, Error> {
let hash = hash256::Hash::from_str(hash256)
.map_err(|e| Error::Unexpected(e.to_string()))?;
Ok(hash)
}
}

let descriptor = Descriptor::<String>::from_str(s)?;
Expand All @@ -672,6 +678,10 @@ impl Descriptor<DescriptorPublicKey> {
fn sha256(&mut self, sha256: &sha256::Hash) -> Result<String, ()> {
Ok(sha256.to_string())
}

fn hash256(&mut self, hash256: &hash256::Hash) -> Result<String, ()> {
Ok(hash256.to_string())
}
}

fn key_to_string(pk: &DescriptorPublicKey, key_map: &KeyMap) -> Result<String, ()> {
Expand Down
4 changes: 2 additions & 2 deletions src/descriptor/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
Pk::RawPkHash: 'a,
{
match self.inner {
WshInner::SortedMulti(ref smv) => smv.for_each_key(pred),
Expand Down Expand Up @@ -442,7 +442,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
Pk::RawPkHash: 'a,
{
pred(&self.pk)
}
Expand Down
2 changes: 1 addition & 1 deletion src/descriptor/sh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Sh<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
Pk::RawPkHash: 'a,
{
match self.inner {
ShInner::Wsh(ref wsh) => wsh.for_each_key(pred),
Expand Down
2 changes: 1 addition & 1 deletion src/descriptor/sortedmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> ForEachKey<Pk> for SortedMultiVec<Pk
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
Pk::RawPkHash: 'a,
{
self.pks.iter().all(|key| pred(key))
}
Expand Down
2 changes: 1 addition & 1 deletion src/descriptor/tr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
where
Pk: 'a,
Pk::Hash: 'a,
Pk::RawPkHash: 'a,
{
let script_keys_res = self
.iter_scripts()
Expand Down
24 changes: 11 additions & 13 deletions src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ use core::fmt;
use core::str::FromStr;

use bitcoin::blockdata::witness::Witness;
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d};
use bitcoin::hashes::{hash160, ripemd160, sha256};
use bitcoin::util::{sighash, taproot};
use bitcoin::{self, secp256k1, TxOut};

use crate::miniscript::context::NoChecks;
use crate::miniscript::ScriptContext;
use crate::prelude::*;
use crate::{Descriptor, Miniscript, Terminal, ToPublicKey};
use crate::{hash256, Descriptor, Miniscript, Terminal, ToPublicKey};

mod error;
mod inner;
Expand Down Expand Up @@ -148,10 +148,11 @@ impl TypedHash160 {
}

impl MiniscriptKey for BitcoinKey {
type Hash = TypedHash160;
type Sha256 = bitcoin::hashes::sha256::Hash;
type RawPkHash = TypedHash160;
type Sha256 = sha256::Hash;
type Hash256 = hash256::Hash;

fn to_pubkeyhash(&self) -> Self::Hash {
fn to_pubkeyhash(&self) -> Self::RawPkHash {
match self {
BitcoinKey::Fullkey(pk) => TypedHash160::FullKey(pk.to_pubkeyhash()),
BitcoinKey::XOnlyPublicKey(pk) => TypedHash160::XonlyKey(pk.to_pubkeyhash()),
Expand Down Expand Up @@ -454,7 +455,7 @@ pub enum HashLockType {
///SHA 256 hashlock
Sha256(sha256::Hash),
///Hash 256 hashlock
Hash256(sha256d::Hash),
Hash256(hash256::Hash),
///Hash160 hashlock
Hash160(hash160::Hash),
///Ripemd160 hashlock
Expand Down Expand Up @@ -1040,7 +1041,7 @@ fn verify_sersig<'txin>(
mod tests {

use bitcoin;
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash};
use bitcoin::secp256k1::{self, Secp256k1};

use super::inner::ToNoChecks;
Expand Down Expand Up @@ -1156,11 +1157,8 @@ mod tests {
let preimage = [0xab as u8; 32];
let sha256_hash = sha256::Hash::hash(&preimage);
let sha256 = no_checks_ms(&format!("sha256({})", sha256_hash));
let sha256d_hash_rev = sha256d::Hash::hash(&preimage);
let mut sha256d_hash_bytes = sha256d_hash_rev.clone().into_inner();
sha256d_hash_bytes.reverse();
let sha256d_hash = sha256d::Hash::from_inner(sha256d_hash_bytes);
let hash256 = no_checks_ms(&format!("hash256({})", sha256d_hash));
let hash256_hash = hash256::Hash::hash(&preimage);
let hash256 = no_checks_ms(&format!("hash256({})", hash256_hash));
let hash160_hash = hash160::Hash::hash(&preimage);
let hash160 = no_checks_ms(&format!("hash160({})", hash160_hash));
let ripemd160_hash = ripemd160::Hash::hash(&preimage);
Expand Down Expand Up @@ -1242,7 +1240,7 @@ mod tests {
assert_eq!(
sha256d_satisfied.unwrap(),
vec![SatisfiedConstraint::HashLock {
hash: HashLockType::Hash256(sha256d_hash_rev),
hash: HashLockType::Hash256(hash256_hash),
preimage: preimage,
}]
);
Expand Down
7 changes: 4 additions & 3 deletions src/interpreter/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@

use bitcoin;
use bitcoin::blockdata::{opcodes, script};
use bitcoin::hashes::{hash160, ripemd160, sha256, sha256d, Hash};
use bitcoin::hashes::{hash160, ripemd160, sha256, Hash};

use super::error::PkEvalErrInner;
use super::{
verify_sersig, BitcoinKey, Error, HashLockType, KeySigPair, SatisfiedConstraint, TypedHash160,
};
use crate::hash256;
use crate::prelude::*;

/// Definition of Stack Element of the Stack used for interpretation of Miniscript.
Expand Down Expand Up @@ -288,13 +289,13 @@ impl<'txin> Stack<'txin> {
/// `SIZE 32 EQUALVERIFY HASH256 h EQUAL`
pub(super) fn evaluate_hash256(
&mut self,
hash: &sha256d::Hash,
hash: &hash256::Hash,
) -> Option<Result<SatisfiedConstraint, Error>> {
if let Some(Element::Push(preimage)) = self.pop() {
if preimage.len() != 32 {
return Some(Err(Error::HashPreimageLengthMismatch));
}
if sha256d::Hash::hash(preimage) == *hash {
if hash256::Hash::hash(preimage) == *hash {
self.push(Element::Satisfied);
Some(Ok(SatisfiedConstraint::HashLock {
hash: HashLockType::Hash256(*hash),
Expand Down
Loading