|
| 1 | +// Copyright (c) 2011-2021 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <rpc/util.h> |
| 6 | +#include <wallet/rpc/util.h> |
| 7 | +#include <wallet/wallet.h> |
| 8 | + |
| 9 | + |
| 10 | +RPCHelpMan walletpassphrase() |
| 11 | +{ |
| 12 | + return RPCHelpMan{"walletpassphrase", |
| 13 | + "\nStores the wallet decryption key in memory for 'timeout' seconds.\n" |
| 14 | + "This is needed prior to performing transactions related to private keys such as sending bitcoins\n" |
| 15 | + "\nNote:\n" |
| 16 | + "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n" |
| 17 | + "time that overrides the old one.\n", |
| 18 | + { |
| 19 | + {"passphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet passphrase"}, |
| 20 | + {"timeout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The time to keep the decryption key in seconds; capped at 100000000 (~3 years)."}, |
| 21 | + }, |
| 22 | + RPCResult{RPCResult::Type::NONE, "", ""}, |
| 23 | + RPCExamples{ |
| 24 | + "\nUnlock the wallet for 60 seconds\n" |
| 25 | + + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 60") + |
| 26 | + "\nLock the wallet again (before 60 seconds)\n" |
| 27 | + + HelpExampleCli("walletlock", "") + |
| 28 | + "\nAs a JSON-RPC call\n" |
| 29 | + + HelpExampleRpc("walletpassphrase", "\"my pass phrase\", 60") |
| 30 | + }, |
| 31 | + [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue |
| 32 | +{ |
| 33 | + std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request); |
| 34 | + if (!wallet) return NullUniValue; |
| 35 | + CWallet* const pwallet = wallet.get(); |
| 36 | + |
| 37 | + int64_t nSleepTime; |
| 38 | + int64_t relock_time; |
| 39 | + // Prevent concurrent calls to walletpassphrase with the same wallet. |
| 40 | + LOCK(pwallet->m_unlock_mutex); |
| 41 | + { |
| 42 | + LOCK(pwallet->cs_wallet); |
| 43 | + |
| 44 | + if (!pwallet->IsCrypted()) { |
| 45 | + throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrase was called."); |
| 46 | + } |
| 47 | + |
| 48 | + // Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed |
| 49 | + SecureString strWalletPass; |
| 50 | + strWalletPass.reserve(100); |
| 51 | + // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string) |
| 52 | + // Alternately, find a way to make request.params[0] mlock()'d to begin with. |
| 53 | + strWalletPass = request.params[0].get_str().c_str(); |
| 54 | + |
| 55 | + // Get the timeout |
| 56 | + nSleepTime = request.params[1].get_int64(); |
| 57 | + // Timeout cannot be negative, otherwise it will relock immediately |
| 58 | + if (nSleepTime < 0) { |
| 59 | + throw JSONRPCError(RPC_INVALID_PARAMETER, "Timeout cannot be negative."); |
| 60 | + } |
| 61 | + // Clamp timeout |
| 62 | + constexpr int64_t MAX_SLEEP_TIME = 100000000; // larger values trigger a macos/libevent bug? |
| 63 | + if (nSleepTime > MAX_SLEEP_TIME) { |
| 64 | + nSleepTime = MAX_SLEEP_TIME; |
| 65 | + } |
| 66 | + |
| 67 | + if (strWalletPass.empty()) { |
| 68 | + throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty"); |
| 69 | + } |
| 70 | + |
| 71 | + if (!pwallet->Unlock(strWalletPass)) { |
| 72 | + throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect."); |
| 73 | + } |
| 74 | + |
| 75 | + pwallet->TopUpKeyPool(); |
| 76 | + |
| 77 | + pwallet->nRelockTime = GetTime() + nSleepTime; |
| 78 | + relock_time = pwallet->nRelockTime; |
| 79 | + } |
| 80 | + |
| 81 | + // rpcRunLater must be called without cs_wallet held otherwise a deadlock |
| 82 | + // can occur. The deadlock would happen when RPCRunLater removes the |
| 83 | + // previous timer (and waits for the callback to finish if already running) |
| 84 | + // and the callback locks cs_wallet. |
| 85 | + AssertLockNotHeld(wallet->cs_wallet); |
| 86 | + // Keep a weak pointer to the wallet so that it is possible to unload the |
| 87 | + // wallet before the following callback is called. If a valid shared pointer |
| 88 | + // is acquired in the callback then the wallet is still loaded. |
| 89 | + std::weak_ptr<CWallet> weak_wallet = wallet; |
| 90 | + pwallet->chain().rpcRunLater(strprintf("lockwallet(%s)", pwallet->GetName()), [weak_wallet, relock_time] { |
| 91 | + if (auto shared_wallet = weak_wallet.lock()) { |
| 92 | + LOCK(shared_wallet->cs_wallet); |
| 93 | + // Skip if this is not the most recent rpcRunLater callback. |
| 94 | + if (shared_wallet->nRelockTime != relock_time) return; |
| 95 | + shared_wallet->Lock(); |
| 96 | + shared_wallet->nRelockTime = 0; |
| 97 | + } |
| 98 | + }, nSleepTime); |
| 99 | + |
| 100 | + return NullUniValue; |
| 101 | +}, |
| 102 | + }; |
| 103 | +} |
| 104 | + |
| 105 | + |
| 106 | +RPCHelpMan walletpassphrasechange() |
| 107 | +{ |
| 108 | + return RPCHelpMan{"walletpassphrasechange", |
| 109 | + "\nChanges the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n", |
| 110 | + { |
| 111 | + {"oldpassphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The current passphrase"}, |
| 112 | + {"newpassphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The new passphrase"}, |
| 113 | + }, |
| 114 | + RPCResult{RPCResult::Type::NONE, "", ""}, |
| 115 | + RPCExamples{ |
| 116 | + HelpExampleCli("walletpassphrasechange", "\"old one\" \"new one\"") |
| 117 | + + HelpExampleRpc("walletpassphrasechange", "\"old one\", \"new one\"") |
| 118 | + }, |
| 119 | + [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue |
| 120 | +{ |
| 121 | + std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request); |
| 122 | + if (!pwallet) return NullUniValue; |
| 123 | + |
| 124 | + LOCK(pwallet->cs_wallet); |
| 125 | + |
| 126 | + if (!pwallet->IsCrypted()) { |
| 127 | + throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrasechange was called."); |
| 128 | + } |
| 129 | + |
| 130 | + // TODO: get rid of these .c_str() calls by implementing SecureString::operator=(std::string) |
| 131 | + // Alternately, find a way to make request.params[0] mlock()'d to begin with. |
| 132 | + SecureString strOldWalletPass; |
| 133 | + strOldWalletPass.reserve(100); |
| 134 | + strOldWalletPass = request.params[0].get_str().c_str(); |
| 135 | + |
| 136 | + SecureString strNewWalletPass; |
| 137 | + strNewWalletPass.reserve(100); |
| 138 | + strNewWalletPass = request.params[1].get_str().c_str(); |
| 139 | + |
| 140 | + if (strOldWalletPass.empty() || strNewWalletPass.empty()) { |
| 141 | + throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty"); |
| 142 | + } |
| 143 | + |
| 144 | + if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) { |
| 145 | + throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect."); |
| 146 | + } |
| 147 | + |
| 148 | + return NullUniValue; |
| 149 | +}, |
| 150 | + }; |
| 151 | +} |
| 152 | + |
| 153 | + |
| 154 | +RPCHelpMan walletlock() |
| 155 | +{ |
| 156 | + return RPCHelpMan{"walletlock", |
| 157 | + "\nRemoves the wallet encryption key from memory, locking the wallet.\n" |
| 158 | + "After calling this method, you will need to call walletpassphrase again\n" |
| 159 | + "before being able to call any methods which require the wallet to be unlocked.\n", |
| 160 | + {}, |
| 161 | + RPCResult{RPCResult::Type::NONE, "", ""}, |
| 162 | + RPCExamples{ |
| 163 | + "\nSet the passphrase for 2 minutes to perform a transaction\n" |
| 164 | + + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 120") + |
| 165 | + "\nPerform a send (requires passphrase set)\n" |
| 166 | + + HelpExampleCli("sendtoaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 1.0") + |
| 167 | + "\nClear the passphrase since we are done before 2 minutes is up\n" |
| 168 | + + HelpExampleCli("walletlock", "") + |
| 169 | + "\nAs a JSON-RPC call\n" |
| 170 | + + HelpExampleRpc("walletlock", "") |
| 171 | + }, |
| 172 | + [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue |
| 173 | +{ |
| 174 | + std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request); |
| 175 | + if (!pwallet) return NullUniValue; |
| 176 | + |
| 177 | + LOCK(pwallet->cs_wallet); |
| 178 | + |
| 179 | + if (!pwallet->IsCrypted()) { |
| 180 | + throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletlock was called."); |
| 181 | + } |
| 182 | + |
| 183 | + pwallet->Lock(); |
| 184 | + pwallet->nRelockTime = 0; |
| 185 | + |
| 186 | + return NullUniValue; |
| 187 | +}, |
| 188 | + }; |
| 189 | +} |
| 190 | + |
| 191 | + |
| 192 | +RPCHelpMan encryptwallet() |
| 193 | +{ |
| 194 | + return RPCHelpMan{"encryptwallet", |
| 195 | + "\nEncrypts the wallet with 'passphrase'. This is for first time encryption.\n" |
| 196 | + "After this, any calls that interact with private keys such as sending or signing \n" |
| 197 | + "will require the passphrase to be set prior the making these calls.\n" |
| 198 | + "Use the walletpassphrase call for this, and then walletlock call.\n" |
| 199 | + "If the wallet is already encrypted, use the walletpassphrasechange call.\n", |
| 200 | + { |
| 201 | + {"passphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The pass phrase to encrypt the wallet with. It must be at least 1 character, but should be long."}, |
| 202 | + }, |
| 203 | + RPCResult{RPCResult::Type::STR, "", "A string with further instructions"}, |
| 204 | + RPCExamples{ |
| 205 | + "\nEncrypt your wallet\n" |
| 206 | + + HelpExampleCli("encryptwallet", "\"my pass phrase\"") + |
| 207 | + "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n" |
| 208 | + + HelpExampleCli("walletpassphrase", "\"my pass phrase\"") + |
| 209 | + "\nNow we can do something like sign\n" |
| 210 | + + HelpExampleCli("signmessage", "\"address\" \"test message\"") + |
| 211 | + "\nNow lock the wallet again by removing the passphrase\n" |
| 212 | + + HelpExampleCli("walletlock", "") + |
| 213 | + "\nAs a JSON-RPC call\n" |
| 214 | + + HelpExampleRpc("encryptwallet", "\"my pass phrase\"") |
| 215 | + }, |
| 216 | + [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue |
| 217 | +{ |
| 218 | + std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request); |
| 219 | + if (!pwallet) return NullUniValue; |
| 220 | + |
| 221 | + LOCK(pwallet->cs_wallet); |
| 222 | + |
| 223 | + if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { |
| 224 | + throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: wallet does not contain private keys, nothing to encrypt."); |
| 225 | + } |
| 226 | + |
| 227 | + if (pwallet->IsCrypted()) { |
| 228 | + throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an encrypted wallet, but encryptwallet was called."); |
| 229 | + } |
| 230 | + |
| 231 | + // TODO: get rid of this .c_str() by implementing SecureString::operator=(std::string) |
| 232 | + // Alternately, find a way to make request.params[0] mlock()'d to begin with. |
| 233 | + SecureString strWalletPass; |
| 234 | + strWalletPass.reserve(100); |
| 235 | + strWalletPass = request.params[0].get_str().c_str(); |
| 236 | + |
| 237 | + if (strWalletPass.empty()) { |
| 238 | + throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase can not be empty"); |
| 239 | + } |
| 240 | + |
| 241 | + if (!pwallet->EncryptWallet(strWalletPass)) { |
| 242 | + throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet."); |
| 243 | + } |
| 244 | + |
| 245 | + return "wallet encrypted; The keypool has been flushed and a new HD seed was generated (if you are using HD). You need to make a new backup."; |
| 246 | +}, |
| 247 | + }; |
| 248 | +} |
0 commit comments