Skip to content

Commit 3d0b112

Browse files
committed
Remove address from DescriptorTrait
In preparation for removing the `DescriptorTrait` move the `address` trait methods onto each individual descriptor. Replaces the `addr` method.
1 parent 4a9e20f commit 3d0b112

File tree

7 files changed

+49
-103
lines changed

7 files changed

+49
-103
lines changed

examples/htlc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::str::FromStr;
1919
use bitcoin::Network;
2020
use miniscript::descriptor::Wsh;
2121
use miniscript::policy::{Concrete, Liftable};
22-
use miniscript::DescriptorTrait;
2322

2423
fn main() {
2524
// HTLC policy with 10:1 odds for happy (co-operative) case compared to uncooperative case.
@@ -66,7 +65,7 @@ fn main() {
6665

6766
// Get the address for this Wsh descriptor.v
6867
assert_eq!(
69-
format!("{}", htlc_descriptor.address(Network::Bitcoin).unwrap()),
68+
format!("{}", htlc_descriptor.address(Network::Bitcoin)),
7069
"bc1qmpfcw7he9z5d9ftfe8qw699azmm2sr8fen903fs4plv007yx0t3qxfmqv5"
7170
);
7271
}

examples/xpub_descriptors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::str::FromStr;
1818

1919
use miniscript::bitcoin::secp256k1::{Secp256k1, Verification};
2020
use miniscript::bitcoin::{Address, Network};
21-
use miniscript::{Descriptor, DescriptorPublicKey, DescriptorTrait, TranslatePk2};
21+
use miniscript::{Descriptor, DescriptorPublicKey, TranslatePk2};
2222

2323
const XPUB_1: &str = "xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB";
2424
const XPUB_2: &str = "xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH";

src/descriptor/bare.rs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,6 @@ where
139139
}
140140

141141
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
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,
@@ -263,9 +256,8 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
263256
addr.script_pubkey()
264257
}
265258

266-
/// Obtain the corresponding script pubkey for this descriptor
267-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
268-
pub fn addr(&self, network: Network) -> Address {
259+
/// Obtains the corresponding script pubkey for this descriptor.
260+
pub fn address(&self, network: Network) -> Address {
269261
Address::p2pkh(&self.pk.to_public_key(), network)
270262
}
271263

@@ -341,13 +333,6 @@ where
341333
}
342334

343335
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
344-
fn address(&self, network: Network) -> Result<Address, Error>
345-
where
346-
Pk: ToPublicKey,
347-
{
348-
Ok(self.addr(network))
349-
}
350-
351336
fn script_pubkey(&self) -> Script
352337
where
353338
Pk: ToPublicKey,

src/descriptor/mod.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +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 Bitcoin address of the descriptor, if one exists
80-
/// Some descriptors like pk() don't have any address.
81-
/// Errors:
82-
/// - On raw/bare descriptors that don't have any address
83-
fn address(&self, network: Network) -> Result<Address, Error>
84-
where
85-
Pk: ToPublicKey;
86-
8779
/// Computes the scriptpubkey of the descriptor
8880
fn script_pubkey(&self) -> Script
8981
where
@@ -407,6 +399,25 @@ impl<Pk: MiniscriptKey> Descriptor<Pk> {
407399
}
408400
}
409401

402+
impl<Pk: MiniscriptKey + ToPublicKey> Descriptor<Pk> {
403+
/// Computes the Bitcoin address of the descriptor, if one exists
404+
///
405+
/// Some descriptors like pk() don't have any address.
406+
///
407+
/// # Errors
408+
/// For raw/bare descriptors that don't have any address.
409+
pub fn address(&self, network: Network) -> Result<Address, Error> {
410+
match *self {
411+
Descriptor::Bare(_) => Err(Error::BareDescriptorAddr),
412+
Descriptor::Pkh(ref pkh) => Ok(pkh.address(network)),
413+
Descriptor::Wpkh(ref wpkh) => Ok(wpkh.address(network)),
414+
Descriptor::Wsh(ref wsh) => Ok(wsh.address(network)),
415+
Descriptor::Sh(ref sh) => Ok(sh.address(network)),
416+
Descriptor::Tr(ref tr) => Ok(tr.address(network)),
417+
}
418+
}
419+
}
420+
410421
impl<P, Q> TranslatePk<P, Q> for Descriptor<P>
411422
where
412423
P: MiniscriptKey,
@@ -438,21 +449,6 @@ where
438449
}
439450

440451
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
441-
/// Computes the Bitcoin address of the descriptor, if one exists
442-
fn address(&self, network: Network) -> Result<Address, Error>
443-
where
444-
Pk: ToPublicKey,
445-
{
446-
match *self {
447-
Descriptor::Bare(ref bare) => bare.address(network),
448-
Descriptor::Pkh(ref pkh) => pkh.address(network),
449-
Descriptor::Wpkh(ref wpkh) => wpkh.address(network),
450-
Descriptor::Wsh(ref wsh) => wsh.address(network),
451-
Descriptor::Sh(ref sh) => sh.address(network),
452-
Descriptor::Tr(ref tr) => tr.address(network),
453-
}
454-
}
455-
456452
/// Computes the scriptpubkey of the descriptor
457453
fn script_pubkey(&self) -> Script
458454
where

src/descriptor/segwitv0.rs

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

