fuzz coverage

Coverage Report

Created: 2025-09-17 22:41

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