15 "Stores the wallet decryption key in memory for 'timeout' seconds.\n"
16 "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
18 "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
19 "time that overrides the old one.\n",
26 "\nUnlock the wallet for 60 seconds\n"
28 "\nLock the wallet again (before 60 seconds)\n"
30 "\nAs a JSON-RPC call\n"
52 strWalletPass.reserve(100);
53 strWalletPass = std::string_view{request.params[0].get_str()};
56 nSleepTime = request.params[1].getInt<int64_t>();
62 constexpr int64_t MAX_SLEEP_TIME = 100000000;
63 if (nSleepTime > MAX_SLEEP_TIME) {
64 nSleepTime = MAX_SLEEP_TIME;
67 if (strWalletPass.empty()) {
71 if (!pwallet->
Unlock(strWalletPass)) {
73 if (strWalletPass.find(
'\0') == std::string::npos) {
77 "It contains a null character (ie - a zero byte). "
78 "If the passphrase was set with a version of this software prior to 25.0, "
79 "please try again with only the characters up to — but not including — "
80 "the first null character. If this is successful, please set a new "
81 "passphrase to avoid this issue in the future.");
87 pwallet->nRelockTime =
GetTime() + nSleepTime;
88 relock_time = pwallet->nRelockTime;
99 std::weak_ptr<CWallet> weak_wallet =
wallet;
101 if (auto shared_wallet = weak_wallet.lock()) {
102 LOCK2(shared_wallet->m_relock_mutex, shared_wallet->cs_wallet);
104 if (shared_wallet->nRelockTime != relock_time) return;
105 shared_wallet->Lock();
106 shared_wallet->nRelockTime = 0;
119 "walletpassphrasechange",
120 "Changes the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n",
127 HelpExampleCli(
"walletpassphrasechange",
"\"old one\" \"new one\"")
128 +
HelpExampleRpc(
"walletpassphrasechange",
"\"old one\", \"new one\"")
135 if (!pwallet->IsCrypted()) {
139 if (pwallet->IsScanningWithPassphrase()) {
140 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.");
143 LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
146 strOldWalletPass.reserve(100);
147 strOldWalletPass = std::string_view{request.params[0].get_str()};
150 strNewWalletPass.reserve(100);
151 strNewWalletPass = std::string_view{request.params[1].get_str()};
153 if (strOldWalletPass.empty() || strNewWalletPass.empty()) {
157 if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
159 if (strOldWalletPass.find(
'\0') == std::string::npos) {
163 "It contains a null character (ie - a zero byte). "
164 "If the old passphrase was set with a version of this software prior to 25.0, "
165 "please try again with only the characters up to — but not including — "
166 "the first null character.");
180 "Removes the wallet encryption key from memory, locking the wallet.\n"
181 "After calling this method, you will need to call walletpassphrase again\n"
182 "before being able to call any methods which require the wallet to be unlocked.\n",
186 "\nSet the passphrase for 2 minutes to perform a transaction\n"
188 "\nPerform a send (requires passphrase set)\n"
190 "\nClear the passphrase since we are done before 2 minutes is up\n"
192 "\nAs a JSON-RPC call\n"
200 if (!pwallet->IsCrypted()) {
204 if (pwallet->IsScanningWithPassphrase()) {
205 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.");
208 LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
211 pwallet->nRelockTime = 0;
223 "Encrypts the wallet with 'passphrase'. This is for first time encryption.\n"
224 "After this, any calls that interact with private keys such as sending or signing \n"
225 "will require the passphrase to be set prior to making these calls.\n"
226 "Use the walletpassphrase call for this, and then walletlock call.\n"
227 "If the wallet is already encrypted, use the walletpassphrasechange call.\n"
229 "For security reasons, the encryption process will generate a new HD seed, resulting\n"
230 "in the creation of a fresh set of active descriptors. Therefore, it is crucial to\n"
231 "securely back up the newly generated wallet file using the backupwallet RPC.\n",
237 "\nEncrypt your wallet\n"
239 "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
241 "\nNow we can do something like sign\n"
243 "\nNow lock the wallet again by removing the passphrase\n"
245 "\nAs a JSON-RPC call\n"
257 if (pwallet->IsCrypted()) {
261 if (pwallet->IsScanningWithPassphrase()) {
262 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.");
265 LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
268 strWalletPass.reserve(100);
269 strWalletPass = std::string_view{request.params[0].get_str()};
271 if (strWalletPass.empty()) {
275 if (!pwallet->EncryptWallet(strWalletPass)) {
279 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.";
virtual void rpcRunLater(const std::string &name, std::function< void()> fn, int64_t seconds)=0
Run function after given number of seconds. Cancel any previous calls with same name.
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
bool TopUpKeyPool(unsigned int kpSize=0)
interfaces::Chain & chain() const
Interface for accessing chain state.
const std::string & GetName() const
Get a name for this wallet for logging/debugging purposes.
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()
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
#define AssertLockNotHeld(cs)
int64_t GetTime()
DEPRECATED Use either ClockType::now() or Now<TimePointType>() if a cast is needed.