Skip to content

Commit 8e1a475

Browse files
committed
wallet: Be able to retrieve single key from descriptors
Adds CWallet::GetKey which retrieves a single key from the descriptors stored in the wallet.
1 parent 85b1fb1 commit 8e1a475

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

src/wallet/wallet.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4507,4 +4507,19 @@ std::set<CExtPubKey> CWallet::GetActiveHDPubKeys() const
45074507
}
45084508
return active_xpubs;
45094509
}
4510+
4511+
std::optional<CKey> CWallet::GetKey(const CKeyID& keyid) const
4512+
{
4513+
Assert(IsWalletFlagSet(WALLET_FLAG_DESCRIPTORS));
4514+
4515+
for (const auto& spkm : GetAllScriptPubKeyMans()) {
4516+
const DescriptorScriptPubKeyMan* desc_spkm = dynamic_cast<DescriptorScriptPubKeyMan*>(spkm);
4517+
assert(desc_spkm);
4518+
LOCK(desc_spkm->cs_desc_man);
4519+
if (std::optional<CKey> key = desc_spkm->GetKey(keyid)) {
4520+
return key;
4521+
}
4522+
}
4523+
return std::nullopt;
4524+
}
45104525
} // namespace wallet

src/wallet/wallet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,10 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
10591059

10601060
//! Retrieve the xpubs in use by the active descriptors
10611061
std::set<CExtPubKey> GetActiveHDPubKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1062+
1063+
//! Find the private key for the given key id from the wallet's descriptors, if available
1064+
//! Returns nullopt when no descriptor has the key or if the wallet is locked.
1065+
std::optional<CKey> GetKey(const CKeyID& keyid) const;
10621066
};
10631067

10641068
/**

0 commit comments

Comments
 (0)