Skip to content

Commit 8b773e6

Browse files
committed
Remove script_code() from DescriptorTrait
Now that `Descriptor` no longer implements `DesciptorTrait` we can remove the `script_code` method from the trait entirely and implement `scipt_code` directly on `Descriptor` calling through to `ecdsa_sighash_script_code` for all non-taproot descriptors.
1 parent f7b27d5 commit 8b773e6

File tree

5 files changed

+23
-78
lines changed

5 files changed

+23
-78
lines changed

src/descriptor/bare.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
8080
self.script_pubkey()
8181
}
8282

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

176168
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Bare<Pk> {
@@ -246,8 +238,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
246238
self.script_pubkey()
247239
}
248240

249-
/// Obtain the pre bip-340 signature script code for this descriptor
250-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
241+
/// Obtains the pre bip-340 signature script code for this descriptor.
251242
pub fn ecdsa_sighash_script_code(&self) -> Script {
252243
self.script_pubkey()
253244
}
@@ -341,13 +332,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
341332
fn max_satisfaction_weight(&self) -> Result<usize, Error> {
342333
Ok(4 * (1 + 73 + BareCtx::pk_len(&self.pk)))
343334
}
344-
345-
fn script_code(&self) -> Result<Script, Error>
346-
where
347-
Pk: ToPublicKey,
348-
{
349-
Ok(self.ecdsa_sighash_script_code())
350-
}
351335
}
352336

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

src/descriptor/mod.rs

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

131121
/// Script descriptor
@@ -484,6 +474,24 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
484474
}
485475
}
486476

477+
/// Computes the `scriptCode` of a transaction output.
478+
///
479+
/// The `scriptCode` is the Script of the previous transaction output being
480+
/// serialized in the sighash when evaluating a `CHECKSIG` & co. OP code.
481+
///
482+
/// # Errors
483+
/// If the descriptor is a taproot descriptor.
484+
pub fn script_code(&self) -> Result<Script, Error> {
485+
match *self {
486+
Descriptor::Bare(ref bare) => Ok(bare.ecdsa_sighash_script_code()),
487+
Descriptor::Pkh(ref pkh) => Ok(pkh.ecdsa_sighash_script_code()),
488+
Descriptor::Wpkh(ref wpkh) => Ok(wpkh.ecdsa_sighash_script_code()),
489+
Descriptor::Wsh(ref wsh) => Ok(wsh.ecdsa_sighash_script_code()),
490+
Descriptor::Sh(ref sh) => Ok(sh.ecdsa_sighash_script_code()),
491+
Descriptor::Tr(_) => Err(Error::TrNoScriptCode),
492+
}
493+
}
494+
487495
/// Returns satisfying non-malleable witness and scriptSig to spend an
488496
/// output controlled by the given descriptor if it possible to
489497
/// construct one using the satisfier S.
@@ -546,22 +554,6 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
546554
Descriptor::Tr(ref tr) => tr.max_satisfaction_weight(),
547555
}
548556
}
549-
550-
/// Get the `scriptCode` of a transaction output.
551-
///
552-
/// The `scriptCode` is the Script of the previous transaction output being serialized in the
553-
/// sighash when evaluating a `CHECKSIG` & co. OP code.
554-
/// Returns Error for Tr descriptors
555-
pub fn script_code(&self) -> Result<Script, Error> {
556-
match *self {
557-
Descriptor::Bare(ref bare) => bare.script_code(),
558-
Descriptor::Pkh(ref pkh) => pkh.script_code(),
559-
Descriptor::Wpkh(ref wpkh) => wpkh.script_code(),
560-
Descriptor::Wsh(ref wsh) => wsh.script_code(),
561-
Descriptor::Sh(ref sh) => sh.script_code(),
562-
Descriptor::Tr(ref tr) => tr.script_code(),
563-
}
564-
}
565557
}
566558

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

src/descriptor/segwitv0.rs

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
109109
}
110110
}
111111

112-
/// Obtain the pre bip-340 signature script code for this descriptor
113-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
112+
/// Obtains the pre bip-340 signature script code for this descriptor.
114113
pub fn ecdsa_sighash_script_code(&self) -> Script {
115114
self.inner_script()
116115
}
@@ -245,13 +244,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
245244
varint_len(max_sat_elems) +
246245
max_sat_size)
247246
}
248-
249-
fn script_code(&self) -> Result<Script, Error>
250-
where
251-
Pk: ToPublicKey,
252-
{
253-
Ok(self.ecdsa_sighash_script_code())
254-
}
255247
}
256248

257249
impl<Pk: MiniscriptKey> ForEachKey<Pk> for Wsh<Pk> {
@@ -358,8 +350,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
358350
self.script_pubkey()
359351
}
360352

361-
/// Obtain the pre bip-340 signature script code for this descriptor
362-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
353+
/// Obtains the pre bip-340 signature script code for this descriptor.
363354
pub fn ecdsa_sighash_script_code(&self) -> Script {
364355
// For SegWit outputs, it is defined by bip-0143 (quoted below) and is different from
365356
// the previous txo's scriptPubKey.
@@ -455,13 +446,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
455446
fn max_satisfaction_weight(&self) -> Result<usize, Error> {
456447
Ok(4 + 1 + 73 + Segwitv0::pk_len(&self.pk))
457448
}
458-
459-
fn script_code(&self) -> Result<Script, Error>
460-
where
461-
Pk: ToPublicKey,
462-
{
463-
Ok(self.ecdsa_sighash_script_code())
464-
}
465449
}
466450

467451
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
@@ -598,13 +598,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
598598
}
599599
max_wieght.ok_or(Error::ImpossibleSatisfaction)
600600
}
601-
602-
fn script_code(&self) -> Result<Script, Error>
603-
where
604-
Pk: ToPublicKey,
605-
{
606-
Err(Error::TrNoScriptCode)
607-
}
608601
}
609602

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

0 commit comments

Comments
 (0)