Skip to content

Commit 03840c2

Browse files
committed
Add CWallet::IsInternalScriptPubKeyMan
1 parent 456e350 commit 03840c2

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/wallet/rpcdump.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1819,11 +1819,10 @@ RPCHelpMan listdescriptors()
18191819
}
18201820
spk.pushKV("desc", descriptor);
18211821
spk.pushKV("timestamp", wallet_descriptor.creation_time);
1822-
const bool active = active_spk_mans.count(desc_spk_man) != 0;
1823-
spk.pushKV("active", active);
1824-
const auto& type = wallet_descriptor.descriptor->GetOutputType();
1825-
if (active && type) {
1826-
spk.pushKV("internal", wallet->GetScriptPubKeyMan(*type, true) == desc_spk_man);
1822+
spk.pushKV("active", active_spk_mans.count(desc_spk_man) != 0);
1823+
const auto internal = wallet->IsInternalScriptPubKeyMan(desc_spk_man);
1824+
if (internal.has_value()) {
1825+
spk.pushKV("internal", *internal);
18271826
}
18281827
if (wallet_descriptor.descriptor->IsRange()) {
18291828
UniValue range(UniValue::VARR);

src/wallet/wallet.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3265,6 +3265,30 @@ DescriptorScriptPubKeyMan* CWallet::GetDescriptorScriptPubKeyMan(const WalletDes
32653265
return nullptr;
32663266
}
32673267

3268+
std::optional<bool> CWallet::IsInternalScriptPubKeyMan(ScriptPubKeyMan* spk_man) const
3269+
{
3270+
// Legacy script pubkey man can't be either external or internal
3271+
if (IsLegacy()) {
3272+
return std::nullopt;
3273+
}
3274+
3275+
// only active ScriptPubKeyMan can be internal
3276+
if (!GetActiveScriptPubKeyMans().count(spk_man)) {
3277+
return std::nullopt;
3278+
}
3279+
3280+
const auto desc_spk_man = dynamic_cast<DescriptorScriptPubKeyMan*>(spk_man);
3281+
if (!desc_spk_man) {
3282+
throw std::runtime_error(std::string(__func__) + ": unexpected ScriptPubKeyMan type.");
3283+
}
3284+
3285+
LOCK(desc_spk_man->cs_desc_man);
3286+
const auto& type = desc_spk_man->GetWalletDescriptor().descriptor->GetOutputType();
3287+
assert(type.has_value());
3288+
3289+
return GetScriptPubKeyMan(*type, /* internal= */ true) == desc_spk_man;
3290+
}
3291+
32683292
ScriptPubKeyMan* CWallet::AddWalletDescriptor(WalletDescriptor& desc, const FlatSigningProvider& signing_provider, const std::string& label, bool internal)
32693293
{
32703294
AssertLockHeld(cs_wallet);

src/wallet/wallet.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -875,6 +875,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
875875
//! Return the DescriptorScriptPubKeyMan for a WalletDescriptor if it is already in the wallet
876876
DescriptorScriptPubKeyMan* GetDescriptorScriptPubKeyMan(const WalletDescriptor& desc) const;
877877

878+
//! Returns whether the provided ScriptPubKeyMan is internal
879+
//! @param[in] spk_man The ScriptPubKeyMan to test
880+
//! @return contains value only for active DescriptorScriptPubKeyMan, otherwise undefined
881+
std::optional<bool> IsInternalScriptPubKeyMan(ScriptPubKeyMan* spk_man) const;
882+
878883
//! Add a descriptor to the wallet, return a ScriptPubKeyMan & associated output type
879884
ScriptPubKeyMan* AddWalletDescriptor(WalletDescriptor& desc, const FlatSigningProvider& signing_provider, const std::string& label, bool internal) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
880885
};

0 commit comments

Comments
 (0)