diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 513e3ebe3c4..55ef2f5e4fc 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4572,6 +4572,14 @@ std::optional CWallet::GetKey(const CKeyID& keyid) const return std::nullopt; } +std::optional CWallet::GetExtKey(const CExtPubKey& xpub) const +{ + if (std::optional key = GetKey(xpub.pubkey.GetID())) { + return CExtKey{xpub, *key}; + } + return std::nullopt; +} + void CWallet::WriteBestBlock() const { AssertLockHeld(cs_wallet); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 4d12c404d64..75b459848a0 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1091,6 +1091,10 @@ public: //! Returns nullopt when no descriptor has the key or if the wallet is locked. std::optional GetKey(const CKeyID& keyid) const; + //! Reconstruct the extended private key for an HD xpub. Returns nullopt when + //! no descriptor has the private key, or the wallet is locked. + std::optional GetExtKey(const CExtPubKey& xpub) const; + //! Disconnect chain notifications and wait for all notifications to be processed void DisconnectChainNotifications(); };