Skip to content

Commit a56fe85

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 `spk`. Please note, patch includes some docs changes to other `DescriptorTrait` methods, a few of these are transitory but since, I believe, they assist in understanding the reasoning/correctness of the current refactoring work they have been left in.
1 parent 2a14fc9 commit a56fe85

File tree

6 files changed

+66
-98
lines changed

6 files changed

+66
-98
lines changed

examples/htlc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ fn main() {
5555
);
5656

5757
assert_eq!(
58-
format!("{:x}", htlc_descriptor.spk()),
58+
format!("{:x}", htlc_descriptor.script_pubkey()),
5959
"0020d853877af928a8d2a569c9c0ed14bd16f6a80ce9cccaf8a6150fd8f7f8867ae2"
6060
);
6161

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: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,18 @@ impl<Pk: MiniscriptKey> Bare<Pk> {
6464
}
6565

6666
impl<Pk: MiniscriptKey + ToPublicKey> Bare<Pk> {
67-
/// Obtain the corresponding script pubkey for this descriptor
68-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
69-
pub fn spk(&self) -> Script {
70-
self.ms.encode()
71-
}
72-
73-
/// Obtain the underlying miniscript for this descriptor
74-
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
67+
/// Obtains the underlying miniscript for this descriptor.
68+
/// Called by [`DescriptorTrait::explicit_script`] for this descriptor.
69+
/// Equivalent to [`DescriptorTrait::script_pubkey`] for this descriptor.
7570
pub fn inner_script(&self) -> Script {
76-
self.spk()
71+
self.script_pubkey()
7772
}
7873

79-
/// Obtain the pre bip-340 signature script code for this descriptor
80-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
74+
/// Obtains the pre bip-340 signature script code for this descriptor.
75+
/// Called by [`DescriptorTrait::script_code`] for this descriptor.
76+
/// Equivalent to [`DescriptorTrait::script_pubkey`] for this descriptor.
8177
pub fn ecdsa_sighash_script_code(&self) -> Script {
82-
self.spk()
78+
self.script_pubkey()
8379
}
8480
}
8581

@@ -150,7 +146,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
150146
where
151147
Pk: ToPublicKey,
152148
{
153-
self.spk()
149+
self.ms.encode()
154150
}
155151

156152
fn unsigned_script_sig(&self) -> Script
@@ -259,29 +255,23 @@ impl<Pk: MiniscriptKey> Pkh<Pk> {
259255
}
260256

261257
impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
262-
/// Obtain the corresponding script pubkey for this descriptor
263-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
264-
pub fn spk(&self) -> Script {
265-
let addr = Address::p2pkh(&self.pk.to_public_key(), Network::Bitcoin);
266-
addr.script_pubkey()
267-
}
268-
269-
/// Obtain the corresponding script pubkey for this descriptor
270-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
258+
/// Obtains the [`Address`] for this descriptor.
271259
pub fn addr(&self, network: Network) -> Address {
272260
Address::p2pkh(&self.pk.to_public_key(), network)
273261
}
274262

275-
/// Obtain the underlying miniscript for this descriptor
276-
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
263+
/// Obtains the underlying miniscript for this descriptor.
264+
/// Called by [`DescriptorTrait::explicit_script`] for this descriptor.
265+
/// Equivalent to [`DescriptorTrait::script_pubkey`] for this descriptor.
277266
pub fn inner_script(&self) -> Script {
278-
self.spk()
267+
self.script_pubkey()
279268
}
280269

281-
/// Obtain the pre bip-340 signature script code for this descriptor
282-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
270+
/// Obtain the pre bip-340 signature script code for this descriptor.
271+
/// Called by [`DescriptorTrait::script_code`] for this descriptor.
272+
/// Equivalent to [`DescriptorTrait::script_pubkey`] for this descriptor.
283273
pub fn ecdsa_sighash_script_code(&self) -> Script {
284-
self.spk()
274+
self.script_pubkey()
285275
}
286276
}
287277

@@ -359,7 +349,9 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
359349
where
360350
Pk: ToPublicKey,
361351
{
362-
self.spk()
352+
// TODO: This is broken, remove the hard coded network.
353+
let addr = Address::p2pkh(&self.pk.to_public_key(), Network::Bitcoin);
354+
addr.script_pubkey()
363355
}
364356

365357
fn unsigned_script_sig(&self) -> Script

src/descriptor/segwitv0.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -79,32 +79,27 @@ impl<Pk: MiniscriptKey> Wsh<Pk> {
7979
}
8080

8181
impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
82-
/// Obtain the corresponding script pubkey for this descriptor
83-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
84-
pub fn spk(&self) -> Script {
85-
self.inner_script().to_v0_p2wsh()
86-
}
87-
88-
/// Obtain the corresponding script pubkey for this descriptor
89-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
82+
/// Obtains the corresponding script pubkey for this descriptor.
83+
/// Called by [`DescriptorTrait::address`] for this descriptor.
9084
pub fn addr(&self, network: Network) -> Address {
9185
match self.inner {
9286
WshInner::SortedMulti(ref smv) => Address::p2wsh(&smv.encode(), network),
9387
WshInner::Ms(ref ms) => Address::p2wsh(&ms.encode(), network),
9488
}
9589
}
9690

