Skip to content

Commit a2de231

Browse files
committed
Remove Hash associated type from RawPkh.
Cleanup Translator trait by removing the translate_pkh method. RawPkh is not parsed from_str. It is only used in miniscript internally to parse scripts.
1 parent f4567c1 commit a2de231

30 files changed

+202
-723
lines changed

examples/taproot.rs

-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::HashMap;
22
use std::str::FromStr;
33

4-
use bitcoin::hashes::hash160;
54
use bitcoin::util::address::WitnessVersion;
65
use bitcoin::Network;
76
use miniscript::descriptor::DescriptorType;
@@ -21,10 +20,6 @@ impl Translator<String, bitcoin::XOnlyPublicKey, ()> for StrPkTranslator {
2120
self.pk_map.get(pk).copied().ok_or(())
2221
}
2322

24-
fn pkh(&mut self, _pkh: &String) -> Result<hash160::Hash, ()> {
25-
unreachable!("Policy doesn't contain any pkh fragment");
26-
}
27-
2823
// We don't need to implement these methods as we are not using them in the policy
2924
// Fail if we encounter any hash fragments.
3025
// See also translate_hash_clone! macro

src/descriptor/bare.rs

-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
167167
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
168168
where
169169
Pk: 'a,
170-
Pk::RawPkHash: 'a,
171170
{
172171
self.ms.for_each_key(pred)
173172
}
@@ -329,7 +328,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {
329328
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
330329
where
331330
Pk: 'a,
332-
Pk::RawPkHash: 'a,
333331
{
334332
pred(&self.pk)
335333
}

src/descriptor/key.rs

-16
Original file line numberDiff line numberDiff line change
@@ -730,8 +730,6 @@ impl<K: InnerXKey> DescriptorXKey<K> {
730730
}
731731

732732
impl MiniscriptKey for DescriptorPublicKey {
733-
// This allows us to be able to derive public keys even for PkH s
734-
type RawPkHash = Self;
735733
type Sha256 = sha256::Hash;
736734
type Hash256 = hash256::Hash;
737735
type Ripemd160 = ripemd160::Hash;
@@ -756,10 +754,6 @@ impl MiniscriptKey for DescriptorPublicKey {
756754
_ => false,
757755
}
758756
}
759-
760-
fn to_pubkeyhash(&self) -> Self {
761-
self.clone()
762-
}
763757
}
764758

