diff --git a/src/wallet/crypter.h b/src/wallet/crypter.h --- a/src/wallet/crypter.h +++ b/src/wallet/crypter.h @@ -116,8 +116,6 @@ */ class CCryptoKeyStore : public CBasicKeyStore { private: - CryptedKeyMap mapCryptedKeys; - CKeyingMaterial vMasterKey; //! if fUseCrypto is true, mapKeys must be empty @@ -134,6 +132,7 @@ bool EncryptKeys(CKeyingMaterial &vMasterKeyIn); bool Unlock(const CKeyingMaterial &vMasterKeyIn); + CryptedKeyMap mapCryptedKeys; public: CCryptoKeyStore() diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -736,8 +736,6 @@ std::map mapAddressBook; - CPubKey vchDefaultKey; - std::set setLockedCoins; const CWalletTx *GetWalletTx(const uint256 &hash) const; @@ -1016,8 +1014,6 @@ return setInternalKeyPool.size() + setExternalKeyPool.size(); } - bool SetDefaultKey(const CPubKey &vchPubKey); - //! signify that a particular wallet feature is now used. this may change //! nWalletVersion and nWalletMaxVersion if those are lower bool SetMinVersion(enum WalletFeature, CWalletDB *pwalletdbIn = nullptr, diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3182,12 +3182,15 @@ } } + // This wallet is in its first run if all of these are empty + fFirstRunRet = mapKeys.empty() && mapCryptedKeys.empty() && + mapWatchKeys.empty() && setWatchOnly.empty() && + mapScripts.empty(); + if (nLoadWalletRet != DB_LOAD_OK) { return nLoadWalletRet; } - fFirstRunRet = !vchDefaultKey.IsValid(); - uiInterface.LoadWallet(this); return DB_LOAD_OK; @@ -3196,7 +3199,6 @@ DBErrors CWallet::ZapSelectTx(std::vector &vHashIn, std::vector &vHashOut) { AssertLockHeld(cs_wallet); // mapWallet - vchDefaultKey = CPubKey(); DBErrors nZapSelectTxRet = CWalletDB(*dbw, "cr+").ZapSelectTx(vHashIn, vHashOut); for (uint256 hash : vHashOut) { @@ -3224,7 +3226,6 @@ } DBErrors CWallet::ZapWalletTx(std::vector &vWtx) { - vchDefaultKey = CPubKey(); DBErrors nZapWalletTxRet = CWalletDB(*dbw, "cr+").ZapWalletTx(vWtx); if (nZapWalletTxRet == DB_NEED_REWRITE) { if (dbw->Rewrite("\x04pool")) { @@ -3296,15 +3297,6 @@ return CWalletDB(*dbw).EraseName(address); } -bool CWallet::SetDefaultKey(const CPubKey &vchPubKey) { - if (!CWalletDB(*dbw).WriteDefaultKey(vchPubKey)) { - return false; - } - - vchDefaultKey = vchPubKey; - return true; -} - /** * Mark old keypool keys as used, and generate all new keys. */ @@ -4224,14 +4216,10 @@ } } - CPubKey newDefaultKey; - if (walletInstance->GetKeyFromPool(newDefaultKey, false)) { - walletInstance->SetDefaultKey(newDefaultKey); - if (!walletInstance->SetAddressBook( - walletInstance->vchDefaultKey.GetID(), "", "receive")) { - InitError(_("Cannot write default address") += "\n"); - return nullptr; - } + // Top up the keypool + if (!walletInstance->TopUpKeyPool()) { + InitError(_("Unable to generate initial keys") += "\n"); + return nullptr; } walletInstance->SetBestChain(chainActive.GetLocator()); diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -190,8 +190,6 @@ bool WriteOrderPosNext(int64_t nOrderPosNext); - bool WriteDefaultKey(const CPubKey &vchPubKey); - bool ReadPool(int64_t nPool, CKeyPool &keypool); bool WritePool(int64_t nPool, const CKeyPool &keypool); bool ErasePool(int64_t nPool); diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -156,10 +156,6 @@ return WriteIC(std::string("orderposnext"), nOrderPosNext); } -bool CWalletDB::WriteDefaultKey(const CPubKey &vchPubKey) { - return WriteIC(std::string("defaultkey"), vchPubKey); -} - bool CWalletDB::ReadPool(int64_t nPool, CKeyPool &keypool) { return batch.Read(std::make_pair(std::string("pool"), nPool), keypool); } @@ -465,7 +461,15 @@ pwallet->LoadKeyMetadata(keyID, keyMeta); } else if (strType == "defaultkey") { - ssValue >> pwallet->vchDefaultKey; + // We don't want or need the default key, but if there is one set, + // we want to make sure that it is valid so that we can detect + // corruption + CPubKey vchPubKey; + ssValue >> vchPubKey; + if (!vchPubKey.IsValid()) { + strErr = "Error reading wallet database: Default Key corrupt"; + return false; + } } else if (strType == "pool") { int64_t nIndex; ssKey >> nIndex; @@ -519,7 +523,6 @@ } DBErrors CWalletDB::LoadWallet(CWallet *pwallet) { - pwallet->vchDefaultKey = CPubKey(); CWalletScanState wss; bool fNoncriticalErrors = false; DBErrors result = DB_LOAD_OK; @@ -560,7 +563,7 @@ if (!ReadKeyValue(pwallet, ssKey, ssValue, wss, strType, strErr)) { // losing keys is considered a catastrophic error, anything else // we assume the user can live with: - if (IsKeyType(strType)) { + if (IsKeyType(strType) || strType == "defaultkey") { result = DB_CORRUPT; } else { // Leave other errors alone, if we try to fix them we might diff --git a/test/functional/keypool-topup.py b/test/functional/keypool-topup.py --- a/test/functional/keypool-topup.py +++ b/test/functional/keypool-topup.py @@ -75,7 +75,7 @@ # Check that we have marked all keys up to the used keypool key as used assert_equal(self.nodes[1].validateaddress( - self.nodes[1].getnewaddress())['hdkeypath'], "m/0'/0'/111'") + self.nodes[1].getnewaddress())['hdkeypath'], "m/0'/0'/110'") if __name__ == '__main__': diff --git a/test/functional/wallet-hd.py b/test/functional/wallet-hd.py --- a/test/functional/wallet-hd.py +++ b/test/functional/wallet-hd.py @@ -56,7 +56,7 @@ for i in range(num_hd_adds): hd_add = self.nodes[1].getnewaddress() hd_info = self.nodes[1].validateaddress(hd_add) - assert_equal(hd_info["hdkeypath"], "m/0'/0'/" + str(i + 1) + "'") + assert_equal(hd_info["hdkeypath"], "m/0'/0'/" + str(i) + "'") assert_equal(hd_info["hdmasterkeyid"], masterkeyid) self.nodes[0].sendtoaddress(hd_add, 1) self.nodes[0].generate(1) @@ -88,7 +88,7 @@ for _ in range(num_hd_adds): hd_add_2 = self.nodes[1].getnewaddress() hd_info_2 = self.nodes[1].validateaddress(hd_add_2) - assert_equal(hd_info_2["hdkeypath"], "m/0'/0'/" + str(_ + 1) + "'") + assert_equal(hd_info_2["hdkeypath"], "m/0'/0'/" + str(_) + "'") assert_equal(hd_info_2["hdmasterkeyid"], masterkeyid) assert_equal(hd_add, hd_add_2) connect_nodes_bi(self.nodes, 0, 1)