Skip to content

Commit c079d9b

Browse files
committed
Remove address() from DescriptorTrait
Now that `Descriptor` no longer implements `DesciptorTrait` we can remove the `address` method from the trait entirely and rename the `addr` helper method to `address` for each descriptor.
1 parent 2861645 commit c079d9b

File tree

5 files changed

+34
-89
lines changed

5 files changed

+34
-89
lines changed

src/descriptor/bare.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
139139
Ok(())
140140
}
141141

142-
fn address(&self, _network: Network) -> Result<Address, Error>
143-
where
144-
Pk: ToPublicKey,
145-
{
146-
Err(Error::BareDescriptorAddr)
147-
}
148-
149142
fn script_pubkey(&self) -> Script
150143
where
151144
Pk: ToPublicKey,
@@ -266,9 +259,8 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
266259
addr.script_pubkey()
267260
}
268261

269-
/// Obtain the corresponding script pubkey for this descriptor
270-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
271-
pub fn addr(&self, network: Network) -> Address {
262+
/// Obtains the corresponding script pubkey for this descriptor.
263+
pub fn address(&self, network: Network) -> Address {
272264
Address::p2pkh(&self.pk.to_public_key(), network)
273265
}
274266

@@ -348,13 +340,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
348340
Ok(())
349341
}
350342

351-
fn address(&self, network: Network) -> Result<Address, Error>
352-
where
353-
Pk: ToPublicKey,
354-
{
355-
Ok(self.addr(network))
356-
}
357-
358343
fn script_pubkey(&self) -> Script
359344
where
360345
Pk: ToPublicKey,

src/descriptor/mod.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,6 @@ pub trait DescriptorTrait<Pk: MiniscriptKey> {
8989
/// The signer may not be able to find satisfactions even if one exists
9090
fn sanity_check(&self) -> Result<(), Error>;
9191

92-
/// Computes the Bitcoin address of the descriptor, if one exists
93-
/// Some descriptors like pk() don't have any address.
94-
/// Errors:
95-
/// - On raw/bare descriptors that don't have any address
96-
fn address(&self, network: Network) -> Result<Address, Error>
97-
where
98-
Pk: ToPublicKey;
99-
10092
/// Computes the scriptpubkey of the descriptor
10193
fn script_pubkey(&self) -> Script
10294
where
@@ -463,14 +455,19 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
463455

464456
impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
465457
/// Computes the Bitcoin address of the descriptor, if one exists
458+
///
459+
/// Some descriptors like pk() don't have any address.
460+
///
461+
/// # Errors
462+
/// For raw/bare descriptors that don't have any address.
466463
pub fn address(&self, network: Network) -> Result<Address, Error> {
467464
match *self {
468-
Descriptor::Bare(ref bare) => bare.address(network),
469-
Descriptor::Pkh(ref pkh) => pkh.address(network),
470-
Descriptor::Wpkh(ref wpkh) => wpkh.address(network),
471-
Descriptor::Wsh(ref wsh) => wsh.address(network),
472-
Descriptor::Sh(ref sh) => sh.address(network),
473-
Descriptor::Tr(ref tr) => tr.address(network),
465+
Descriptor::Bare(_) => Err(Error::BareDescriptorAddr),
466+
Descriptor::Pkh(ref pkh) => Ok(pkh.address(network)),
467+
Descriptor::Wpkh(ref wpkh) => Ok(wpkh.address(network)),
468+
Descriptor::Wsh(ref wsh) => Ok(wsh.address(network)),
469+
Descriptor::Sh(ref sh) => Ok(sh.address(network)),
470+
Descriptor::Tr(ref tr) => Ok(tr.address(network)),
474471
}
475472
}
476473

src/descriptor/segwitv0.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,8 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
8585
self.inner_script().to_v0_p2wsh()
8686
}
8787

88-
/// Obtain the corresponding script pubkey for this descriptor
89-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
90-
pub fn addr(&self, network: Network) -> Address {
88+
/// Obtains the corresponding script pubkey for this descriptor.
89+
pub fn address(&self, network: Network) -> Address {
9190
match self.inner {
9291
WshInner::SortedMulti(ref smv) => Address::p2wsh(&smv.encode(), network),
9392
WshInner::Ms(ref ms) => Address::p2wsh(&ms.encode(), network),
@@ -199,13 +198,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
199198
Ok(())
200199
}
201200

202-
fn address(&self, network: Network) -> Result<Address, Error>
203-
where
204-
Pk: ToPublicKey,
205-
{
206-
Ok(self.addr(network))
207-
}
208-
209201
fn script_pubkey(&self) -> Script
210202
where
211203
Pk: ToPublicKey,
@@ -367,9 +359,8 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
367359
addr.script_pubkey()
368360
}
369361

370-
/// Obtain the corresponding script pubkey for this descriptor
371-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
372-
pub fn addr(&self, network: Network) -> Address {
362+
/// Obtains the corresponding script pubkey for this descriptor.
363+
pub fn address(&self, network: Network) -> Address {
373364
Address::p2wpkh(&self.pk.to_public_key(), network)
374365
.expect("Rust Miniscript types don't allow uncompressed pks in segwit descriptors")
375366
}
@@ -461,13 +452,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
461452
}
462453
}
463454

