Skip to content

Commit 4a9e20f

Browse files
committed
Remove sanity_check from DescriptorTrait
In preparation for removing the `DescriptorTrait` move the `sanity_check` trait methods onto each individual descriptor.
1 parent 4980604 commit 4a9e20f

File tree

5 files changed

+65
-72
lines changed

5 files changed

+65
-72
lines changed

src/descriptor/bare.rs

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

6571
impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
@@ -133,11 +139,6 @@ where
133139
}
134140

135141
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
136-
fn sanity_check(&self) -> Result<(), Error> {
137-
self.ms.sanity_check()?;
138-
Ok(())
139-
}
140-
141142
fn address(&self, _network: Network) -> Result<Address, Error>
142143
where
143144
Pk: ToPublicKey,
@@ -340,10 +341,6 @@ where
340341
}
341342

342343
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
343-
fn sanity_check(&self) -> Result<(), Error> {
344-
Ok(())
345-
}
346-
347344
fn address(&self, network: Network) -> Result<Address, Error>
348345
where
349346
Pk: ToPublicKey,

src/descriptor/mod.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,6 @@ pub type KeyMap = HashMap<DescriptorPublicKey, DescriptorSecretKey>;
7676
// because of traits cannot know underlying generic of Self.
7777
// Thus, we must implement additional trait for translate function
7878
pub trait DescriptorTrait<Pk: MiniscriptKey> {
79-
/// Whether the descriptor is safe
80-
/// Checks whether all the spend paths in the descriptor are possible
81-
/// on the bitcoin network under the current standardness and consensus rules
82-
/// Also checks whether the descriptor requires signauture on all spend paths
83-
/// And whether the script is malleable.
84-
/// In general, all the guarantees of miniscript hold only for safe scripts.
85-
/// All the analysis guarantees of miniscript only hold safe scripts.
86-
/// The signer may not be able to find satisfactions even if one exists
87-
fn sanity_check(&self) -> Result<(), Error>;
88-
8979
/// Computes the Bitcoin address of the descriptor, if one exists
9080
/// Some descriptors like pk() don't have any address.
9181
/// Errors:
@@ -395,6 +385,26 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
395385
Descriptor::Tr(ref _tr) => DescriptorType::Tr,
396386
}
397387
}
388+
389+
/// Checks whether the descriptor is safe.
390+
///
391+
/// Checks whether all the spend paths in the descriptor are possible on the
392+
/// bitcoin network under the current standardness and consensus rules. Also
393+
/// checks whether the descriptor requires signatures on all spend paths and
394+
/// whether the script is malleable.
395+
///
396+
/// In general, all the guarantees of miniscript hold only for safe scripts.
397+
/// The signer may not be able to find satisfactions even if one exists.
398+
pub fn sanity_check(&self) -> Result<(), Error> {
399+
match *self {
400+
Descriptor::Bare(ref bare) => bare.sanity_check(),
401+
Descriptor::Pkh(_) => Ok(()),
402+
Descriptor::Wpkh(ref wpkh) => wpkh.sanity_check(),
403+
Descriptor::Wsh(ref wsh) => wsh.sanity_check(),
404+
Descriptor::Sh(ref sh) => sh.sanity_check(),
405+
Descriptor::Tr(ref tr) => tr.sanity_check(),
406+
}
407+
}
398408
}
399409

400410
impl<P, Q> TranslatePk<P, Q> for Descriptor<P>
@@ -428,24 +438,6 @@ where
428438
}
429439

430440
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
431-
/// Whether the descriptor is safe
432-
/// Checks whether all the spend paths in the descriptor are possible
433-
/// on the bitcoin network under the current standardness and consensus rules
434-
/// Also checks whether the descriptor requires signauture on all spend paths
435-
/// And whether the script is malleable.
436-
/// In general, all the guarantees of miniscript hold only for safe scripts.
437-
/// All the analysis guarantees of miniscript only hold safe scripts.
438-
/// The signer may not be able to find satisfactions even if one exists
439-
fn sanity_check(&self) -> Result<(), Error> {
440-
match *self {
441-
Descriptor::Bare(ref bare) => bare.sanity_check(),
442-
Descriptor::Pkh(ref pkh) => pkh.sanity_check(),
443-
Descriptor::Wpkh(ref wpkh) => wpkh.sanity_check(),
444-
Descriptor::Wsh(ref wsh) => wsh.sanity_check(),
445-
Descriptor::Sh(ref sh) => sh.sanity_check(),
446-
Descriptor::Tr(ref tr) => tr.sanity_check(),
447-
}
448-
}
449441
/// Computes the Bitcoin address of the descriptor, if one exists
450442
fn address(&self, network: Network) -> Result<Address, Error>
451443
where

src/descriptor/segwitv0.rs

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

7988
impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
@@ -189,14 +198,6 @@ where
189198
}
190199

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

354366
impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
@@ -444,16 +456,6 @@ where
444456
}
445457

446458
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
447-
fn sanity_check(&self) -> Result<(), Error> {
448-
if self.pk.is_uncompressed() {
449-
Err(Error::ContextError(ScriptContextError::CompressedOnly(
450-
self.pk.to_string(),
451-
)))
452-
} else {
453-
Ok(())
454-
}
455-
}
456-
457459
fn address(&self, network: Network) -> Result<Address, Error>
458460
where
459461
Pk: ToPublicKey,

src/descriptor/sh.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,17 @@ impl<Pk: MiniscriptKey> Sh<Pk> {
180180
}
181181
}
182182

183+
/// Checks whether the descriptor is safe.
184+
pub fn sanity_check(&self) -> Result<(), Error> {
185+
match self.inner {
186+
ShInner::Wsh(ref wsh) => wsh.sanity_check()?,
187+
ShInner::Wpkh(ref wpkh) => wpkh.sanity_check()?,
188+
ShInner::SortedMulti(ref smv) => smv.sanity_check()?,
189+
ShInner::Ms(ref ms) => ms.sanity_check()?,
190+
}
191+
Ok(())
192+
}
193+
183194
/// Create a new p2sh wrapped wsh sortedmulti descriptor from threshold
184195
/// `k` and Vec of `pks`
185196
pub fn new_wsh_sortedmulti(k: usize, pks: Vec<Pk>) -> Result<Self, Error> {
@@ -263,16 +274,6 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
263274
}
264275

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

src/descriptor/tr.rs

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

275283
impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
@@ -548,13 +556,6 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
548556
}
549557

550558
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
551-
fn sanity_check(&self) -> Result<(), Error> {
552-
for (_depth, ms) in self.iter_scripts() {
553-
ms.sanity_check()?;
554-
}
555-
Ok(())
556-
}
557-
558559
fn address(&self, network: Network) -> Result<Address, Error>
559560
where
560561
Pk: ToPublicKey,

0 commit comments

Comments
 (0)