Skip to content

Commit e08f833

Browse files
committed
Remove script_pubkey from DescriptorTrait
In preparation for removing the `DescriptorTrait` move the `script_pubkey` trait methods onto each individual descriptor. Replaces the `spk` method. Note that once upon a time `spk` was an infallible version of `script_pubkey` but now `script_pubkey` is itself infallible so this patch just renames the `spk` method and removes the trait method.
1 parent 3d0b112 commit e08f833

File tree

8 files changed

+41
-94
lines changed

8 files changed

+41
-94
lines changed

examples/htlc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn main() {
5353

5454
// Get the scriptPpubkey for this Wsh descriptor.
5555
assert_eq!(
56-
format!("{:x}", htlc_descriptor.spk()),
56+
format!("{:x}", htlc_descriptor.script_pubkey()),
5757
"0020d853877af928a8d2a569c9c0ed14bd16f6a80ce9cccaf8a6150fd8f7f8867ae2"
5858
);
5959

examples/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn main() {
4444
// us to call infallible methods for getting script pubkey.
4545
if let Descriptor::Wsh(wsh) = &desc {
4646
assert_eq!(
47-
format!("{:x}", wsh.spk()),
47+
format!("{:x}", wsh.script_pubkey()),
4848
"0020daef16dd7c946a3e735a6e43310cb2ce33dfd14a04f76bf8241a16654cb2f0f9"
4949
);
5050
}

src/descriptor/bare.rs

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,21 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
6969
}
7070

7171
impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
72-
/// Obtain the corresponding script pubkey for this descriptor
73-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
74-
pub fn spk(&self) -> Script {
72+
/// Obtains the corresponding script pubkey for this descriptor.
73+
pub fn script_pubkey(&self) -> Script {
7574
self.ms.encode()
7675
}
7776

7877
/// Obtain the underlying miniscript for this descriptor
7978
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
8079
pub fn inner_script(&self) -> Script {
81-
self.spk()
80+
self.script_pubkey()
8281
}
8382

8483
/// Obtain the pre bip-340 signature script code for this descriptor
8584
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
8685
pub fn ecdsa_sighash_script_code(&self) -> Script {
87-
self.spk()
86+
self.script_pubkey()
8887
}
8988
}
9089

@@ -139,13 +138,6 @@ where
139138
}
140139

141140
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
142-
fn script_pubkey(&self) -> Script
143-
where
144-
Pk: ToPublicKey,
145-
{
146-
self.spk()
147-
}
148-
149141
fn unsigned_script_sig(&self) -> Script
150142
where
151143
Pk: ToPublicKey,
@@ -249,10 +241,11 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
249241
}
250242

251243
impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
252-
/// Obtain the corresponding script pubkey for this descriptor
253-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
254-
pub fn spk(&self) -> Script {
255-
let addr = Address::p2pkh(&self.pk.to_public_key(), Network::Bitcoin);
244+
/// Obtains the corresponding script pubkey for this descriptor.
245+
pub fn script_pubkey(&self) -> Script {
246+
// Fine to hard code the `Network` here because we immediately call
247+
// `script_pubkey` which does not use the `network` field of `Address`.
248+
let addr = self.address(Network::Bitcoin);
256249
addr.script_pubkey()
257250
}
258251

@@ -264,13 +257,13 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
264257
/// Obtain the underlying miniscript for this descriptor
265258
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
266259
pub fn inner_script(&self) -> Script {
267-
self.spk()
260+
self.script_pubkey()
268261
}
269262

270263
/// Obtain the pre bip-340 signature script code for this descriptor
271264
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
272265
pub fn ecdsa_sighash_script_code(&self) -> Script {
273-
self.spk()
266+
self.script_pubkey()
274267
}
275268
}
276269

@@ -333,13 +326,6 @@ where
333326
}
334327

335328
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
336-
fn script_pubkey(&self) -> Script
337-
where
338-
Pk: ToPublicKey,
339-
{
340-
self.spk()
341-
}
342-
343329
fn unsigned_script_sig(&self) -> Script
344330
where
345331
Pk: ToPublicKey,

