Skip to content

Commit

Permalink
Merge commit 'd5615acda1a7fdc4041a11c1736af139b8c7ebe8' into update_2…
Browse files Browse the repository at this point in the history
…022_10

d5615ac Merge rust-bitcoin/rust-miniscript#461: Cleanly separate experimental/insane miniscripts from sane miniscripts

I will revisit how to combine ExtParams API with Extensions later. Right
now, we only make the things compile and test
  • Loading branch information
sanket1729 committed Oct 20, 2022
2 parents 0ab4959 + d5615ac commit 8b53a6c
Show file tree
Hide file tree
Showing 39 changed files with 695 additions and 1,091 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ rand = ["bitcoin/rand"]
[dependencies]
bitcoin = "0.29.1"
elements = "0.21.0"
bitcoin-miniscript = {package = "miniscript", git = "https://github.com/rust-bitcoin/rust-miniscript", rev = "51d66d1eab3930fbe053a33816d9f80f8ad57255"}
bitcoin-miniscript = {package = "miniscript", git = "https://github.com/rust-bitcoin/rust-miniscript", rev = "d5615acda1a7fdc4041a11c1736af139b8c7ebe8"}

# Do NOT use this as a feature! Use the `serde` feature instead.
actual-serde = { package = "serde", version = "1.0", optional = true }
Expand Down
4 changes: 2 additions & 2 deletions examples/htlc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ fn main() {
assert!(htlc_descriptor.sanity_check().is_ok());
assert_eq!(
format!("{}", htlc_descriptor),
"elwsh(andor(pk(022222222222222222222222222222222222222222222222222222222222222222),sha256(1111111111111111111111111111111111111111111111111111111111111111),and_v(v:pkh(51814f108670aced2d77c1805ddd6634bc9d4731),older(4444))))#dq09llks"
"elwsh(andor(pk(022222222222222222222222222222222222222222222222222222222222222222),sha256(1111111111111111111111111111111111111111111111111111111111111111),and_v(v:pkh(020202020202020202020202020202020202020202020202020202020202020202),older(4444))))#lfytrjen"
);

// Lift the descriptor into an abstract policy.
assert_eq!(
format!("{}", htlc_descriptor.lift().unwrap()),
"or(and(pkh(4377a5acd66dc5cb67148a24818d1e51fa183bd2),sha256(1111111111111111111111111111111111111111111111111111111111111111)),and(pkh(51814f108670aced2d77c1805ddd6634bc9d4731),older(4444)))"
"or(and(pk(022222222222222222222222222222222222222222222222222222222222222222),sha256(1111111111111111111111111111111111111111111111111111111111111111)),and(pk(020202020202020202020202020202020202020202020202020202020202020202),older(4444)))"
);

// Get the scriptPpubkey for this Wsh descriptor.
Expand Down
28 changes: 7 additions & 21 deletions examples/taproot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ extern crate elements_miniscript as miniscript;
use std::collections::HashMap;
use std::str::FromStr;

use bitcoin::hashes::{hash160, ripemd160, sha256};
use bitcoin::util::address::WitnessVersion;
use miniscript::descriptor::DescriptorType;
use miniscript::policy::Concrete;
use miniscript::{hash256, Descriptor, Miniscript, NoExt, Tap, TranslatePk, Translator};
use miniscript::{
translate_hash_fail, Descriptor, Miniscript, NoExt, 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 @@ -22,25 +23,10 @@ impl Translator<String, bitcoin::XOnlyPublicKey, ()> for StrPkTranslator {
self.pk_map.get(pk).copied().ok_or(())
}

fn pkh(&mut self, _pkh: &String) -> Result<hash160::Hash, ()> {
unreachable!("Policy doesn't contain any pkh fragment");
}

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 ripemd160(&mut self, _ripemd160: &String) -> Result<ripemd160::Hash, ()> {
unreachable!("Policy does not contain any ripemd160 fragment");
}

fn hash160(&mut self, _hash160: &String) -> Result<hash160::Hash, ()> {
unreachable!("Policy does not contain any hash160 fragment");
}
// We don't need to implement these methods as we are not using them in the policy
// Fail if we encounter any hash fragments.
// See also translate_hash_clone! macro
translate_hash_fail!(String, bitcoin::XOnlyPublicKey, ());
}

fn main() {
Expand Down
4 changes: 1 addition & 3 deletions src/descriptor/bare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ 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::RawPkHash: 'a,
{
self.ms.for_each_key(pred)
}
Expand Down Expand Up @@ -306,7 +305,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Pkh<Pk> {

impl<Pk: MiniscriptKey> Liftable<Pk> for Pkh<Pk> {
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
Ok(semantic::Policy::Key(self.pk.clone()))
}
}

Expand Down Expand Up @@ -341,7 +340,6 @@ 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::RawPkHash: 'a,
{
pred(&self.pk)
}
Expand Down
1 change: 0 additions & 1 deletion src/descriptor/csfs_cov/cov.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,6 @@ impl<Pk: MiniscriptKey, Ext: Extension> ForEachKey<Pk> for LegacyCSFSCov<Pk, Ext
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
where
Pk: 'a,
Pk::RawPkHash: 'a,
{
pred(&self.pk) && self.ms.for_any_key(pred)
}
Expand Down
16 changes: 0 additions & 16 deletions src/descriptor/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,6 @@ impl<K: InnerXKey> DescriptorXKey<K> {
}

impl MiniscriptKey for DescriptorPublicKey {
// This allows us to be able to derive public keys even for PkH s
type RawPkHash = Self;
type Sha256 = sha256::Hash;
type Hash256 = hash256::Hash;
type Ripemd160 = ripemd160::Hash;
Expand All @@ -736,10 +734,6 @@ impl MiniscriptKey for DescriptorPublicKey {
_ => false,
}
}

fn to_pubkeyhash(&self) -> Self {
self.clone()
}
}

impl DefiniteDescriptorKey {
Expand Down Expand Up @@ -798,8 +792,6 @@ impl fmt::Display for DefiniteDescriptorKey {
}

impl MiniscriptKey for DefiniteDescriptorKey {
// This allows us to be able to derive public keys even for PkH s
type RawPkHash = Self;
type Sha256 = sha256::Hash;
type Hash256 = hash256::Hash;
type Ripemd160 = ripemd160::Hash;
Expand All @@ -812,10 +804,6 @@ impl MiniscriptKey for DefiniteDescriptorKey {
fn is_x_only_key(&self) -> bool {
self.0.is_x_only_key()
}

fn to_pubkeyhash(&self) -> Self {
self.clone()
}
}

impl ToPublicKey for DefiniteDescriptorKey {
Expand All @@ -824,10 +812,6 @@ impl ToPublicKey for DefiniteDescriptorKey {
self.0.derive_public_key(&secp).unwrap()
}

fn hash_to_hash160(hash: &Self) -> hash160::Hash {
hash.to_public_key().to_pubkeyhash()
}

fn to_sha256(hash: &sha256::Hash) -> sha256::Hash {
*hash
}
Expand Down
67 changes: 5 additions & 62 deletions src/descriptor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,6 @@ impl<Pk: MiniscriptKey, T: Extension> ForEachKey<Pk> for Descriptor<Pk, T> {
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
where
Pk: 'a,
Pk::RawPkHash: 'a,
{
match *self {
Descriptor::Bare(ref bare) => bare.for_each_key(pred),
Expand Down Expand Up @@ -796,25 +795,7 @@ impl<Ext: Extension + ParseableExt> Descriptor<DescriptorPublicKey, Ext> {
Ok(pk.clone().at_derivation_index(self.0))
}

fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<DefiniteDescriptorKey, ()> {
Ok(pkh.clone().at_derivation_index(self.0))
}

fn sha256(&mut self, sha256: &sha256::Hash) -> Result<sha256::Hash, ()> {
Ok(*sha256)
}

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

fn ripemd160(&mut self, ripemd160: &ripemd160::Hash) -> Result<ripemd160::Hash, ()> {
Ok(*ripemd160)
}

fn hash160(&mut self, hash160: &hash160::Hash) -> Result<hash160::Hash, ()> {
Ok(*hash160)
}
translate_hash_clone!(DescriptorPublicKey, DescriptorPublicKey, ());
}
self.translate_pk(&mut Derivator(index))
.expect("BIP 32 key index substitution cannot fail")
Expand Down Expand Up @@ -904,10 +885,6 @@ impl<Ext: Extension + ParseableExt> Descriptor<DescriptorPublicKey, Ext> {
parse_key(pk, &mut self.0, self.1)
}

fn pkh(&mut self, pkh: &String) -> Result<DescriptorPublicKey, Error> {
parse_key(pkh, &mut self.0, self.1)
}

fn sha256(&mut self, sha256: &String) -> Result<sha256::Hash, Error> {
let hash =
sha256::Hash::from_str(sha256).map_err(|e| Error::Unexpected(e.to_string()))?;
Expand Down Expand Up @@ -950,10 +927,6 @@ impl<Ext: Extension + ParseableExt> Descriptor<DescriptorPublicKey, Ext> {
key_to_string(pk, self.0)
}

fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<String, ()> {
key_to_string(pkh, self.0)
}

fn sha256(&mut self, sha256: &sha256::Hash) -> Result<String, ()> {
Ok(sha256.to_string())
}
Expand Down Expand Up @@ -1054,37 +1027,7 @@ impl<Ext: Extension> Descriptor<DefiniteDescriptorKey, Ext> {
pk.derive_public_key(&self.0)
}

fn pkh(
&mut self,
pkh: &DefiniteDescriptorKey,
) -> Result<bitcoin::hashes::hash160::Hash, ConversionError> {
Ok(pkh.derive_public_key(&self.0)?.to_pubkeyhash())
}

fn sha256(&mut self, sha256: &sha256::Hash) -> Result<sha256::Hash, ConversionError> {
Ok(*sha256)
}

fn hash256(
&mut self,
hash256: &hash256::Hash,
) -> Result<hash256::Hash, ConversionError> {
Ok(*hash256)
}

fn ripemd160(
&mut self,
ripemd160: &ripemd160::Hash,
) -> Result<ripemd160::Hash, ConversionError> {
Ok(*ripemd160)
}

fn hash160(
&mut self,
hash160: &hash160::Hash,
) -> Result<hash160::Hash, ConversionError> {
Ok(*hash160)
}
translate_hash_clone!(DefiniteDescriptorKey, bitcoin::PublicKey, ConversionError);
}

let derived = self.translate_pk(&mut Derivator(secp))?;
Expand Down Expand Up @@ -1195,7 +1138,7 @@ mod tests {
use crate::miniscript::satisfy::ElementsSig;
#[cfg(feature = "compiler")]
use crate::policy;
use crate::{hex_script, Descriptor, DummyKey, Error, Miniscript, Satisfier};
use crate::{hex_script, Descriptor, DummyKey, Error, Miniscript, NoExt, Satisfier};

type StdDescriptor = Descriptor<PublicKey, CovenantExt<CovExtArgs>>;
const TEST_PK: &'static str =
Expand Down Expand Up @@ -1667,8 +1610,8 @@ mod tests {
assert_eq!(
descriptor,
format!(
"eltr({},{{pk({}),{{pk({}),or_d(pk({}),pkh(516ca378e588a7ed71336147e2a72848b20aca1a))}}}})#3cw4zduf",
p1, p2, p3, p4,
"eltr({},{{pk({}),{{pk({}),or_d(pk({}),pkh({}))}}}})#y9kzzx3w",
p1, p2, p3, p4, p5
)
)
}
Expand Down
18 changes: 2 additions & 16 deletions src/descriptor/pegin/legacy_pegin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ impl fmt::Display for LegacyPeginKey {
}

impl MiniscriptKey for LegacyPeginKey {
type RawPkHash = hash160::Hash;
type Sha256 = sha256::Hash;
type Hash256 = hash256::Hash;
type Ripemd160 = ripemd160::Hash;
Expand All @@ -104,14 +103,6 @@ impl MiniscriptKey for LegacyPeginKey {
fn is_uncompressed(&self) -> bool {
false
}

fn to_pubkeyhash(&self) -> Self::RawPkHash {
let pk = match *self {
LegacyPeginKey::Functionary(ref pk) => pk,
LegacyPeginKey::NonFunctionary(ref pk) => pk,
};
MiniscriptKey::to_pubkeyhash(pk)
}
}

/// Legacy Pegin Descriptor
Expand Down Expand Up @@ -248,17 +239,12 @@ impl<Pk: MiniscriptKey> LegacyPegin<Pk> {
};
struct TranslateUnTweak;

impl bitcoin_miniscript::PkTranslator<LegacyPeginKey, bitcoin::PublicKey, ()> for TranslateUnTweak {
impl bitcoin_miniscript::Translator<LegacyPeginKey, bitcoin::PublicKey, ()> for TranslateUnTweak {
fn pk(&mut self, pk: &LegacyPeginKey) -> Result<bitcoin::PublicKey, ()> {
Ok(*pk.as_untweaked())
}

fn pkh(
&mut self,
_pkh: &<LegacyPeginKey as MiniscriptKey>::RawPkHash,
) -> Result<<bitcoin::PublicKey as MiniscriptKey>::RawPkHash, ()> {
unreachable!("No keyhashes in elements descriptors")
}
bitcoin_miniscript::translate_hash_clone!(LegacyPeginKey, bitcoin::PublicKey, ());
}
let mut t = TranslateUnTweak;

Expand Down
6 changes: 1 addition & 5 deletions src/descriptor/segwitv0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ 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::RawPkHash: 'a,
{
match self.inner {
WshInner::SortedMulti(ref smv) => smv.for_each_key(pred),
Expand Down Expand Up @@ -369,9 +368,7 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
pub(super) fn from_inner_tree(top: &expression::Tree<'_>) -> Result<Self, Error>
where
Pk: FromStr,
Pk::RawPkHash: FromStr,
<Pk as FromStr>::Err: ToString,
<<Pk as MiniscriptKey>::RawPkHash as FromStr>::Err: ToString,
{
if top.name == "wpkh" && top.args.len() == 1 {
Ok(Wpkh::new(expression::terminal(&top.args[0], |pk| {
Expand Down Expand Up @@ -468,7 +465,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Wpkh<Pk> {

impl<Pk: MiniscriptKey> Liftable<Pk> for Wpkh<Pk> {
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
Ok(semantic::Policy::Key(self.pk.clone()))
}
}

Expand Down Expand Up @@ -503,7 +500,6 @@ 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::RawPkHash: 'a,
{
pred(&self.pk)
}
Expand Down
3 changes: 1 addition & 2 deletions src/descriptor/sh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Sh<Pk> {
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
match self.inner {
ShInner::Wsh(ref wsh) => wsh.lift(),
ShInner::Wpkh(ref pk) => Ok(semantic::Policy::KeyHash(pk.as_inner().to_pubkeyhash())),
ShInner::Wpkh(ref pk) => Ok(semantic::Policy::Key(pk.as_inner().clone())),
ShInner::SortedMulti(ref smv) => smv.lift(),
ShInner::Ms(ref ms) => ms.lift(),
}
Expand Down Expand Up @@ -376,7 +376,6 @@ 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::RawPkHash: 'a,
{
match self.inner {
ShInner::Wsh(ref wsh) => wsh.for_each_key(pred),
Expand Down
6 changes: 2 additions & 4 deletions src/descriptor/sortedmulti.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ 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::RawPkHash: 'a,
{
self.pks.iter().all(|key| pred(key))
}
Expand Down Expand Up @@ -217,9 +216,8 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> policy::Liftable<Pk> for SortedMulti
let ret = policy::semantic::Policy::Threshold(
self.k,
self.pks
.clone()
.into_iter()
.map(|k| policy::semantic::Policy::KeyHash(k.to_pubkeyhash()))
.iter()
.map(|k| policy::semantic::Policy::Key(k.clone()))
.collect(),
);
Ok(ret)
Expand Down
Loading

0 comments on commit 8b53a6c

Please sign in to comment.