Coverage Report

Created: 2026-09-02 14:16

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