|
9 | 9 |
|
10 | 10 | #include <chainparamsbase.h>
|
11 | 11 | #include <clientversion.h>
|
| 12 | +#include <optional.h> |
12 | 13 | #include <rpc/client.h>
|
13 | 14 | #include <rpc/protocol.h>
|
14 | 15 | #include <rpc/request.h>
|
@@ -304,7 +305,7 @@ class DefaultRequestHandler: public BaseRequestHandler {
|
304 | 305 | }
|
305 | 306 | };
|
306 | 307 |
|
307 |
| -static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, const std::vector<std::string>& args) |
| 308 | +static UniValue CallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const Optional<std::string>& rpcwallet = {}) |
308 | 309 | {
|
309 | 310 | std::string host;
|
310 | 311 | // In preference order, we choose the following for the port:
|
@@ -369,14 +370,12 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
|
369 | 370 |
|
370 | 371 | // check if we should use a special wallet endpoint
|
371 | 372 | std::string endpoint = "/";
|
372 |
| - if (!gArgs.GetArgs("-rpcwallet").empty()) { |
373 |
| - std::string walletName = gArgs.GetArg("-rpcwallet", ""); |
374 |
| - char *encodedURI = evhttp_uriencode(walletName.data(), walletName.size(), false); |
| 373 | + if (rpcwallet) { |
| 374 | + char* encodedURI = evhttp_uriencode(rpcwallet->data(), rpcwallet->size(), false); |
375 | 375 | if (encodedURI) {
|
376 |
| - endpoint = "/wallet/"+ std::string(encodedURI); |
| 376 | + endpoint = "/wallet/" + std::string(encodedURI); |
377 | 377 | free(encodedURI);
|
378 |
| - } |
379 |
| - else { |
| 378 | + } else { |
380 | 379 | throw CConnectionFailed("uri-encode failed");
|
381 | 380 | }
|
382 | 381 | }
|
@@ -423,17 +422,18 @@ static UniValue CallRPC(BaseRequestHandler *rh, const std::string& strMethod, co
|
423 | 422 | *
|
424 | 423 | * @param[in] rh Pointer to RequestHandler.
|
425 | 424 | * @param[in] strMethod Reference to const string method to forward to CallRPC.
|
| 425 | + * @param[in] rpcwallet Reference to const optional string wallet name to forward to CallRPC. |
426 | 426 | * @returns the RPC response as a UniValue object.
|
427 | 427 | * @throws a CConnectionFailed std::runtime_error if connection failed or RPC server still in warmup.
|
428 | 428 | */
|
429 |
| -static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args) |
| 429 | +static UniValue ConnectAndCallRPC(BaseRequestHandler* rh, const std::string& strMethod, const std::vector<std::string>& args, const Optional<std::string>& rpcwallet = {}) |
430 | 430 | {
|
431 | 431 | UniValue response(UniValue::VOBJ);
|
432 | 432 | // Execute and handle connection failures with -rpcwait.
|
433 | 433 | const bool fWait = gArgs.GetBoolArg("-rpcwait", false);
|
434 | 434 | do {
|
435 | 435 | try {
|
436 |
| - response = CallRPC(rh, strMethod, args); |
| 436 | + response = CallRPC(rh, strMethod, args, rpcwallet); |
437 | 437 | if (fWait) {
|
438 | 438 | const UniValue& error = find_value(response, "error");
|
439 | 439 | if (!error.isNull() && error["code"].get_int() == RPC_IN_WARMUP) {
|
@@ -519,7 +519,9 @@ static int CommandLineRPC(int argc, char *argv[])
|
519 | 519 | method = args[0];
|
520 | 520 | args.erase(args.begin()); // Remove trailing method name from arguments vector
|
521 | 521 | }
|
522 |
| - const UniValue reply = ConnectAndCallRPC(rh.get(), method, args); |
| 522 | + Optional<std::string> wallet_name{}; |
| 523 | + if (gArgs.IsArgSet("-rpcwallet")) wallet_name = gArgs.GetArg("-rpcwallet", ""); |
| 524 | + const UniValue reply = ConnectAndCallRPC(rh.get(), method, args, wallet_name); |
523 | 525 |
|
524 | 526 | // Parse reply
|
525 | 527 | UniValue result = find_value(reply, "result");
|
|
0 commit comments