Coverage Report

Created: 2026-09-14 20:36

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/interfaces/wallet.h
Line
Count
Source
1
// Copyright (c) 2018-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
#ifndef BITCOIN_INTERFACES_WALLET_H
6
#define BITCOIN_INTERFACES_WALLET_H
7
8
#include <addresstype.h>
9
#include <common/signmessage.h>
10
#include <common/types.h>
11
#include <consensus/amount.h>
12
#include <interfaces/chain.h>
13
#include <primitives/transaction.h>
14
#include <pubkey.h>
15
#include <support/allocators/secure.h>
16
#include <util/expected.h>
17
#include <util/fs.h>
18
#include <util/result.h>
19
#include <util/ui_change_type.h>
20
#include <wallet/types.h>
21
22
#include <compare>
23
#include <cstddef>
24
#include <cstdint>
25
#include <functional>
26
#include <map>
27
#include <memory>
28
#include <optional>
29
#include <set>
30
#include <string>
31
#include <tuple>
32
#include <utility>
33
#include <vector>
34
35
class ArgsManager;
36
class CScript;
37
class PartiallySignedTransaction;
38
class uint256;
39
enum class FeeReason;
40
enum class OutputType;
41
struct bilingual_str;
42
struct CExtKey;
43
44
namespace wallet {
45
class CCoinControl;
46
class CWallet;
47
struct CRecipient;
48
struct WalletContext;
49
} // namespace wallet
50
51
namespace interfaces {
52
53
class Handler;
54
struct WalletAddress;
55
struct WalletBalances;
56
struct WalletTx;
57
struct WalletTxOut;
58
struct WalletTxStatus;
59
struct WalletMigrationResult;
60
61
//! Interface for accessing a wallet.
62
class Wallet
63
{
64
public:
65
5
    virtual ~Wallet() = default;
66
67
    //! Encrypt wallet.
68
    virtual bool encryptWallet(const SecureString& wallet_passphrase) = 0;
69
70
    //! Return whether wallet is encrypted.
71
    virtual bool isCrypted() = 0;
72
73
    //! Lock wallet.
74
    virtual bool lock() = 0;
75
76
    //! Unlock wallet.
77
    virtual bool unlock(const SecureString& wallet_passphrase) = 0;
78
79
    //! Return whether wallet is locked.
80
    virtual bool isLocked() = 0;
81
82
    //! Change wallet passphrase.
83
    virtual bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
84
        const SecureString& new_wallet_passphrase) = 0;
85
86
    //! Abort a rescan.
87
    virtual void abortRescan() = 0;
88
89
    //! Back up wallet.
90
    virtual bool backupWallet(const std::string& filename) = 0;
91
92
    //! Get wallet name.
93
    virtual std::string getWalletName() = 0;
94
95
    // Get a new address.
96
    virtual util::Result<CTxDestination> getNewDestination(OutputType type, const std::string& label) = 0;
97
98
    //! Get public key.
99
    virtual bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) = 0;
100
101
    //! Generate and add a new HD key to the wallet.
102
    //! Requires the wallet to be unlocked. Returns a `WalletError` with code
103
    //! `WalletErrorCode::UnlockNeeded` if the wallet is locked.
104
    //!
105
    //! Return the master xpub for the added HD key, or a `WalletError` on failure.
106
    virtual util::Expected<CExtPubKey, wallet::WalletError> addHDKey(const std::optional<CExtKey>& key) = 0;
107
108
    //! Sign message
109
    virtual SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) = 0;
110
111
    //! Return whether wallet has private key.
112
    virtual bool isSpendable(const CTxDestination& dest) = 0;
113
114
    //! Add or update address.
115
    virtual bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::optional<wallet::AddressPurpose>& purpose) = 0;
116
117
    // Remove address.
118
    virtual bool delAddressBook(const CTxDestination& dest) = 0;
119
120
    //! Look up address in wallet, return whether exists.
121
    virtual bool getAddress(const CTxDestination& dest,
122
        std::string* name,
123
        wallet::AddressPurpose* purpose) = 0;
124
125
    //! Get wallet address list.
126
    virtual std::vector<WalletAddress> getAddresses() = 0;
127
128
    //! Get receive requests.
129
    virtual std::vector<std::string> getAddressReceiveRequests() = 0;
130
131
    //! Save or remove receive request.
