Skip to content

Commit 31e0720

Browse files
committed
Add wallet endpoint support to bitcoin-cli (-usewallet)
1 parent dd2185c commit 31e0720

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

src/bitcoin-cli.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ std::string HelpMessageCli()
4646
strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections"));
4747
strUsage += HelpMessageOpt("-rpcclienttimeout=<n>", strprintf(_("Timeout in seconds during HTTP requests, or 0 for no timeout. (default: %d)"), DEFAULT_HTTP_CLIENT_TIMEOUT));
4848
strUsage += HelpMessageOpt("-stdin", _("Read extra arguments from standard input, one per line until EOF/Ctrl-D (recommended for sensitive information such as passphrases)"));
49+
strUsage += HelpMessageOpt("-usewallet=<walletname>", _("Send RPC for non-default wallet on RPC server (argument is wallet filename in bitcoind directory, required if bitcoind/-Qt runs with multiple wallets)"));
4950

5051
return strUsage;
5152
}
@@ -241,7 +242,20 @@ UniValue CallRPC(const std::string& strMethod, const UniValue& params)
241242
assert(output_buffer);
242243
evbuffer_add(output_buffer, strRequest.data(), strRequest.size());
243244

244-
int r = evhttp_make_request(evcon.get(), req.get(), EVHTTP_REQ_POST, "/");
245+
// check if we should use a special wallet endpoint
246+
std::string endpoint = "/";
247+
std::string walletName = GetArg("-usewallet", "");
248+
if (!walletName.empty()) {
249+
char *encodedURI = evhttp_uriencode(walletName.c_str(), walletName.size(), false);
250+
if (encodedURI) {
251+
endpoint = "/wallet/"+ std::string(encodedURI);
252+
free(encodedURI);
253+
}
254+
else {
255+
throw CConnectionFailed("uri-encode failed");
256+
}
257+
}
258+
int r = evhttp_make_request(evcon.get(), req.get(), EVHTTP_REQ_POST, endpoint.c_str());
245259
req.release(); // ownership moved to evcon in above call
246260
if (r != 0) {
247261
throw CConnectionFailed("send http request failed");

src/httpserver.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,3 +666,14 @@ void UnregisterHTTPHandler(const std::string &prefix, bool exactMatch)
666666
}
667667
}
668668

669+
std::string urlDecode(const std::string &urlEncoded) {
670+
std::string res;
671+
if (!urlEncoded.empty()) {
672+
char *decoded = evhttp_uridecode(urlEncoded.c_str(), false, NULL);
673+
if (decoded) {
674+
res = std::string(decoded);
675+
free(decoded);
676+
}
677+
}
678+
return res;
679+
}

src/httpserver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,6 @@ class HTTPEvent
148148
struct event* ev;
149149
};
150150

151+
std::string urlDecode(const std::string &urlEncoded);
152+
151153
#endif // BITCOIN_HTTPSERVER_H

0 commit comments

Comments
 (0)