Skip to content

Commit 9dd0371

Browse files
committed
descriptor: Improve addr/address
Currently we have some duplicate code and unusual error wrapping. Correctly handle the error returned when calculating an address. Use `expect` when we have already checked for the failure. Improve the docs, explaining what is called when and what its equivalent to.
1 parent 45e80d1 commit 9dd0371

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

src/descriptor/bare.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
269269
}
270270

271271
/// Obtains the [`Address`] for this descriptor.
272+
/// Called by [`DescriptorTrait::address`] for this descriptor.
272273
pub fn addr(&self, network: Network) -> Address {
273274
Address::p2pkh(&self.pk.to_public_key(), network)
274275
}

src/descriptor/segwitv0.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
369369
}
370370

371371
/// Obtains the [`Address`] for this descriptor.
372+
/// Non failing version of [`DescriptorTrait::address`] for this descriptor.
372373
pub fn addr(&self, network: Network) -> Address {
373374
bitcoin::Address::p2wpkh(&self.pk.to_public_key(), network)
374375
.expect("Rust Miniscript types don't allow uncompressed pks in segwit descriptors")
@@ -466,7 +467,8 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
466467
where
467468
Pk: ToPublicKey,
468469
{
469-
Ok(self.addr(network))
470+
let addr = Address::p2wpkh(&self.pk.to_public_key(), network)?;
471+
Ok(addr)
470472
}
471473

472474
fn script_pubkey(&self) -> Script

src/descriptor/sh.rs

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

222222
/// Obtains the [`Address`] for this descriptor.
223+
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor.
223224
pub fn addr(&self, network: Network) -> Address {
225+
self.address_fallible(network)
226+
.expect("size checked in Miniscritp")
227+
}
228+
229+
fn address_fallible(&self, network: Network) -> Result<Address, Error>
230+
where
231+
Pk: ToPublicKey,
232+
{
224233
match self.inner {
225-
ShInner::Wsh(ref wsh) => {
226-
Address::p2sh(&wsh.spk(), network).expect("Size checked in Miniscript")
227-
}
228-
ShInner::Wpkh(ref wpkh) => {
229-
Address::p2sh(&wpkh.spk(), network).expect("Size checked in Miniscript")
230-
}
231-
ShInner::SortedMulti(ref smv) => {
232-
Address::p2sh(&smv.encode(), network).expect("Size checked in Miniscript")
233-
}
234-
ShInner::Ms(ref ms) => {
235-
Address::p2sh(&ms.encode(), network).expect("Size checked in Miniscript")
236-
}
234+
ShInner::Wsh(ref wsh) => Ok(Address::p2sh(&wsh.spk(), network)?),
235+
ShInner::Wpkh(ref wpkh) => Ok(Address::p2sh(&wpkh.spk(), network)?),
236+
ShInner::SortedMulti(ref smv) => Ok(Address::p2sh(&smv.encode(), network)?),
237+
ShInner::Ms(ref ms) => Ok(Address::p2sh(&ms.encode(), network)?),
237238
}
238239
}
239240

@@ -278,12 +279,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
278279
where
279280
Pk: ToPublicKey,
280281
{
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-
}
282+
self.address_fallible(network)
287283
}
288284

289285
fn script_pubkey(&self) -> Script

src/descriptor/tr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
286286
}
287287

288288
/// Obtains the [`Address`] for this descriptor.
289+
/// Called by [`DescriptorTrait::address`] for this descriptor.
289290
pub fn addr(&self, network: Network) -> Result<Address, Error> {
290291
let spend_info = self.spend_info();
291292
Ok(Address::p2tr_tweaked(spend_info.output_key(), network))

0 commit comments

Comments
 (0)