Skip to content

Commit

Permalink
rpc: add "warnings" field to RPCs {create,load,unload,restore}wallet
Browse files Browse the repository at this point in the history
This new "warnings" field is a JSON array of strings intended to replace the
"warning" string field in these four RPCs, to better handle returning multiple
warning messages and for consistency with other wallet RPCs.
  • Loading branch information
jonatack committed Apr 10, 2023
1 parent 079d8cd commit 4a1e479
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/rpc/util.cpp
Expand Up @@ -1175,8 +1175,25 @@ UniValue GetServicesNames(ServiceFlags services)
return servicesNames;
}

/** Convert a vector of bilingual strings to a UniValue::VARR containing their original untranslated values. */
[[nodiscard]] static UniValue BilingualStringsToUniValue(const std::vector<bilingual_str>& bilingual_strings)
{
CHECK_NONFATAL(!bilingual_strings.empty());
UniValue result{UniValue::VARR};
for (const auto& s : bilingual_strings) {
result.push_back(s.original);
}
return result;
}

void PushWarnings(const UniValue& warnings, UniValue& obj)
{
if (warnings.empty()) return;
obj.pushKV("warnings", warnings);
}

void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj)
{
if (warnings.empty()) return;
obj.pushKV("warnings", BilingualStringsToUniValue(warnings));
}
1 change: 1 addition & 0 deletions src/rpc/util.h
Expand Up @@ -388,5 +388,6 @@ class RPCHelpMan
* @param[out] obj UniValue object to push the warnings array object to.
*/
void PushWarnings(const UniValue& warnings, UniValue& obj);
void PushWarnings(const std::vector<bilingual_str>& warnings, UniValue& obj);

#endif // BITCOIN_RPC_UTIL_H
5 changes: 5 additions & 0 deletions src/wallet/rpc/backup.cpp
Expand Up @@ -1904,6 +1904,10 @@ RPCHelpMan restorewallet()
{
{RPCResult::Type::STR, "name", "The wallet name if restored successfully."},
{RPCResult::Type::STR, "warning", "Warning messages, if any, related to restoring the wallet. Multiple messages will be delimited by newlines."},
{RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to restoring the wallet.",
{
{RPCResult::Type::STR, "", ""},
}},
}
},
RPCExamples{
Expand Down Expand Up @@ -1934,6 +1938,7 @@ RPCHelpMan restorewallet()
UniValue obj(UniValue::VOBJ);
obj.pushKV("name", wallet->GetName());
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original);
PushWarnings(warnings, obj);

return obj;

Expand Down
16 changes: 16 additions & 0 deletions src/wallet/rpc/wallet.cpp
Expand Up @@ -208,6 +208,10 @@ static RPCHelpMan loadwallet()
{
{RPCResult::Type::STR, "name", "The wallet name if loaded successfully."},
{RPCResult::Type::STR, "warning", "Warning messages, if any, related to loading the wallet. Multiple messages will be delimited by newlines."},
{RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to loading the wallet.",
{
{RPCResult::Type::STR, "", ""},
}},
}
},
RPCExamples{
Expand Down Expand Up @@ -241,6 +245,7 @@ static RPCHelpMan loadwallet()
UniValue obj(UniValue::VOBJ);
obj.pushKV("name", wallet->GetName());
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original);
PushWarnings(warnings, obj);

return obj;
},
Expand Down Expand Up @@ -336,6 +341,10 @@ static RPCHelpMan createwallet()
{
{RPCResult::Type::STR, "name", "The wallet name if created successfully. If the wallet was created using a full path, the wallet_name will be the full path."},
{RPCResult::Type::STR, "warning", "Warning messages, if any, related to creating the wallet. Multiple messages will be delimited by newlines."},
{RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to creating the wallet.",
{
{RPCResult::Type::STR, "", ""},
}},
}
},
RPCExamples{
Expand Down Expand Up @@ -406,6 +415,7 @@ static RPCHelpMan createwallet()
UniValue obj(UniValue::VOBJ);
obj.pushKV("name", wallet->GetName());
obj.pushKV("warning", Join(warnings, Untranslated("\n")).original);
PushWarnings(warnings, obj);

return obj;
},
Expand All @@ -423,6 +433,10 @@ static RPCHelpMan unloadwallet()
},
RPCResult{RPCResult::Type::OBJ, "", "", {
{RPCResult::Type::STR, "warning", "Warning messages, if any, related to unloading the wallet. Multiple messages will be delimited by newlines."},
{RPCResult::Type::ARR, "warnings", /*optional=*/true, "Warning messages, if any, related to unloading the wallet.",
{
{RPCResult::Type::STR, "", ""},
}},
}},
RPCExamples{
HelpExampleCli("unloadwallet", "wallet_name")
Expand Down Expand Up @@ -465,6 +479,8 @@ static RPCHelpMan unloadwallet()

UniValue result(UniValue::VOBJ);
result.pushKV("warning", Join(warnings, Untranslated("\n")).original);
PushWarnings(warnings, result);

return result;
},
};
Expand Down

0 comments on commit 4a1e479

Please sign in to comment.