Skip to content

Commit 7e5bd23

Browse files
committed
Remove the DescriptorTrait
The only remaining methods in `DescriptorTrait` are the get satisfaction and satisfy methods. As we did for all the other methods we can implement the two `get_*` methods on the individual descriptor types and put `satisfy` directly on `Descriptor`.
1 parent 7f2a531 commit 7e5bd23

File tree

9 files changed

+63
-84
lines changed

9 files changed

+63
-84
lines changed

examples/htlc.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ extern crate miniscript;
2020
use bitcoin::Network;
2121
use miniscript::descriptor::Wsh;
2222
use miniscript::policy::{Concrete, Liftable};
23-
use miniscript::DescriptorTrait;
2423
use std::str::FromStr;
2524

2625
fn main() {

integration_test/src/test_cpp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use miniscript::miniscript::iter;
1313
use miniscript::psbt::PsbtExt;
1414
use miniscript::MiniscriptKey;
1515
use miniscript::Segwitv0;
16-
use miniscript::{Descriptor, DescriptorTrait, Miniscript};
16+
use miniscript::{Descriptor, Miniscript};
1717
use std::collections::BTreeMap;
1818
use std::fs::File;
1919
use std::io::{self, BufRead};

integration_test/src/test_desc.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use bitcoin::util::{psbt, sighash};
1313
use bitcoin::{self, Amount, OutPoint, SchnorrSig, Script, Transaction, TxIn, TxOut, Txid};
1414
use bitcoincore_rpc::{json, Client, RpcApi};
1515
use miniscript::miniscript::iter;
16-
use miniscript::psbt::{PsbtInputExt, PsbtExt};
17-
use miniscript::{Descriptor, DescriptorTrait, Miniscript, ToPublicKey};
16+
use miniscript::psbt::{PsbtExt, PsbtInputExt};
17+
use miniscript::{Descriptor, Miniscript, ToPublicKey};
1818
use miniscript::{MiniscriptKey, ScriptContext};
1919
use std::collections::BTreeMap;
2020

src/descriptor/bare.rs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ use crate::{
3131
TranslatePk,
3232
};
3333

34-
use super::{
35-
checksum::{desc_checksum, verify_checksum},
36-
DescriptorTrait,
37-
};
34+
use super::checksum::{desc_checksum, verify_checksum};
3835

3936
/// Create a Bare Descriptor. That is descriptor that is
4037
/// not wrapped in sh or wsh. This covers the Pk descriptor
@@ -150,10 +147,12 @@ where
150147
}
151148
}
152149

153-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
154-
fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
150+
impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
151+
/// Returns satisfying non-malleable witness and scriptSig with minimum
152+
/// weight to spend an output controlled by the given descriptor if it is
153+
/// possible to construct one using the `satisfier`.
154+
pub fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
155155
where
156-
Pk: ToPublicKey,
157156
S: Satisfier<Pk>,
158157
{
159158
let ms = self.ms.satisfy(satisfier)?;
@@ -162,9 +161,11 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
162161
Ok((witness, script_sig))
163162
}
164163

165-
fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
164+
/// Returns satisfying, possibly malleable, witness and scriptSig with
165+
/// minimum weight to spend an output controlled by the given descriptor if
166+
/// it is possible to construct one using the `satisfier`.
167+
pub fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
166168
where
167-
Pk: ToPublicKey,
168169
S: Satisfier<Pk>,
169170
{
170171
let ms = self.ms.satisfy_malleable(satisfier)?;
@@ -323,10 +324,12 @@ where
323324
}
324325
}
325326

326-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
327-
fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
327+
impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
328+
/// Returns satisfying non-malleable witness and scriptSig with minimum
329+
/// weight to spend an output controlled by the given descriptor if it is
330+
/// possible to construct one using the `satisfier`.
331+
pub fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
328332
where
329-
Pk: ToPublicKey,
330333
S: Satisfier<Pk>,
331334
{
332335
if let Some(sig) = satisfier.lookup_ecdsa_sig(&self.pk) {
@@ -342,9 +345,11 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
342345
}
343346
}
344347