132
    virtual bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) = 0;
133
134
    //! Display address on external signer
135
    virtual util::Result<void> displayAddress(const CTxDestination& dest) = 0;
136
137
    //! Lock coin.
138
    virtual bool lockCoin(const COutPoint& output, bool write_to_db) = 0;
139
140
    //! Unlock coin.
141
    virtual bool unlockCoin(const COutPoint& output) = 0;
142
143
    //! Return whether coin is locked.
144
    virtual bool isLockedCoin(const COutPoint& output) = 0;
145
146
    //! List locked coins.
147
    virtual void listLockedCoins(std::vector<COutPoint>& outputs) = 0;
148
149
    //! Create transaction.
150
    virtual util::Result<wallet::CreatedTransactionResult> createTransaction(const std::vector<wallet::CRecipient>& recipients,
151
        const wallet::CCoinControl& coin_control,
152
        bool sign,
153
        std::optional<unsigned int> change_pos) = 0;
154
155
    //! Commit transaction.
156
    virtual void commitTransaction(CTransactionRef tx, const std::vector<std::string>& messages) = 0;
157
158
    //! Return whether transaction can be abandoned.
159
    virtual bool transactionCanBeAbandoned(const Txid& txid) = 0;
160
161
    //! Abandon transaction.
162
    virtual bool abandonTransaction(const Txid& txid) = 0;
163
164
    //! Return whether transaction can be bumped.
165
    virtual bool transactionCanBeBumped(const Txid& txid) = 0;
166
167
    //! Create bump transaction.
168
    virtual bool createBumpTransaction(const Txid& txid,
169
        const wallet::CCoinControl& coin_control,
170
        std::vector<bilingual_str>& errors,
171
        CAmount& old_fee,
172
        CAmount& new_fee,
173
        CMutableTransaction& mtx) = 0;
174
175
    //! Sign bump transaction.
176
    virtual bool signBumpTransaction(CMutableTransaction& mtx) = 0;
177
178
    //! Commit bump transaction.
179
    virtual bool commitBumpTransaction(const Txid& txid,
180
        CMutableTransaction&& mtx,
181
        std::vector<bilingual_str>& errors,
182
        Txid& bumped_txid) = 0;
183
184
    //! Get a transaction.
185
    virtual CTransactionRef getTx(const Txid& txid) = 0;
186
187
    //! Get transaction information.
188
    virtual WalletTx getWalletTx(const Txid& txid) = 0;
189
190
    //! Get list of all wallet transactions.
191
    virtual std::set<WalletTx> getWalletTxs() = 0;
192
193
    //! Try to get updated status for a particular transaction, if possible without blocking.
194
    virtual bool tryGetTxStatus(const Txid& txid,
195
        WalletTxStatus& tx_status,
196
        int& num_blocks,
197
        int64_t& block_time) = 0;
198
199
    //! Get transaction details.
200
    virtual WalletTx getWalletTxDetails(const Txid& txid,
201
        WalletTxStatus& tx_status,
202
        std::vector<std::string>& messages,
203
        std::vector<std::string>& payment_requests,
204
        bool& in_mempool,
205
        int& num_blocks) = 0;
206
207
    //! Fill PSBT.
208
    virtual std::optional<common::PSBTError> fillPSBT(const common::PSBTFillOptions& options,
209
        size_t* n_signed,
210
        PartiallySignedTransaction& psbtx,
211
        bool& complete) = 0;
212
213
    //! Get balances.
214
    virtual WalletBalances getBalances() = 0;
215
216
    //! Get balances if possible without blocking.
217
    virtual bool tryGetBalances(WalletBalances& balances, uint256& block_hash) = 0;
218
219
    //! Get balance.
220
    virtual CAmount getBalance() = 0;
221
222
    //! Get available balance.
223
    virtual CAmount getAvailableBalance(const wallet::CCoinControl& coin_control) = 0;
224
225
    //! Return whether transaction input belongs to wallet.
226
    virtual bool txinIsMine(const CTxIn& txin) = 0;
227
228
    //! Return whether transaction output belongs to wallet.
229
    virtual bool txoutIsMine(const CTxOut& txout) = 0;
230
231
    //! Return debit amount if transaction input belongs to wallet.
232
    virtual CAmount getDebit(const CTxIn& txin) = 0;
