diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index b9d850c7b..a99aef610 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -4164,6 +4164,23 @@ static UniValue setstakesplitthreshold(const JSONRPCRequest& request) } } +static UniValue eraseallwatches(const JSONRPCRequest& request) +{ + if (request.fHelp || request.params.size() != 0) + throw std::runtime_error( + "eraseallwatches\n" + "Deletes all watch-only addresses from wallet\n" + "\nResult:\n" + "n (bool) success or not\n" + "\nExamples:\n" + + HelpExampleCli("eraseallwatches", "") + HelpExampleRpc("eraseallwatches", "")); + + CWallet * const pwallet = GetWalletForJSONRPCRequest(request); + pwallet->DelAllWatchOnly(); + + return std::string{"true"}; +} + // presstab HyperStake static UniValue getstakesplitthreshold(const JSONRPCRequest& request) { @@ -4244,6 +4261,7 @@ static const CRPCCommand commands[] = { "wallet", "rescanblockchain", &rescanblockchain, {"start_height", "stop_height"} }, { "wallet", "setstakesplitthreshold", &setstakesplitthreshold, {"threshold_amount"}}, { "wallet", "getstakesplitthreshold", &getstakesplitthreshold, {} }, + { "wallet", "eraseallwatches", &eraseallwatches, {} }, /** Account functions (deprecated) */ { "wallet", "getaccountaddress", &getlabeladdress, {"account"} }, diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 1b32c6a01..198c23120 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -153,23 +153,25 @@ class CAffectedKeysVisitor : public boost::static_visitor { void CWallet::LoadContractsFromDB() { LOCK(cs_wallet); - for(auto &&contractTx : std::move(tposContractsTxLoadedFromDB)) - { - if(!LoadTPoSContract(contractTx)) - { + for (auto &&contractTx : std::move(tposContractsTxLoadedFromDB)) { + if (!LoadTPoSContract(contractTx)) { // if contract was not added, there is a big chance that it's deprecated, let's cleanup watch only address - if(TPoSUtils::IsTPoSMerchantContract(this, contractTx.tx)) - { + if (TPoSUtils::IsTPoSMerchantContract(this, contractTx.tx)) { auto tposContract = TPoSContract::FromTPoSContractTx(contractTx.tx); - if(tposContract.IsValid()) - { + if (tposContract.IsValid()) { auto script = tposContract.scriptTPoSAddress; - if(HaveWatchOnly(script)) - { + if (HaveWatchOnly(script)) { RemoveWatchOnly(script); } } } + } else { + if (TPoSUtils::IsTPoSMerchantContract(this, contractTx.tx)) { + auto tposContract = TPoSContract::FromTPoSContractTx(contractTx.tx); + if (tposContract.IsValid() && !HaveWatchOnly(tposContract.scriptTPoSAddress)) { + AddWatchOnly(tposContract.scriptTPoSAddress); + } + } } } } @@ -4018,6 +4020,20 @@ bool CWallet::DelAddressBook(const CTxDestination& address) return WalletBatch(*database).EraseName(EncodeDestination(address)); } +void CWallet::DelAllWatchOnly() +{ + WatchOnlySet setTmpWatchOnly; + { + LOCK(cs_wallet); + setWatchOnly.swap(setTmpWatchOnly); + } + + WalletBatch batch(*database); + for (auto &&script : setTmpWatchOnly) { + batch.EraseWatchOnly(script); + } +} + const std::string& CWallet::GetLabelName(const CScript& scriptPubKey) const { CTxDestination address; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 15c0ee9e4..aac68017b 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -1135,6 +1135,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface bool DelAddressBook(const CTxDestination& address); + void DelAllWatchOnly(); + const std::string& GetLabelName(const CScript& scriptPubKey) const; void Inventory(const uint256 &hash) override