464-
fn address(&self, network: Network) -> Result<Address, Error>
465-
where
466-
Pk: ToPublicKey,
467-
{
468-
Ok(self.addr(network))
469-
}
470-
471455
fn script_pubkey(&self) -> Script
472456
where
473457
Pk: ToPublicKey,

src/descriptor/sh.rs

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -219,22 +219,21 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
219219
}
220220
}
221221

222-
/// Obtain the corresponding script pubkey for this descriptor
223-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
224-
pub fn addr(&self, network: Network) -> Address {
222+
/// Obtains the corresponding address for this descriptor.
223+
pub fn address(&self, network: Network) -> Address {
224+
let addr = self.address_fallible(network);
225+
226+
// Size is checked in `check_global_consensus_validity`.
227+
assert!(addr.is_ok());
228+
addr.expect("only fails if size > MAX_SCRIPT_ELEMENT_SIZE")
229+
}
230+
231+
fn address_fallible(&self, network: Network) -> Result<Address, Error> {
225232
match self.inner {
226-
ShInner::Wsh(ref wsh) => {
227-
Address::p2sh(&wsh.spk(), network).expect("Size checked in Miniscript")
228-
}
229-
ShInner::Wpkh(ref wpkh) => {
230-
Address::p2sh(&wpkh.spk(), network).expect("Size checked in Miniscript")
231-
}
232-
ShInner::SortedMulti(ref smv) => {
233-
Address::p2sh(&smv.encode(), network).expect("Size checked in Miniscript")
234-
}
235-
ShInner::Ms(ref ms) => {
236-
Address::p2sh(&ms.encode(), network).expect("Size checked in Miniscript")
237-
}
233+
ShInner::Wsh(ref wsh) => Ok(Address::p2sh(&wsh.script_pubkey(), network)?),
234+
ShInner::Wpkh(ref wpkh) => Ok(Address::p2sh(&wpkh.script_pubkey(), network)?),
235+
ShInner::SortedMulti(ref smv) => Ok(Address::p2sh(&smv.encode(), network)?),
236+
ShInner::Ms(ref ms) => Ok(Address::p2sh(&ms.encode(), network)?),
238237
}
239238
}
240239

@@ -275,18 +274,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
275274
Ok(())
276275
}
277276

278-
fn address(&self, network: Network) -> Result<Address, Error>
279-
where
280-
Pk: ToPublicKey,
281-
{
282-
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)?),
285-
ShInner::SortedMulti(ref smv) => Ok(Address::p2sh(&smv.encode(), network)?),
286-
ShInner::Ms(ref ms) => Ok(Address::p2sh(&ms.encode(), network)?),
287-
}
288-
}
289-
290277
fn script_pubkey(&self) -> Script
291278
where
292279
Pk: ToPublicKey,

src/descriptor/tr.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,10 @@ impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
285285
.into_script()
286286
}
287287

288-
/// Obtain the corresponding script pubkey for this descriptor
289-
/// Same as[`DescriptorTrait::address`] for this descriptor
290-
pub fn addr(&self, network: Network) -> Result<Address, Error> {
288+
/// Obtains the corresponding address for this descriptor.
289+
pub fn address(&self, network: Network) -> Address {
291290
let spend_info = self.spend_info();
292-
Ok(Address::p2tr_tweaked(spend_info.output_key(), network))
291+
Address::p2tr_tweaked(spend_info.output_key(), network)
293292
}
294293
}
295294

@@ -560,13 +559,6 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
560559
Ok(())
561560
}
562561

563-
fn address(&self, network: Network) -> Result<Address, Error>
564-
where
565-
Pk: ToPublicKey,
566-
{
567-
self.addr(network)
568-
}
569-
570562
fn script_pubkey(&self) -> Script
571563
where
572564
Pk: ToPublicKey,

0 commit comments

Comments
 (0)