97-
/// Obtain the underlying miniscript for this descriptor
98-
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
91+
/// Obtains the underlying miniscript for this descriptor.
92+
/// Called by [`DescriptorTrait::explicit_script`] for this descriptor.
9993
pub fn inner_script(&self) -> Script {
10094
match self.inner {
10195
WshInner::SortedMulti(ref smv) => smv.encode(),
10296
WshInner::Ms(ref ms) => ms.encode(),
10397
}
10498
}
10599

106-
/// Obtain the pre bip-340 signature script code for this descriptor
107-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
100+
/// Obtains the pre bip-340 signature script code for this descriptor.
101+
/// Called by [`DescriptorTrait::script_code`] for this descriptor.
102+
/// Equivalent to `self.inner_script`.
108103
pub fn ecdsa_sighash_script_code(&self) -> Script {
109104
self.inner_script()
110105
}
@@ -210,7 +205,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
210205
where
211206
Pk: ToPublicKey,
212207
{
213-
self.spk()
208+
self.inner_script().to_v0_p2wsh()
214209
}
215210

216211
fn unsigned_script_sig(&self) -> Script
@@ -359,29 +354,21 @@ impl<Pk: MiniscriptKey> Wpkh<Pk> {
359354
}
360355

361356
impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
362-
/// Obtain the corresponding script pubkey for this descriptor
363-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
364-
pub fn spk(&self) -> Script {
365-
let addr = Address::p2wpkh(&self.pk.to_public_key(), Network::Bitcoin)
366-
.expect("wpkh descriptors have compressed keys");
367-
addr.script_pubkey()
368-
}
369-
370-
/// Obtain the corresponding script pubkey for this descriptor
371-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
357+
/// Obtains the [`Address`] for this descriptor.
372358
pub fn addr(&self, network: Network) -> Address {
373359
Address::p2wpkh(&self.pk.to_public_key(), network)
374360
.expect("Rust Miniscript types don't allow uncompressed pks in segwit descriptors")
375361
}
376362

377-
/// Obtain the underlying miniscript for this descriptor
378-
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
363+
/// Obtains the underlying miniscript for this descriptor.
364+
/// Called by [`DescriptorTrait::explicit_script`] for this descriptor.
365+
/// Equivalent to [`DescriptorTrait::script_pubkey`] for this descriptor.
379366
pub fn inner_script(&self) -> Script {
380-
self.spk()
367+
self.script_pubkey()
381368
}
382369

