Skip to content

Commit df70352

Browse files
committed
Remove the spk method
The `spk` method was an infallible version of `script_pubkey`, now that `script_pubkey` is itself infallible we no longer need both methods. Remove `script_pubkey` for the `DescriptorTrait` and rename `spk` to `script_pubkey`.
1 parent 5f209c4 commit df70352

File tree

6 files changed

+22
-75
lines changed

6 files changed

+22
-75
lines changed

examples/parse.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ fn main() {
4747
// This allows us to call infallible methods for getting script pubkey
4848
if let Descriptor::Wsh(wsh) = &my_descriptor {
4949
assert_eq!(
50-
format!("{:x}", wsh.spk()),
50+
format!("{:x}", wsh.script_pubkey()),
5151
"0020daef16dd7c946a3e735a6e43310cb2ce33dfd14a04f76bf8241a16654cb2f0f9"
5252
);
5353
} else {

src/descriptor/bare.rs

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,22 +70,21 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
7070
}
7171

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

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

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

@@ -140,13 +139,6 @@ where
140139
}
141140

142141
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
143-
fn script_pubkey(&self) -> Script
144-
where
145-
Pk: ToPublicKey,
146-
{
147-
self.spk()
148-
}
149-
150142
fn unsigned_script_sig(&self) -> Script
151143
where
152144
Pk: ToPublicKey,
@@ -253,9 +245,8 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
253245
}
254246

255247
impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
256-
/// Obtain the corresponding script pubkey for this descriptor
257-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
258-
pub fn spk(&self) -> Script {
248+
/// Obtains the corresponding script pubkey for this descriptor.
249+
pub fn script_pubkey(&self) -> Script {
259250
let addr = Address::p2pkh(&self.pk.to_public_key(), Network::Bitcoin);
260251
addr.script_pubkey()
261252
}
@@ -268,13 +259,13 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
268259
/// Obtain the underlying miniscript for this descriptor
269260
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
270261
pub fn inner_script(&self) -> Script {
271-
self.spk()
262+
self.script_pubkey()
272263
}
273264

274265
/// Obtain the pre bip-340 signature script code for this descriptor
275266
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
276267
pub fn ecdsa_sighash_script_code(&self) -> Script {
277-
self.spk()
268+
self.script_pubkey()
278269
}
279270
}
280271

@@ -337,13 +328,6 @@ where
337328
}
338329

339330
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
340-
fn script_pubkey(&self) -> Script
341-
where
342-
Pk: ToPublicKey,
343-
{
344-
self.spk()
345-
}
346-
347331
fn unsigned_script_sig(&self) -> Script
348332
where
349333
Pk: ToPublicKey,

src/descriptor/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +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-
/// Computes the scriptpubkey of the descriptor
83-
fn script_pubkey(&self) -> Script
84-
where
85-
Pk: ToPublicKey;
86-
8782
/// Computes the scriptSig that will be in place for an unsigned
8883
/// input spending an output with this descriptor. For pre-segwit
8984
/// 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
@@ -88,9 +88,8 @@ impl<Pk: MiniscriptKey> Wsh<Pk> {
8888
}
8989

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

@@ -199,13 +198,6 @@ where
199198
}
200199

201200
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
202-
fn script_pubkey(&self) -> Script
203-
where
204-
Pk: ToPublicKey,
205-
{
206-
self.spk()
207-
}
208-
209201
fn unsigned_script_sig(&self) -> Script
210202
where
211203
Pk: ToPublicKey,
@@ -363,9 +355,8 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
363355
}
364356

365357
impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
366-
/// Obtain the corresponding script pubkey for this descriptor
367-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
368-
pub fn spk(&self) -> Script {
358+
/// Obtains the corresponding script pubkey for this descriptor.
359+
pub fn script_pubkey(&self) -> Script {
369360
let addr = Address::p2wpkh(&self.pk.to_public_key(), Network::Bitcoin)
370361
.expect("wpkh descriptors have compressed keys");
371362
addr.script_pubkey()
@@ -380,7 +371,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
380371
/// Obtain the underlying miniscript for this descriptor
381372
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
382373
pub fn inner_script(&self) -> Script {
383-
self.spk()
374+
self.script_pubkey()
384375
}
385376

386377
/// Obtain the pre bip-340 signature script code for this descriptor
@@ -454,13 +445,6 @@ where
454445
}
455446

456447
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
457-
fn script_pubkey(&self) -> Script
458-
where
459-
Pk: ToPublicKey,
460-
{
461-
self.spk()
462-
}
463-
464448
fn unsigned_script_sig(&self) -> Script
465449
where
466450
Pk: ToPublicKey,

src/descriptor/sh.rs

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

221221
impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
222-
/// Obtain the corresponding script pubkey for this descriptor
223-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
224-
pub fn spk(&self) -> Script {
222+
/// Obtains the corresponding script pubkey for this descriptor.
223+
pub fn script_pubkey(&self) -> Script {
225224
match self.inner {
226-
ShInner::Wsh(ref wsh) => wsh.spk().to_p2sh(),
227-
ShInner::Wpkh(ref wpkh) => wpkh.spk().to_p2sh(),
225+
ShInner::Wsh(ref wsh) => wsh.script_pubkey().to_p2sh(),
226+
ShInner::Wpkh(ref wpkh) => wpkh.script_pubkey().to_p2sh(),
228227
ShInner::SortedMulti(ref smv) => smv.encode().to_p2sh(),
229228
ShInner::Ms(ref ms) => ms.encode().to_p2sh(),
230229
}
@@ -253,7 +252,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
253252
pub fn inner_script(&self) -> Script {
254253
match self.inner {
255254
ShInner::Wsh(ref wsh) => wsh.inner_script(),
256-
ShInner::Wpkh(ref wpkh) => wpkh.spk(),
255+
ShInner::Wpkh(ref wpkh) => wpkh.script_pubkey(),
257256
ShInner::SortedMulti(ref smv) => smv.encode(),
258257
ShInner::Ms(ref ms) => ms.encode(),
259258
}
@@ -275,13 +274,6 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
275274
}
276275

277276
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
278-
fn script_pubkey(&self) -> Script
279-
where
280-
Pk: ToPublicKey,
281-
{
282-
self.spk()
283-
}
284-
285277
fn unsigned_script_sig(&self) -> Script
286278
where
287279
Pk: ToPublicKey,
@@ -295,7 +287,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
295287
.into_script()
296288
}
297289
ShInner::Wpkh(ref wpkh) => {
298-
let redeem_script = wpkh.spk();
290+
let redeem_script = wpkh.script_pubkey();
299291
script::Builder::new()
300292
.push_slice(&redeem_script[..])
301293
.into_script()

src/descriptor/tr.rs

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

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

562561
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
563-
fn script_pubkey(&self) -> Script
564-
where
565-
Pk: ToPublicKey,
566-
{
567-
self.spk()
568-
}
569-
570562
fn unsigned_script_sig(&self) -> Script
571563
where
572564
Pk: ToPublicKey,

0 commit comments

Comments
 (0)