follow-up Merge #17260: Split some CWallet functions into new LegacyScriptPubKeyMan

Some changes are missing or incorrectly backported during CWallet refactoring in #17260, #17261 such as:
 - Missing changes for CWallet::GetOldestKeyPoolTime
 - useless check of spk_man existance in getnewaddress
 - GetHDChain is used assuming it exists only legacy keymanager
 - using internal spk_man API instead wallet's in getwalletinfo
This commit is contained in:
Konstantin Akimov 2023-08-29 13:26:47 +07:00 committed by PastaPastaPasta
parent fa23a64471
commit 6183bd1085
2 changed files with 21 additions and 12 deletions

View File

@ -258,10 +258,6 @@ UniValue getnewaddress(const JSONRPCRequest& request)
if (!wallet) return NullUniValue;
CWallet* const pwallet = wallet.get();
LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan();
if (!spk_man) {
throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command");
}
LOCK(pwallet->cs_wallet);
if (!pwallet->CanGetAddresses()) {
@ -2560,9 +2556,9 @@ static UniValue getwalletinfo(const JSONRPCRequest& request)
if (spk_man) {
obj.pushKV("timefirstkey", spk_man->GetTimeFirstKey());
obj.pushKV("keypoololdest", spk_man->GetOldestKeyPoolTime());
obj.pushKV("keypoolsize", (int64_t)spk_man->KeypoolCountExternalKeys());
obj.pushKV("keypoolsize_hd_internal", (int64_t)(spk_man->KeypoolCountInternalKeys()));
}
obj.pushKV("keypoolsize", (int64_t)pwallet->KeypoolCountExternalKeys());
obj.pushKV("keypoolsize_hd_internal", (int64_t)(pwallet->KeypoolCountInternalKeys()));
obj.pushKV("keys_left", pwallet->nKeysLeftSinceAutoBackup);
if (pwallet->IsCrypted())
obj.pushKV("unlocked_until", pwallet->nRelockTime);

View File

@ -649,9 +649,11 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
// must get current HD chain before EncryptKeys
CHDChain hdChainCurrent;
// GetHDChain exist only at legacy.... how to validate it? just do static cast? we don't have any other type yet so may be ok temporary!
if (auto spk_man = GetLegacyScriptPubKeyMan()) {
spk_man->GetHDChain(hdChainCurrent);
for (const auto& spk_man_pair : m_spk_managers) {
auto spk_man = spk_man_pair.second.get();
LegacyScriptPubKeyMan *spk_man_legacy = dynamic_cast<LegacyScriptPubKeyMan*>(spk_man);
if (spk_man_legacy != nullptr) spk_man_legacy->GetHDChain(hdChainCurrent);
if (!spk_man->Encrypt(_vMasterKey, encrypted_batch)) {
encrypted_batch->TxnAbort();
delete encrypted_batch;
@ -661,10 +663,10 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
assert(false);
}
if (!hdChainCurrent.IsNull()) {
assert(spk_man->EncryptHDChain(_vMasterKey));
assert(spk_man_legacy->EncryptHDChain(_vMasterKey));
CHDChain hdChainCrypted;
assert(spk_man->GetHDChain(hdChainCrypted));
assert(spk_man_legacy->GetHDChain(hdChainCrypted));
DBG(
tfm::format(std::cout, "EncryptWallet -- current seed: '%s'\n", HexStr(hdChainCurrent.GetSeed()));
@ -675,7 +677,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
assert(hdChainCurrent.GetID() == hdChainCrypted.GetID());
assert(hdChainCurrent.GetSeedHash() != hdChainCrypted.GetSeedHash());
assert(spk_man->SetCryptedHDChain(*encrypted_batch, hdChainCrypted, false));
assert(spk_man_legacy->SetCryptedHDChain(*encrypted_batch, hdChainCrypted, false));
}
}
@ -4034,6 +4036,17 @@ bool CWallet::GetNewChangeDestination(CTxDestination& dest, std::string& error)
return true;
}
int64_t CWallet::GetOldestKeyPoolTime() const
{
LOCK(cs_wallet);
int64_t oldestKey = std::numeric_limits<int64_t>::max();
for (const auto& spk_man_pair : m_spk_managers) {
oldestKey = std::min(oldestKey, spk_man_pair.second->GetOldestKeyPoolTime());
}
return oldestKey;
}
void CWallet::MarkDestinationsDirty(const std::set<CTxDestination>& destinations) {
for (auto& entry : mapWallet) {
CWalletTx& wtx = entry.second;