Skip to content

Commit c88ea19

Browse files
committed
descriptor: Remove unnecessary path
Using the `bitcoin::` path is necessary for keys but for other well known types, like `Address`, using the path does not add to the readability of the code. (This is subjective.)
1 parent de74dc0 commit c88ea19

File tree

5 files changed

+43
-56
lines changed

5 files changed

+43
-56
lines changed

src/descriptor/bare.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
use std::{fmt, str::FromStr};
2222

23-
use bitcoin::{self, blockdata::script, Script};
23+
use bitcoin::{self, blockdata::script, Address, Network, Script};
2424

2525
use crate::expression::{self, FromTree};
2626
use crate::miniscript::context::ScriptContext;
@@ -139,7 +139,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
139139
Ok(())
140140
}
141141

142-
fn address(&self, _network: bitcoin::Network) -> Result<bitcoin::Address, Error>
142+
fn address(&self, _network: Network) -> Result<Address, Error>
143143
where
144144
Pk: ToPublicKey,
145145
{
@@ -262,14 +262,14 @@ impl<Pk: MiniscriptKey + ToPublicKey> Pkh<Pk> {
262262
/// Obtain the corresponding script pubkey for this descriptor
263263
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
264264
pub fn spk(&self) -> Script {
265-
let addr = bitcoin::Address::p2pkh(&self.pk.to_public_key(), bitcoin::Network::Bitcoin);
265+
let addr = Address::p2pkh(&self.pk.to_public_key(), Network::Bitcoin);
266266
addr.script_pubkey()
267267
}
268268

269269
/// Obtain the corresponding script pubkey for this descriptor
270270
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
271-
pub fn addr(&self, network: bitcoin::Network) -> bitcoin::Address {
272-
bitcoin::Address::p2pkh(&self.pk.to_public_key(), network)
271+
pub fn addr(&self, network: Network) -> Address {
272+
Address::p2pkh(&self.pk.to_public_key(), network)
273273
}
274274

275275
/// Obtain the underlying miniscript for this descriptor
@@ -348,7 +348,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
348348
Ok(())
349349
}
350350

351-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
351+
fn address(&self, network: Network) -> Result<Address, Error>
352352
where
353353
Pk: ToPublicKey,
354354
{

src/descriptor/mod.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use std::{
3232

3333
use bitcoin::blockdata::witness::Witness;
3434
use bitcoin::util::address::WitnessVersion;
35-
use bitcoin::{self, secp256k1, Script};
35+
use bitcoin::{self, secp256k1, Address, Network, Script, TxIn};
3636

3737
use self::checksum::verify_checksum;
3838
use crate::expression;
@@ -96,7 +96,7 @@ pub trait DescriptorTrait<Pk: MiniscriptKey> {
9696
/// Some descriptors like pk() don't have any address.
9797
/// Errors:
9898
/// - On raw/bare descriptors that don't have any address
99-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
99+
fn address(&self, network: Network) -> Result<Address, Error>
100100
where
101101
Pk: ToPublicKey;
102102

@@ -145,7 +145,7 @@ pub trait DescriptorTrait<Pk: MiniscriptKey> {
145145
/// Attempts to produce a non-malleable satisfying witness and scriptSig to spend an
146146
/// output controlled by the given descriptor; add the data to a given
147147
/// `TxIn` output.
148-
fn satisfy<S>(&self, txin: &mut bitcoin::TxIn, satisfier: S) -> Result<(), Error>
148+
fn satisfy<S>(&self, txin: &mut TxIn, satisfier: S) -> Result<(), Error>
149149
where
150150
Pk: ToPublicKey,
151151
S: Satisfier<Pk>,
@@ -500,7 +500,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
500500
}
501501
}
502502
/// Computes the Bitcoin address of the descriptor, if one exists
503-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
503+
fn address(&self, network: Network) -> Result<Address, Error>
504504
where
505505
Pk: ToPublicKey,
506506
{
@@ -984,16 +984,14 @@ mod tests {
984984
)
985985
);
986986
assert_eq!(
987-
bare.address(bitcoin::Network::Bitcoin)
988-
.unwrap_err()
989-
.to_string(),
987+
bare.address(Network::Bitcoin).unwrap_err().to_string(),
990988
"Bare descriptors don't have address"
991989
);
992990

993991
let pk = StdDescriptor::from_str(TEST_PK).unwrap();
994992
assert_eq!(
995993
pk.script_pubkey(),
996-
bitcoin::Script::from(vec![
994+
Script::from(vec![
997995
0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
998996
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
999997
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xac,
@@ -1020,7 +1018,7 @@ mod tests {
10201018
.into_script()
10211019
);
10221020
assert_eq!(
1023-
pkh.address(bitcoin::Network::Bitcoin,).unwrap().to_string(),
1021+
pkh.address(Network::Bitcoin,).unwrap().to_string(),
10241022
"1D7nRvrRgzCg9kYBwhPH3j3Gs6SmsRg3Wq"
10251023
);
10261024

@@ -1041,9 +1039,7 @@ mod tests {
10411039
.into_script()
10421040
);
10431041
assert_eq!(
1044-
wpkh.address(bitcoin::Network::Bitcoin,)
1045-
.unwrap()
1046-
.to_string(),
1042+
wpkh.address(Network::Bitcoin,).unwrap().to_string(),
10471043
"bc1qsn57m9drscflq5nl76z6ny52hck5w4x5wqd9yt"
10481044
);
10491045

@@ -1065,10 +1061,7 @@ mod tests {
10651061
.into_script()
10661062
);
10671063
assert_eq!(
1068-
shwpkh
1069-
.address(bitcoin::Network::Bitcoin,)
1070-
.unwrap()
1071-
.to_string(),
1064+
shwpkh.address(Network::Bitcoin,).unwrap().to_string(),
10721065
"3PjMEzoveVbvajcnDDuxcJhsuqPHgydQXq"
10731066
);
10741067

@@ -1090,7 +1083,7 @@ mod tests {
10901083
.into_script()
10911084
);
10921085
assert_eq!(
1093-
sh.address(bitcoin::Network::Bitcoin,).unwrap().to_string(),
1086+
sh.address(Network::Bitcoin,).unwrap().to_string(),
10941087
"3HDbdvM9CQ6ASnQFUkWw6Z4t3qNwMesJE9"
10951088
);
10961089

@@ -1116,7 +1109,7 @@ mod tests {
11161109
.into_script()
11171110
);
11181111
assert_eq!(
1119-
wsh.address(bitcoin::Network::Bitcoin,).unwrap().to_string(),
1112+
wsh.address(Network::Bitcoin,).unwrap().to_string(),
11201113
"bc1qlymeahyfsv2jm3upw3urqp6m65ufde9seedl7umh0lth6yjt5zzsk33tv6"
11211114
);
11221115

@@ -1138,10 +1131,7 @@ mod tests {
11381131
.into_script()
11391132
);
11401133
assert_eq!(
1141-
shwsh
1142-
.address(bitcoin::Network::Bitcoin,)
1143-
.unwrap()
1144-
.to_string(),
1134+
shwsh.address(Network::Bitcoin,).unwrap().to_string(),
11451135
"38cTksiyPT2b1uGRVbVqHdDhW9vKs84N6Z"
11461136
);
11471137
}

src/descriptor/segwitv0.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
1919
use std::{fmt, str::FromStr};
2020

21-
use bitcoin::{self, Script};
21+
use bitcoin::{self, Address, Network, Script};
2222

2323
use crate::expression::{self, FromTree};
2424
use crate::miniscript::context::{ScriptContext, ScriptContextError};
@@ -87,10 +87,10 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
8787

8888
/// Obtain the corresponding script pubkey for this descriptor
8989
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
90-
pub fn addr(&self, network: bitcoin::Network) -> bitcoin::Address {
90+
pub fn addr(&self, network: Network) -> Address {
9191
match self.inner {
92-
WshInner::SortedMulti(ref smv) => bitcoin::Address::p2wsh(&smv.encode(), network),
93-
WshInner::Ms(ref ms) => bitcoin::Address::p2wsh(&ms.encode(), network),
92+
WshInner::SortedMulti(ref smv) => Address::p2wsh(&smv.encode(), network),
93+
WshInner::Ms(ref ms) => Address::p2wsh(&ms.encode(), network),
9494
}
9595
}
9696

@@ -199,7 +199,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
199199
Ok(())
200200
}
201201

202-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
202+
fn address(&self, network: Network) -> Result<Address, Error>
203203
where
204204
Pk: ToPublicKey,
205205
{
@@ -362,15 +362,15 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
362362
/// Obtain the corresponding script pubkey for this descriptor
363363
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
364364
pub fn spk(&self) -> Script {
365-
let addr = bitcoin::Address::p2wpkh(&self.pk.to_public_key(), bitcoin::Network::Bitcoin)
365+
let addr = Address::p2wpkh(&self.pk.to_public_key(), Network::Bitcoin)
366366
.expect("wpkh descriptors have compressed keys");
367367
addr.script_pubkey()
368368
}
369369

370370
/// Obtain the corresponding script pubkey for this descriptor
371371
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
372-
pub fn addr(&self, network: bitcoin::Network) -> bitcoin::Address {
373-
bitcoin::Address::p2wpkh(&self.pk.to_public_key(), network)
372+
pub fn addr(&self, network: Network) -> Address {
373+
Address::p2wpkh(&self.pk.to_public_key(), network)
374374
.expect("Rust Miniscript types don't allow uncompressed pks in segwit descriptors")
375375
}
376376

@@ -387,7 +387,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
387387
// the previous txo's scriptPubKey.
388388
// The item 5:
389389
// - For P2WPKH witness program, the scriptCode is `0x1976a914{20-byte-pubkey-hash}88ac`.
390-
let addr = bitcoin::Address::p2pkh(&self.pk.to_public_key(), bitcoin::Network::Bitcoin);
390+
let addr = Address::p2pkh(&self.pk.to_public_key(), Network::Bitcoin);
391391
addr.script_pubkey()
392392
}
393393
}
@@ -461,7 +461,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
461461
}
462462
}
463463

464-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
464+
fn address(&self, network: Network) -> Result<Address, Error>
465465
where
466466
Pk: ToPublicKey,
467467
{

src/descriptor/sh.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
2121
use std::{fmt, str::FromStr};
2222

23-
use bitcoin::{self, blockdata::script, Script};
23+
use bitcoin::{self, blockdata::script, Address, Network, Script};
2424

2525
use crate::expression::{self, FromTree};
2626
use crate::miniscript::context::ScriptContext;
@@ -221,19 +221,19 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
221221

222222
/// Obtain the corresponding script pubkey for this descriptor
223223
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
224-
pub fn addr(&self, network: bitcoin::Network) -> bitcoin::Address {
224+
pub fn addr(&self, network: Network) -> Address {
225225
match self.inner {
226226
ShInner::Wsh(ref wsh) => {
227-
bitcoin::Address::p2sh(&wsh.spk(), network).expect("Size checked in Miniscript")
227+
Address::p2sh(&wsh.spk(), network).expect("Size checked in Miniscript")
228228
}
229229
ShInner::Wpkh(ref wpkh) => {
230-
bitcoin::Address::p2sh(&wpkh.spk(), network).expect("Size checked in Miniscript")
230+
Address::p2sh(&wpkh.spk(), network).expect("Size checked in Miniscript")
231231
}
232232
ShInner::SortedMulti(ref smv) => {
233-
bitcoin::Address::p2sh(&smv.encode(), network).expect("Size checked in Miniscript")
233+
Address::p2sh(&smv.encode(), network).expect("Size checked in Miniscript")
234234
}
235235
ShInner::Ms(ref ms) => {
236-
bitcoin::Address::p2sh(&ms.encode(), network).expect("Size checked in Miniscript")
236+
Address::p2sh(&ms.encode(), network).expect("Size checked in Miniscript")
237237
}
238238
}
239239
}
@@ -275,15 +275,15 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
275275
Ok(())
276276
}
277277

278-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
278+
fn address(&self, network: Network) -> Result<Address, Error>
279279
where
280280
Pk: ToPublicKey,
281281
{
282282
match self.inner {
283-
ShInner::Wsh(ref wsh) => Ok(bitcoin::Address::p2sh(&wsh.spk(), network)?),
284-
ShInner::Wpkh(ref wpkh) => Ok(bitcoin::Address::p2sh(&wpkh.spk(), network)?),
285-
ShInner::SortedMulti(ref smv) => Ok(bitcoin::Address::p2sh(&smv.encode(), network)?),
286-
ShInner::Ms(ref ms) => Ok(bitcoin::Address::p2sh(&ms.encode(), network)?),
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)?),
287287
}
288288
}
289289

src/descriptor/tr.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use bitcoin::util::taproot::{
1616
LeafVersion, TaprootBuilder, TaprootBuilderError, TaprootSpendInfo, TAPROOT_CONTROL_BASE_SIZE,
1717
TAPROOT_CONTROL_MAX_NODE_COUNT, TAPROOT_CONTROL_NODE_SIZE,
1818
};
19-
use bitcoin::{self, secp256k1, Script};
19+
use bitcoin::{self, secp256k1, Address, Network, Script};
2020
use std::cmp::{self, max};
2121
use std::hash;
2222
use std::sync::{Arc, Mutex};
@@ -287,12 +287,9 @@ impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
287287

288288
/// Obtain the corresponding script pubkey for this descriptor
289289
/// Same as[`DescriptorTrait::address`] for this descriptor
290-
pub fn addr(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error> {
290+
pub fn addr(&self, network: Network) -> Result<Address, Error> {
291291
let spend_info = self.spend_info();
292-
Ok(bitcoin::Address::p2tr_tweaked(
293-
spend_info.output_key(),
294-
network,
295-
))
292+
Ok(Address::p2tr_tweaked(spend_info.output_key(), network))
296293
}
297294
}
298295

@@ -563,7 +560,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
563560
Ok(())
564561
}
565562

566-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
563+
fn address(&self, network: Network) -> Result<Address, Error>
567564
where
568565
Pk: ToPublicKey,
569566
{

0 commit comments

Comments
 (0)