345-
fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
348+
/// Returns satisfying, possibly malleable, witness and scriptSig with
349+
/// minimum weight to spend an output controlled by the given descriptor if
350+
/// it is possible to construct one using the `satisfier`.
351+
pub fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
346352
where
347-
Pk: ToPublicKey,
348353
S: Satisfier<Pk>,
349354
{
350355
self.get_satisfaction(satisfier)

src/descriptor/mod.rs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -72,45 +72,6 @@ pub use self::key::{
7272
/// public key from the descriptor.
7373
pub type KeyMap = HashMap<DescriptorPublicKey, DescriptorSecretKey>;
7474

75-
/// A general trait for Bitcoin descriptor.
76-
/// Offers function for witness cost estimation, script pubkey creation
77-
/// satisfaction using the [Satisfier] trait.
78-
// Unfortunately, the translation function cannot be added to trait
79-
// because of traits cannot know underlying generic of Self.
80-
// Thus, we must implement additional trait for translate function
81-
pub trait DescriptorTrait<Pk: MiniscriptKey> {
82-
/// Returns satisfying non-malleable witness and scriptSig with minimum weight to spend an
83-
/// output controlled by the given descriptor if it possible to
84-
/// construct one using the satisfier S.
85-
fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
86-
where
87-
Pk: ToPublicKey,
88-
S: Satisfier<Pk>;
89-
90-
/// Returns satisfying, possibly malleable witness and scriptSig to spend an
91-
/// output controlled by the given descriptor if it possible to
92-
/// construct one using the satisfier S.
93-
fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
94-
where
95-
Pk: ToPublicKey,
96-
S: Satisfier<Pk>;
97-
98-
/// Attempts to produce a non-malleable satisfying witness and scriptSig to spend an
99-
/// output controlled by the given descriptor; add the data to a given
100-
/// `TxIn` output.
101-
fn satisfy<S>(&self, txin: &mut TxIn, satisfier: S) -> Result<(), Error>
102-
where
103-
Pk: ToPublicKey,
104-
S: Satisfier<Pk>,
105-
{
106-
// easy default implementation
107-
let (witness, script_sig) = self.get_satisfaction(satisfier)?;
108-
txin.witness = Witness::from_vec(witness);
109-
txin.script_sig = script_sig;
110-
Ok(())
111-
}
112-
}
113-
11475
/// Script descriptor
11576
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
11677
pub enum Descriptor<Pk: MiniscriptKey> {

src/descriptor/segwitv0.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use crate::{
3131

3232
use super::{
3333
checksum::{desc_checksum, verify_checksum},
34-
DescriptorTrait, SortedMultiVec,
34+
SortedMultiVec,
3535
};
3636
/// A Segwitv0 wsh descriptor
3737
#[derive(Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
@@ -224,10 +224,12 @@ where
224224
}
225225
}
226226

227-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
228-
fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
227+
impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
228+
/// Returns satisfying non-malleable witness and scriptSig with minimum
229+
/// weight to spend an output controlled by the given descriptor if it is
230+
/// possible to construct one using the `satisfier`.
231+
pub fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
229232
where
230-
Pk: ToPublicKey,
231233
S: Satisfier<Pk>,
232234
{
233235
let mut witness = match self.inner {
@@ -240,9 +242,11 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
240242
Ok((witness, script_sig))
241243
}
242244

243-
fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
245+
/// Returns satisfying, possibly malleable, witness and scriptSig with
246+
/// minimum weight to spend an output controlled by the given descriptor if
247+
/// it is possible to construct one using the `satisfier`.
248+
pub fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
244249
where
245-
Pk: ToPublicKey,
246250
S: Satisfier<Pk>,
247251
{
248252
let mut witness = match self.inner {
@@ -438,10 +442,12 @@ where
438442
}
439443
}
440444

441-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
442-
fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
445+
impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
446+
/// Returns satisfying non-malleable witness and scriptSig with minimum
447+
/// weight to spend an output controlled by the given descriptor if it is
448+
/// possible to construct one using the `satisfier`.
449+
pub fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
443450
where
444-
Pk: ToPublicKey,
445451
S: Satisfier<Pk>,
446452
{
447453
if let Some(sig) = satisfier.lookup_ecdsa_sig(&self.pk) {
@@ -454,9 +460,11 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
454460
}
455461
}
456462

457-
fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
463+
/// Returns satisfying, possibly malleable, witness and scriptSig with
464+
/// minimum weight to spend an output controlled by the given descriptor if
465+
/// it is possible to construct one using the `satisfier`.
466+
pub fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
458467
where
459-
Pk: ToPublicKey,
460468
S: Satisfier<Pk>,
461469
{
462470
self.get_satisfaction(satisfier)

src/descriptor/sh.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use crate::{
3434

3535
use super::{
3636
checksum::{desc_checksum, verify_checksum},
37-
DescriptorTrait, SortedMultiVec, Wpkh, Wsh,
37+
SortedMultiVec, Wpkh, Wsh,
3838
};
3939

4040
/// A Legacy p2sh Descriptor
@@ -278,7 +278,6 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
278278
}
279279

280280
/// Obtain the underlying miniscript for this descriptor
281-
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
282281
pub fn inner_script(&self) -> Script {
283282
match self.inner {
284283
ShInner::Wsh(ref wsh) => wsh.inner_script(),
@@ -328,10 +327,12 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
328327
}
329328
}
330329

331-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
332-
fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
330+
impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
331+
/// Returns satisfying non-malleable witness and scriptSig with minimum
332+
/// weight to spend an output controlled by the given descriptor if it is
333+
/// possible to construct one using the `satisfier`.
334+
pub fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
333335
where
334-
Pk: ToPublicKey,
335336
S: Satisfier<Pk>,
336337
{
337338
let script_sig = self.unsigned_script_sig();
@@ -361,9 +362,11 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
361362
}
362363
}
363364

364-
fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
365+
/// Returns satisfying, possibly malleable, witness and scriptSig with
366+
/// minimum weight to spend an output controlled by the given descriptor if
367+
/// it is possible to construct one using the `satisfier`.
368+
pub fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
365369
where
366-
Pk: ToPublicKey,
367370
S: Satisfier<Pk>,
368371
{
369372
let script_sig = self.unsigned_script_sig();

src/descriptor/tr.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::policy::semantic::Policy;
44
use crate::policy::Liftable;
55
use crate::util::{varint_len, witness_size};
6-
use crate::{DescriptorTrait, ForEach, ForEachKey, Satisfier, ToPublicKey, TranslatePk};
6+
use crate::{ForEach, ForEachKey, Satisfier, ToPublicKey, TranslatePk};
77

88
use super::checksum::{desc_checksum, verify_checksum};
99
use crate::errstr;
@@ -591,18 +591,22 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
591591
}
592592
}
593593

594-
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
595-
fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
594+
impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
595+
/// Returns satisfying non-malleable witness and scriptSig with minimum
596+
/// weight to spend an output controlled by the given descriptor if it is
597+
/// possible to construct one using the `satisfier`.
598+
pub fn get_satisfaction<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
596599
where
597-
Pk: ToPublicKey,
598600
S: Satisfier<Pk>,
599601
{
600602
best_tap_spend(&self, satisfier, false /* allow_mall */)
601603
}
602604

603-
fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
605+
/// Returns satisfying, possibly malleable, witness and scriptSig with
606+
/// minimum weight to spend an output controlled by the given descriptor if
607+
/// it is possible to construct one using the `satisfier`.
608+
pub fn get_satisfaction_mall<S>(&self, satisfier: S) -> Result<(Vec<Vec<u8>>, Script), Error>
604609
where
605-
Pk: ToPublicKey,
606610
S: Satisfier<Pk>,
607611
{
608612
best_tap_spend(&self, satisfier, true /* allow_mall */)

src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
//! extern crate miniscript;
5858
//!
5959
//! use std::str::FromStr;
60-
//! use miniscript::{DescriptorTrait};
6160
//!
6261
//! fn main() {
6362
//! let desc = miniscript::Descriptor::<
@@ -122,7 +121,7 @@ use std::{error, fmt, hash, str};
122121
use bitcoin::blockdata::{opcodes, script};
123122
use bitcoin::hashes::{hash160, sha256, Hash};
124123

125-
pub use crate::descriptor::{Descriptor, DescriptorPublicKey, DescriptorTrait};
124+
pub use crate::descriptor::{Descriptor, DescriptorPublicKey};
126125
pub use crate::interpreter::Interpreter;
127126
pub use crate::miniscript::context::{BareCtx, Legacy, ScriptContext, Segwitv0, Tap};
128127
pub use crate::miniscript::decode::Terminal;
@@ -714,7 +713,7 @@ impl fmt::Display for Error {
714713
write!(f, "MultiA too many keys {}", k)
715714
}
716715
Error::TaprootSpendInfoUnavialable => {
717-
write!(f, "Taproot Spend Info not computed. Hint: Did you call `compute_spend_info` before calling methods from DescriptorTrait")
716+
write!(f, "Taproot Spend Info not computed")
718717
}
719718
Error::TrNoScriptCode => {
720719
write!(f, "No script code for Tr descriptors")

0 commit comments

Comments
 (0)