233
234
    //! Return credit amount if transaction input belongs to wallet.
235
    virtual CAmount getCredit(const CTxOut& txout) = 0;
236
237
    //! Return AvailableCoins + LockedCoins grouped by wallet address.
238
    //! (put change in one group with wallet address)
239
    using CoinsList = std::map<CTxDestination, std::vector<std::tuple<COutPoint, WalletTxOut>>>;
240
    virtual CoinsList listCoins() = 0;
241
242
    //! Return wallet transaction output information.
243
    virtual std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) = 0;
244
245
    //! Get required fee.
246
    virtual CAmount getRequiredFee(unsigned int tx_bytes) = 0;
247
248
    //! Get minimum fee.
249
    virtual CAmount getMinimumFee(unsigned int tx_bytes,
250
        const wallet::CCoinControl& coin_control,
251
        std::optional<int>* returned_target,
252
        FeeReason* reason) = 0;
253
254
    //! Get tx confirm target.
255
    virtual unsigned int getConfirmTarget() = 0;
256
257
    // Return whether HD enabled.
258
    virtual bool hdEnabled() = 0;
259
260
    // Return whether the wallet is blank.
261
    virtual bool canGetAddresses() = 0;
262
263
    // Return whether private keys enabled.
264
    virtual bool privateKeysDisabled() = 0;
265
266
    // Return whether the wallet contains a Taproot scriptPubKeyMan
267
    virtual bool taprootEnabled() = 0;
268
269
    // Return whether wallet uses an external signer.
270
    virtual bool hasExternalSigner() = 0;
271
272
    // Get default address type.
273
    virtual OutputType getDefaultAddressType() = 0;
274
275
    //! Get max tx fee.
276
    virtual CAmount getDefaultMaxTxFee() = 0;
277
278
    // Remove wallet.
279
    virtual void remove() = 0;
280
281
    //! Register handler for unload message.
282
    using UnloadFn = std::function<void()>;
283
    virtual std::unique_ptr<Handler> handleUnload(UnloadFn fn) = 0;
284
285
    //! Register handler for show progress messages.
286
    using ShowProgressFn = std::function<void(const std::string& title, int progress)>;
287
    virtual std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) = 0;
288
289
    //! Register handler for status changed messages.
290
    using StatusChangedFn = std::function<void()>;
291
    virtual std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) = 0;
292
293
    //! Register handler for address book changed messages.
294
    using AddressBookChangedFn = std::function<void(const CTxDestination& address,
295
        const std::string& label,
296
        bool is_mine,
297
        wallet::AddressPurpose purpose,
298
        ChangeType status)>;
299
    virtual std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) = 0;
300
301
    //! Register handler for transaction changed messages.
302
    using TransactionChangedFn = std::function<void(const Txid& txid, ChangeType status)>;
303
    virtual std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) = 0;
304
305
    //! Register handler for keypool changed messages.
306
    using CanGetAddressesChangedFn = std::function<void()>;
307
    virtual std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) = 0;
308
309
    //! Return pointer to internal wallet class, useful for testing.
310
0
    virtual wallet::CWallet* wallet() { return nullptr; }
311
312
    //! Export a watchonly wallet file. See CWallet::ExportWatchOnlyWallet
313
    virtual util::Result<std::string> exportWatchOnlyWallet(const fs::path& destination) = 0;
314
};
315
316
//! Wallet chain client that in addition to having chain client methods for
317
//! starting up, shutting down, and registering RPCs, also has additional
318
//! methods (called by the GUI) to load and create wallets.
319
class WalletLoader : public ChainClient
320
{
321
public:
322
    //! Create new wallet.
323
    virtual util::Result<std::unique_ptr<Wallet>> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, std::vector<bilingual_str>& warnings) = 0;
324
325
    //! Load existing wallet.
326
    virtual util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) = 0;
327
328
    //! Return default wallet directory.
329
    virtual std::string getWalletDir() = 0;
330
331
    //! Restore backup wallet
332
    virtual util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings, bool load_after_restore) = 0;
333
334
    //! Migrate a wallet
335
    virtual util::Result<WalletMigrationResult> migrateWallet(const std::string& name, const SecureString& passphrase, bool load_wallet) = 0;
336
337
    //! Returns true if wallet stores encryption keys
