Coverage Report

Created: 2026-07-23 20:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/wallet/rpc/encrypt.cpp
Line
Count
Source
1
// Copyright (c) 2011-present 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 <scheduler.h>
7
#include <wallet/context.h>
8
#include <wallet/rpc/util.h>
9
#include <wallet/wallet.h>
10
11
12
namespace wallet {
13
RPCMethod walletpassphrase()
14
876
{
15
876
    return RPCMethod{
16
876
        "walletpassphrase",
17
876
        "Stores the wallet decryption key in memory for 'timeout' seconds.\n"
18
876
                "This is needed prior to performing transactions related to private keys such as sending bitcoins\n"
19
876
            "\nNote:\n"
20
876
            "Issuing the walletpassphrase command while the wallet is already unlocked will set a new unlock\n"
21
876
            "time that overrides the old one.\n",
22
876
                {
23
876
                    {"passphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The wallet passphrase"},
24
876
                    {"timeout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The time to keep the decryption key in seconds; capped at 100000000 (~3 years)."},
25
876
                },
26
876
                RPCResult{RPCResult::Type::NONE, "", ""},
27
876
                RPCExamples{
28
876
            "\nUnlock the wallet for 60 seconds\n"
29
876
            + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 60") +
30
876
            "\nLock the wallet again (before 60 seconds)\n"
31
876
            + HelpExampleCli("walletlock", "") +
32
876
            "\nAs a JSON-RPC call\n"
33
876
            + HelpExampleRpc("walletpassphrase", "\"my pass phrase\", 60")
34
876
                },
35
876
        [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
36
876
{
37
55
    std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
38
55
    if (!wallet) return UniValue::VNULL;
39
55
    CWallet* const pwallet = wallet.get();
40
41
55
    int64_t nSleepTime;
42
55
    int64_t relock_time;
43
    // Prevent concurrent calls to walletpassphrase with the same wallet.
44
55
    LOCK(pwallet->m_unlock_mutex);
45
55
    {
46
55
        LOCK(pwallet->cs_wallet);
47
48
55
        if (!pwallet->HasEncryptionKeys()) {
49
6
            throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrase was called.");
50
6
        }
51
52
        // Note that the walletpassphrase is stored in request.params[0] which is not mlock()ed
53
49
        SecureString strWalletPass;
54
49
        strWalletPass.reserve(100);
55
49
        strWalletPass = std::string_view{request.params[0].get_str()};
56
57
        // Get the timeout
58
49
        nSleepTime = request.params[1].getInt<int64_t>();
59
        // Timeout cannot be negative, otherwise it will relock immediately
60
49
        if (nSleepTime < 0) {
61
1
            throw JSONRPCError(RPC_INVALID_PARAMETER, "Timeout cannot be negative.");
62
1
        }
63
        // Clamp timeout to ~3 years to avoid overflow when computing the relock time
64
48
        constexpr int64_t MAX_SLEEP_TIME = 100000000;
65
48
        if (nSleepTime > MAX_SLEEP_TIME) {
66
1
            nSleepTime = MAX_SLEEP_TIME;
67
1
        }
68
69
48
        if (strWalletPass.empty()) {
70
1
            throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase cannot be empty");
71
1
        }
72
73
47
        if (!pwallet->Unlock(strWalletPass)) {
74
            // Check if the passphrase has a null character (see #27067 for details)
75
4
            if (strWalletPass.find('\0') == std::string::npos) {
76
3
                throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
77
3
            } else {
78
1
                throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered is incorrect. "
79
1
                                                                    "It contains a null character (ie - a zero byte). "
80
1
                                                                    "If the passphrase was set with a version of this software prior to 25.0, "
81
1
                                                                    "please try again with only the characters up to — but not including — "
82
1
                                                                    "the first null character. If this is successful, please set a new "
83
1
                                                                    "passphrase to avoid this issue in the future.");
84
1
            }
85
4
        }
86
87
43
        pwallet->TopUpKeyPool();
88
89
43
        pwallet->nRelockTime = GetTime() + nSleepTime;
90
43
        relock_time = pwallet->nRelockTime;
91
43
    }
92
93
    // Get wallet scheduler to queue up the relock callback in the future.
94
    // Scheduled events don't get destructed until they are executed,
95
    // and they are executed in series in a single scheduler thread so
96
    // no cs_wallet lock is needed.
97
0
    WalletContext& context = EnsureWalletContext(request.context);
98
    // Keep a weak pointer to the wallet so that it is possible to unload the
99
    // wallet before the following callback is called. If a valid shared pointer
100
    // is acquired in the callback then the wallet is still loaded.
101
43
    std::weak_ptr<CWallet> weak_wallet = wallet;
102
43
    context.scheduler->scheduleFromNow([weak_wallet, relock_time] {
103
5
        if (auto shared_wallet = weak_wallet.lock()) {
104
3
            LOCK2(shared_wallet->m_relock_mutex, shared_wallet->cs_wallet);
105
            // Skip if this is not the most recent relock callback.
106
3
            if (shared_wallet->nRelockTime != relock_time) return;
107
3
            shared_wallet->Lock();
108
3
            shared_wallet->nRelockTime = 0;
109
3
        }
110
5
    }, std::chrono::seconds(nSleepTime));
111
112
43
    return UniValue::VNULL;
113
47
},
114
876
    };
115
876
}
116
117
118
RPCMethod walletpassphrasechange()
119
828
{
120
828
    return RPCMethod{
121
828
        "walletpassphrasechange",
122
828
        "Changes the wallet passphrase from 'oldpassphrase' to 'newpassphrase'.\n",
123
828
                {
124
828
                    {"oldpassphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The current passphrase"},
125
828
                    {"newpassphrase", RPCArg::Type::STR, RPCArg::Optional::NO, "The new passphrase"},
126
828
                },
127
828
                RPCResult{RPCResult::Type::NONE, "", ""},
128
828
                RPCExamples{
129
828
                    HelpExampleCli("walletpassphrasechange", "\"old one\" \"new one\"")
130
828
            + HelpExampleRpc("walletpassphrasechange", "\"old one\", \"new one\"")
131
828
                },
132
828
        [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
133
828
{
134
7
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
135
7
    if (!pwallet) return UniValue::VNULL;
136
137
7
    if (!pwallet->HasEncryptionKeys()) {
138
1
        throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletpassphrasechange was called.");
139
1
    }
140
141
6
    if (pwallet->IsScanningWithPassphrase()) {
142
1
        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
1
    }
144
145
5
    LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
146
147
5
    SecureString strOldWalletPass;
148
5
    strOldWalletPass.reserve(100);
149
5
    strOldWalletPass = std::string_view{request.params[0].get_str()};
150
151
5
    SecureString strNewWalletPass;
152
5
    strNewWalletPass.reserve(100);
153
5
    strNewWalletPass = std::string_view{request.params[1].get_str()};
154
155
5
    if (strOldWalletPass.empty() || strNewWalletPass.empty()) {
156
1
        throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase cannot be empty");
157
1
    }
158
159
4
    if (!pwallet->ChangeWalletPassphrase(strOldWalletPass, strNewWalletPass)) {
160
        // Check if the old passphrase had a null character (see #27067 for details)
161
2
        if (strOldWalletPass.find('\0') == std::string::npos) {
162
1
            throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The wallet passphrase entered was incorrect.");
163
1
        } else {
164
1
            throw JSONRPCError(RPC_WALLET_PASSPHRASE_INCORRECT, "Error: The old wallet passphrase entered is incorrect. "
165
1
                                                                "It contains a null character (ie - a zero byte). "
166
1
                                                                "If the old passphrase was set with a version of this software prior to 25.0, "
167
1
                                                                "please try again with only the characters up to — but not including — "
168
1
                                                                "the first null character.");
169
1
        }
170
2
    }
171
172
2
    return UniValue::VNULL;
173
4
},
174
828
    };
175
828
}
176
177
178
RPCMethod walletlock()
179
846
{
180
846
    return RPCMethod{
181
846
        "walletlock",
182
846
        "Removes the wallet encryption key from memory, locking the wallet.\n"
183
846
                "After calling this method, you will need to call walletpassphrase again\n"
184
846
                "before being able to call any methods which require the wallet to be unlocked.\n",
185
846
                {},
186
846
                RPCResult{RPCResult::Type::NONE, "", ""},
187
846
                RPCExamples{
188
846
            "\nSet the passphrase for 2 minutes to perform a transaction\n"
189
846
            + HelpExampleCli("walletpassphrase", "\"my pass phrase\" 120") +
190
846
            "\nPerform a send (requires passphrase set)\n"
191
846
            + HelpExampleCli("sendtoaddress", "\"" + EXAMPLE_ADDRESS[0] + "\" 1.0") +
192
846
            "\nClear the passphrase since we are done before 2 minutes is up\n"
193
846
            + HelpExampleCli("walletlock", "") +
194
846
            "\nAs a JSON-RPC call\n"
195
846
            + HelpExampleRpc("walletlock", "")
196
846
                },
197
846
        [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
198
846
{
199
25
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
200
25
    if (!pwallet) return UniValue::VNULL;
201
202
25
    if (!pwallet->HasEncryptionKeys()) {
203
0
        throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an unencrypted wallet, but walletlock was called.");
204
0
    }
205
206
25
    if (pwallet->IsScanningWithPassphrase()) {
207
1
        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
1
    }
209
210
24
    LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
211
212
24
    pwallet->Lock();
213
24
    pwallet->nRelockTime = 0;
214
215
24
    return UniValue::VNULL;
216
25
},
217
846
    };
218
846
}
219
220
221
RPCMethod encryptwallet()
222
842
{
223
842
    return RPCMethod{
224
842
        "encryptwallet",
225
842
        "Encrypts the wallet with 'passphrase'. This is for first time encryption.\n"
226
842
        "After this, any calls that interact with private keys such as sending or signing \n"
227
842
        "will require the passphrase to be set prior to making these calls.\n"
228
842
                "Use the walletpassphrase call for this, and then walletlock call.\n"
229
842
                "If the wallet is already encrypted, use the walletpassphrasechange call.\n"
230
842
                "** IMPORTANT **\n"
231
842
                "For security reasons, the encryption process will generate a new HD seed, resulting\n"
232
842
                "in the creation of a fresh set of active descriptors. Therefore, it is crucial to\n"
233
842
                "securely back up the newly generated wallet file using the backupwallet RPC.\n",
234
842
                {
235
842
                    {"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."},
236
842
                },
237
842
                RPCResult{RPCResult::Type::STR, "", "A string with further instructions"},
238
842
                RPCExamples{
239
842
            "\nEncrypt your wallet\n"
240
842
            + HelpExampleCli("encryptwallet", "\"my pass phrase\"") +
241
842
            "\nNow set the passphrase to use the wallet, such as for signing or sending bitcoin\n"
242
842
            + HelpExampleCli("walletpassphrase", "\"my pass phrase\"") +
243
842
            "\nNow we can do something like sign\n"
244
842
            + HelpExampleCli("signmessage", "\"address\" \"test message\"") +
245
842
            "\nNow lock the wallet again by removing the passphrase\n"
246
842
            + HelpExampleCli("walletlock", "") +
247
842
            "\nAs a JSON-RPC call\n"
248
842
            + HelpExampleRpc("encryptwallet", "\"my pass phrase\"")
249
842
                },
250
842
        [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
251
842
{
252
21
    std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
253
21
    if (!pwallet) return UniValue::VNULL;
254
255
21
    if (pwallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
256
3
        throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: wallet does not contain private keys, nothing to encrypt.");
257
3
    }
258
259
18
    if (pwallet->HasEncryptionKeys()) {
260
1
        throw JSONRPCError(RPC_WALLET_WRONG_ENC_STATE, "Error: running with an encrypted wallet, but encryptwallet was called.");
261
1
    }
262
263
17
    if (pwallet->IsScanningWithPassphrase()) {
264
0
        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
0
    }
266
267
17
    LOCK2(pwallet->m_relock_mutex, pwallet->cs_wallet);
268
269
17
    SecureString strWalletPass;
270
17
    strWalletPass.reserve(100);
271
17
    strWalletPass = std::string_view{request.params[0].get_str()};
272
273
17
    if (strWalletPass.empty()) {
274
1
        throw JSONRPCError(RPC_INVALID_PARAMETER, "passphrase cannot be empty");
275
1
    }
276
277
16
    if (!pwallet->EncryptWallet(strWalletPass)) {
278
0
        throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: Failed to encrypt the wallet.");
279
0
    }
280
281
16
    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.";
282
16
},
283
842
    };
284
842
}
285
} // namespace wallet