Skip to content

Commit 8714e9d

Browse files
committed
Merge #1503: feat(wallet): simplify public_descriptor fn and remove `get_descriptor_for_keych…
e7ec5a8 refactor(wallet)!: remove redundant get_descriptor_for_keychain (Giovanni Napoli) Pull request description: Fixes #1501 ### Description Simplifies `public_descriptor` function by using `get_descriptor` and removes `get_descriptor_for_keychain`. ### Notes to the reviewers Tested with `cargo test --all-features`. ### Changelog notice - Simplifies `public_descriptor` function and removes `get_descriptor_for_keychain` ### Checklists #### All Submissions: * [X] I've signed all my commits * [X] I followed the [contribution guidelines](https://github.com/bitcoindevkit/bdk/blob/master/CONTRIBUTING.md) * [X] I ran `cargo fmt` and `cargo clippy` before committing ACKs for top commit: notmandatory: ACK e7ec5a8 storopoli: ACK e7ec5a8 Tree-SHA512: 5981190ac882e08e42b1be53c55afb70c4ba7e7a99a8ae2a4e05f0618d0eb23849ce544024bb406e6a6918d9e9757d9ff6ad5a701cd9814b686e36f1ea16b44a
2 parents 43f093d + e7ec5a8 commit 8714e9d

File tree

4 files changed

+22
-28
lines changed

4 files changed

+22
-28
lines changed

