Skip to content

Commit 5f209c4

Browse files
committed
Remove sanity_checks() from DescriptorTrait
Now that `Descriptor` no longer implements `DesciptorTrait` we can remove the `sanity_checks` method from the trait entirely and implement the method directly on the individual descriptors.
1 parent c079d9b commit 5f209c4

File tree

5 files changed

+54
-62
lines changed

5 files changed

+54
-62
lines changed

src/descriptor/bare.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
6161
pub fn as_inner(&self) -> &Miniscript<Pk, BareCtx> {
6262
&self.ms
6363
}
64+
65+
/// Checks whether the descriptor is safe.
66+
pub fn sanity_check(&self) -> Result<(), Error> {
67+
self.ms.sanity_check()?;
68+
Ok(())
69+
}
6470
}
6571

6672
impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
@@ -134,11 +140,6 @@ where
134140
}
135141

136142
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
137-
fn sanity_check(&self) -> Result<(), Error> {
138-
self.ms.sanity_check()?;
139-
Ok(())
140-
}
141-
142143
fn script_pubkey(&self) -> Script
143144
where
144145
Pk: ToPublicKey,
@@ -336,10 +337,6 @@ where
336337
}
337338

338339
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
339-
fn sanity_check(&self) -> Result<(), Error> {
340-
Ok(())
341-
}
342-
343340
fn script_pubkey(&self) -> Script
344341
where
345342
Pk: ToPublicKey,

src/descriptor/mod.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@ pub type KeyMap = HashMap<DescriptorPublicKey, DescriptorSecretKey>;
7979
// because of traits cannot know underlying generic of Self.
8080
// Thus, we must implement additional trait for translate function
8181
pub trait DescriptorTrait<Pk: MiniscriptKey> {
82-
/// Whether the descriptor is safe
83-
/// Checks whether all the spend paths in the descriptor are possible
84-
/// on the bitcoin network under the current standardness and consensus rules
85-
/// Also checks whether the descriptor requires signauture on all spend paths
86-
/// And whether the script is malleable.
87-
/// In general, all the guarantees of miniscript hold only for safe scripts.
88-
/// All the analysis guarantees of miniscript only hold safe scripts.
89-
/// The signer may not be able to find satisfactions even if one exists
90-
fn sanity_check(&self) -> Result<(), Error>;
91-
9282
/// Computes the scriptpubkey of the descriptor
9383
fn script_pubkey(&self) -> Script
9484
where
@@ -433,18 +423,19 @@ impl<P: MiniscriptKey, Q: MiniscriptKey> TranslatePk<P, Q> for Descriptor<P> {
433423
}
434424

435425
impl<Pk: MiniscriptKey> Descriptor<Pk> {
436-
/// Whether the descriptor is safe
437-
/// Checks whether all the spend paths in the descriptor are possible
438-
/// on the bitcoin network under the current standardness and consensus rules
439-
/// Also checks whether the descriptor requires signauture on all spend paths
440-
/// And whether the script is malleable.
426+
/// Checks whether the descriptor is safe.
427+
///
428+
/// Checks whether all the spend paths in the descriptor are possible on the
429+
/// bitcoin network under the current standardness and consensus rules. Also
430+
/// checks whether the descriptor requires signatures on all spend paths and
431+
/// whether the script is malleable.
432+
///
441433
/// In general, all the guarantees of miniscript hold only for safe scripts.
442-
/// All the analysis guarantees of miniscript only hold safe scripts.
443-
/// The signer may not be able to find satisfactions even if one exists
434+
/// The signer may not be able to find satisfactions even if one exists.
444435
pub fn sanity_check(&self) -> Result<(), Error> {
445436
match *self {
446437
Descriptor::Bare(ref bare) => bare.sanity_check(),
447-
Descriptor::Pkh(ref pkh) => pkh.sanity_check(),
438+
Descriptor::Pkh(_) => Ok(()),
448439
Descriptor::Wpkh(ref wpkh) => wpkh.sanity_check(),
449440
Descriptor::Wsh(ref wsh) => wsh.sanity_check(),
450441
Descriptor::Sh(ref sh) => sh.sanity_check(),

src/descriptor/segwitv0.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ impl<Pk: MiniscriptKey> Wsh<Pk> {
7676
WshInner::Ms(ref ms) => format!("wsh({})", ms),
7777
}
7878
}
79+
80+
/// Checks whether the descriptor is safe.
81+
pub fn sanity_check(&self) -> Result<(), Error> {
82+
match self.inner {
83+
WshInner::SortedMulti(ref smv) => smv.sanity_check()?,
84+
WshInner::Ms(ref ms) => ms.sanity_check()?,
85+
}
86+
Ok(())
87+
}
7988
}
8089

8190
impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
@@ -190,14 +199,6 @@ where
190199
}
191200

192201
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
193-
fn sanity_check(&self) -> Result<(), Error> {
194-
match self.inner {
195-
WshInner::SortedMulti(ref smv) => smv.sanity_check()?,
196-
WshInner::Ms(ref ms) => ms.sanity_check()?,
197-
}
198-
Ok(())
199-
}
200-
201202
fn script_pubkey(&self) -> Script
202203
where
203204
Pk: ToPublicKey,
@@ -348,6 +349,17 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
348349
pub fn to_string_no_checksum(&self) -> String {
349350
format!("wpkh({})", self.pk)
350351
}
352+
353+
/// Checks whether the descriptor is safe.
354+
pub fn sanity_check(&self) -> Result<(), Error> {
355+
if self.pk.is_uncompressed() {
356+
Err(Error::ContextError(ScriptContextError::CompressedOnly(
357+
self.pk.to_string(),
358+
)))
359+
} else {
360+
Ok(())
361+
}
362+
}
351363
}
352364