src/descriptor/mod.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +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-
/// Computes the scriptpubkey of the descriptor
80-
fn script_pubkey(&self) -> Script
81-
where
82-
Pk: ToPublicKey;
83-
8479
/// Computes the scriptSig that will be in place for an unsigned
8580
/// input spending an output with this descriptor. For pre-segwit
8681
/// descriptors, which use the scriptSig for signatures, this
@@ -416,6 +411,18 @@ impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
416411
Descriptor::Tr(ref tr) => Ok(tr.address(network)),
417412
}
418413
}
414+
415+
/// Computes the scriptpubkey of the descriptor.
416+
pub fn script_pubkey(&self) -> Script {
417+
match *self {
418+
Descriptor::Bare(ref bare) => bare.script_pubkey(),
419+
Descriptor::Pkh(ref pkh) => pkh.script_pubkey(),
420+
Descriptor::Wpkh(ref wpkh) => wpkh.script_pubkey(),
421+
Descriptor::Wsh(ref wsh) => wsh.script_pubkey(),
422+
Descriptor::Sh(ref sh) => sh.script_pubkey(),
423+
Descriptor::Tr(ref tr) => tr.script_pubkey(),
424+
}
425+
}
419426
}
420427

421428
impl<P, Q> TranslatePk<P, Q> for Descriptor<P>
@@ -449,21 +456,6 @@ where
449456
}
450457

451458
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
452-
/// Computes the scriptpubkey of the descriptor
453-
fn script_pubkey(&self) -> Script
454-
where
455-
Pk: ToPublicKey,
456-
{
457-
match *self {
458-
Descriptor::Bare(ref bare) => bare.script_pubkey(),
459-
Descriptor::Pkh(ref pkh) => pkh.script_pubkey(),
460-
Descriptor::Wpkh(ref wpkh) => wpkh.script_pubkey(),
461-
Descriptor::Wsh(ref wsh) => wsh.script_pubkey(),
462-
Descriptor::Sh(ref sh) => sh.script_pubkey(),
463-
Descriptor::Tr(ref tr) => tr.script_pubkey(),
464-
}
465-
}
466-
467459
/// Computes the scriptSig that will be in place for an unsigned
468460
/// input spending an output with this descriptor. For pre-segwit
469461
/// descriptors, which use the scriptSig for signatures, this

src/descriptor/segwitv0.rs

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,8 @@ impl<Pk: MiniscriptKey> Wsh<Pk> {
8686
}
8787

8888
impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
89-
/// Obtain the corresponding script pubkey for this descriptor
90-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
91-
pub fn spk(&self) -> Script {
89+
/// Obtains the corresponding script pubkey for this descriptor.
90+
pub fn script_pubkey(&self) -> Script {
9291
self.inner_script().to_v0_p2wsh()
9392
}
9493

@@ -197,13 +196,6 @@ where
197196
}
198197

199198
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
200-
fn script_pubkey(&self) -> Script
201-
where
202-
Pk: ToPublicKey,
203-
{
204-
self.spk()
205-
}
206-
207199
fn unsigned_script_sig(&self) -> Script
208200
where
209201
Pk: ToPublicKey,
@@ -356,9 +348,8 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
356348
}
357349

