18 "Stores the wallet decryption key in memory for 'timeout' seconds.\n"
19 "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
21 "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
22 "time that overrides the old one.\n",
29 "\nUnlock the wallet for 60 seconds\n"
31 "\nLock the wallet again (before 60 seconds)\n"
33 "\nAs a JSON-RPC call\n"
55 strWalletPass.reserve(100);
56 strWalletPass = std::string_view{request.params[0].get_str()};
59 nSleepTime = request.params[1].getInt<int64_t>();
65 constexpr int64_t MAX_SLEEP_TIME = 100000000;
66 if (nSleepTime > MAX_SLEEP_TIME) {
67 nSleepTime = MAX_SLEEP_TIME;
70 if (strWalletPass.empty()) {
74 if (
auto unlocked{pwallet->
Unlock(strWalletPass)}; !unlocked) {
80 pwallet->nRelockTime =
GetTime() + nSleepTime;
81 relock_time = pwallet->nRelockTime;
92 std::weak_ptr<CWallet> weak_wallet =
wallet;
94 if (
auto shared_wallet = weak_wallet.lock()) {
95 LOCK2(shared_wallet->m_relock_mutex, shared_wallet->cs_wallet);
97 if (shared_wallet->nRelockTime != relock_time) return;
98 shared_wallet->Lock();
99 shared_wallet->nRelockTime = 0;
101 }, std::chrono::seconds(nSleepTime));
112 "walletpassphrasechange",
113 "Changes the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n",
120 HelpExampleCli(
"walletpassphrasechange",
"\"old one\" \"new one\"")
121 +
HelpExampleRpc(
"walletpassphrasechange",
"\"old one\", \"new one\"")
128 if (!pwallet->HasEncryptionKeys()) {
132 if (pwallet->Scanner().IsScanningWithPassphrase()) {
133 throw JSONRPCError(
RPC_WALLET_ERROR,
"Error: the wallet is currently being used to rescan the blockchain for related transactions. Please call `abortrescan` before changing the passphrase.");
136 LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
139 strOldWalletPass.reserve(100);
140 strOldWalletPass = std::string_view{request.params[0].get_str()};
143 strNewWalletPass.reserve(100);
144 strNewWalletPass = std::string_view{request.params[1].get_str()};
146 if (strOldWalletPass.empty() || strNewWalletPass.empty()) {
150 if (
auto changed{pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)}; !changed) {
164 "Removes the wallet encryption key from memory, locking the wallet.\n"
165 "After calling this method, you will need to call walletpassphrase again\n"
166 "before being able to call any methods which require the wallet to be unlocked.\n",
170 "\nSet the passphrase for 2 minutes to perform a transaction\n"
172 "\nPerform a send (requires passphrase set)\n"
174 "\nClear the passphrase since we are done before 2 minutes is up\n"
176 "\nAs a JSON-RPC call\n"
184 if (!pwallet->HasEncryptionKeys()) {
188 if (pwallet->Scanner().IsScanningWithPassphrase()) {
189 throw JSONRPCError(
RPC_WALLET_ERROR,
"Error: the wallet is currently being used to rescan the blockchain for related transactions. Please call `abortrescan` before locking the wallet.");
192 LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
195 pwallet->nRelockTime = 0;
207 "Encrypts the wallet with 'passphrase'. This is for first time encryption.\n"
208 "After this, any calls that interact with private keys such as sending or signing \n"
209 "will require the passphrase to be set prior to making these calls.\n"
210 "Use the walletpassphrase call for this, and then walletlock call.\n"
211 "If the wallet is already encrypted, use the walletpassphrasechange call.\n"
213 "For security reasons, the encryption process will generate a new HD seed, resulting\n"
214 "in the creation of a fresh set of active descriptors. Therefore, it is crucial to\n"
215 "securely back up the newly generated wallet file using the backupwallet RPC.\n",
221 "\nEncrypt your wallet\n"
223 "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
225 "\nNow we can do something like sign\n"
227 "\nNow lock the wallet again by removing the passphrase\n"
229 "\nAs a JSON-RPC call\n"
241 if (pwallet->HasEncryptionKeys()) {
245 if (pwallet->Scanner().IsScanningWithPassphrase()) {
246 throw JSONRPCError(
RPC_WALLET_ERROR,
"Error: the wallet is currently being used to rescan the blockchain for related transactions. Please call `abortrescan` before encrypting the wallet.");
249 LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
252 strWalletPass.reserve(100);
253 strWalletPass = std::string_view{request.params[0].get_str()};
255 if (strWalletPass.empty()) {
259 if (!pwallet->EncryptWallet(strWalletPass)) {
263 return "wallet encrypted; The keypool has been flushed and a new HD seed was generated. You need to make a new backup with the backupwallet RPC.";
void scheduleFromNow(Function f, std::chrono::milliseconds delta) EXCLUSIVE_LOCKS_REQUIRED(!newTaskMutex)
Call f once after the delta has passed.
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
bool TopUpKeyPool(unsigned int kpSize=0)
bool Unlock(const CKeyingMaterial &vMasterKeyIn)
bool HasEncryptionKeys() const override
RecursiveMutex cs_wallet
Main wallet lock.
RPCMethod walletpassphrase()
std::shared_ptr< CWallet > GetWalletForJSONRPCRequest(const JSONRPCRequest &request)
Figures out what wallet, if any, to use for a JSONRPCRequest.
RPCMethod walletpassphrasechange()
RPCErrorCode HandleWalletErrorCode(const WalletErrorCode code)
WalletContext & EnsureWalletContext(const std::any &context)
RPCMethod encryptwallet()
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
UniValue JSONRPCError(int code, const std::string &message)
@ RPC_WALLET_WRONG_ENC_STATE
Command given in wrong wallet encryption state (encrypting an encrypted wallet etc....
@ RPC_WALLET_ENCRYPTION_FAILED
Failed to encrypt the wallet.
@ RPC_INVALID_PARAMETER
Invalid, missing or duplicate parameter.
@ RPC_WALLET_ERROR
Wallet errors.
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
const std::string EXAMPLE_ADDRESS[2]
Example bech32 addresses for the RPCExamples help documentation.
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
WalletContext struct containing references to state shared between CWallet instances,...
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.
std::shared_ptr< CWallet > wallet