Skip to content

Commit a50d9e6

Browse files
committed
rpcwallet: default include_watchonly to true for watchonly wallets
The logic before would only include watchonly addresses if it was explicitly set in the rpc argument. This changes the logic like so: If the include_watchonly argument is missing, check the WALLET_FLAG_DISABLE_PRIVATE_KEYS flag to determine if we're working with a watchonly wallet. If so, default include_watchonly to true. If the include_watchonly argument is explicit set to false, we still disable them from the listing. Although this would always return nothing, it might be still useful in situations where you want to explicitly filter out watchonly addresses regardless of what wallet you are dealing with. Signed-off-by: William Casarin <[email protected]>
1 parent 536590f commit a50d9e6

File tree

1 file changed

+36
-17
lines changed

1 file changed

+36
-17
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,23 @@ static inline bool GetAvoidReuseFlag(CWallet * const pwallet, const UniValue& pa
5252
return avoid_reuse;
5353
}
5454

55+
56+
/** Used by RPC commands that have an include_watchonly parameter.
57+
* We default to true for watchonly wallets if include_watchonly isn't
58+
* explicitly set.
59+
*/
60+
static bool ParseIncludeWatchonly(const UniValue& include_watchonly, const CWallet& pwallet)
61+
{
62+
if (include_watchonly.isNull()) {
63+
// if include_watchonly isn't explicitly set, then check if we have a watchonly wallet
64+
return pwallet.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS);
65+
}
66+
67+
// otherwise return whatever include_watchonly was set to
68+
return include_watchonly.get_bool();
69+
}
70+
71+
5572
/** Checks if a CKey is in the given CWallet compressed or otherwise*/
5673
bool HaveKey(const CWallet& wallet, const CKey& key)
5774
{
@@ -748,10 +765,7 @@ static UniValue getbalance(const JSONRPCRequest& request)
748765
min_depth = request.params[1].get_int();
749766
}
750767

751-
bool include_watchonly = false;
752-
if (!request.params[2].isNull() && request.params[2].get_bool()) {
753-
include_watchonly = true;
754-
}
768+
bool include_watchonly = ParseIncludeWatchonly(request.params[2], *pwallet);
755769

756770
bool avoid_reuse = GetAvoidReuseFlag(pwallet, request.params[3]);
757771

@@ -1033,9 +1047,10 @@ static UniValue ListReceived(interfaces::Chain::Lock& locked_chain, CWallet * co
10331047
fIncludeEmpty = params[1].get_bool();
10341048

10351049
isminefilter filter = ISMINE_SPENDABLE;
1036-
if(!params[2].isNull())
1037-
if(params[2].get_bool())
1038-
filter = filter | ISMINE_WATCH_ONLY;
1050+
1051+
if (ParseIncludeWatchonly(params[2], *pwallet)) {
1052+
filter |= ISMINE_WATCH_ONLY;
1053+
}
10391054

10401055
bool has_filtered_address = false;
10411056
CTxDestination filtered_address = CNoDestination();
@@ -1434,9 +1449,10 @@ UniValue listtransactions(const JSONRPCRequest& request)
14341449
if (!request.params[2].isNull())
14351450
nFrom = request.params[2].get_int();
14361451
isminefilter filter = ISMINE_SPENDABLE;
1437-
if(!request.params[3].isNull())
1438-
if(request.params[3].get_bool())
1439-
filter = filter | ISMINE_WATCH_ONLY;
1452+
1453+
if (ParseIncludeWatchonly(request.params[3], *pwallet)) {
1454+
filter |= ISMINE_WATCH_ONLY;
1455+
}
14401456

14411457
if (nCount < 0)
14421458
throw JSONRPCError(RPC_INVALID_PARAMETER, "Negative count");
@@ -1579,8 +1595,8 @@ static UniValue listsinceblock(const JSONRPCRequest& request)
15791595
}
15801596
}
15811597

1582-
if (!request.params[2].isNull() && request.params[2].get_bool()) {
1583-
filter = filter | ISMINE_WATCH_ONLY;
1598+
if (ParseIncludeWatchonly(request.params[2], *pwallet)) {
1599+
filter |= ISMINE_WATCH_ONLY;
15841600
}
15851601

15861602
bool include_removed = (request.params[3].isNull() || request.params[3].get_bool());
@@ -1697,9 +1713,10 @@ static UniValue gettransaction(const JSONRPCRequest& request)
16971713
uint256 hash(ParseHashV(request.params[0], "txid"));
16981714

16991715
isminefilter filter = ISMINE_SPENDABLE;
1700-
if(!request.params[1].isNull())
1701-
if(request.params[1].get_bool())
1702-
filter = filter | ISMINE_WATCH_ONLY;
1716+
1717+
if (ParseIncludeWatchonly(request.params[1], *pwallet)) {
1718+
filter |= ISMINE_WATCH_ONLY;
1719+
}
17031720

17041721
UniValue entry(UniValue::VOBJ);
17051722
auto it = pwallet->mapWallet.find(hash);
@@ -3014,8 +3031,7 @@ void FundTransaction(CWallet* const pwallet, CMutableTransaction& tx, CAmount& f
30143031
}
30153032
}
30163033

3017-
if (options.exists("includeWatching"))
3018-
coinControl.fAllowWatchOnly = options["includeWatching"].get_bool();
3034+
coinControl.fAllowWatchOnly = ParseIncludeWatchonly(options["includeWatching"], *pwallet);
30193035

30203036
if (options.exists("lockUnspents"))
30213037
lockUnspents = options["lockUnspents"].get_bool();
@@ -3047,6 +3063,9 @@ void FundTransaction(CWallet* const pwallet, CMutableTransaction& tx, CAmount& f
30473063
}
30483064
}
30493065
}
3066+
} else {
3067+
// if options is null and not a bool
3068+
coinControl.fAllowWatchOnly = ParseIncludeWatchonly(NullUniValue, *pwallet);
30503069
}
30513070

30523071
if (tx.vout.size() == 0)

0 commit comments

Comments
 (0)