Skip to content

Commit 77e840c

Browse files
committed
Added RPC functionallity to zap watch only addresses
1 parent 44f61c6 commit 77e840c

3 files changed

Lines changed: 46 additions & 10 deletions

File tree

src/wallet/rpcwallet.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4164,6 +4164,23 @@ static UniValue setstakesplitthreshold(const JSONRPCRequest& request)
41644164
}
41654165
}
41664166

4167+
static UniValue eraseallwatches(const JSONRPCRequest& request)
4168+
{
4169+
if (request.fHelp || request.params.size() != 0)
4170+
throw std::runtime_error(
4171+
"eraseallwatches\n"
4172+
"Deletes all watch-only addresses from wallet\n"
4173+
"\nResult:\n"
4174+
"n (bool) success or not\n"
4175+
"\nExamples:\n" +
4176+
HelpExampleCli("eraseallwatches", "") + HelpExampleRpc("eraseallwatches", ""));
4177+
4178+
CWallet * const pwallet = GetWalletForJSONRPCRequest(request);
4179+
pwallet->DelAllWatchOnly();
4180+
4181+
return std::string{"true"};
4182+
}
4183+
41674184
// presstab HyperStake
41684185
static UniValue getstakesplitthreshold(const JSONRPCRequest& request)
41694186
{
@@ -4244,6 +4261,7 @@ static const CRPCCommand commands[] =
42444261
{ "wallet", "rescanblockchain", &rescanblockchain, {"start_height", "stop_height"} },
42454262
{ "wallet", "setstakesplitthreshold", &setstakesplitthreshold, {"threshold_amount"}},
42464263
{ "wallet", "getstakesplitthreshold", &getstakesplitthreshold, {} },
4264+
{ "wallet", "eraseallwatches", &eraseallwatches, {} },
42474265

42484266
/** Account functions (deprecated) */
42494267
{ "wallet", "getaccountaddress", &getlabeladdress, {"account"} },

src/wallet/wallet.cpp

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,23 +153,25 @@ class CAffectedKeysVisitor : public boost::static_visitor<void> {
153153
void CWallet::LoadContractsFromDB()
154154
{
155155
LOCK(cs_wallet);
156-
for(auto &&contractTx : std::move(tposContractsTxLoadedFromDB))
157-
{
158-
if(!LoadTPoSContract(contractTx))
159-
{
156+
for (auto &&contractTx : std::move(tposContractsTxLoadedFromDB)) {
157+
if (!LoadTPoSContract(contractTx)) {
160158
// if contract was not added, there is a big chance that it's deprecated, let's cleanup watch only address
161-
if(TPoSUtils::IsTPoSMerchantContract(this, contractTx.tx))
162-
{
159+
if (TPoSUtils::IsTPoSMerchantContract(this, contractTx.tx)) {
163160
auto tposContract = TPoSContract::FromTPoSContractTx(contractTx.tx);
164-
if(tposContract.IsValid())
165-
{
161+
if (tposContract.IsValid()) {
166162
auto script = tposContract.scriptTPoSAddress;
167-
if(HaveWatchOnly(script))
168-
{
163+
if (HaveWatchOnly(script)) {
169164
RemoveWatchOnly(script);
170165
}
171166
}
172167
}
168+
} else {
169+
if (TPoSUtils::IsTPoSMerchantContract(this, contractTx.tx)) {
170+
auto tposContract = TPoSContract::FromTPoSContractTx(contractTx.tx);
171+
if (tposContract.IsValid() && !HaveWatchOnly(tposContract.scriptTPoSAddress)) {
172+
AddWatchOnly(tposContract.scriptTPoSAddress);
173+
}
174+
}
173175
}
174176
}
175177
}
@@ -4018,6 +4020,20 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
40184020
return WalletBatch(*database).EraseName(EncodeDestination(address));
40194021
}
40204022

4023+
void CWallet::DelAllWatchOnly()
4024+
{
4025+
WatchOnlySet setTmpWatchOnly;
4026+
{
4027+
LOCK(cs_wallet);
4028+
setWatchOnly.swap(setTmpWatchOnly);
4029+
}
4030+
4031+
WalletBatch batch(*database);
4032+
for (auto &&script : setTmpWatchOnly) {
4033+
batch.EraseWatchOnly(script);
4034+
}
4035+
}
4036+
40214037
const std::string& CWallet::GetLabelName(const CScript& scriptPubKey) const
40224038
{
40234039
CTxDestination address;

src/wallet/wallet.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,8 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
11351135

11361136
bool DelAddressBook(const CTxDestination& address);
11371137

1138+
void DelAllWatchOnly();
1139+
11381140
const std::string& GetLabelName(const CScript& scriptPubKey) const;
11391141

11401142
void Inventory(const uint256 &hash) override

0 commit comments

Comments
 (0)