diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -2404,20 +2404,15 @@ nSleepTime = MAX_SLEEP_TIME; } - if (strWalletPass.length() > 0) { - if (!pwallet->Unlock(strWalletPass)) { - throw JSONRPCError( - RPC_WALLET_PASSPHRASE_INCORRECT, - "Error: The wallet passphrase entered was incorrect."); - } - } else { - throw std::runtime_error(RPCHelpMan{ - "walletpassphrase", - "Stores the wallet decryption key in memory for seconds.", - { - {"passphrase", RPCArg::Type::STR, false}, - {"timeout", RPCArg::Type::NUM, false}, - }}.ToString()); + if (strWalletPass.empty()) { + throw JSONRPCError(RPC_INVALID_PARAMETER, + "passphrase can not be empty"); + } + + if (!pwallet->Unlock(strWalletPass)) { + throw JSONRPCError( + RPC_WALLET_PASSPHRASE_INCORRECT, + "Error: The wallet passphrase entered was incorrect."); } pwallet->TopUpKeyPool(); @@ -2482,15 +2477,9 @@ strNewWalletPass.reserve(100); strNewWalletPass = request.params[1].get_str().c_str(); - if (strOldWalletPass.length() < 1 || strNewWalletPass.length() < 1) { - throw std::runtime_error(RPCHelpMan{ - "walletpassphrasechange", - "Changes the wallet passphrase from to " - ".", - { - {"oldpassphrase", RPCArg::Type::STR, false}, - {"newpassphrase", RPCArg::Type::STR, false}, - }}.ToString()); + if (strOldWalletPass.empty() || strNewWalletPass.empty()) { + throw JSONRPCError(RPC_INVALID_PARAMETER, + "passphrase can not be empty"); } if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) { @@ -2616,13 +2605,9 @@ strWalletPass.reserve(100); strWalletPass = request.params[0].get_str().c_str(); - if (strWalletPass.length() < 1) { - throw std::runtime_error(RPCHelpMan{ - "encryptwallet", - "Encrypts the wallet with .", - { - {"passphrase", RPCArg::Type::STR, false}, - }}.ToString()); + if (strWalletPass.empty()) { + throw JSONRPCError(RPC_INVALID_PARAMETER, + "passphrase can not be empty"); } if (!pwallet->EncryptWallet(strWalletPass)) { diff --git a/test/functional/wallet_encryption.py b/test/functional/wallet_encryption.py --- a/test/functional/wallet_encryption.py +++ b/test/functional/wallet_encryption.py @@ -33,8 +33,18 @@ privkey = self.nodes[0].dumpprivkey(address) assert_equal(privkey[:1], "c") assert_equal(len(privkey), 52) + assert_raises_rpc_error( + -15, "Error: running with an unencrypted wallet, but walletpassphrase was called", + self.nodes[0].walletpassphrase, 'ff', 1) + assert_raises_rpc_error( + -15, + "Error: running with an unencrypted wallet, but walletpassphrasechange was called.", + self.nodes[0].walletpassphrasechange, 'ff', 'ff') # Encrypt the wallet + assert_raises_rpc_error( + -8, "passphrase can not be empty", + self.nodes[0].encryptwallet, '') self.nodes[0].encryptwallet(passphrase) # Check the encrypted wallet is marked as locked on initialization @@ -44,6 +54,15 @@ assert_raises_rpc_error( -13, "Please enter the wallet passphrase with walletpassphrase first", self.nodes[0].dumpprivkey, address) + assert_raises_rpc_error( + -15, "Error: running with an encrypted wallet, but encryptwallet was called.", + self.nodes[0].encryptwallet, 'ff') + assert_raises_rpc_error( + -8, "passphrase can not be empty", + self.nodes[0].walletpassphrase, '', 1) + assert_raises_rpc_error( + -8, "passphrase can not be empty", + self.nodes[0].walletpassphrasechange, '', 'ff') # Check that walletpassphrase works self.nodes[0].walletpassphrase(passphrase, 2)