95-
/// Obtain the corresponding script pubkey for this descriptor
96-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
97-
pub fn addr(&self, network: Network) -> Address {
95+
/// Obtains the corresponding script pubkey for this descriptor.
96+
pub fn address(&self, network: Network) -> Address {
9897
match self.inner {
9998
WshInner::SortedMulti(ref smv) => Address::p2wsh(&smv.encode(), network),
10099
WshInner::Ms(ref ms) => Address::p2wsh(&ms.encode(), network),
@@ -198,13 +197,6 @@ where
198197
}
199198

200199
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
201-
fn address(&self, network: Network) -> Result<Address, Error>
202-
where
203-
Pk: ToPublicKey,
204-
{
205-
Ok(self.addr(network))
206-
}
207-
208200
fn script_pubkey(&self) -> Script
209201
where
210202
Pk: ToPublicKey,
@@ -372,9 +364,8 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
372364
addr.script_pubkey()
373365
}
374366

375-
/// Obtain the corresponding script pubkey for this descriptor
376-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
377-
pub fn addr(&self, network: Network) -> Address {
367+
/// Obtains the corresponding script pubkey for this descriptor.
368+
pub fn address(&self, network: Network) -> Address {
378369
Address::p2wpkh(&self.pk.to_public_key(), network)
379370
.expect("Rust Miniscript types don't allow uncompressed pks in segwit descriptors")
380371
}
@@ -456,13 +447,6 @@ where
456447
}
457448

458449
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
459-
fn address(&self, network: Network) -> Result<Address, Error>
460-
where
461-
Pk: ToPublicKey,
462-
{
463-
Ok(self.addr(network))
464-
}
465-
466450
fn script_pubkey(&self) -> Script
467451
where
468452
Pk: ToPublicKey,

src/descriptor/sh.rs

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -228,23 +228,25 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
228228
}
229229
}
230230

231-
/// Obtain the corresponding script pubkey for this descriptor
232-
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
233-
pub fn addr(&self, network: Network) -> Address {
234-
match self.inner {
235-
ShInner::Wsh(ref wsh) => {
236-
Address::p2sh(&wsh.spk(), network).expect("Size checked in Miniscript")
237-
}
238-
ShInner::Wpkh(ref wpkh) => {
239-
Address::p2sh(&wpkh.spk(), network).expect("Size checked in Miniscript")
240-
}
241-
ShInner::SortedMulti(ref smv) => {
242-
Address::p2sh(&smv.encode(), network).expect("Size checked in Miniscript")
243-
}
244-
ShInner::Ms(ref ms) => {
245-
Address::p2sh(&ms.encode(), network).expect("Size checked in Miniscript")
246-
}
247-
}
231+
/// Obtains the corresponding address for this descriptor.
232+
pub fn address(&self, network: Network) -> Address {
233+
let addr = self.address_fallible(network);
234+
235+
// Size is checked in `check_global_consensus_validity`.
236+
assert!(addr.is_ok());
237+
addr.expect("only fails if size > MAX_SCRIPT_ELEMENT_SIZE")
238+
}
239+
240+
fn address_fallible(&self, network: Network) -> Result<Address, Error> {
241+
let script = match self.inner {
242+
ShInner::Wsh(ref wsh) => wsh.script_pubkey(),
243+
ShInner::Wpkh(ref wpkh) => wpkh.script_pubkey(),
244+
ShInner::SortedMulti(ref smv) => smv.encode(),
245+
ShInner::Ms(ref ms) => ms.encode(),
246+
};
247+
let address = Address::p2sh(&script, network)?;
248+
249+
Ok(address)
248250
}
249251

250252
/// Obtain the underlying miniscript for this descriptor
@@ -274,18 +276,6 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
274276
}
275277

276278
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
277-
fn address(&self, network: Network) -> Result<Address, Error>
278-
where
279-
Pk: ToPublicKey,
280-
{
281-
match self.inner {
282-
ShInner::Wsh(ref wsh) => Ok(Address::p2sh(&wsh.spk(), network)?),
283-
ShInner::Wpkh(ref wpkh) => Ok(Address::p2sh(&wpkh.spk(), network)?),
284-
ShInner::SortedMulti(ref smv) => Ok(Address::p2sh(&smv.encode(), network)?),
285-
ShInner::Ms(ref ms) => Ok(Address::p2sh(&ms.encode(), network)?),
286-
}
287-
}
288-
289279
fn script_pubkey(&self) -> Script
290280
where
291281
Pk: ToPublicKey,

src/descriptor/tr.rs

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -292,11 +292,10 @@ impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
292292
.into_script()
293293
}
294294

295-
/// Obtain the corresponding script pubkey for this descriptor
296-
/// Same as[`DescriptorTrait::address`] for this descriptor
297-
pub fn addr(&self, network: Network) -> Result<Address, Error> {
295+
/// Obtains the corresponding address for this descriptor.
296+
pub fn address(&self, network: Network) -> Address {
298297
let spend_info = self.spend_info();
299-
Ok(Address::p2tr_tweaked(spend_info.output_key(), network))
298+
Address::p2tr_tweaked(spend_info.output_key(), network)
300299
}
301300
}
302301

@@ -556,13 +555,6 @@ impl<Pk: MiniscriptKey> Liftable<Pk> for Tr<Pk> {
556555
}
557556

558557
impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
559-
fn address(&self, network: Network) -> Result<Address, Error>
560-
where
561-
Pk: ToPublicKey,
562-
{
563-
self.addr(network)
564-
}
565-
566558
fn script_pubkey(&self) -> Script
567559
where
568560
Pk: ToPublicKey,

0 commit comments

Comments
 (0)