383-
/// Obtain the pre bip-340 signature script code for this descriptor
384-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
370+
/// Obtains the pre bip-340 signature script code for this descriptor.
371+
/// Called by [`DescriptorTrait::script_code`] for this descriptor.
385372
pub fn ecdsa_sighash_script_code(&self) -> Script {
386373
// For SegWit outputs, it is defined by bip-0143 (quoted below) and is different from
387374
// the previous txo's scriptPubKey.
@@ -472,7 +459,10 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
472459
where
473460
Pk: ToPublicKey,
474461
{
475-
self.spk()
462+
// TODO: This is broken, remove the hard coded network.
463+
let addr = Address::p2wpkh(&self.pk.to_public_key(), Network::Bitcoin)
464+
.expect("wpkh descriptors have compressed keys");
465+
addr.script_pubkey()
476466
}
477467

478468
fn unsigned_script_sig(&self) -> Script

src/descriptor/sh.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -208,26 +208,14 @@ impl<Pk: MiniscriptKey> Sh<Pk> {
208208
}
209209

210210
impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
211-
/// Obtain the corresponding script pubkey for this descriptor
212-
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
213-
pub fn spk(&self) -> Script {
214-
match self.inner {
215-
ShInner::Wsh(ref wsh) => wsh.spk().to_p2sh(),
216-
ShInner::Wpkh(ref wpkh) => wpkh.spk().to_p2sh(),
217-
ShInner::SortedMulti(ref smv) => smv.encode().to_p2sh(),
218-
ShInner::Ms(ref ms) => ms.encode().to_p2sh(),
219-
}
220-
}
221-
222-
/// Obtain the corresponding script pubkey for this descriptor
223-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
211+
/// Obtains the [`Address`] for this descriptor.
224212
pub fn addr(&self, network: Network) -> Address {
225213
match self.inner {
226214
ShInner::Wsh(ref wsh) => {
227-
Address::p2sh(&wsh.spk(), network).expect("Size checked in Miniscript")
215+
Address::p2sh(&wsh.script_pubkey(), network).expect("Size checked in Miniscript")
228216
}
229217
ShInner::Wpkh(ref wpkh) => {
230-
Address::p2sh(&wpkh.spk(), network).expect("Size checked in Miniscript")
218+
Address::p2sh(&wpkh.script_pubkey(), network).expect("Size checked in Miniscript")
231219
}
232220
ShInner::SortedMulti(ref smv) => {
233221
Address::p2sh(&smv.encode(), network).expect("Size checked in Miniscript")
@@ -238,19 +226,19 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
238226
}
239227
}
240228

241-
/// Obtain the underlying miniscript for this descriptor
242-
/// Non failing verion of [`DescriptorTrait::explicit_script`] for this descriptor
229+
/// Obtains the underlying miniscript for this descriptor.
230+
/// Called by [`DescriptorTrait::explicit_script`] for this descriptor.
243231
pub fn inner_script(&self) -> Script {
244232
match self.inner {
245233
ShInner::Wsh(ref wsh) => wsh.inner_script(),
246-
ShInner::Wpkh(ref wpkh) => wpkh.spk(),
234+
ShInner::Wpkh(ref wpkh) => wpkh.inner_script(),
247235
ShInner::SortedMulti(ref smv) => smv.encode(),
248236
ShInner::Ms(ref ms) => ms.encode(),
249237
}
250238
}
251239

252-
/// Obtain the pre bip-340 signature script code for this descriptor
253-
/// Non failing verion of [`DescriptorTrait::script_code`] for this descriptor
240+
/// Obtain the pre bip-340 signature script code for this descriptor.
241+
/// Called by [`DescriptorTrait::script_code`] for this descriptor.
254242
pub fn ecdsa_sighash_script_code(&self) -> Script {
255243
match self.inner {
256244
// - For P2WSH witness program, if the witnessScript does not contain any `OP_CODESEPARATOR`,
@@ -280,8 +268,8 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
280268
Pk: ToPublicKey,
281269
{
282270
match self.inner {
283-
ShInner::Wsh(ref wsh) => Ok(Address::p2sh(&wsh.spk(), network)?),
284-
ShInner::Wpkh(ref wpkh) => Ok(Address::p2sh(&wpkh.spk(), network)?),
271+
ShInner::Wsh(ref wsh) => Ok(Address::p2sh(&wsh.script_pubkey(), network)?),
272+
ShInner::Wpkh(ref wpkh) => Ok(Address::p2sh(&wpkh.script_pubkey(), network)?),
285273
ShInner::SortedMulti(ref smv) => Ok(Address::p2sh(&smv.encode(), network)?),
286274
ShInner::Ms(ref ms) => Ok(Address::p2sh(&ms.encode(), network)?),
287275
}
@@ -291,7 +279,12 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
291279
where
292280
Pk: ToPublicKey,
293281
{
294-
self.spk()
282+
match self.inner {
283+
ShInner::Wsh(ref wsh) => wsh.script_pubkey().to_p2sh(),
284+
ShInner::Wpkh(ref wpkh) => wpkh.script_pubkey().to_p2sh(),
285+
ShInner::SortedMulti(ref smv) => smv.encode().to_p2sh(),
286+
ShInner::Ms(ref ms) => ms.encode().to_p2sh(),
287+
}
295288
}
296289

297290
fn unsigned_script_sig(&self) -> Script
@@ -307,7 +300,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
307300
.into_script()
308301
}
309302
ShInner::Wpkh(ref wpkh) => {
310-
let redeem_script = wpkh.spk();
303+
let redeem_script = wpkh.script_pubkey();
311304
script::Builder::new()
312305
.push_slice(&redeem_script[..])
313306
.into_script()

src/descriptor/tr.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -274,19 +274,7 @@ impl<Pk: MiniscriptKey> Tr<Pk> {
274274
}
275275

276276
impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
277-
/// Obtain the corresponding script pubkey for this descriptor
278-
/// Same as[`DescriptorTrait::script_pubkey`] for this descriptor
279-
pub fn spk(&self) -> Script {
280-
let output_key = self.spend_info().output_key();
281-
let builder = bitcoin::blockdata::script::Builder::new();
282-
builder
283-
.push_opcode(opcodes::all::OP_PUSHNUM_1)
284-
.push_slice(&output_key.serialize())
285-
.into_script()
286-
}
287-
288-
/// Obtain the corresponding script pubkey for this descriptor
289-
/// Same as[`DescriptorTrait::address`] for this descriptor
277+
/// Obtains the [`Address`] for this descriptor.
290278
pub fn addr(&self, network: Network) -> Result<Address, Error> {
291279
let spend_info = self.spend_info();
292280
Ok(Address::p2tr_tweaked(spend_info.output_key(), network))
@@ -571,7 +559,12 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
571559
where
572560
Pk: ToPublicKey,
573561
{
574-
self.spk()
562+
let output_key = self.spend_info().output_key();
563+
let builder = bitcoin::blockdata::script::Builder::new();
564+
builder
565+
.push_opcode(opcodes::all::OP_PUSHNUM_1)
566+
.push_slice(&output_key.serialize())
567+
.into_script()
575568
}
576569

577570
fn unsigned_script_sig(&self) -> Script

0 commit comments

Comments
 (0)