diff --git a/src/wallet/scriptpubkeyman.cpp b/src/wallet/scriptpubkeyman.cpp index 1eecdd113a517..66c2c1b3fc05f 100644 --- a/src/wallet/scriptpubkeyman.cpp +++ b/src/wallet/scriptpubkeyman.cpp @@ -65,13 +65,15 @@ bool ScriptPubKeyMan::CanGetAddresses(const uint8_t& type) { LOCK(wallet->cs_wallet); // Check if the keypool has keys + const bool isHDEnabled = IsHDEnabled(); bool keypool_has_keys = false; - if (type == HDChain::ChangeType::INTERNAL && IsHDEnabled()) { + if (isHDEnabled && type == HDChain::ChangeType::INTERNAL) { keypool_has_keys = setInternalKeyPool.size() > 0; - } else if (type == HDChain::ChangeType::EXTERNAL) { - keypool_has_keys = KeypoolCountExternalKeys() > 0; - } else if (type == HDChain::ChangeType::STAKING) { + } else if (isHDEnabled && type == HDChain::ChangeType::STAKING) { keypool_has_keys = setStakingKeyPool.size() > 0; + } else { + // either external key was requested or HD is not enabled + keypool_has_keys = KeypoolCountExternalKeys() > 0; } // If the keypool doesn't have keys, check if we can generate them if (!keypool_has_keys) { @@ -159,10 +161,10 @@ bool ScriptPubKeyMan::GetKeyFromPool(CPubKey& result, const uint8_t& changeType) bool ScriptPubKeyMan::GetReservedKey(const uint8_t& changeType, int64_t& index, CKeyPool& keypool) { if (!CanGetAddresses(changeType)) { - return false; + return error("%s : Cannot get address for change type %d", __func__, changeType); } if (!ReserveKeyFromKeyPool(index, keypool, changeType)) { - return false; + return error("%s : Cannot reserve key from pool for change type %d", __func__, changeType); } return true; } @@ -237,15 +239,15 @@ void ScriptPubKeyMan::ReturnDestination(int64_t nIndex, const uint8_t& type, con // Return to key pool { LOCK(wallet->cs_wallet); - bool fInternal = type == HDChain::ChangeType::INTERNAL; - if (fInternal) { + const bool isHDEnabled = IsHDEnabled(); + if (isHDEnabled && type == HDChain::ChangeType::INTERNAL) { setInternalKeyPool.insert(nIndex); - } else if (!set_pre_split_keypool.empty()) { + } else if (isHDEnabled && type == HDChain::ChangeType::STAKING) { + setStakingKeyPool.insert(nIndex); + } else if (isHDEnabled && !set_pre_split_keypool.empty()) { set_pre_split_keypool.insert(nIndex); - } else if (type == HDChain::ChangeType::EXTERNAL) { + } else { setExternalKeyPool.insert(nIndex); - } else if (type == HDChain::ChangeType::STAKING) { - setStakingKeyPool.insert(nIndex); } CKeyID& pubkey_id = m_index_to_reserved_key.at(nIndex); m_pool_key_to_index[pubkey_id] = nIndex; @@ -380,8 +382,9 @@ bool ScriptPubKeyMan::TopUp(unsigned int kpSize) int64_t missingStaking = std::max(std::max((int64_t) nTargetSize, (int64_t) 1) - (int64_t)setStakingKeyPool.size(), (int64_t) 0); if (!IsHDEnabled()) { - // don't create extra internal keys + // don't create extra internal or staking keys missingInternal = 0; + missingStaking = 0; } CWalletDB batch(wallet->strWalletFile); @@ -418,16 +421,13 @@ void ScriptPubKeyMan::AddKeypoolPubkeyWithDB(const CPubKey& pubkey, const uint8_ throw std::runtime_error(std::string(__func__) + ": writing imported pubkey failed"); } - switch (type) { - case HDChain::ChangeType::EXTERNAL: - setExternalKeyPool.insert(index); - break; - case HDChain::ChangeType::INTERNAL: - setInternalKeyPool.insert(index); - break; - case HDChain::ChangeType::STAKING: - setStakingKeyPool.insert(index); - break; + const bool isHDEnabled = IsHDEnabled(); + if (isHDEnabled && type == HDChain::ChangeType::INTERNAL) { + setInternalKeyPool.insert(index); + } else if (isHDEnabled && type == HDChain::ChangeType::STAKING) { + setStakingKeyPool.insert(index); + } else { + setExternalKeyPool.insert(index); } m_pool_key_to_index[pubkey.GetID()] = index; @@ -684,4 +684,4 @@ void ScriptPubKeyMan::SetHDChain(CHDChain& chain, bool memonly) // Sanity check if (!wallet->HaveKey(hdChain.GetID())) throw std::runtime_error(std::string(__func__) + ": Not found seed in wallet"); -} \ No newline at end of file +}