17 "Stores the wallet decryption key in memory for 'timeout' seconds.\n"
18 "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
20 "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
21 "time that overrides the old one.\n",
28 "\nUnlock the wallet for 60 seconds\n"
30 "\nLock the wallet again (before 60 seconds)\n"
32 "\nAs a JSON-RPC call\n"
54 strWalletPass.reserve(100);
55 strWalletPass = std::string_view{request.params[0].get_str()};
58 nSleepTime = request.params[1].getInt<int64_t>();
64 constexpr int64_t MAX_SLEEP_TIME = 100000000;
65 if (nSleepTime > MAX_SLEEP_TIME) {
66 nSleepTime = MAX_SLEEP_TIME;
69 if (strWalletPass.empty()) {
73 if (!pwallet->
Unlock(strWalletPass)) {
75 if (strWalletPass.find(
'\0') == std::string::npos) {
79 "It contains a null character (ie - a zero byte). "
80 "If the passphrase was set with a version of this software prior to 25.0, "
81 "please try again with only the characters up to — but not including — "
82 "the first null character. If this is successful, please set a new "
83 "passphrase to avoid this issue in the future.");
89 pwallet->nRelockTime =
GetTime() + nSleepTime;
90 relock_time = pwallet->nRelockTime;
101 std::weak_ptr<CWallet> weak_wallet =
wallet;
103 if (
auto shared_wallet = weak_wallet.lock()) {
104 LOCK2(shared_wallet->m_relock_mutex, shared_wallet->cs_wallet);
106 if (shared_wallet->nRelockTime != relock_time) return;
107 shared_wallet->Lock();
108 shared_wallet->nRelockTime = 0;
110 }, std::chrono::seconds(nSleepTime));
121 "walletpassphrasechange",
122 "Changes the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n",
129 HelpExampleCli(
"walletpassphrasechange",
"\"old one\" \"new one\"")
130 +
HelpExampleRpc(
"walletpassphrasechange",
"\"old one\", \"new one\"")
137 if (!pwallet->IsCrypted()) {
141 if (pwallet->IsScanningWithPassphrase()) {
142 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.");
145 LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
148 strOldWalletPass.reserve(100);
149 strOldWalletPass = std::string_view{request.params[0].get_str()};
152 strNewWalletPass.reserve(100);
153 strNewWalletPass = std::string_view{request.params[1].get_str()};
155 if (strOldWalletPass.empty() || strNewWalletPass.empty()) {
159 if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
161 if (strOldWalletPass.find(
'\0') == std::string::npos) {
165 "It contains a null character (ie - a zero byte). "
166 "If the old passphrase was set with a version of this software prior to 25.0, "
167 "please try again with only the characters up to — but not including — "
168 "the first null character.");
182 "Removes the wallet encryption key from memory, locking the wallet.\n"
183 "After calling this method, you will need to call walletpassphrase again\n"
184 "before being able to call any methods which require the wallet to be unlocked.\n",
188 "\nSet the passphrase for 2 minutes to perform a transaction\n"
190 "\nPerform a send (requires passphrase set)\n"
192 "\nClear the passphrase since we are done before 2 minutes is up\n"
194 "\nAs a JSON-RPC call\n"
202 if (!pwallet->IsCrypted()) {
206 if (pwallet->IsScanningWithPassphrase()) {
207 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.");
210 LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
213 pwallet->nRelockTime = 0;
225 "Encrypts the wallet with 'passphrase'. This is for first time encryption.\n"
226 "After this, any calls that interact with private keys such as sending or signing \n"
227 "will require the passphrase to be set prior to making these calls.\n"
228 "Use the walletpassphrase call for this, and then walletlock call.\n"
229 "If the wallet is already encrypted, use the walletpassphrasechange call.\n"
231 "For security reasons, the encryption process will generate a new HD seed, resulting\n"
232 "in the creation of a fresh set of active descriptors. Therefore, it is crucial to\n"
233 "securely back up the newly generated wallet file using the backupwallet RPC.\n",
239 "\nEncrypt your wallet\n"
241 "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
243 "\nNow we can do something like sign\n"
245 "\nNow lock the wallet again by removing the passphrase\n"
247 "\nAs a JSON-RPC call\n"
259 if (pwallet->IsCrypted()) {
263 if (pwallet->IsScanningWithPassphrase()) {
264 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.");
267 LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
270 strWalletPass.reserve(100);
271 strWalletPass = std::string_view{request.params[0].get_str()};
273 if (strWalletPass.empty()) {
277 if (!pwallet->EncryptWallet(strWalletPass)) {
281 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)
RecursiveMutex cs_wallet
Main wallet lock.
std::shared_ptr< CWallet > GetWalletForJSONRPCRequest(const JSONRPCRequest &request)
Figures out what wallet, if any, to use for a JSONRPCRequest.
RPCHelpMan walletpassphrase()
WalletContext & EnsureWalletContext(const std::any &context)
RPCHelpMan walletpassphrasechange()
RPCHelpMan 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.
@ RPC_WALLET_PASSPHRASE_INCORRECT
The wallet passphrase entered was incorrect.
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.