Skip to content

Commit 52a480d

Browse files
committed
Remove script_code from DescriptorTrait
In preparation for removing the `DescriptorTrait` move the `script_code` trait methods onto each individual descriptor. Fix docs on related methods also (e.g. `inner_scipt`). Implement `script_code` directly on `Descriptor`.
1 parent c6884fc commit 52a480d

File tree

5 files changed

+27
-85
lines changed

5 files changed

+27
-85
lines changed

src/descriptor/bare.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,12 @@ impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
7474
self.ms.encode()
7575
}
7676

77-
/// Obtain the underlying miniscript for this descriptor
77+
/// Obtains the underlying miniscript for this descriptor.
7878
pub fn inner_script(&self) -> Script {
7979
self.script_pubkey()
8080
}
8181

82-
/// Obtain the pre bip-340 signature script code for this descriptor
83-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
82+
/// Obtains the pre bip-340 signature script code for this descriptor.
8483
pub fn ecdsa_sighash_script_code(&self) -> Script {
8584
self.script_pubkey()
8685
}
@@ -163,13 +162,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
163162
let scriptsig_len = self.ms.max_satisfaction_size()?;
164163
Ok(4 * (varint_len(scriptsig_len) + scriptsig_len))
165164
}
166-
167-
fn script_code(&self) -> Result<Script, Error>
168-
where
169-
Pk: ToPublicKey,
170-
{
171-
Ok(self.ecdsa_sighash_script_code())
172-
}
173165
}
174166

175167
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
@@ -239,13 +231,12 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
239231
Address::p2pkh(&self.pk.to_public_key(), network)
240232
}
241233

242-
/// Obtain the underlying miniscript for this descriptor
234+
/// Obtains the underlying miniscript for this descriptor.
243235
pub fn inner_script(&self) -> Script {
244236
self.script_pubkey()
245237
}
246238

247-
/// Obtain the pre bip-340 signature script code for this descriptor
248-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
239+
/// Obtains the pre bip-340 signature script code for this descriptor.
249240
pub fn ecdsa_sighash_script_code(&self) -> Script {
250241
self.script_pubkey()
251242
}
@@ -339,13 +330,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
339330
fn max_satisfaction_weight(&self) -> Result<usize, Error> {
340331
Ok(4 * (1 + 73 + BareCtx::pk_len(&self.pk)))
341332
}
342-
343-
fn script_code(&self) -> Result<Script, Error>
344-
where
345-
Pk: ToPublicKey,
346-
{
347-
Ok(self.ecdsa_sighash_script_code())
348-
}
349333
}
350334

351335
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Pkh<Pk> {

src/descriptor/mod.rs

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,6 @@ pub trait DescriptorTrait<Pk: MiniscriptKey> {
113113
/// scriptSig and witness stack length.
114114
/// Returns Error when the descriptor is impossible to safisfy (ex: sh(OP_FALSE))
115115
fn max_satisfaction_weight(&self) -> Result<usize, Error>;
116-
117-
/// Get the `scriptCode` of a transaction output.
118-
///
119-
/// The `scriptCode` is the Script of the previous transaction output being serialized in the
120-
/// sighash when evaluating a `CHECKSIG` & co. OP code.
121-
/// Errors:
122-
/// - When the descriptor is Tr
123-
fn script_code(&self) -> Result<Script, Error>
124-
where
125-
Pk: ToPublicKey;
126116
}
127117

128118
/// Script descriptor
@@ -437,6 +427,24 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
437427
Descriptor::Tr(_) => Err(Error::TrNoScriptCode),
438428
}
439429
}
430+
431+
/// Computes the `scriptCode` of a transaction output.
432+
///
433+
/// The `scriptCode` is the Script of the previous transaction output being
434+
/// serialized in the sighash when evaluating a `CHECKSIG` & co. OP code.
435+
///
436+
/// # Errors
437+
/// If the descriptor is a taproot descriptor.
438+
pub fn script_code(&self) -> Result<Script, Error> {
439+
match *self {
440+
Descriptor::Bare(ref bare) => Ok(bare.ecdsa_sighash_script_code()),
441+
Descriptor::Pkh(ref pkh) => Ok(pkh.ecdsa_sighash_script_code()),
442+
Descriptor::Wpkh(ref wpkh) => Ok(wpkh.ecdsa_sighash_script_code()),
443+
Descriptor::Wsh(ref wsh) => Ok(wsh.ecdsa_sighash_script_code()),
444+
Descriptor::Sh(ref sh) => Ok(sh.ecdsa_sighash_script_code()),
445+
Descriptor::Tr(_) => Err(Error::TrNoScriptCode),
446+
}
447+
}
440448
}
441449

