Skip to content

Commit 92bcca3

Browse files
committed
rpc: Input-from-stdin mode for bitcoin-cli
Implements bitcoin#7442 by adding an option `-stdin` which reads additional arguments from stdin, one per line. For example ```bash echo -e "mysecretcode\n120" | src/bitcoin-cli -stdin walletpassphrase echo -e "walletpassphrase\nmysecretcode\n120" | src/bitcoin-cli -stdin ```
1 parent 8b70a64 commit 92bcca3

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/bitcoin-cli.cpp

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ std::string HelpMessageCli()
4343
strUsage += HelpMessageOpt("-rpcuser=<user>", _("Username for JSON-RPC connections"));
4444
strUsage += HelpMessageOpt("-rpcpassword=<pw>", _("Password for JSON-RPC connections"));
4545
strUsage += HelpMessageOpt("-rpcclienttimeout=<n>", strprintf(_("Timeout during HTTP requests (default: %d)"), DEFAULT_HTTP_CLIENT_TIMEOUT));
46+
strUsage += HelpMessageOpt("-stdin", _("Read extra arguments from standard input, one per line until EOF/Ctrl-D (recommended for sensitive information such as passphrases)"));
4647

4748
return strUsage;
4849
}
@@ -232,15 +233,17 @@ int CommandLineRPC(int argc, char *argv[])
232233
argc--;
233234
argv++;
234235
}
235-
236-
// Method
237-
if (argc < 2)
238-
throw runtime_error("too few parameters");
239-
string strMethod = argv[1];
240-
241-
// Parameters default to strings
242-
std::vector<std::string> strParams(&argv[2], &argv[argc]);
243-
UniValue params = RPCConvertValues(strMethod, strParams);
236+
std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]);
237+
if (GetBoolArg("-stdin", false)) {
238+
// Read one arg per line from stdin and append
239+
std::string line;
240+
while (std::getline(std::cin,line))
241+
args.push_back(line);
242+
}
243+
if (args.size() < 1)
244+
throw runtime_error("too few parameters (need at least command)");
245+
std::string strMethod = args[0];
246+
UniValue params = RPCConvertValues(strMethod, std::vector<std::string>(args.begin()+1, args.end()));
244247

245248
// Execute and handle connection failures with -rpcwait
246249
const bool fWait = GetBoolArg("-rpcwait", false);

0 commit comments

Comments
 (0)