Skip to content

Commit

Permalink
wallet: allowing walletpassphrase timeout to be empty
Browse files Browse the repository at this point in the history
In this change we allow the timeout for walletpassphrase to be
optional,
if left empty then we then use the MAX_SLEEP_TIME amount

context from bitcoin#28403 (comment)
  • Loading branch information
kevkevinpal committed Sep 11, 2023
1 parent 8f7b9eb commit db16f18
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/wallet/rpc/encrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ RPCHelpMan walletpassphrase()
"time that overrides the old one.\n",
{
{"passphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet passphrase"},
{"timeout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The time to keep the decryption key in seconds; capped at 100000000 (~3 years)."},
{"timeout", RPCArg::Type::NUM, RPCArg::Optional::OMITTED, "The time to keep the decryption key in seconds; capped at 100000000 (~3 years), will use cap if left empty"},
},
RPCResult{RPCResult::Type::NONE, "", ""},
RPCExamples{
Expand All @@ -35,7 +35,8 @@ RPCHelpMan walletpassphrase()
if (!wallet) return UniValue::VNULL;
CWallet* const pwallet = wallet.get();

int64_t nSleepTime;
constexpr int64_t MAX_SLEEP_TIME = 100000000; // larger values trigger a macos/libevent bug?
int64_t nSleepTime = MAX_SLEEP_TIME;
int64_t relock_time;
// Prevent concurrent calls to walletpassphrase with the same wallet.
LOCK(pwallet->m_unlock_mutex);
Expand All @@ -51,16 +52,17 @@ RPCHelpMan walletpassphrase()
strWalletPass.reserve(100);
strWalletPass = std::string_view{request.params[0].get_str()};

// Get the timeout
nSleepTime = request.params[1].getInt<int64_t>();
// Timeout cannot be negative, otherwise it will relock immediately
if (nSleepTime < 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Timeout cannot be negative.");
}
// Clamp timeout
constexpr int64_t MAX_SLEEP_TIME = 100000000; // larger values trigger a macos/libevent bug?
if (nSleepTime > MAX_SLEEP_TIME) {
nSleepTime = MAX_SLEEP_TIME;
if(!request.params[1].isNull()){
// Get the timeout
nSleepTime = request.params[1].getInt<int64_t>();
// Timeout cannot be negative, otherwise it will relock immediately
if (nSleepTime < 0) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Timeout cannot be negative.");
}
// Clamp timeout
if (nSleepTime > MAX_SLEEP_TIME) {
nSleepTime = MAX_SLEEP_TIME;
}
}

if (strWalletPass.empty()) {
Expand Down

0 comments on commit db16f18

Please sign in to comment.