Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get keychain for script #1201

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/bdk/src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1986,6 +1986,14 @@ impl<D> Wallet<D> {
pub fn local_chain(&self) -> &LocalChain {
&self.chain
}

/// Get the [`KeychainKind`] and derivation index for a given [`Script`].
///
/// This will return `None` if the script is not in the wallet.
// TODO: Generalize this implementation once we make the wallet generic over K
pub fn which_keychain_derived(&self, spk: &Script) -> Option<&(KeychainKind, u32)> {
self.indexed_graph.index.index_of_spk(spk)
}
}

impl<D> AsRef<bdk_chain::tx_graph::TxGraph<ConfirmationTimeAnchor>> for Wallet<D> {
Expand Down
15 changes: 15 additions & 0 deletions crates/bdk/tests/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ fn receive_output_in_latest_block(wallet: &mut Wallet, value: u64) -> OutPoint {
// OP_PUSH.
const P2WPKH_FAKE_WITNESS_SIZE: usize = 106;

#[test]
fn test_which_keychain_derived_script() {
let (mut segwit_wallet, _) = get_funded_wallet(get_test_wpkh());
let addr = segwit_wallet.get_address(New);
let script = addr.script_pubkey();
let keychain = segwit_wallet.which_keychain_derived(&script).unwrap();
assert_eq!(keychain.0, KeychainKind::External);

let (mut taproot_wallet, _) = get_funded_wallet(get_test_tr_single_sig());
let addr = taproot_wallet.get_address(New);
let script = addr.script_pubkey();
let keychain = segwit_wallet.which_keychain_derived(&script);
assert!(keychain.is_none());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to checking the keychain, we could possibly also check the correct spk index is returned from which_keychain_derived.

diff
 fn test_which_keychain_derived_script() {
     let (mut segwit_wallet, _) = get_funded_wallet(get_test_wpkh());
     let addr = segwit_wallet.get_address(New);
     let script = addr.script_pubkey();
     let keychain = segwit_wallet.which_keychain_derived(&script).unwrap();
     assert_eq!(keychain.0, KeychainKind::External);
 
     let (mut taproot_wallet, _) = get_funded_wallet(get_test_tr_single_sig());
     let addr = taproot_wallet.get_address(New);
     let script = addr.script_pubkey();
     let keychain = segwit_wallet.which_keychain_derived(&script);
     assert!(keychain.is_none());
+
+    // find correct derivation index from spk
+    let desc = get_test_tr_single_sig_xprv();
+    let mut wallet = Wallet::new_no_persist(desc, None, Network::Testnet).unwrap();
+    let spk = wallet.get_address(New).address.script_pubkey();
+    let (_k, i) = wallet.which_keychain_derived(&spk).unwrap();
+    assert_eq!(i, &0);
+
+    // reveal more addresses. the last revealed index should now be 10.
+    for _ in 0..10 {
+        let _ = wallet.get_address(New);
+    }
+    let spk = wallet.get_address(Peek(10)).address.script_pubkey();
+    let (_k, i) = wallet.which_keychain_derived(&spk).unwrap();
+    assert_eq!(i, &10);
 }

}

#[test]
fn test_descriptor_checksum() {
let (wallet, _) = get_funded_wallet(get_test_wpkh());
Expand Down
Loading