Skip to content

Commit 8b53a6c

Browse files
committed
Merge commit 'd5615acda1a7fdc4041a11c1736af139b8c7ebe8' into update_2022_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
2 parents 0ab4959 + d5615ac commit 8b53a6c

39 files changed

+695
-1091
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ rand = ["bitcoin/rand"]
1818
[dependencies]
1919
bitcoin = "0.29.1"
2020
elements = "0.21.0"
21-
bitcoin-miniscript = {package = "miniscript", git = "https://github.com/rust-bitcoin/rust-miniscript", rev = "51d66d1eab3930fbe053a33816d9f80f8ad57255"}
21+
bitcoin-miniscript = {package = "miniscript", git = "https://github.com/rust-bitcoin/rust-miniscript", rev = "d5615acda1a7fdc4041a11c1736af139b8c7ebe8"}
2222

2323
# Do NOT use this as a feature! Use the `serde` feature instead.
2424
actual-serde = { package = "serde", version = "1.0", optional = true }

examples/htlc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ fn main() {
4444
assert!(htlc_descriptor.sanity_check().is_ok());
4545
assert_eq!(
4646
format!("{}", htlc_descriptor),
47-
"elwsh(andor(pk(022222222222222222222222222222222222222222222222222222222222222222),sha256(1111111111111111111111111111111111111111111111111111111111111111),and_v(v:pkh(51814f108670aced2d77c1805ddd6634bc9d4731),older(4444))))#dq09llks"
47+
"elwsh(andor(pk(022222222222222222222222222222222222222222222222222222222222222222),sha256(1111111111111111111111111111111111111111111111111111111111111111),and_v(v:pkh(020202020202020202020202020202020202020202020202020202020202020202),older(4444))))#lfytrjen"
4848
);
4949

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

5656
// Get the scriptPpubkey for this Wsh descriptor.

examples/taproot.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ extern crate elements_miniscript as miniscript;
33
use std::collections::HashMap;
44
use std::str::FromStr;
55

6-
use bitcoin::hashes::{hash160, ripemd160, sha256};
76
use bitcoin::util::address::WitnessVersion;
87
use miniscript::descriptor::DescriptorType;
98
use miniscript::policy::Concrete;
10-
use miniscript::{hash256, Descriptor, Miniscript, NoExt, Tap, TranslatePk, Translator};
9+
use miniscript::{
10+
translate_hash_fail, Descriptor, Miniscript, NoExt, Tap, TranslatePk, Translator,
11+
};
1112
use secp256k1::{rand, KeyPair};
1213

1314
// Refer to https://github.com/sanket1729/adv_btc_workshop/blob/master/workshop.md#creating-a-taproot-descriptor
@@ -22,25 +23,10 @@ impl Translator<String, bitcoin::XOnlyPublicKey, ()> for StrPkTranslator {
2223
self.pk_map.get(pk).copied().ok_or(())
2324
}
2425

25-
fn pkh(&mut self, _pkh: &String) -> Result<hash160::Hash, ()> {
26-
unreachable!("Policy doesn't contain any pkh fragment");
27-
}
28-
29-
fn sha256(&mut self, _sha256: &String) -> Result<sha256::Hash, ()> {
30-
unreachable!("Policy does not contain any sha256 fragment");
31-
}
32-
33-
fn hash256(&mut self, _sha256: &String) -> Result<hash256::Hash, ()> {
34-
unreachable!("Policy does not contain any hash256 fragment");
35-
}
36-
37-
fn ripemd160(&mut self, _ripemd160: &String) -> Result<ripemd160::Hash, ()> {
38-
unreachable!("Policy does not contain any ripemd160 fragment");
39-
}
40-
41-
fn hash160(&mut self, _hash160: &String) -> Result<hash160::Hash, ()> {
42-
unreachable!("Policy does not contain any hash160 fragment");
43-
}
26+
// We don't need to implement these methods as we are not using them in the policy
27+
// Fail if we encounter any hash fragments.
28+
// See also translate_hash_clone! macro
29+
translate_hash_fail!(String, bitcoin::XOnlyPublicKey, ());
4430
}
4531

