Skip to content

Commit e7ec5a8

Browse files
committed
refactor(wallet)!: remove redundant get_descriptor_for_keychain
Simplify Wallet::public_descriptor() and update Wallet internals to use public_descriptor() instead of get_descriptor_for_keychain().
1 parent a112b4d commit e7ec5a8

4 files changed

Lines changed: 22 additions & 28 deletions

File tree

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
@@ -296,7 +296,7 @@ impl<'a, Cs> TxBuilder<'a, Cs> {
296296
.collect::<Result<Vec<_>, _>>()?;
297297

298298
for utxo in utxos {
299-
let descriptor = wallet.get_descriptor_for_keychain(utxo.keychain);
299+
let descriptor = wallet.public_descriptor(utxo.keychain);
300300
let satisfaction_weight = descriptor.max_weight_to_satisfy().unwrap();
301301
self.params.utxos.push(WeightedUtxo {
302302
satisfaction_weight,

crates/wallet/tests/wallet.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ fn load_recovers_wallet() -> anyhow::Result<()> {
151151
);
152152
let secp = Secp256k1::new();
153153
assert_eq!(
154-
*wallet.get_descriptor_for_keychain(KeychainKind::External),
154+
*wallet.public_descriptor(KeychainKind::External),
155155
desc.into_wallet_descriptor(&secp, wallet.network())
156156
.unwrap()
157157
.0
@@ -1402,7 +1402,7 @@ fn test_add_foreign_utxo() {
14021402
.assume_checked();
14031403
let utxo = wallet2.list_unspent().next().expect("must take!");
14041404
let foreign_utxo_satisfaction = wallet2
1405-
.get_descriptor_for_keychain(KeychainKind::External)
1405+
.public_descriptor(KeychainKind::External)
14061406
.max_weight_to_satisfy()
14071407
.unwrap();
14081408

@@ -1478,7 +1478,7 @@ fn test_calculate_fee_with_missing_foreign_utxo() {
14781478
.assume_checked();
14791479
let utxo = wallet2.list_unspent().next().expect("must take!");
14801480
let foreign_utxo_satisfaction = wallet2
1481-
.get_descriptor_for_keychain(KeychainKind::External)
1481+
.public_descriptor(KeychainKind::External)
14821482
.max_weight_to_satisfy()
14831483
.unwrap();
14841484

@@ -1503,7 +1503,7 @@ fn test_add_foreign_utxo_invalid_psbt_input() {
15031503
let (mut wallet, _) = get_funded_wallet_wpkh();
15041504
let outpoint = wallet.list_unspent().next().expect("must exist").outpoint;
15051505
let foreign_utxo_satisfaction = wallet
1506-
.get_descriptor_for_keychain(KeychainKind::External)
1506+
.public_descriptor(KeychainKind::External)
15071507
.max_weight_to_satisfy()
15081508
.unwrap();
15091509

@@ -1524,7 +1524,7 @@ fn test_add_foreign_utxo_where_outpoint_doesnt_match_psbt_input() {
15241524
let tx2 = wallet2.get_tx(txid2).unwrap().tx_node.tx.clone();
15251525

15261526
let satisfaction_weight = wallet2
1527-
.get_descriptor_for_keychain(KeychainKind::External)
1527+
.public_descriptor(KeychainKind::External)
15281528
.max_weight_to_satisfy()
15291529
.unwrap();
15301530

@@ -1568,7 +1568,7 @@ fn test_add_foreign_utxo_only_witness_utxo() {
15681568
let utxo2 = wallet2.list_unspent().next().unwrap();
15691569

15701570
let satisfaction_weight = wallet2
1571-
.get_descriptor_for_keychain(KeychainKind::External)
1571+
.public_descriptor(KeychainKind::External)
15721572
.max_weight_to_satisfy()
15731573
.unwrap();
15741574

@@ -3400,7 +3400,7 @@ fn test_taproot_foreign_utxo() {
34003400
let utxo = wallet2.list_unspent().next().unwrap();
34013401
let psbt_input = wallet2.get_psbt_input(utxo.clone(), None, false).unwrap();
34023402
let foreign_utxo_satisfaction = wallet2
3403-
.get_descriptor_for_keychain(KeychainKind::External)
3403+
.public_descriptor(KeychainKind::External)
34043404
.max_weight_to_satisfy()
34053405
.unwrap();
34063406

0 commit comments

Comments
 (0)