338
    virtual bool isEncrypted(const std::string& wallet_name) = 0;
339
340
    //! Return available wallets in wallet directory.
341
    virtual std::vector<std::pair<std::string, std::string>> listWalletDir() = 0;
342
343
    //! Return interfaces for accessing wallets (if any).
344
    virtual std::vector<std::unique_ptr<Wallet>> getWallets() = 0;
345
346
    //! Register handler for load wallet messages. This callback is triggered by
347
    //! createWallet and loadWallet above, and also triggered when wallets are
348
    //! loaded at startup or by RPC.
349
    using LoadWalletFn = std::function<void(std::unique_ptr<Wallet> wallet)>;
350
    virtual std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) = 0;
351
352
    //! Return pointer to internal context, useful for testing.
353
0
    virtual wallet::WalletContext* context() { return nullptr; }
354
};
355
356
//! Information about one wallet address.
357
struct WalletAddress
358
{
359
    CTxDestination dest;
360
    bool is_mine;
361
    wallet::AddressPurpose purpose;
362
    std::string name;
363
364
    WalletAddress(CTxDestination dest, bool is_mine, wallet::AddressPurpose purpose, std::string name)
365
0
        : dest(std::move(dest)), is_mine(is_mine), purpose(std::move(purpose)), name(std::move(name))
366
0
    {
367
0
    }
368
};
369
370
//! Collection of wallet balances.
371
struct WalletBalances
372
{
373
    CAmount balance = 0;
374
    CAmount unconfirmed_balance = 0;
375
    CAmount immature_balance = 0;
376
    CAmount used_balance = 0;
377
    CAmount nonmempool_balance = 0;
378
379
    bool balanceChanged(const WalletBalances& prev) const
380
0
    {
381
0
        return balance != prev.balance || unconfirmed_balance != prev.unconfirmed_balance ||
382
0
               immature_balance != prev.immature_balance ||
383
0
               used_balance != prev.used_balance || nonmempool_balance != prev.nonmempool_balance;
384
0
    }
385
};
386
387
// Wallet transaction information.
388
struct WalletTx
389
{
390
    CTransactionRef tx;
391
    std::vector<bool> txin_is_mine;
392
    std::vector<bool> txout_is_mine;
393
    std::vector<bool> txout_is_change;
394
    std::vector<CTxDestination> txout_address;
395
    std::vector<bool> txout_address_is_mine;
396
    CAmount credit;
397
    CAmount debit;
398
    CAmount change;
399
    int64_t time;
400
    std::optional<std::string> from; // Deprecated
401
    std::optional<std::string> message; // Deprecated
402
    std::optional<std::string> comment;
403
    std::optional<std::string> comment_to;
404
    bool is_coinbase;
405
406
0
    bool operator<(const WalletTx& a) const { return tx->GetHash() < a.tx->GetHash(); }
407
};
408
409
//! Updated transaction status.
410
struct WalletTxStatus
411
{
412
    int block_height;
413
    int blocks_to_maturity;
414
    int depth_in_main_chain;
415
    unsigned int time_received;
416
    uint32_t lock_time;
417
    bool is_trusted;
418
    bool is_abandoned;
419
    bool is_coinbase;
420
    bool is_in_main_chain;
421
};
422
423
//! Wallet transaction output.
424
struct WalletTxOut
425
{
426
    CTxOut txout;
427
    int64_t time;
428
    int depth_in_main_chain = -1;
429
    bool is_spent = false;
430
};
431
432
//! Migrated wallet info
433
struct WalletMigrationResult
434
{
435
    std::unique_ptr<Wallet> wallet;
436
    std::optional<std::string> watchonly_wallet_name;
437
    std::optional<std::string> solvables_wallet_name;
438
    fs::path backup_path;
439
};
440
441
//! Return implementation of Wallet interface. This function is defined in
442
//! dummywallet.cpp and throws if the wallet component is not compiled.
443
std::unique_ptr<Wallet> MakeWallet(wallet::WalletContext& context, const std::shared_ptr<wallet::CWallet>& wallet);
444
445
//! Return implementation of ChainClient interface for a wallet loader. This
446
//! function will be undefined in builds where ENABLE_WALLET is false.
447
std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, ArgsManager& args);
448
449
} // namespace interfaces
450
451
#endif // BITCOIN_INTERFACES_WALLET_H