4632
fn main() {

src/descriptor/bare.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
175175
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
176176
where
177177
Pk: 'a,
178-
Pk::RawPkHash: 'a,
179178
{
180179
self.ms.for_each_key(pred)
181180
}
@@ -306,7 +305,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Pkh<Pk> {
306305

307306
impl<Pk: MiniscriptKey> Liftable<Pk> for Pkh<Pk> {
308307
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
309-
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
308+
Ok(semantic::Policy::Key(self.pk.clone()))
310309
}
311310
}
312311

@@ -341,7 +340,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
341340
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
342341
where
343342
Pk: 'a,
344-
Pk::RawPkHash: 'a,
345343
{
346344
pred(&self.pk)
347345
}

src/descriptor/csfs_cov/cov.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,6 @@ impl<Pk: MiniscriptKey, Ext: Extension> ForEachKey<Pk> for LegacyCSFSCov<Pk, Ext
460460
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
461461
where
462462
Pk: 'a,
463-
Pk::RawPkHash: 'a,
464463
{
465464
pred(&self.pk) && self.ms.for_any_key(pred)
466465
}

src/descriptor/key.rs

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,6 @@ impl<K: InnerXKey> DescriptorXKey<K> {
710710
}
711711

712712
impl MiniscriptKey for DescriptorPublicKey {
713-
// This allows us to be able to derive public keys even for PkH s
714-
type RawPkHash = Self;
715713
type Sha256 = sha256::Hash;
716714
type Hash256 = hash256::Hash;
717715
type Ripemd160 = ripemd160::Hash;
@@ -736,10 +734,6 @@ impl MiniscriptKey for DescriptorPublicKey {
736734
_ => false,
737735
}
738736
}
739-
740-
fn to_pubkeyhash(&self) -> Self {
741-
self.clone()
742-
}
743737
}
744738

