Skip to content

Commit

Permalink
Added RPC functionallity to zap watch only addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
durkmurder committed Sep 24, 2020
1 parent 44f61c6 commit 77e840c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
18 changes: 18 additions & 0 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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"} },
Expand Down
36 changes: 26 additions & 10 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,25 @@ class CAffectedKeysVisitor : public boost::static_visitor<void> {
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);
}
}
}
}
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 77e840c

Please sign in to comment.