Skip to content

Commit ea50a11

Browse files
pablomartin4btcluke-jr
authored andcommitted
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. Github-Pull: bitcoin#32845 Rebased-From: b635bc0
1 parent 8cb6ab0 commit ea50a11

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
@@ -44,6 +44,27 @@ bool ParseIncludeWatchonly(const UniValue& include_watchonly, const CWallet& wal
4444
return include_watchonly.get_bool();
4545
}
4646

47+
std::string EnsureUniqueWalletName(const JSONRPCRequest& request, const std::string* wallet_name)
48+
{
49+
std::string endpoint_wallet;
50+
if (GetWalletNameFromJSONRPCRequest(request, endpoint_wallet)) {
51+
// wallet endpoint was used
52+
if (wallet_name && *wallet_name != endpoint_wallet) {
53+
throw JSONRPCError(RPC_INVALID_PARAMETER,
54+
"The RPC endpoint wallet and the wallet name parameter specify different wallets");
55+
}
56+
return endpoint_wallet;
57+
}
58+
59+
// Not a wallet endpoint; parameter must be provided
60+
if (!wallet_name) {
61+
throw JSONRPCError(RPC_INVALID_PARAMETER,
62+
"Either the RPC endpoint wallet or the wallet name parameter must be provided");
63+
}
64+
65+
return *wallet_name;
66+
}
67+
4768
bool GetWalletNameFromJSONRPCRequest(const JSONRPCRequest& request, std::string& wallet_name)
4869
{
4970
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)