765759
impl DefiniteDescriptorKey {
@@ -818,8 +812,6 @@ impl fmt::Display for DefiniteDescriptorKey {
818812
}
819813

820814
impl MiniscriptKey for DefiniteDescriptorKey {
821-
// This allows us to be able to derive public keys even for PkH s
822-
type RawPkHash = Self;
823815
type Sha256 = sha256::Hash;
824816
type Hash256 = hash256::Hash;
825817
type Ripemd160 = ripemd160::Hash;
@@ -832,10 +824,6 @@ impl MiniscriptKey for DefiniteDescriptorKey {
832824
fn is_x_only_key(&self) -> bool {
833825
self.0.is_x_only_key()
834826
}
835-
836-
fn to_pubkeyhash(&self) -> Self {
837-
self.clone()
838-
}
839827
}
840828

841829
impl ToPublicKey for DefiniteDescriptorKey {
@@ -844,10 +832,6 @@ impl ToPublicKey for DefiniteDescriptorKey {
844832
self.0.derive_public_key(&secp).unwrap()
845833
}
846834

847-
fn hash_to_hash160(hash: &Self) -> hash160::Hash {
848-
hash.to_public_key().to_pubkeyhash()
849-
}
850-
851835
fn to_sha256(hash: &sha256::Hash) -> sha256::Hash {
852836
*hash
853837
}

src/descriptor/mod.rs

+2-22
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
498498
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
499499
where
500500
Pk: 'a,
501-
Pk::RawPkHash: 'a,
502501
{
503502
match *self {
504503
Descriptor::Bare(ref bare) => bare.for_each_key(pred),
@@ -537,10 +536,6 @@ impl Descriptor<DescriptorPublicKey> {
537536
Ok(pk.clone().at_derivation_index(self.0))
538537
}
539538

540-
fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<DefiniteDescriptorKey, ()> {
541-
Ok(pkh.clone().at_derivation_index(self.0))
542-
}
543-
544539
translate_hash_clone!(DescriptorPublicKey, DescriptorPublicKey, ());
545540
}
546541
self.translate_pk(&mut Derivator(index))
@@ -631,10 +626,6 @@ impl Descriptor<DescriptorPublicKey> {
631626
parse_key(pk, &mut self.0, self.1)
632627
}
633628

634-
fn pkh(&mut self, pkh: &String) -> Result<DescriptorPublicKey, Error> {
635-
parse_key(pkh, &mut self.0, self.1)
636-
}
637-
638629
fn sha256(&mut self, sha256: &String) -> Result<sha256::Hash, Error> {
639630
let hash =
640631
sha256::Hash::from_str(sha256).map_err(|e| Error::Unexpected(e.to_string()))?;
@@ -677,10 +668,6 @@ impl Descriptor<DescriptorPublicKey> {
677668
key_to_string(pk, self.0)
678669
}
679670

680-
fn pkh(&mut self, pkh: &DescriptorPublicKey) -> Result<String, ()> {
681-
key_to_string(pkh, self.0)
682-
}
683-
684671
fn sha256(&mut self, sha256: &sha256::Hash) -> Result<String, ()> {
685672
Ok(sha256.to_string())
686673
}
@@ -778,13 +765,6 @@ impl Descriptor<DefiniteDescriptorKey> {
778765
pk.derive_public_key(&self.0)
779766
}
780767

781-
fn pkh(
782-
&mut self,
783-
pkh: &DefiniteDescriptorKey,
784-
) -> Result<bitcoin::hashes::hash160::Hash, ConversionError> {
785-
Ok(pkh.derive_public_key(&self.0)?.to_pubkeyhash())
786-
}
787-
788768
translate_hash_clone!(DefiniteDescriptorKey, bitcoin::PublicKey, ConversionError);
789769
}
790770

@@ -1344,8 +1324,8 @@ mod tests {
13441324
assert_eq!(
13451325
descriptor,
13461326
format!(
1347-
"tr({},{{pk({}),{{pk({}),or_d(pk({}),pkh(516ca378e588a7ed71336147e2a72848b20aca1a))}}}})#xz8ny8ae",
1348-
p1, p2, p3, p4,
1327+
"tr({},{{pk({}),{{pk({}),or_d(pk({}),pkh({}))}}}})#tvu28c0s",
1328+
p1, p2, p3, p4, p5
13491329
)
13501330
)
13511331
}

src/descriptor/segwitv0.rs

-2
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
249249
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
250250
where
251251
Pk: 'a,
252-
Pk::RawPkHash: 'a,
253252
{
254253
match self.inner {
255254
WshInner::SortedMulti(ref smv) => smv.for_each_key(pred),
@@ -442,7 +441,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {
442441
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
443442
where
444443
Pk: 'a,
445-
Pk::RawPkHash: 'a,
446444
{
447445
pred(&self.pk)
448446
}

src/descriptor/sh.rs

-1
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Sh<Pk> {
380380
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, pred: F) -> bool
381381
where
382382
Pk: 'a,
383-
Pk::RawPkHash: 'a,
384383
{
385384
match self.inner {
386385
ShInner::Wsh(ref wsh) => wsh.for_each_key(pred),

src/descriptor/sortedmulti.rs

-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> ForEachKey<Pk> for SortedMultiVec<Pk
115115
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
116116
where
117117
Pk: 'a,
118-
Pk::RawPkHash: 'a,
119118
{
120119
self.pks.iter().all(|key| pred(key))
121120
}

src/descriptor/tr.rs

-1
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,6 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {
559559
fn for_each_key<'a, F: FnMut(&'a Pk) -> bool>(&'a self, mut pred: F) -> bool
560560
where
561561
Pk: 'a,
562-
Pk::RawPkHash: 'a,
563562
{
564563
let script_keys_res = self
565564
.iter_scripts()

src/interpreter/inner.rs

+13-22
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use bitcoin::blockdata::witness::Witness;
1717
use bitcoin::hashes::{hash160, sha256, Hash};
1818
use bitcoin::util::taproot::{ControlBlock, TAPROOT_ANNEX_PREFIX};
1919

20-
use super::{stack, BitcoinKey, Error, Stack, TypedHash160};
21-
use crate::miniscript::context::{NoChecks, ScriptContext};
20+
use super::{stack, BitcoinKey, Error, Stack};
21+
use crate::miniscript::context::{NoChecks, ScriptContext, SigType};
2222
use crate::prelude::*;
23-
use crate::{BareCtx, Legacy, Miniscript, MiniscriptKey, Segwitv0, Tap, Translator};
23+
use crate::{BareCtx, Legacy, Miniscript, Segwitv0, Tap, ToPublicKey, Translator};
2424

2525
/// Attempts to parse a slice as a Bitcoin public key, checking compressedness
2626
/// if asked to, but otherwise dropping it
@@ -143,7 +143,8 @@ pub(super) fn from_txdata<'txin>(
143143
match ssig_stack.pop() {
144144
Some(elem) => {
145145
let pk = pk_from_stack_elem(&elem, false)?;
146-
if *spk == bitcoin::Script::new_p2pkh(&pk.to_pubkeyhash().into()) {
146+
if *spk == bitcoin::Script::new_p2pkh(&pk.to_pubkeyhash(SigType::Ecdsa).into())
147+
{
147148
Ok((
148149
Inner::PublicKey(pk.into(), PubkeyType::Pkh),
149150
ssig_stack,
@@ -164,11 +165,12 @@ pub(super) fn from_txdata<'txin>(
164165
match wit_stack.pop() {
165166
Some(elem) => {
166167
let pk = pk_from_stack_elem(&elem, true)?;
167-
if *spk == bitcoin::Script::new_v0_p2wpkh(&pk.to_pubkeyhash().into()) {
168+
let hash160 = pk.to_pubkeyhash(SigType::Ecdsa);
169+
if *spk == bitcoin::Script::new_v0_p2wpkh(&hash160.into()) {
168170
Ok((
169171
Inner::PublicKey(pk.into(), PubkeyType::Wpkh),
170172
wit_stack,
171-
Some(bitcoin::Script::new_p2pkh(&pk.to_pubkeyhash().into())), // bip143, why..
173+
Some(bitcoin::Script::new_p2pkh(&hash160.into())), // bip143, why..
172174
))
173175
} else {
174176
Err(Error::IncorrectWPubkeyHash)
@@ -274,17 +276,13 @@ pub(super) fn from_txdata<'txin>(
274276
Err(Error::NonEmptyScriptSig)
275277
} else {
276278
let pk = pk_from_stack_elem(&elem, true)?;
277-
if slice
278-
== &bitcoin::Script::new_v0_p2wpkh(
279-
&pk.to_pubkeyhash().into(),
280-
)[..]
279+
let hash160 = pk.to_pubkeyhash(SigType::Ecdsa);
280+
if slice == &bitcoin::Script::new_v0_p2wpkh(&hash160.into())[..]
281281
{
282282
Ok((
283283
Inner::PublicKey(pk.into(), PubkeyType::ShWpkh),
284284
wit_stack,
285-
Some(bitcoin::Script::new_p2pkh(
286-
&pk.to_pubkeyhash().into(),
287-
)), // bip143, why..
285+
Some(bitcoin::Script::new_p2pkh(&hash160.into())), // bip143, why..
288286
))
289287
} else {
290288
Err(Error::IncorrectWScriptHash)
@@ -382,10 +380,6 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::PublicKey, Ctx> {
382380
Ok(BitcoinKey::Fullkey(*pk))
383381
}
384382

385-
fn pkh(&mut self, pkh: &hash160::Hash) -> Result<TypedHash160, ()> {
386-
Ok(TypedHash160::FullKey(*pkh))
387-
}
388-
389383
translate_hash_clone!(bitcoin::PublicKey, BitcoinKey, ());
390384
}
391385

@@ -404,9 +398,6 @@ impl<Ctx: ScriptContext> ToNoChecks for Miniscript<bitcoin::XOnlyPublicKey, Ctx>
404398
Ok(BitcoinKey::XOnlyPublicKey(*pk))
405399
}
406400

407-
fn pkh(&mut self, pkh: &hash160::Hash) -> Result<TypedHash160, ()> {
408-
Ok(TypedHash160::XonlyKey(*pkh))
409-
}
410401
translate_hash_clone!(bitcoin::XOnlyPublicKey, BitcoinKey, ());
411402
}
412403
self.real_translate_pk(&mut TranslateXOnlyPk)
@@ -452,8 +443,8 @@ mod tests {
452443
)
453444
.unwrap();
454445

455-
let pkhash = key.to_pubkeyhash().into();
456-
let wpkhash = key.to_pubkeyhash().into();
446+
let pkhash = key.to_pubkeyhash(SigType::Ecdsa).into();
447+
let wpkhash = key.to_pubkeyhash(SigType::Ecdsa).into();
457448
let wpkh_spk = bitcoin::Script::new_v0_p2wpkh(&wpkhash);
458449
let wpkh_scripthash = hash160::Hash::hash(&wpkh_spk[..]).into();
459450

0 commit comments

Comments
 (0)