refactor: Return std::optional from GetWalletNameFromJSONRPCRequest
What changed, and why it matters
This commit is a straightforward internal code cleanup. It changes one helper function so that it returns a wallet name directly (using std::optional) instead of writing it into a caller-provided variable and returning true/false. There is no change to user-visible behavior, no bug fix, and no security relevance.
No security action needed. Treat as normal code-quality refactor.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch refactors GetWalletNameFromJSONRPCRequest from an output-parameter style (bool return, std::string& out) to a std::optional
Changed components
src/wallet/rpc/util.cppsrc/wallet/rpc/util.hInspect captured patch +9 / −12
diff --git a/src/wallet/rpc/util.cpp b/src/wallet/rpc/util.cpp
index 2b980149..77a8745c 100644
--- a/src/wallet/rpc/util.cpp
+++ b/src/wallet/rpc/util.cpp
@@ -32,14 +32,13 @@ bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
std::string EnsureUniqueWalletName(const JSONRPCRequest& request, std::optional<std::string_view> wallet_name)
{
- std::string endpoint_wallet;
- if (GetWalletNameFromJSONRPCRequest(request, endpoint_wallet)) {
+ if (auto endpoint_wallet{GetWalletNameFromJSONRPCRequest(request)}) {
// wallet endpoint was used
- if (wallet_name && *wallet_name != endpoint_wallet) {
+ if (wallet_name && *wallet_name != *endpoint_wallet) {
throw JSONRPCError(RPC_INVALID_PARAMETER,
"The RPC endpoint wallet and the wallet name parameter specify different wallets");
}
- return endpoint_wallet;
+ return *endpoint_wallet;
}
// Not a wallet endpoint; parameter must be provided
@@ -51,14 +50,13 @@ std::string EnsureUniqueWalletName(const JSONRPCRequest& request, std::optional<
return std::string{*wallet_name};
}
-bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
+std::optional<std::string> GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request)
{
if (request.URI.starts_with(WALLET_ENDPOINT_BASE)) {
// wallet endpoint was used
- wallet_name = UrlDecode(std::string_view{request.URI}.substr(WALLET_ENDPOINT_BASE.size()));
- return true;
+ return UrlDecode(std::string_view{request.URI}.substr(WALLET_ENDPOINT_BASE.size()));
}
- return false;
+ return std::nullopt;
}
std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request)
@@ -66,9 +64,8 @@ std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& reques
CHECK_NONFATAL(request.mode == JSONRPCRequest::EXECUTE);
WalletContext& context = EnsureWalletContext(request.context);
- std::string wallet_name;
- if (GetWalletNameFromJSONRPCRequest(request, wallet_name)) {
- std::shared_ptr<CWallet> pwallet = GetWallet(context, wallet_name);
+ if (auto wallet_name{GetWalletNameFromJSONRPCRequest(request)}) {
+ std::shared_ptr<CWallet> pwallet{GetWallet(context, *wallet_name)};
if (!pwallet) throw JSONRPCError(RPC_WALLET_NOT_FOUND, "Requested wallet does not exist or is not loaded");
return pwallet;
}
diff --git a/src/wallet/rpc/util.h b/src/wallet/rpc/util.h
index 89729218..88fdc663 100644
--- a/src/wallet/rpc/util.h
+++ b/src/wallet/rpc/util.h
@@ -39,7 +39,7 @@ static const RPCResult RESULT_LAST_PROCESSED_BLOCK { RPCResult::Type::OBJ, "last
* @return nullptr if no wallet should be used, or a pointer to the CWallet
*/
std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request);
-bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name);
+std::optional<std::string> GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request);
/**
* Ensures that a wallet name is specified across the endpoint and wallet_name.
* Throws `RPC_INVALID_PARAMETER` if none or different wallet names are specified.
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.