Skip to content

Commit

Permalink
follow-up Merge bitcoin#17260: Split some CWallet functions into new …
Browse files Browse the repository at this point in the history
…LegacyScriptPubKeyMan

Some changes are missing or incorrectly backported during CWallet refactoring in bitcoin#17260, bitcoin#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
  • Loading branch information
knst committed Sep 16, 2023
1 parent b65cea5 commit 9f1d5e5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 12 deletions.
8 changes: 2 additions & 6 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down Expand Up @@ -2555,9 +2551,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);
Expand Down
25 changes: 19 additions & 6 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()));
Expand All @@ -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));
}
}

Expand Down Expand Up @@ -4040,6 +4042,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;
Expand Down

0 comments on commit 9f1d5e5

Please sign in to comment.