Skip to content

Commit b635bc0

Browse files
rpc, util: Add EnsureUniqueWalletName
Add a new function called EnsureUniqueWalletNamet that returns the selected wallet name across the RPC request endpoint and wallet_name. Supports the case where the wallet_name argument may be omitted—either when using a wallet endpoint, or when not provided at all. In the latter case, if no wallet endpoint is used, an error is raised. Internally reuses the existing implementation to avoid redundant URL decoding and logic duplication. This is a preparatory change for upcoming refactoring of unloadwallet and migratewallet, which will adopt EnsureUniqueWalletName for improved clarity and consistency.
1 parent a8bff38 commit b635bc0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/wallet/rpc/util.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ bool GetAvoidReuseFlag(const CWallet& wallet, const UniValue& param) {
2929
return avoid_reuse;
3030
}
3131

32+
std::string EnsureUniqueWalletName(const JSONRPCRequest& request, const std::string* wallet_name)
33+
{
34+
std::string endpoint_wallet;
35+
if (GetWalletNameFromJSONRPCRequest(request, endpoint_wallet)) {
36+
// wallet endpoint was used
37+
if (wallet_name && *wallet_name != endpoint_wallet) {
38+
throw JSONRPCError(RPC_INVALID_PARAMETER,
39+
"The RPC endpoint wallet and the wallet name parameter specify different wallets");
40+
}
41+
return endpoint_wallet;
42+
}
43+
44+
// Not a wallet endpoint; parameter must be provided
45+
if (!wallet_name) {
46+
throw JSONRPCError(RPC_INVALID_PARAMETER,
47+
"Either the RPC endpoint wallet or the wallet name parameter must be provided");
48+
}
49+
50+
return *wallet_name;
51+
}
52+
3253
bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
3354
{
3455
if (request.URI.starts_with(WALLET_ENDPOINT_BASE)) {

src/wallet/rpc/util.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ static const RPCResult RESULT_LAST_PROCESSED_BLOCK { RPCResult::Type::OBJ, "last
3838
*/
3939
std::shared_ptr<CWallet> GetWalletForJSONRPCRequest(const JSONRPCRequest& request);
4040
bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name);
41+
/**
42+
* Ensures that a wallet name is specified across the endpoint and wallet_name.
43+
* Throws `RPC_INVALID_PARAMETER` if none or different wallet names are specified.
44+
*/
45+
std::string EnsureUniqueWalletName(const JSONRPCRequest& request, const std::string* wallet_name);
4146

4247
void EnsureWalletIsUnlocked(const CWallet&);
4348
WalletContext& EnsureWalletContext(const std::any& context);

0 commit comments

Comments
 (0)