358350
impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
359-
/// Obtain the corresponding script pubkey for this descriptor
360-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
361-
pub fn spk(&self) -> Script {
351+
/// Obtains the corresponding script pubkey for this descriptor.
352+
pub fn script_pubkey(&self) -> Script {
362353
let addr = Address::p2wpkh(&self.pk.to_public_key(), Network::Bitcoin)
363354
.expect("wpkh descriptors have compressed keys");
364355
addr.script_pubkey()
@@ -373,7 +364,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
373364
/// Obtain the underlying miniscript for this descriptor
374365
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
375366
pub fn inner_script(&self) -> Script {
376-
self.spk()
367+
self.script_pubkey()
377368
}
378369

379370
/// Obtain the pre bip-340 signature script code for this descriptor
@@ -447,13 +438,6 @@ where
447438
}
448439

449440
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
450-
fn script_pubkey(&self) -> Script
451-
where
452-
Pk: ToPublicKey,
453-
{
454-
self.spk()
455-
}
456-
457441
fn unsigned_script_sig(&self) -> Script
458442
where
459443
Pk: ToPublicKey,

src/descriptor/sh.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -217,12 +217,11 @@ impl<Pk: MiniscriptKey> Sh<Pk> {
217217
}
218218

219219
impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
220-
/// Obtain the corresponding script pubkey for this descriptor
221-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
222-
pub fn spk(&self) -> Script {
220+
/// Obtains the corresponding script pubkey for this descriptor.
221+
pub fn script_pubkey(&self) -> Script {
223222
match self.inner {
224-
ShInner::Wsh(ref wsh) => wsh.spk().to_p2sh(),
225-
ShInner::Wpkh(ref wpkh) => wpkh.spk().to_p2sh(),
223+
ShInner::Wsh(ref wsh) => wsh.script_pubkey().to_p2sh(),
224+
ShInner::Wpkh(ref wpkh) => wpkh.script_pubkey().to_p2sh(),
226225
ShInner::SortedMulti(ref smv) => smv.encode().to_p2sh(),
227226
ShInner::Ms(ref ms) => ms.encode().to_p2sh(),
228227
}
@@ -254,7 +253,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
254253
pub fn inner_script(&self) -> Script {
255254
match self.inner {
256255
ShInner::Wsh(ref wsh) => wsh.inner_script(),
257-
ShInner::Wpkh(ref wpkh) => wpkh.spk(),
256+
ShInner::Wpkh(ref wpkh) => wpkh.script_pubkey(),
258257
ShInner::SortedMulti(ref smv) => smv.encode(),
259258
ShInner::Ms(ref ms) => ms.encode(),
260259
}
@@ -276,13 +275,6 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
276275
}
277276

278277
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
279-
fn script_pubkey(&self) -> Script
280-
where
281-
Pk: ToPublicKey,
282-
{
283-
self.spk()
284-
}
285-
286278
fn unsigned_script_sig(&self) -> Script
287279
where
288280
Pk: ToPublicKey,
@@ -296,7 +288,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
296288
.into_script()
297289
}
298290
ShInner::Wpkh(ref wpkh) => {
299-
let redeem_script = wpkh.spk();
291+
let redeem_script = wpkh.script_pubkey();
300292
script::Builder::new()
301293
.push_slice(&redeem_script[..])
302294
.into_script()

src/descriptor/tr.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,8 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
281281
}
282282

283283
impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
284-
/// Obtain the corresponding script pubkey for this descriptor
285-
/// Same as[`DescriptorTrait::script_pubkey`] for this descriptor
286-
pub fn spk(&self) -> Script {
284+
/// Obtains the corresponding script pubkey for this descriptor.
285+
pub fn script_pubkey(&self) -> Script {
287286
let output_key = self.spend_info().output_key();
288287
let builder = bitcoin::blockdata::script::Builder::new();
289288
builder
@@ -555,13 +554,6 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
555554
}
556555

557556
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
558-
fn script_pubkey(&self) -> Script
559-
where
560-
Pk: ToPublicKey,
561-
{
562-
self.spk()
563-
}
564-
565557
fn unsigned_script_sig(&self) -> Script
566558
where
567559
Pk: ToPublicKey,

src/psbt/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ use crate::miniscript::iter::PkPkh;
3434
use crate::miniscript::limits::SEQUENCE_LOCKTIME_DISABLE_FLAG;
3535
use crate::miniscript::satisfy::{After, Older};
3636
use crate::{
37-
descriptor, interpreter, Descriptor, DescriptorPublicKey, DescriptorTrait, MiniscriptKey,
38-
Preimage32, Satisfier, ToPublicKey, TranslatePk, TranslatePk2,
37+
descriptor, interpreter, Descriptor, DescriptorPublicKey, MiniscriptKey, Preimage32, Satisfier,
38+
ToPublicKey, TranslatePk, TranslatePk2,
3939
};
4040

4141
mod finalizer;
@@ -1152,6 +1152,7 @@ mod tests {
11521152
use bitcoin::{OutPoint, TxIn, TxOut, XOnlyPublicKey};
11531153

11541154
use super::*;
1155+
use crate::descriptor::DescriptorTrait;
11551156
use crate::Miniscript;
11561157

11571158
#[test]

0 commit comments

Comments
 (0)