Skip to content

Commit 7f11fba

Browse files
committed
cli: add -stdinwalletpassphrase for (slightly more) secure CLI
1 parent 0da503e commit 7f11fba

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/bitcoin-cli.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <support/events.h>
2828

2929
#include <univalue.h>
30+
#include <compat/stdin.h>
3031

3132
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
3233

@@ -58,7 +59,8 @@ static void SetupCliArgs()
5859
gArgs.AddArg("-rpcwait", "Wait for RPC server to start", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
5960
gArgs.AddArg("-rpcwallet=<walletname>", "Send RPC for non-default wallet on RPC server (needs to exactly match corresponding -wallet option passed to bitcoind). This changes the RPC endpoint used, e.g. http://127.0.0.1:8332/wallet/<walletname>", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
6061
gArgs.AddArg("-stdin", "Read extra arguments from standard input, one per line until EOF/Ctrl-D (recommended for sensitive information such as passphrases). When combined with -stdinrpcpass, the first line from standard input is used for the RPC password.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
61-
gArgs.AddArg("-stdinrpcpass", "Read RPC password from standard input as a single line. When combined with -stdin, the first line from standard input is used for the RPC password.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
62+
gArgs.AddArg("-stdinrpcpass", "Read RPC password from standard input as a single line. When combined with -stdin, the first line from standard input is used for the RPC password. When combined with -stdinwalletpassphrase, -stdinrpcpass consumes the first line, and -stdinwalletpassphrase consumes the second.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
63+
gArgs.AddArg("-stdinwalletpassphrase", "Read wallet passphrase from standard input as a single line. When combined with -stdin, the first line from standard input is used for the wallet passphrase.", ArgsManager::ALLOW_ANY, OptionsCategory::OPTIONS);
6264
}
6365

6466
/** libevent event log callback */
@@ -411,12 +413,32 @@ static int CommandLineRPC(int argc, char *argv[])
411413
}
412414
std::string rpcPass;
413415
if (gArgs.GetBoolArg("-stdinrpcpass", false)) {
416+
NO_STDIN_ECHO();
417+
if (!StdinReady()) {
418+
fputs("RPC password> ", stderr);
419+
fflush(stderr);
420+
}
414421
if (!std::getline(std::cin, rpcPass)) {
415422
throw std::runtime_error("-stdinrpcpass specified but failed to read from standard input");
416423
}
417424
gArgs.ForceSetArg("-rpcpassword", rpcPass);
418425
}
419426
std::vector<std::string> args = std::vector<std::string>(&argv[1], &argv[argc]);
427+
if (gArgs.GetBoolArg("-stdinwalletpassphrase", false)) {
428+
NO_STDIN_ECHO();
429+
std::string walletPass;
430+
if (args.size() < 1 || args[0].substr(0, 16) != "walletpassphrase") {
431+
throw std::runtime_error("-stdinwalletpassphrase is only applicable for walletpassphrase(change)");
432+
}
433+
if (!StdinReady()) {
434+
fputs("Wallet passphrase> ", stderr);
435+
fflush(stderr);
436+
}
437+
if (!std::getline(std::cin, walletPass)) {
438+
throw std::runtime_error("-stdinwalletpassphrase specified but failed to read from standard input");
439+
}
440+
args.insert(args.begin() + 1, walletPass);
441+
}
420442
if (gArgs.GetBoolArg("-stdin", false)) {
421443
// Read one arg per line from stdin and append
422444
std::string line;

0 commit comments

Comments
 (0)