Skip to content

Commit 03d4bd6

Browse files
committed
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 104eb55 commit 03d4bd6

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
@@ -22,7 +22,7 @@ use std::fmt;
2222
use std::str::FromStr;
2323

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

2727
use super::checksum::{desc_checksum, verify_checksum};
2828
use super::DescriptorTrait;
@@ -138,7 +138,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Bare<Pk> {
138138
Ok(())
139139
}
140140

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

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

271271
/// Obtain the underlying miniscript for this descriptor
@@ -344,7 +344,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Pkh<Pk> {
344344
Ok(())
345345
}
346346

347-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
347+
fn address(&self, network: Network) -> Result<Address, Error>
348348
where
349349
Pk: ToPublicKey,
350350
{

src/descriptor/mod.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use std::sync::Arc;
3131

3232
use bitcoin::blockdata::witness::Witness;
3333
use bitcoin::util::address::WitnessVersion;
34-
use bitcoin::{self, secp256k1, Script};
34+
use bitcoin::{self, secp256k1, Address, Network, Script, TxIn};
3535

3636
use self::checksum::verify_checksum;
3737
use crate::miniscript::{Legacy, Miniscript, Segwitv0};
@@ -93,7 +93,7 @@ pub trait DescriptorTrait<Pk: MiniscriptKey> {
9393
/// Some descriptors like pk() don't have any address.
9494
/// Errors:
9595
/// - On raw/bare descriptors that don't have any address
96-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
96+
fn address(&self, network: Network) -> Result<Address, Error>
9797
where
9898
Pk: ToPublicKey;
9999

@@ -142,7 +142,7 @@ pub trait DescriptorTrait<Pk: MiniscriptKey> {
142142
/// Attempts to produce a non-malleable satisfying witness and scriptSig to spend an
143143
/// output controlled by the given descriptor; add the data to a given
144144
/// `TxIn` output.
145-
fn satisfy<S>(&self, txin: &mut bitcoin::TxIn, satisfier: S) -> Result<(), Error>
145+
fn satisfy<S>(&self, txin: &mut TxIn, satisfier: S) -> Result<(), Error>
146146
where
147147
Pk: ToPublicKey,
148148
S: Satisfier<Pk>,
@@ -487,7 +487,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Descriptor<Pk> {
487487
}
488488
}
489489
/// Computes the Bitcoin address of the descriptor, if one exists
490-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
490+
fn address(&self, network: Network) -> Result<Address, Error>
491491
where
492492
Pk: ToPublicKey,
493493
{
@@ -971,16 +971,14 @@ mod tests {
971971
)
972972
);
973973
assert_eq!(
974-
bare.address(bitcoin::Network::Bitcoin)
975-
.unwrap_err()
976-
.to_string(),
974+
bare.address(Network::Bitcoin).unwrap_err().to_string(),
977975
"Bare descriptors don't have address"
978976
);
979977

980978
let pk = StdDescriptor::from_str(TEST_PK).unwrap();
981979
assert_eq!(
982980
pk.script_pubkey(),
983-
bitcoin::Script::from(vec![
981+
Script::from(vec![
984982
0x21, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
985983
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
986984
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xac,
@@ -1007,7 +1005,7 @@ mod tests {
10071005
.into_script()
10081006
);
10091007
assert_eq!(
1010-
pkh.address(bitcoin::Network::Bitcoin,).unwrap().to_string(),
1008+
pkh.address(Network::Bitcoin,).unwrap().to_string(),
10111009
"1D7nRvrRgzCg9kYBwhPH3j3Gs6SmsRg3Wq"
10121010
);
10131011

@@ -1028,9 +1026,7 @@ mod tests {
10281026
.into_script()
10291027
);
10301028
assert_eq!(
1031-
wpkh.address(bitcoin::Network::Bitcoin,)
1032-
.unwrap()
1033-
.to_string(),
1029+
wpkh.address(Network::Bitcoin,).unwrap().to_string(),
10341030
"bc1qsn57m9drscflq5nl76z6ny52hck5w4x5wqd9yt"
10351031
);
10361032

@@ -1052,10 +1048,7 @@ mod tests {
10521048
.into_script()
10531049
);
10541050
assert_eq!(
1055-
shwpkh
1056-
.address(bitcoin::Network::Bitcoin,)
1057-
.unwrap()
1058-
.to_string(),
1051+
shwpkh.address(Network::Bitcoin,).unwrap().to_string(),
10591052
"3PjMEzoveVbvajcnDDuxcJhsuqPHgydQXq"
10601053
);
10611054

@@ -1077,7 +1070,7 @@ mod tests {
10771070
.into_script()
10781071
);
10791072
assert_eq!(
1080-
sh.address(bitcoin::Network::Bitcoin,).unwrap().to_string(),
1073+
sh.address(Network::Bitcoin,).unwrap().to_string(),
10811074
"3HDbdvM9CQ6ASnQFUkWw6Z4t3qNwMesJE9"
10821075
);
10831076

@@ -1103,7 +1096,7 @@ mod tests {
11031096
.into_script()
11041097
);
11051098
assert_eq!(
1106-
wsh.address(bitcoin::Network::Bitcoin,).unwrap().to_string(),
1099+
wsh.address(Network::Bitcoin,).unwrap().to_string(),
11071100
"bc1qlymeahyfsv2jm3upw3urqp6m65ufde9seedl7umh0lth6yjt5zzsk33tv6"
11081101
);
11091102

@@ -1125,10 +1118,7 @@ mod tests {
11251118
.into_script()
11261119
);
11271120
assert_eq!(
1128-
shwsh
1129-
.address(bitcoin::Network::Bitcoin,)
1130-
.unwrap()
1131-
.to_string(),
1121+
shwsh.address(Network::Bitcoin,).unwrap().to_string(),
11321122
"38cTksiyPT2b1uGRVbVqHdDhW9vKs84N6Z"
11331123
);
11341124
}

src/descriptor/segwitv0.rs

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

22-
use bitcoin::{self, Script};
22+
use bitcoin::{self, Address, Network, Script};
2323

2424
use super::checksum::{desc_checksum, verify_checksum};
2525
use super::{DescriptorTrait, SortedMultiVec};
@@ -85,10 +85,10 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wsh<Pk> {
8585

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

@@ -197,7 +197,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wsh<Pk> {
197197
Ok(())
198198
}
199199

200-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
200+
fn address(&self, network: Network) -> Result<Address, Error>
201201
where
202202
Pk: ToPublicKey,
203203
{
@@ -355,15 +355,15 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
355355
/// Obtain the corresponding script pubkey for this descriptor
356356
/// Non failing verion of [`DescriptorTrait::script_pubkey`] for this descriptor
357357
pub fn spk(&self) -> Script {
358-
let addr = bitcoin::Address::p2wpkh(&self.pk.to_public_key(), bitcoin::Network::Bitcoin)
358+
let addr = Address::p2wpkh(&self.pk.to_public_key(), Network::Bitcoin)
359359
.expect("wpkh descriptors have compressed keys");
360360
addr.script_pubkey()
361361
}
362362

363363
/// Obtain the corresponding script pubkey for this descriptor
364364
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
365-
pub fn addr(&self, network: bitcoin::Network) -> bitcoin::Address {
366-
bitcoin::Address::p2wpkh(&self.pk.to_public_key(), network)
365+
pub fn addr(&self, network: Network) -> Address {
366+
Address::p2wpkh(&self.pk.to_public_key(), network)
367367
.expect("Rust Miniscript types don't allow uncompressed pks in segwit descriptors")
368368
}
369369

@@ -380,7 +380,7 @@ impl<Pk: MiniscriptKey + ToPublicKey> Wpkh<Pk> {
380380
// the previous txo's scriptPubKey.
381381
// The item 5:
382382
// - For P2WPKH witness program, the scriptCode is `0x1976a914{20-byte-pubkey-hash}88ac`.
383-
let addr = bitcoin::Address::p2pkh(&self.pk.to_public_key(), bitcoin::Network::Bitcoin);
383+
let addr = Address::p2pkh(&self.pk.to_public_key(), Network::Bitcoin);
384384
addr.script_pubkey()
385385
}
386386
}
@@ -454,7 +454,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Wpkh<Pk> {
454454
}
455455
}
456456

457-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
457+
fn address(&self, network: Network) -> Result<Address, Error>
458458
where
459459
Pk: ToPublicKey,
460460
{

src/descriptor/sh.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::fmt;
2222
use std::str::FromStr;
2323

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

2727
use super::checksum::{desc_checksum, verify_checksum};
2828
use super::{DescriptorTrait, SortedMultiVec, Wpkh, Wsh};
@@ -219,19 +219,19 @@ impl<Pk: MiniscriptKey + ToPublicKey> Sh<Pk> {
219219

220220
/// Obtain the corresponding script pubkey for this descriptor
221221
/// Non failing verion of [`DescriptorTrait::address`] for this descriptor
222-
pub fn addr(&self, network: bitcoin::Network) -> bitcoin::Address {
222+
pub fn addr(&self, network: Network) -> Address {
223223
match self.inner {
224224
ShInner::Wsh(ref wsh) => {
225-
bitcoin::Address::p2sh(&wsh.spk(), network).expect("Size checked in Miniscript")
225+
Address::p2sh(&wsh.spk(), network).expect("Size checked in Miniscript")
226226
}
227227
ShInner::Wpkh(ref wpkh) => {
228-
bitcoin::Address::p2sh(&wpkh.spk(), network).expect("Size checked in Miniscript")
228+
Address::p2sh(&wpkh.spk(), network).expect("Size checked in Miniscript")
229229
}
230230
ShInner::SortedMulti(ref smv) => {
231-
bitcoin::Address::p2sh(&smv.encode(), network).expect("Size checked in Miniscript")
231+
Address::p2sh(&smv.encode(), network).expect("Size checked in Miniscript")
232232
}
233233
ShInner::Ms(ref ms) => {
234-
bitcoin::Address::p2sh(&ms.encode(), network).expect("Size checked in Miniscript")
234+
Address::p2sh(&ms.encode(), network).expect("Size checked in Miniscript")
235235
}
236236
}
237237
}
@@ -273,15 +273,15 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Sh<Pk> {
273273
Ok(())
274274
}
275275

276-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
276+
fn address(&self, network: Network) -> Result<Address, Error>
277277
where
278278
Pk: ToPublicKey,
279279
{
280280
match self.inner {
281-
ShInner::Wsh(ref wsh) => Ok(bitcoin::Address::p2sh(&wsh.spk(), network)?),
282-
ShInner::Wpkh(ref wpkh) => Ok(bitcoin::Address::p2sh(&wpkh.spk(), network)?),
283-
ShInner::SortedMulti(ref smv) => Ok(bitcoin::Address::p2sh(&smv.encode(), network)?),
284-
ShInner::Ms(ref ms) => Ok(bitcoin::Address::p2sh(&ms.encode(), network)?),
281+
ShInner::Wsh(ref wsh) => Ok(Address::p2sh(&wsh.spk(), network)?),
282+
ShInner::Wpkh(ref wpkh) => Ok(Address::p2sh(&wpkh.spk(), network)?),
283+
ShInner::SortedMulti(ref smv) => Ok(Address::p2sh(&smv.encode(), network)?),
284+
ShInner::Ms(ref ms) => Ok(Address::p2sh(&ms.encode(), network)?),
285285
}
286286
}
287287

src/descriptor/tr.rs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use bitcoin::util::taproot::{
1010
LeafVersion, TaprootBuilder, TaprootBuilderError, TaprootSpendInfo, TAPROOT_CONTROL_BASE_SIZE,
1111
TAPROOT_CONTROL_MAX_NODE_COUNT, TAPROOT_CONTROL_NODE_SIZE,
1212
};
13-
use bitcoin::{self, secp256k1, Script};
13+
use bitcoin::{secp256k1, Address, Network, Script};
1414

1515
use super::checksum::{desc_checksum, verify_checksum};
1616
use crate::expression::{self, FromTree};
@@ -286,12 +286,9 @@ impl<Pk: MiniscriptKey + ToPublicKey> Tr<Pk> {
286286

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

@@ -558,7 +555,7 @@ impl<Pk: MiniscriptKey> DescriptorTrait<Pk> for Tr<Pk> {
558555
Ok(())
559556
}
560557

561-
fn address(&self, network: bitcoin::Network) -> Result<bitcoin::Address, Error>
558+
fn address(&self, network: Network) -> Result<Address, Error>
562559
where
563560
Pk: ToPublicKey,
564561
{

0 commit comments

Comments
 (0)