442450
impl<P, Q> TranslatePk<P, Q> for Descriptor<P>
@@ -520,25 +528,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
520528
Descriptor::Tr(ref tr) => tr.max_satisfaction_weight(),
521529
}
522530
}
523-
524-
/// Get the `scriptCode` of a transaction output.
525-
///
526-
/// The `scriptCode` is the Script of the previous transaction output being serialized in the
527-
/// sighash when evaluating a `CHECKSIG` & co. OP code.
528-
/// Returns Error for Tr descriptors
529-
fn script_code(&self) -> Result<Script, Error>
530-
where
531-
Pk: ToPublicKey,
532-
{
533-
match *self {
534-
Descriptor::Bare(ref bare) => bare.script_code(),
535-
Descriptor::Pkh(ref pkh) => pkh.script_code(),
536-
Descriptor::Wpkh(ref wpkh) => wpkh.script_code(),
537-
Descriptor::Wsh(ref wsh) => wsh.script_code(),
538-
Descriptor::Sh(ref sh) => sh.script_code(),
539-
Descriptor::Tr(ref tr) => tr.script_code(),
540-
}
541-
}
542531
}
543532

544533
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {

src/descriptor/segwitv0.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,15 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
9999
}
100100
}
101101

102-
/// Obtain the underlying miniscript for this descriptor
102+
/// Obtains the underlying miniscript for this descriptor.
103103
pub fn inner_script(&self) -> Script {
104104
match self.inner {
105105
WshInner::SortedMulti(ref smv) => smv.encode(),
106106
WshInner::Ms(ref ms) => ms.encode(),
107107
}
108108
}
109109

110-
/// Obtain the pre bip-340 signature script code for this descriptor
111-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
110+
/// Obtains the pre bip-340 signature script code for this descriptor.
112111
pub fn ecdsa_sighash_script_code(&self) -> Script {
113112
self.inner_script()
114113
}
@@ -243,13 +242,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
243242
varint_len(max_sat_elems) +
244243
max_sat_size)
245244
}
246-
247-
fn script_code(&self) -> Result<Script, Error>
248-
where
249-
Pk: ToPublicKey,
250-
{
251-
Ok(self.ecdsa_sighash_script_code())
252-
}
253245
}
254246

255247
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
@@ -346,13 +338,12 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
346338
.expect("Rust Miniscript types don't allow uncompressed pks in segwit descriptors")
347339
}
348340

349-
/// Obtain the underlying miniscript for this descriptor
341+
/// Obtains the underlying miniscript for this descriptor.
350342
pub fn inner_script(&self) -> Script {
351343
self.script_pubkey()
352344
}
353345

354-
/// Obtain the pre bip-340 signature script code for this descriptor
355-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
346+
/// Obtains the pre bip-340 signature script code for this descriptor.
356347
pub fn ecdsa_sighash_script_code(&self) -> Script {
357348
// For SegWit outputs, it is defined by bip-0143 (quoted below) and is different from
358349
// the previous txo's scriptPubKey.
@@ -448,13 +439,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
448439
fn max_satisfaction_weight(&self) -> Result<usize, Error> {
449440
Ok(4 + 1 + 73 + Segwitv0::pk_len(&self.pk))
450441
}
451-
452-
fn script_code(&self) -> Result<Script, Error>
453-
where
454-
Pk: ToPublicKey,
455-
{
456-
Ok(self.ecdsa_sighash_script_code())
457-
}
458442
}
459443

460444
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wpkh<Pk> {

src/descriptor/sh.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
258258
}
259259
}
260260

261-
/// Obtain the pre bip-340 signature script code for this descriptor
262-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
261+
/// Obtains the pre bip-340 signature script code for this descriptor.
263262
pub fn ecdsa_sighash_script_code(&self) -> Script {
264263
match self.inner {
265264
// - For P2WSH witness program, if the witnessScript does not contain any `OP_CODESEPARATOR`,
@@ -374,13 +373,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
374373
}
375374
})
376375
}
377-
378-
fn script_code(&self) -> Result<Script, Error>
379-
where
380-
Pk: ToPublicKey,
381-
{
382-
Ok(self.ecdsa_sighash_script_code())
383-
}
384376
}
385377

386378
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Sh<Pk> {

src/descriptor/tr.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -593,13 +593,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
593593
}
594594
max_wieght.ok_or(Error::ImpossibleSatisfaction)
595595
}
596-
597-
fn script_code(&self) -> Result<Script, Error>
598-
where
599-
Pk: ToPublicKey,
600-
{
601-
Err(Error::TrNoScriptCode)
602-
}
603596
}
604597

605598
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Tr<Pk> {

0 commit comments

Comments
 (0)