crates/wallet/src/wallet/export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl FullyNodedExport {
116116
include_blockheight: bool,
117117
) -> Result<Self, &'static str> {
118118
let descriptor = wallet
119-
.get_descriptor_for_keychain(KeychainKind::External)
119+
.public_descriptor(KeychainKind::External)
120120
.to_string_with_secret(
121121
&wallet
122122
.get_signers(KeychainKind::External)
@@ -144,7 +144,7 @@ impl FullyNodedExport {
144144

145145
let change_descriptor = {
146146
let descriptor = wallet
147-
.get_descriptor_for_keychain(KeychainKind::Internal)
147+
.public_descriptor(KeychainKind::Internal)
148148
.to_string_with_secret(
149149
&wallet
150150
.get_signers(KeychainKind::Internal)

crates/wallet/src/wallet/mod.rs

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,7 +1595,7 @@ impl Wallet {
15951595
let weighted_utxo = match txout_index.index_of_spk(&txout.script_pubkey) {
15961596
Some(&(keychain, derivation_index)) => {
15971597
let satisfaction_weight = self
1598-
.get_descriptor_for_keychain(keychain)
1598+
.public_descriptor(keychain)
15991599
.max_weight_to_satisfy()
16001600
.unwrap();
16011601
WeightedUtxo {
@@ -1763,16 +1763,15 @@ impl Wallet {
17631763
)
17641764
}
17651765

1766-
/// Return the "public" version of the wallet's descriptor, meaning a new descriptor that has
1767-
/// the same structure but with every secret key removed
1766+
/// Returns the descriptor used to create addresses for a particular `keychain`.
1767+
/// It's the "public" version of the wallet's descriptor, meaning a new descriptor that has
1768+
/// the same structure but with the all secret keys replaced by their corresponding public key.
17681769
///
1769-
/// This can be used to build a watch-only version of a wallet
1770+
/// This can be used to build a watch-only version of a wallet.
17701771
pub fn public_descriptor(&self, keychain: KeychainKind) -> &ExtendedDescriptor {
17711772
self.indexed_graph
17721773
.index
1773-
.keychains()
1774-
.find(|(k, _)| *k == &keychain)
1775-
.map(|(_, d)| d)
1774+
.get_descriptor(&keychain)
17761775
.expect("keychain must exist")
17771776
}
17781777

@@ -1878,11 +1877,6 @@ impl Wallet {
18781877
&self.secp
18791878
}
18801879

1881-
/// Returns the descriptor used to create addresses for a particular `keychain`.
1882-
pub fn get_descriptor_for_keychain(&self, keychain: KeychainKind) -> &ExtendedDescriptor {
1883-
self.public_descriptor(keychain)
1884-
}
1885-
18861880
/// The derivation index of this wallet. It will return `None` if it has not derived any addresses.
18871881
/// Otherwise, it will return the index of the highest address it has derived.
18881882
pub fn derivation_index(&self, keychain: KeychainKind) -> Option<u32> {
@@ -1918,7 +1912,7 @@ impl Wallet {
19181912
.indexed_graph
19191913
.index
19201914
.index_of_spk(&txout.script_pubkey)?;
1921-
let descriptor = self.get_descriptor_for_keychain(keychain);
1915+
let descriptor = self.public_descriptor(keychain);
19221916
descriptor.at_derivation_index(child).ok()
19231917
}
19241918

@@ -1927,7 +1921,7 @@ impl Wallet {
19271921
.map(|utxo| {
19281922
let keychain = utxo.keychain;
19291923
(utxo, {
1930-
self.get_descriptor_for_keychain(keychain)
1924+
self.public_descriptor(keychain)
19311925
.max_weight_to_satisfy()
19321926
.unwrap()
19331927
})
@@ -2140,7 +2134,7 @@ impl Wallet {
21402134
..psbt::Input::default()
21412135
};
21422136

2143-
let desc = self.get_descriptor_for_keychain(keychain);
2137+
let desc = self.public_descriptor(keychain);
21442138
let derived_descriptor = desc
21452139
.at_derivation_index(child)
21462140
.expect("child can't be hardened");
@@ -2180,7 +2174,7 @@ impl Wallet {
21802174
if let Some(&(keychain, child)) =
21812175
self.indexed_graph.index.index_of_spk(&out.script_pubkey)
21822176
{
2183-
let desc = self.get_descriptor_for_keychain(keychain);
2177+
let desc = self.public_descriptor(keychain);
21842178
let desc = desc
21852179
.at_derivation_index(child)
21862180
.expect("child can't be hardened");
@@ -2200,9 +2194,9 @@ impl Wallet {
22002194

22012195
/// Return the checksum of the public descriptor associated to `keychain`
22022196
///
2203-
/// Internally calls [`Self::get_descriptor_for_keychain`] to fetch the right descriptor
2197+
/// Internally calls [`Self::public_descriptor`] to fetch the right descriptor
22042198
pub fn descriptor_checksum(&self, keychain: KeychainKind) -> String {
2205-
self.get_descriptor_for_keychain(keychain)
2199+
self.public_descriptor(keychain)
22062200
.to_string()
22072201
.split_once('#')
22082202
.unwrap()

crates/wallet/src/wallet/tx_builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ impl<'a, Cs> TxBuilder<'a, Cs> {
299299
.collect::<Result<Vec<_>, _>>()?;
300300

301301
for utxo in utxos {
302-
let descriptor = wallet.get_descriptor_for_keychain(utxo.keychain);
302+
let descriptor = wallet.public_descriptor(utxo.keychain);
303303
let satisfaction_weight = descriptor.max_weight_to_satisfy().unwrap();
304304
self.params.utxos.push(WeightedUtxo {
305305
satisfaction_weight,

crates/wallet/tests/wallet.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ fn load_recovers_wallet() -> anyhow::Result<()> {
153153
);
154154
let secp = Secp256k1::new();
155155
assert_eq!(
156-
*wallet.get_descriptor_for_keychain(KeychainKind::External),
156+
*wallet.public_descriptor(KeychainKind::External),
157157
desc.into_wallet_descriptor(&secp, wallet.network())
158158
.unwrap()
159159
.0
@@ -1423,7 +1423,7 @@ fn test_add_foreign_utxo() {
14231423
.assume_checked();
14241424
let utxo = wallet2.list_unspent().next().expect("must take!");
14251425
let foreign_utxo_satisfaction = wallet2
1426-
.get_descriptor_for_keychain(KeychainKind::External)
1426+
.public_descriptor(KeychainKind::External)
14271427
.max_weight_to_satisfy()
14281428
.unwrap();
14291429

@@ -1499,7 +1499,7 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
14991499
.assume_checked();
15001500
let utxo = wallet2.list_unspent().next().expect("must take!");
15011501
let foreign_utxo_satisfaction = wallet2
1502-
.get_descriptor_for_keychain(KeychainKind::External)
1502+
.public_descriptor(KeychainKind::External)
15031503
.max_weight_to_satisfy()
15041504
.unwrap();
15051505

@@ -1524,7 +1524,7 @@ fn test_add_foreign_utxo_invalid_psbt_input() {
15241524
let (mut wallet, _) = get_funded_wallet_wpkh();
15251525
let outpoint = wallet.list_unspent().next().expect("must exist").outpoint;
15261526
let foreign_utxo_satisfaction = wallet
1527-
.get_descriptor_for_keychain(KeychainKind::External)
1527+
.public_descriptor(KeychainKind::External)
15281528
.max_weight_to_satisfy()
15291529
.unwrap();
15301530

@@ -1545,7 +1545,7 @@ fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() {
15451545
let tx2 = wallet2.get_tx(txid2).unwrap().tx_node.tx.clone();
15461546

15471547
let satisfaction_weight = wallet2
1548-
.get_descriptor_for_keychain(KeychainKind::External)
1548+
.public_descriptor(KeychainKind::External)
15491549
.max_weight_to_satisfy()
15501550
.unwrap();
15511551

@@ -1589,7 +1589,7 @@ fn test_add_foreign_utxo_only_witness_utxo() {
15891589
let utxo2 = wallet2.list_unspent().next().unwrap();
15901590

15911591
let satisfaction_weight = wallet2
1592-
.get_descriptor_for_keychain(KeychainKind::External)
1592+
.public_descriptor(KeychainKind::External)
15931593
.max_weight_to_satisfy()
15941594
.unwrap();
15951595

@@ -3421,7 +3421,7 @@ fn test_taproot_foreign_utxo() {
34213421
let utxo = wallet2.list_unspent().next().unwrap();
34223422
let psbt_input = wallet2.get_psbt_input(utxo.clone(), None, false).unwrap();
34233423
let foreign_utxo_satisfaction = wallet2
3424-
.get_descriptor_for_keychain(KeychainKind::External)
3424+
.public_descriptor(KeychainKind::External)
34253425
.max_weight_to_satisfy()
34263426
.unwrap();
34273427

0 commit comments

Comments
 (0)