353365
impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
@@ -442,16 +454,6 @@ where
442454
}
443455

444456
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
445-
fn sanity_check(&self) -> Result<(), Error> {
446-
if self.pk.is_uncompressed() {
447-
Err(Error::ContextError(ScriptContextError::CompressedOnly(
448-
self.pk.to_string(),
449-
)))
450-
} else {
451-
Ok(())
452-
}
453-
}
454-
455457
fn script_pubkey(&self) -> Script
456458
where
457459
Pk: ToPublicKey,

src/descriptor/sh.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,17 @@ impl<Pk: MiniscriptKey> Sh<Pk> {
205205
inner: ShInner::Wpkh(wpkh),
206206
}
207207
}
208+
209+
/// Checks whether the descriptor is safe.
210+
pub fn sanity_check(&self) -> Result<(), Error> {
211+
match self.inner {
212+
ShInner::Wsh(ref wsh) => wsh.sanity_check()?,
213+
ShInner::Wpkh(ref wpkh) => wpkh.sanity_check()?,
214+
ShInner::SortedMulti(ref smv) => smv.sanity_check()?,
215+
ShInner::Ms(ref ms) => ms.sanity_check()?,
216+
}
217+
Ok(())
218+
}
208219
}
209220

210221
impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
@@ -264,16 +275,6 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
264275
}
265276

266277
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
267-
fn sanity_check(&self) -> Result<(), Error> {
268-
match self.inner {
269-
ShInner::Wsh(ref wsh) => wsh.sanity_check()?,
270-
ShInner::Wpkh(ref wpkh) => wpkh.sanity_check()?,
271-
ShInner::SortedMulti(ref smv) => smv.sanity_check()?,
272-
ShInner::Ms(ref ms) => ms.sanity_check()?,
273-
}
274-
Ok(())
275-
}
276-
277278
fn script_pubkey(&self) -> Script
278279
where
279280
Pk: ToPublicKey,

src/descriptor/tr.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
271271
*self.spend_info.lock().expect("Lock poisoned") = Some(Arc::clone(&spend_info));
272272
spend_info
273273
}
274+
275+
/// Checks whether the descriptor is safe.
276+
pub fn sanity_check(&self) -> Result<(), Error> {
277+
for (_depth, ms) in self.iter_scripts() {
278+
ms.sanity_check()?;
279+
}
280+
Ok(())
281+
}
274282
}
275283

276284
impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
@@ -552,13 +560,6 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
552560
}
553561

554562
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
555-
fn sanity_check(&self) -> Result<(), Error> {
556-
for (_depth, ms) in self.iter_scripts() {
557-
ms.sanity_check()?;
558-
}
559-
Ok(())
560-
}
561-
562563
fn script_pubkey(&self) -> Script
563564
where
564565
Pk: ToPublicKey,

0 commit comments

Comments
 (0)