745739
impl DefiniteDescriptorKey {
@@ -798,8 +792,6 @@ impl fmt::Display for DefiniteDescriptorKey {
798792
}
799793

800794
impl MiniscriptKey for DefiniteDescriptorKey {
801-
// This allows us to be able to derive public keys even for PkH s
802-
type RawPkHash = Self;
803795
type Sha256 = sha256::Hash;
804796
type Hash256 = hash256::Hash;
805797
type Ripemd160 = ripemd160::Hash;
@@ -812,10 +804,6 @@ impl MiniscriptKey for DefiniteDescriptorKey {
812804
fn is_x_only_key(&self) -> bool {
813805
self.0.is_x_only_key()
814806
}
815-
816-
fn to_pubkeyhash(&self) -> Self {
817-
self.clone()
818-
}
819807
}
820808

821809
impl ToPublicKey for DefiniteDescriptorKey {
@@ -824,10 +812,6 @@ impl ToPublicKey for DefiniteDescriptorKey {
824812
self.0.derive_public_key(&secp).unwrap()
825813
}
826814

827-
fn hash_to_hash160(hash: &Self) -> hash160::Hash {
828-
hash.to_public_key().to_pubkeyhash()
829-
}
830-
831815
fn to_sha256(hash: &sha256::Hash) -> sha256::Hash {
832816
*hash
833817
}

src/descriptor/mod.rs

Lines changed: 5 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,6 @@ impl<Pk: MiniscriptKey, T: Extension> ForEachKey<Pk> for Descriptor<Pk, T> {
755755
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
756756
where
757757
Pk: 'a,
758-
Pk::RawPkHash: 'a,
759758
{
760759
match *self {
761760
Descriptor::Bare(ref bare) => bare.for_each_key(pred),
@@ -796,25 +795,7 @@ impl<Ext: Extension + ParseableExt> Descriptor<DescriptorPublicKey, Ext> {
796795
Ok(pk.clone().at_derivation_index(self.0))
797796
}
798797

799-
fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<DefiniteDescriptorKey, ()> {
800-
Ok(pkh.clone().at_derivation_index(self.0))
801-
}
802-
803-
fn sha256(&mut self, sha256: &sha256::Hash) -> Result<sha256::Hash, ()> {
804-
Ok(*sha256)
805-
}
806-
807-
fn hash256(&mut self, hash256: &hash256::Hash) -> Result<hash256::Hash, ()> {
808-
Ok(*hash256)
809-
}
810-
811-
fn ripemd160(&mut self, ripemd160: &ripemd160::Hash) -> Result<ripemd160::Hash, ()> {
812-
Ok(*ripemd160)
813-
}
814-
815-
fn hash160(&mut self, hash160: &hash160::Hash) -> Result<hash160::Hash, ()> {
816-
Ok(*hash160)
817-
}
798+
translate_hash_clone!(DescriptorPublicKey, DescriptorPublicKey, ());
818799
}
819800
self.translate_pk(&mut Derivator(index))
820801
.expect("BIP 32 key index substitution cannot fail")
@@ -904,10 +885,6 @@ impl<Ext: Extension + ParseableExt> Descriptor<DescriptorPublicKey, Ext> {
904885
parse_key(pk, &mut self.0, self.1)
905886
}
906887

907-
fn pkh(&mut self, pkh: &String) -> Result<DescriptorPublicKey, Error> {
908-
parse_key(pkh, &mut self.0, self.1)
909-
}
910-
911888
fn sha256(&mut self, sha256: &String) -> Result<sha256::Hash, Error> {
912889
let hash =
913890
sha256::Hash::from_str(sha256).map_err(|e| Error::Unexpected(e.to_string()))?;
@@ -950,10 +927,6 @@ impl<Ext: Extension + ParseableExt> Descriptor<DescriptorPublicKey, Ext> {
950927
key_to_string(pk, self.0)
951928
}
952929

953-
fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<String, ()> {
954-
key_to_string(pkh, self.0)
955-
}
956-
957930
fn sha256(&mut self, sha256: &sha256::Hash) -> Result<String, ()> {
958931
Ok(sha256.to_string())
959932
}
@@ -1054,37 +1027,7 @@ impl<Ext: Extension> Descriptor<DefiniteDescriptorKey, Ext> {
10541027
pk.derive_public_key(&self.0)
10551028
}
10561029

1057-
fn pkh(
1058-
&mut self,
1059-
pkh: &DefiniteDescriptorKey,
1060-
) -> Result<bitcoin::hashes::hash160::Hash, ConversionError> {
1061-
Ok(pkh.derive_public_key(&self.0)?.to_pubkeyhash())
1062-
}
1063-
1064-
fn sha256(&mut self, sha256: &sha256::Hash) -> Result<sha256::Hash, ConversionError> {
1065-
Ok(*sha256)
1066-
}
1067-
1068-
fn hash256(
1069-
&mut self,
1070-
hash256: &hash256::Hash,
1071-
) -> Result<hash256::Hash, ConversionError> {
1072-
Ok(*hash256)
1073-
}
1074-
1075-
fn ripemd160(
1076-
&mut self,
1077-
ripemd160: &ripemd160::Hash,
1078-
) -> Result<ripemd160::Hash, ConversionError> {
1079-
Ok(*ripemd160)
1080-
}
1081-
1082-
fn hash160(
1083-
&mut self,
1084-
hash160: &hash160::Hash,
1085-
) -> Result<hash160::Hash, ConversionError> {
1086-
Ok(*hash160)
1087-
}
1030+
translate_hash_clone!(DefiniteDescriptorKey, bitcoin::PublicKey, ConversionError);
10881031
}
10891032

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

12001143
type StdDescriptor = Descriptor<PublicKey, CovenantExt<CovExtArgs>>;
12011144
const TEST_PK: &'static str =
@@ -1667,8 +1610,8 @@ mod tests {
16671610
assert_eq!(
16681611
descriptor,
16691612
format!(
1670-
"eltr({},{{pk({}),{{pk({}),or_d(pk({}),pkh(516ca378e588a7ed71336147e2a72848b20aca1a))}}}})#3cw4zduf",
1671-
p1, p2, p3, p4,
1613+
"eltr({},{{pk({}),{{pk({}),or_d(pk({}),pkh({}))}}}})#y9kzzx3w",
1614+
p1, p2, p3, p4, p5
16721615
)
16731616
)
16741617
}

src/descriptor/pegin/legacy_pegin.rs

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ impl fmt::Display for LegacyPeginKey {
9595
}
9696

9797
impl MiniscriptKey for LegacyPeginKey {
98-
type RawPkHash = hash160::Hash;
9998
type Sha256 = sha256::Hash;
10099
type Hash256 = hash256::Hash;
101100
type Ripemd160 = ripemd160::Hash;
@@ -104,14 +103,6 @@ impl MiniscriptKey for LegacyPeginKey {
104103
fn is_uncompressed(&self) -> bool {
105104
false
106105
}
107-
108-
fn to_pubkeyhash(&self) -> Self::RawPkHash {
109-
let pk = match *self {
110-
LegacyPeginKey::Functionary(ref pk) => pk,
111-
LegacyPeginKey::NonFunctionary(ref pk) => pk,
112-
};
113-
MiniscriptKey::to_pubkeyhash(pk)
114-
}
115106
}
116107

117108
/// Legacy Pegin Descriptor
@@ -248,17 +239,12 @@ impl<Pk: MiniscriptKey> LegacyPegin<Pk> {
248239
};
249240
struct TranslateUnTweak;
250241

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

256-
fn pkh(
257-
&mut self,
258-
_pkh: &<LegacyPeginKey as MiniscriptKey>::RawPkHash,
259-
) -> Result<<bitcoin::PublicKey as MiniscriptKey>::RawPkHash, ()> {
260-
unreachable!("No keyhashes in elements descriptors")
261-
}
247+
bitcoin_miniscript::translate_hash_clone!(LegacyPeginKey, bitcoin::PublicKey, ());
262248
}
263249
let mut t = TranslateUnTweak;
264250

src/descriptor/segwitv0.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
285285
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
286286
where
287287
Pk: 'a,
288-
Pk::RawPkHash: 'a,
289288
{
290289
match self.inner {
291290
WshInner::SortedMulti(ref smv) => smv.for_each_key(pred),
@@ -369,9 +368,7 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
369368
pub(super) fn from_inner_tree(top: &expression::Tree<'_>) -> Result<Self, Error>
370369
where
371370
Pk: FromStr,
372-
Pk::RawPkHash: FromStr,
373371
<Pk as FromStr>::Err: ToString,
374-
<<Pk as MiniscriptKey>::RawPkHash as FromStr>::Err: ToString,
375372
{
376373
if top.name == "wpkh" && top.args.len() == 1 {
377374
Ok(Wpkh::new(expression::terminal(&top.args[0], |pk| {
@@ -468,7 +465,7 @@ impl<Pk: MiniscriptKey> fmt::Display for Wpkh<Pk> {
468465

469466
impl<Pk: MiniscriptKey> Liftable<Pk> for Wpkh<Pk> {
470467
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
471-
Ok(semantic::Policy::KeyHash(self.pk.to_pubkeyhash()))
468+
Ok(semantic::Policy::Key(self.pk.clone()))
472469
}
473470
}
474471

@@ -503,7 +500,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
503500
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
504501
where
505502
Pk: 'a,
506-
Pk::RawPkHash: 'a,
507503
{
508504
pred(&self.pk)
509505
}

src/descriptor/sh.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Sh<Pk> {
5858
fn lift(&self) -> Result<semantic::Policy<Pk>, Error> {
5959
match self.inner {
6060
ShInner::Wsh(ref wsh) => wsh.lift(),
61-
ShInner::Wpkh(ref pk) => Ok(semantic::Policy::KeyHash(pk.as_inner().to_pubkeyhash())),
61+
ShInner::Wpkh(ref pk) => Ok(semantic::Policy::Key(pk.as_inner().clone())),
6262
ShInner::SortedMulti(ref smv) => smv.lift(),
6363
ShInner::Ms(ref ms) => ms.lift(),
6464
}
@@ -376,7 +376,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Sh<Pk> {
376376
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
377377
where
378378
Pk: 'a,
379-
Pk::RawPkHash: 'a,
380379
{
381380
match self.inner {
382381
ShInner::Wsh(ref wsh) => wsh.for_each_key(pred),

0 commit comments

Comments
 (0)