LCOV - code coverage report
Current view: top level - src/interfaces - wallet.h (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 3 5 60.0 %
Date: 2022-04-21 14:51:19 Functions: 2 4 50.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 0 0 -

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

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a