Skip to content

Commit f904723

Browse files
committed
wallet/rpc: add setwalletflag RPC and MUTABLE_WALLET_FLAGS
1 parent 8247a0d commit f904723

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
141141
{ "setban", 2, "bantime" },
142142
{ "setban", 3, "absolute" },
143143
{ "setnetworkactive", 0, "state" },
144+
{ "setwalletflag", 1, "value" },
144145
{ "getmempoolancestors", 1, "verbose" },
145146
{ "getmempooldescendants", 1, "verbose" },
146147
{ "bumpfee", 1, "options" },

src/wallet/rpcwallet.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2650,6 +2650,76 @@ static UniValue loadwallet(const JSONRPCRequest& request)
26502650
return obj;
26512651
}
26522652

2653+
static UniValue setwalletflag(const JSONRPCRequest& request)
2654+
{
2655+
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
2656+
CWallet* const pwallet = wallet.get();
2657+
2658+
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) {
2659+
return NullUniValue;
2660+
}
2661+
2662+
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2) {
2663+
std::string flags = "";
2664+
for (auto& it : WALLET_FLAG_MAP)
2665+
if (it.second & MUTABLE_WALLET_FLAGS)
2666+
flags += (flags == "" ? "" : ", ") + it.first;
2667+
throw std::runtime_error(
2668+
RPCHelpMan{"setwalletflag",
2669+
"\nChange the state of the given wallet flag for a wallet.\n",
2670+
{
2671+
{"flag", RPCArg::Type::STR, RPCArg::Optional::NO, "The name of the flag to change. Current available flags: " + flags},
2672+
{"value", RPCArg::Type::BOOL, /* default */ "true", "The new state."},
2673+
},
2674+
RPCResult{
2675+
"{\n"
2676+
" \"flag_name\": string (string) The name of the flag that was modified\n"
2677+
" \"flag_state\": bool (bool) The new state of the flag\n"
2678+
" \"warnings\": string (string) Any warnings associated with the change\n"
2679+
"}\n"
2680+
},
2681+
RPCExamples{
2682+
HelpExampleCli("setwalletflag", "avoid_reuse")
2683+
+ HelpExampleRpc("setwalletflag", "\"avoid_reuse\"")
2684+
},
2685+
}.ToString());
2686+
}
2687+
2688+
std::string flag_str = request.params[0].get_str();
2689+
bool value = request.params[1].isNull() || request.params[1].get_bool();
2690+
2691+
if (!WALLET_FLAG_MAP.count(flag_str)) {
2692+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Unknown wallet flag: %s", flag_str));
2693+
}
2694+
2695+
auto flag = WALLET_FLAG_MAP.at(flag_str);
2696+
2697+
if (!(flag & MUTABLE_WALLET_FLAGS)) {
2698+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is immutable: %s", flag_str));
2699+
}
2700+
2701+
UniValue res(UniValue::VOBJ);
2702+
2703+
if (pwallet->IsWalletFlagSet(flag) == value) {
2704+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Wallet flag is already set to %s: %s", value ? "true" : "false", flag_str));
2705+
}
2706+
2707+
res.pushKV("flag_name", flag_str);
2708+
res.pushKV("flag_state", value);
2709+
2710+
if (value) {
2711+
pwallet->SetWalletFlag(flag);
2712+
} else {
2713+
pwallet->UnsetWalletFlag(flag);
2714+
}
2715+
2716+
if (flag && value && WALLET_FLAG_CAVEATS.count(flag)) {
2717+
res.pushKV("warnings", WALLET_FLAG_CAVEATS.at(flag));
2718+
}
2719+
2720+
return res;
2721+
}
2722+
26532723
static UniValue createwallet(const JSONRPCRequest& request)
26542724
{
26552725
const RPCHelpMan help{
@@ -4232,6 +4302,7 @@ static const CRPCCommand commands[] =
42324302
{ "wallet", "sethdseed", &sethdseed, {"newkeypool","seed"} },
42334303
{ "wallet", "setlabel", &setlabel, {"address","label"} },
42344304
{ "wallet", "settxfee", &settxfee, {"amount"} },
4305+
{ "wallet", "setwalletflag", &setwalletflag, {"flag","value"} },
42354306
{ "wallet", "signmessage", &signmessage, {"address","message"} },
42364307
{ "wallet", "signrawtransactionwithwallet", &signrawtransactionwithwallet, {"hexstring","prevtxs","sighashtype"} },
42374308
{ "wallet", "unloadwallet", &unloadwallet, {"wallet_name"} },

src/wallet/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ static constexpr uint64_t KNOWN_WALLET_FLAGS =
149149
| WALLET_FLAG_KEY_ORIGIN_METADATA
150150
| WALLET_FLAG_DISABLE_PRIVATE_KEYS;
151151

152+
static constexpr uint64_t MUTABLE_WALLET_FLAGS =
153+
WALLET_FLAG_AVOID_REUSE;
154+
152155
static const std::map<std::string,WalletFlags> WALLET_FLAG_MAP{
153156
{"avoid_reuse", WALLET_FLAG_AVOID_REUSE},
154157
{"blank", WALLET_FLAG_BLANK_WALLET},

0 commit comments

Comments
 (0)