LCOV - code coverage report
Current view: top level - src/wallet - interfaces.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 14 447 3.1 %
Date: 2023-11-10 23:46:46 Functions: 0 116 0.0 %
Branches: 11 456 2.4 %

           Branch data     Line data    Source code
       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                 :            : #include <interfaces/wallet.h>
       6                 :            : 
       7                 :            : #include <common/args.h>
       8                 :            : #include <consensus/amount.h>
       9                 :            : #include <interfaces/chain.h>
      10                 :            : #include <interfaces/handler.h>
      11                 :            : #include <policy/fees.h>
      12                 :            : #include <primitives/transaction.h>
      13                 :            : #include <rpc/server.h>
      14                 :            : #include <support/allocators/secure.h>
      15                 :            : #include <sync.h>
      16                 :            : #include <uint256.h>
      17         [ +  - ]:          2 : #include <util/check.h>
      18         [ +  - ]:          2 : #include <util/translation.h>
      19                 :            : #include <util/ui_change_type.h>
      20                 :            : #include <wallet/coincontrol.h>
      21                 :            : #include <wallet/context.h>
      22                 :            : #include <wallet/feebumper.h>
      23                 :            : #include <wallet/fees.h>
      24                 :            : #include <wallet/types.h>
      25                 :            : #include <wallet/load.h>
      26                 :            : #include <wallet/receive.h>
      27                 :          2 : #include <wallet/rpc/wallet.h>
      28                 :            : #include <wallet/spend.h>
      29                 :            : #include <wallet/wallet.h>
      30                 :            : 
      31                 :            : #include <memory>
      32                 :            : #include <string>
      33                 :            : #include <utility>
      34                 :            : #include <vector>
      35                 :            : 
      36                 :            : using interfaces::Chain;
      37                 :            : using interfaces::FoundBlock;
      38                 :            : using interfaces::Handler;
      39                 :            : using interfaces::MakeSignalHandler;
      40                 :            : using interfaces::Wallet;
      41                 :            : using interfaces::WalletAddress;
      42                 :            : using interfaces::WalletBalances;
      43                 :            : using interfaces::WalletLoader;
      44                 :            : using interfaces::WalletMigrationResult;
      45                 :            : using interfaces::WalletOrderForm;
      46                 :            : using interfaces::WalletTx;
      47                 :            : using interfaces::WalletTxOut;
      48                 :            : using interfaces::WalletTxStatus;
      49                 :            : using interfaces::WalletValueMap;
      50                 :            : 
      51                 :            : namespace wallet {
      52                 :            : // All members of the classes in this namespace are intentionally public, as the
      53         [ #  # ]:          0 : // classes themselves are private.
      54                 :            : namespace {
      55                 :            : //! Construct wallet tx struct.
      56                 :          0 : WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
      57                 :            : {
      58                 :          0 :     LOCK(wallet.cs_wallet);
      59                 :          0 :     WalletTx result;
      60                 :          0 :     result.tx = wtx.tx;
      61         [ #  # ]:          0 :     result.txin_is_mine.reserve(wtx.tx->vin.size());
      62         [ #  # ]:          0 :     for (const auto& txin : wtx.tx->vin) {
      63 [ #  # ][ #  # ]:          0 :         result.txin_is_mine.emplace_back(InputIsMine(wallet, txin));
      64                 :            :     }
      65         [ #  # ]:          0 :     result.txout_is_mine.reserve(wtx.tx->vout.size());
      66         [ #  # ]:          0 :     result.txout_address.reserve(wtx.tx->vout.size());
      67         [ #  # ]:          0 :     result.txout_address_is_mine.reserve(wtx.tx->vout.size());
      68         [ #  # ]:          0 :     for (const auto& txout : wtx.tx->vout) {
      69 [ #  # ][ #  # ]:          0 :         result.txout_is_mine.emplace_back(wallet.IsMine(txout));
      70 [ #  # ][ #  # ]:          0 :         result.txout_is_change.push_back(OutputIsChange(wallet, txout));
      71         [ #  # ]:          0 :         result.txout_address.emplace_back();
      72 [ #  # ][ #  # ]:          0 :         result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ?
                 [ #  # ]
      73         [ #  # ]:          0 :                                                       wallet.IsMine(result.txout_address.back()) :
      74                 :            :                                                       ISMINE_NO);
      75                 :            :     }
      76         [ #  # ]:          0 :     result.credit = CachedTxGetCredit(wallet, wtx, ISMINE_ALL);
      77         [ #  # ]:          0 :     result.debit = CachedTxGetDebit(wallet, wtx, ISMINE_ALL);
      78         [ #  # ]:          0 :     result.change = CachedTxGetChange(wallet, wtx);
      79         [ #  # ]:          0 :     result.time = wtx.GetTxTime();
      80         [ #  # ]:          0 :     result.value_map = wtx.mapValue;
      81         [ #  # ]:          0 :     result.is_coinbase = wtx.IsCoinBase();
      82                 :          0 :     return result;
      83         [ #  # ]:          0 : }
      84                 :            : 
      85                 :            : //! Construct wallet tx status struct.
      86                 :          0 : WalletTxStatus MakeWalletTxStatus(const CWallet& wallet, const CWalletTx& wtx)
      87                 :            :     EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
      88                 :            : {
      89                 :          0 :     AssertLockHeld(wallet.cs_wallet);
      90                 :            : 
      91                 :          2 :     WalletTxStatus result;
      92                 :          0 :     result.block_height =
      93         [ #  # ]:          0 :         wtx.state<TxStateConfirmed>() ? wtx.state<TxStateConfirmed>()->confirmed_block_height :
      94         [ #  # ]:          0 :         wtx.state<TxStateConflicted>() ? wtx.state<TxStateConflicted>()->conflicting_block_height :
      95                 :          0 :         std::numeric_limits<int>::max();
      96                 :          0 :     result.blocks_to_maturity = wallet.GetTxBlocksToMaturity(wtx);
      97                 :          0 :     result.depth_in_main_chain = wallet.GetTxDepthInMainChain(wtx);
      98                 :          0 :     result.time_received = wtx.nTimeReceived;
      99                 :          2 :     result.lock_time = wtx.tx->nLockTime;
     100                 :          0 :     result.is_trusted = CachedTxIsTrusted(wallet, wtx);
     101                 :          0 :     result.is_abandoned = wtx.isAbandoned();
     102                 :          0 :     result.is_coinbase = wtx.IsCoinBase();
     103                 :          0 :     result.is_in_main_chain = wallet.IsTxInMainChain(wtx);
     104                 :          0 :     return result;
     105                 :            : }
     106                 :            : 
     107                 :            : //! Construct wallet TxOut struct.
     108                 :          0 : WalletTxOut MakeWalletTxOut(const CWallet& wallet,
     109                 :            :     const CWalletTx& wtx,
     110                 :            :     int n,
     111                 :            :     int depth) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
     112                 :            : {
     113                 :          0 :     WalletTxOut result;
     114         [ #  # ]:          0 :     result.txout = wtx.tx->vout[n];
     115         [ #  # ]:          0 :     result.time = wtx.GetTxTime();
     116                 :          0 :     result.depth_in_main_chain = depth;
     117 [ #  # ][ #  # ]:          0 :     result.is_spent = wallet.IsSpent(COutPoint(wtx.GetHash(), n));
         [ #  # ][ #  # ]
     118                 :          0 :     return result;
     119         [ #  # ]:          0 : }
     120                 :            : 
     121                 :          0 : WalletTxOut MakeWalletTxOut(const CWallet& wallet,
     122                 :            :     const COutput& output) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
     123                 :            : {
     124                 :          0 :     WalletTxOut result;
     125         [ #  # ]:          0 :     result.txout = output.txout;
     126                 :          0 :     result.time = output.time;
     127                 :          0 :     result.depth_in_main_chain = output.depth;
     128         [ #  # ]:          0 :     result.is_spent = wallet.IsSpent(output.outpoint);
     129                 :          0 :     return result;
     130         [ #  # ]:          0 : }
     131                 :            : 
     132                 :          0 : class WalletImpl : public Wallet
     133                 :            : {
     134                 :            : public:
     135                 :          0 :     explicit WalletImpl(WalletContext& context, const std::shared_ptr<CWallet>& wallet) : m_context(context), m_wallet(wallet) {}
     136                 :            : 
     137                 :          0 :     bool encryptWallet(const SecureString& wallet_passphrase) override
     138                 :            :     {
     139                 :          0 :         return m_wallet->EncryptWallet(wallet_passphrase);
     140                 :            :     }
     141                 :          0 :     bool isCrypted() override { return m_wallet->IsCrypted(); }
     142                 :          0 :     bool lock() override { return m_wallet->Lock(); }
     143                 :          0 :     bool unlock(const SecureString& wallet_passphrase) override { return m_wallet->Unlock(wallet_passphrase); }
     144                 :          0 :     bool isLocked() override { return m_wallet->IsLocked(); }
     145                 :          0 :     bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
     146                 :            :         const SecureString& new_wallet_passphrase) override
     147                 :            :     {
     148                 :          0 :         return m_wallet->ChangeWalletPassphrase(old_wallet_passphrase, new_wallet_passphrase);
     149                 :            :     }
     150                 :          0 :     void abortRescan() override { m_wallet->AbortRescan(); }
     151                 :          0 :     bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); }
     152                 :          0 :     std::string getWalletName() override { return m_wallet->GetName(); }
     153                 :          0 :     util::Result<CTxDestination> getNewDestination(const OutputType type, const std::string& label) override
     154                 :            :     {
     155                 :          0 :         LOCK(m_wallet->cs_wallet);
     156 [ #  # ][ #  # ]:          0 :         return m_wallet->GetNewDestination(type, label);
     157                 :          0 :     }
     158                 :          0 :     bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override
     159                 :            :     {
     160                 :          0 :         std::unique_ptr<SigningProvider> provider = m_wallet->GetSolvingProvider(script);
     161         [ #  # ]:          0 :         if (provider) {
     162         [ #  # ]:          0 :             return provider->GetPubKey(address, pub_key);
     163         [ +  - ]:          2 :         }
     164         [ +  - ]:          2 :         return false;
     165         [ +  - ]:          2 :     }
     166         [ +  - ]:          2 :     SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) override
     167         [ +  - ]:          2 :     {
     168         [ +  - ]:          2 :         return m_wallet->SignMessage(message, pkhash, str_sig);
     169         [ +  - ]:          2 :     }
     170         [ +  - ]:          2 :     bool isSpendable(const CTxDestination& dest) override
     171                 :            :     {
     172                 :          0 :         LOCK(m_wallet->cs_wallet);
     173         [ #  # ]:          0 :         return m_wallet->IsMine(dest) & ISMINE_SPENDABLE;
     174                 :          0 :     }
     175                 :          0 :     bool haveWatchOnly() override
     176                 :            :     {
     177                 :          0 :         auto spk_man = m_wallet->GetLegacyScriptPubKeyMan();
     178         [ #  # ]:          0 :         if (spk_man) {
     179                 :          0 :             return spk_man->HaveWatchOnly();
     180                 :            :         }
     181                 :          0 :         return false;
     182                 :          0 :     };
     183                 :          0 :     bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::optional<AddressPurpose>& purpose) override
     184                 :            :     {
     185                 :          0 :         return m_wallet->SetAddressBook(dest, name, purpose);
     186                 :            :     }
     187                 :          0 :     bool delAddressBook(const CTxDestination& dest) override
     188                 :            :     {
     189                 :          0 :         return m_wallet->DelAddressBook(dest);
     190                 :            :     }
     191                 :          0 :     bool getAddress(const CTxDestination& dest,
     192                 :            :         std::string* name,
     193                 :            :         isminetype* is_mine,
     194                 :            :         AddressPurpose* purpose) override
     195                 :            :     {
     196                 :          0 :         LOCK(m_wallet->cs_wallet);
     197         [ #  # ]:          0 :         const auto& entry = m_wallet->FindAddressBookEntry(dest, /*allow_change=*/false);
     198         [ #  # ]:          0 :         if (!entry) return false; // addr not found
     199         [ #  # ]:          0 :         if (name) {
     200         [ #  # ]:          0 :             *name = entry->GetLabel();
     201                 :          0 :         }
     202                 :          0 :         std::optional<isminetype> dest_is_mine;
     203 [ #  # ][ #  # ]:          0 :         if (is_mine || purpose) {
     204         [ #  # ]:          0 :             dest_is_mine = m_wallet->IsMine(dest);
     205                 :          0 :         }
     206         [ #  # ]:          0 :         if (is_mine) {
     207                 :          0 :             *is_mine = *dest_is_mine;
     208                 :          0 :         }
     209         [ #  # ]:          0 :         if (purpose) {
     210                 :            :             // In very old wallets, address purpose may not be recorded so we derive it from IsMine
     211         [ #  # ]:          0 :             *purpose = entry->purpose.value_or(*dest_is_mine ? AddressPurpose::RECEIVE : AddressPurpose::SEND);
     212                 :          0 :         }
     213                 :          0 :         return true;
     214                 :          0 :     }
     215                 :          0 :     std::vector<WalletAddress> getAddresses() const override
     216                 :            :     {
     217                 :          0 :         LOCK(m_wallet->cs_wallet);
     218                 :          0 :         std::vector<WalletAddress> result;
     219         [ #  # ]:          0 :         m_wallet->ForEachAddrBookEntry([&](const CTxDestination& dest, const std::string& label, bool is_change, const std::optional<AddressPurpose>& purpose) EXCLUSIVE_LOCKS_REQUIRED(m_wallet->cs_wallet) {
     220         [ #  # ]:          0 :             if (is_change) return;
     221                 :          0 :             isminetype is_mine = m_wallet->IsMine(dest);
     222                 :            :             // In very old wallets, address purpose may not be recorded so we derive it from IsMine
     223                 :          0 :             result.emplace_back(dest, is_mine, purpose.value_or(is_mine ? AddressPurpose::RECEIVE : AddressPurpose::SEND), label);
     224                 :          0 :         });
     225                 :          0 :         return result;
     226         [ #  # ]:          0 :     }
     227                 :          0 :     std::vector<std::string> getAddressReceiveRequests() override {
     228                 :          0 :         LOCK(m_wallet->cs_wallet);
     229         [ #  # ]:          0 :         return m_wallet->GetAddressReceiveRequests();
     230                 :          0 :     }
     231                 :          0 :     bool setAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& value) override {
     232                 :            :         // Note: The setAddressReceiveRequest interface used by the GUI to store
     233                 :            :         // receive requests is a little awkward and could be improved in the
     234                 :            :         // future:
     235                 :            :         //
     236                 :            :         // - The same method is used to save requests and erase them, but
     237                 :            :         //   having separate methods could be clearer and prevent bugs.
     238                 :            :         //
     239                 :            :         // - Request ids are passed as strings even though they are generated as
     240                 :            :         //   integers.
     241                 :            :         //
     242                 :            :         // - Multiple requests can be stored for the same address, but it might
     243                 :            :         //   be better to only allow one request or only keep the current one.
     244                 :          0 :         LOCK(m_wallet->cs_wallet);
     245 [ #  # ][ #  # ]:          0 :         WalletBatch batch{m_wallet->GetDatabase()};
     246 [ #  # ][ #  # ]:          0 :         return value.empty() ? m_wallet->EraseAddressReceiveRequest(batch, dest, id)
     247         [ #  # ]:          0 :                              : m_wallet->SetAddressReceiveRequest(batch, dest, id, value);
     248                 :          0 :     }
     249                 :          0 :     bool displayAddress(const CTxDestination& dest) override
     250                 :            :     {
     251                 :          0 :         LOCK(m_wallet->cs_wallet);
     252         [ #  # ]:          0 :         return m_wallet->DisplayAddress(dest);
     253                 :          0 :     }
     254                 :          0 :     bool lockCoin(const COutPoint& output, const bool write_to_db) override
     255                 :            :     {
     256                 :          0 :         LOCK(m_wallet->cs_wallet);
     257 [ #  # ][ #  # ]:          0 :         std::unique_ptr<WalletBatch> batch = write_to_db ? std::make_unique<WalletBatch>(m_wallet->GetDatabase()) : nullptr;
     258         [ #  # ]:          0 :         return m_wallet->LockCoin(output, batch.get());
     259                 :          0 :     }
     260                 :          0 :     bool unlockCoin(const COutPoint& output) override
     261                 :            :     {
     262                 :          0 :         LOCK(m_wallet->cs_wallet);
     263         [ #  # ]:          0 :         std::unique_ptr<WalletBatch> batch = std::make_unique<WalletBatch>(m_wallet->GetDatabase());
     264 [ +  - ][ #  # ]:          2 :         return m_wallet->UnlockCoin(output, batch.get());
     265                 :          0 :     }
     266                 :          0 :     bool isLockedCoin(const COutPoint& output) override
     267                 :            :     {
     268                 :          0 :         LOCK(m_wallet->cs_wallet);
     269         [ #  # ]:          0 :         return m_wallet->IsLockedCoin(output);
     270                 :          0 :     }
     271                 :          0 :     void listLockedCoins(std::vector<COutPoint>& outputs) override
     272                 :            :     {
     273                 :          0 :         LOCK(m_wallet->cs_wallet);
     274         [ #  # ]:          0 :         return m_wallet->ListLockedCoins(outputs);
     275                 :          0 :     }
     276                 :          0 :     util::Result<CTransactionRef> createTransaction(const std::vector<CRecipient>& recipients,
     277                 :            :         const CCoinControl& coin_control,
     278                 :            :         bool sign,
     279                 :            :         int& change_pos,
     280                 :            :         CAmount& fee) override
     281                 :            :     {
     282                 :          0 :         LOCK(m_wallet->cs_wallet);
     283 [ #  # ][ #  # ]:          0 :         auto res = CreateTransaction(*m_wallet, recipients, change_pos,
     284                 :          0 :                                      coin_control, sign);
     285 [ #  # ][ #  # ]:          0 :         if (!res) return util::Error{util::ErrorString(res)};
                 [ #  # ]
     286         [ #  # ]:          0 :         const auto& txr = *res;
     287                 :          0 :         fee = txr.fee;
     288                 :          0 :         change_pos = txr.change_pos;
     289                 :            : 
     290         [ #  # ]:          0 :         return txr.tx;
     291                 :          0 :     }
     292                 :          0 :     void commitTransaction(CTransactionRef tx,
     293                 :            :         WalletValueMap value_map,
     294                 :            :         WalletOrderForm order_form) override
     295                 :            :     {
     296                 :          0 :         LOCK(m_wallet->cs_wallet);
     297         [ #  # ]:          0 :         m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form));
     298                 :          0 :     }
     299                 :          0 :     bool transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); }
     300                 :          0 :     bool abandonTransaction(const uint256& txid) override
     301                 :            :     {
     302                 :          0 :         LOCK(m_wallet->cs_wallet);
     303         [ #  # ]:          0 :         return m_wallet->AbandonTransaction(txid);
     304                 :          0 :     }
     305                 :          0 :     bool transactionCanBeBumped(const uint256& txid) override
     306                 :            :     {
     307                 :          0 :         return feebumper::TransactionCanBeBumped(*m_wallet.get(), txid);
     308                 :            :     }
     309                 :          0 :     bool createBumpTransaction(const uint256& txid,
     310                 :            :         const CCoinControl& coin_control,
     311                 :            :         std::vector<bilingual_str>& errors,
     312                 :            :         CAmount& old_fee,
     313                 :            :         CAmount& new_fee,
     314                 :            :         CMutableTransaction& mtx) override
     315                 :            :     {
     316                 :          0 :         std::vector<CTxOut> outputs; // just an empty list of new recipients for now
     317         [ #  # ]:          0 :         return feebumper::CreateRateBumpTransaction(*m_wallet.get(), txid, coin_control, errors, old_fee, new_fee, mtx, /* require_mine= */ true, outputs) == feebumper::Result::OK;
     318                 :          0 :     }
     319                 :          0 :     bool signBumpTransaction(CMutableTransaction& mtx) override { return feebumper::SignTransaction(*m_wallet.get(), mtx); }
     320                 :          0 :     bool commitBumpTransaction(const uint256& txid,
     321                 :            :         CMutableTransaction&& mtx,
     322                 :            :         std::vector<bilingual_str>& errors,
     323                 :            :         uint256& bumped_txid) override
     324                 :            :     {
     325                 :          0 :         return feebumper::CommitTransaction(*m_wallet.get(), txid, std::move(mtx), errors, bumped_txid) ==
     326                 :            :                feebumper::Result::OK;
     327                 :            :     }
     328                 :          0 :     CTransactionRef getTx(const uint256& txid) override
     329                 :            :     {
     330                 :          0 :         LOCK(m_wallet->cs_wallet);
     331         [ #  # ]:          0 :         auto mi = m_wallet->mapWallet.find(txid);
     332         [ #  # ]:          0 :         if (mi != m_wallet->mapWallet.end()) {
     333                 :          0 :             return mi->second.tx;
     334                 :            :         }
     335                 :          0 :         return {};
     336                 :          0 :     }
     337                 :          0 :     WalletTx getWalletTx(const uint256& txid) override
     338                 :            :     {
     339                 :          0 :         LOCK(m_wallet->cs_wallet);
     340         [ #  # ]:          0 :         auto mi = m_wallet->mapWallet.find(txid);
     341         [ #  # ]:          0 :         if (mi != m_wallet->mapWallet.end()) {
     342         [ #  # ]:          0 :             return MakeWalletTx(*m_wallet, mi->second);
     343                 :            :         }
     344                 :          0 :         return {};
     345                 :          0 :     }
     346                 :          0 :     std::set<WalletTx> getWalletTxs() override
     347                 :            :     {
     348                 :          0 :         LOCK(m_wallet->cs_wallet);
     349                 :          0 :         std::set<WalletTx> result;
     350         [ #  # ]:          0 :         for (const auto& entry : m_wallet->mapWallet) {
     351 [ #  # ][ #  # ]:          0 :             result.emplace(MakeWalletTx(*m_wallet, entry.second));
     352                 :            :         }
     353                 :          0 :         return result;
     354         [ #  # ]:          0 :     }
     355                 :          0 :     bool tryGetTxStatus(const uint256& txid,
     356                 :            :         interfaces::WalletTxStatus& tx_status,
     357                 :            :         int& num_blocks,
     358                 :            :         int64_t& block_time) override
     359                 :            :     {
     360                 :          0 :         TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
     361 [ #  # ][ #  # ]:          0 :         if (!locked_wallet) {
     362                 :          0 :             return false;
     363                 :            :         }
     364         [ #  # ]:          0 :         auto mi = m_wallet->mapWallet.find(txid);
     365         [ #  # ]:          0 :         if (mi == m_wallet->mapWallet.end()) {
     366                 :          0 :             return false;
     367                 :            :         }
     368         [ #  # ]:          0 :         num_blocks = m_wallet->GetLastBlockHeight();
     369                 :          0 :         block_time = -1;
     370 [ #  # ][ #  # ]:          0 :         CHECK_NONFATAL(m_wallet->chain().findBlock(m_wallet->GetLastBlockHash(), FoundBlock().time(block_time)));
         [ #  # ][ #  # ]
                 [ #  # ]
     371         [ #  # ]:          0 :         tx_status = MakeWalletTxStatus(*m_wallet, mi->second);
     372                 :          0 :         return true;
     373                 :          0 :     }
     374                 :          0 :     WalletTx getWalletTxDetails(const uint256& txid,
     375                 :            :         WalletTxStatus& tx_status,
     376                 :            :         WalletOrderForm& order_form,
     377                 :            :         bool& in_mempool,
     378                 :            :         int& num_blocks) override
     379                 :            :     {
     380                 :          0 :         LOCK(m_wallet->cs_wallet);
     381         [ #  # ]:          0 :         auto mi = m_wallet->mapWallet.find(txid);
     382         [ #  # ]:          0 :         if (mi != m_wallet->mapWallet.end()) {
     383         [ #  # ]:          0 :             num_blocks = m_wallet->GetLastBlockHeight();
     384         [ #  # ]:          0 :             in_mempool = mi->second.InMempool();
     385         [ #  # ]:          0 :             order_form = mi->second.vOrderForm;
     386         [ #  # ]:          0 :             tx_status = MakeWalletTxStatus(*m_wallet, mi->second);
     387         [ #  # ]:          0 :             return MakeWalletTx(*m_wallet, mi->second);
     388                 :            :         }
     389                 :          0 :         return {};
     390                 :          0 :     }
     391                 :          0 :     TransactionError fillPSBT(int sighash_type,
     392                 :            :         bool sign,
     393                 :            :         bool bip32derivs,
     394                 :            :         size_t* n_signed,
     395                 :            :         PartiallySignedTransaction& psbtx,
     396                 :            :         bool& complete) override
     397                 :            :     {
     398                 :          0 :         return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign, bip32derivs, n_signed);
     399                 :            :     }
     400                 :          0 :     WalletBalances getBalances() override
     401                 :            :     {
     402                 :          0 :         const auto bal = GetBalance(*m_wallet);
     403                 :          0 :         WalletBalances result;
     404                 :          0 :         result.balance = bal.m_mine_trusted;
     405                 :          0 :         result.unconfirmed_balance = bal.m_mine_untrusted_pending;
     406                 :          0 :         result.immature_balance = bal.m_mine_immature;
     407                 :          0 :         result.have_watch_only = haveWatchOnly();
     408         [ #  # ]:          0 :         if (result.have_watch_only) {
     409                 :          0 :             result.watch_only_balance = bal.m_watchonly_trusted;
     410                 :          0 :             result.unconfirmed_watch_only_balance = bal.m_watchonly_untrusted_pending;
     411                 :          0 :             result.immature_watch_only_balance = bal.m_watchonly_immature;
     412                 :          0 :         }
     413                 :          0 :         return result;
     414                 :            :     }
     415                 :          0 :     bool tryGetBalances(WalletBalances& balances, uint256& block_hash) override
     416                 :            :     {
     417                 :          0 :         TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
     418         [ #  # ]:          0 :         if (!locked_wallet) {
     419                 :          0 :             return false;
     420                 :            :         }
     421                 :          0 :         block_hash = m_wallet->GetLastBlockHash();
     422         [ #  # ]:          0 :         balances = getBalances();
     423                 :          0 :         return true;
     424                 :          0 :     }
     425                 :          0 :     CAmount getBalance() override { return GetBalance(*m_wallet).m_mine_trusted; }
     426                 :          0 :     CAmount getAvailableBalance(const CCoinControl& coin_control) override
     427                 :            :     {
     428                 :          0 :         LOCK(m_wallet->cs_wallet);
     429                 :          0 :         CAmount total_amount = 0;
     430                 :            :         // Fetch selected coins total amount
     431 [ #  # ][ #  # ]:          0 :         if (coin_control.HasSelected()) {
     432                 :          0 :             FastRandomContext rng{};
     433         [ #  # ]:          0 :             CoinSelectionParams params(rng);
     434                 :            :             // Note: for now, swallow any error.
     435 [ #  # ][ #  # ]:          0 :             if (auto res = FetchSelectedInputs(*m_wallet, coin_control, params)) {
     436         [ #  # ]:          0 :                 total_amount += res->total_amount;
     437                 :          0 :             }
     438                 :          0 :         }
     439                 :            : 
     440                 :            :         // And fetch the wallet available coins
     441         [ #  # ]:          0 :         if (coin_control.m_allow_other_inputs) {
     442 [ #  # ][ #  # ]:          0 :             total_amount += AvailableCoins(*m_wallet, &coin_control).GetTotalAmount();
     443                 :          0 :         }
     444                 :            : 
     445                 :          0 :         return total_amount;
     446                 :          0 :     }
     447                 :          0 :     isminetype txinIsMine(const CTxIn& txin) override
     448                 :            :     {
     449                 :          0 :         LOCK(m_wallet->cs_wallet);
     450         [ #  # ]:          0 :         return InputIsMine(*m_wallet, txin);
     451                 :          0 :     }
     452                 :          0 :     isminetype txoutIsMine(const CTxOut& txout) override
     453                 :            :     {
     454                 :          0 :         LOCK(m_wallet->cs_wallet);
     455         [ #  # ]:          0 :         return m_wallet->IsMine(txout);
     456                 :          0 :     }
     457                 :          0 :     CAmount getDebit(const CTxIn& txin, isminefilter filter) override
     458                 :            :     {
     459                 :          0 :         LOCK(m_wallet->cs_wallet);
     460         [ #  # ]:          0 :         return m_wallet->GetDebit(txin, filter);
     461                 :          0 :     }
     462                 :          0 :     CAmount getCredit(const CTxOut& txout, isminefilter filter) override
     463                 :            :     {
     464                 :          0 :         LOCK(m_wallet->cs_wallet);
     465         [ #  # ]:          0 :         return OutputGetCredit(*m_wallet, txout, filter);
     466                 :          0 :     }
     467                 :          0 :     CoinsList listCoins() override
     468                 :            :     {
     469                 :          0 :         LOCK(m_wallet->cs_wallet);
     470                 :          0 :         CoinsList result;
     471 [ #  # ][ #  # ]:          0 :         for (const auto& entry : ListCoins(*m_wallet)) {
     472         [ #  # ]:          0 :             auto& group = result[entry.first];
     473         [ #  # ]:          0 :             for (const auto& coin : entry.second) {
     474         [ #  # ]:          0 :                 group.emplace_back(coin.outpoint,
     475         [ #  # ]:          0 :                     MakeWalletTxOut(*m_wallet, coin));
     476                 :            :             }
     477                 :            :         }
     478                 :          0 :         return result;
     479         [ #  # ]:          0 :     }
     480                 :          0 :     std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) override
     481                 :            :     {
     482                 :          0 :         LOCK(m_wallet->cs_wallet);
     483                 :          0 :         std::vector<WalletTxOut> result;
     484         [ #  # ]:          0 :         result.reserve(outputs.size());
     485         [ #  # ]:          0 :         for (const auto& output : outputs) {
     486         [ #  # ]:          0 :             result.emplace_back();
     487         [ #  # ]:          0 :             auto it = m_wallet->mapWallet.find(output.hash);
     488         [ #  # ]:          0 :             if (it != m_wallet->mapWallet.end()) {
     489         [ #  # ]:          0 :                 int depth = m_wallet->GetTxDepthInMainChain(it->second);
     490         [ #  # ]:          0 :                 if (depth >= 0) {
     491         [ #  # ]:          0 :                     result.back() = MakeWalletTxOut(*m_wallet, it->second, output.n, depth);
     492                 :          0 :                 }
     493                 :          0 :             }
     494                 :            :         }
     495                 :          0 :         return result;
     496         [ #  # ]:          0 :     }
     497                 :          0 :     CAmount getRequiredFee(unsigned int tx_bytes) override { return GetRequiredFee(*m_wallet, tx_bytes); }
     498                 :          0 :     CAmount getMinimumFee(unsigned int tx_bytes,
     499                 :            :         const CCoinControl& coin_control,
     500                 :            :         int* returned_target,
     501                 :            :         FeeReason* reason) override
     502                 :            :     {
     503                 :          0 :         FeeCalculation fee_calc;
     504                 :            :         CAmount result;
     505                 :          0 :         result = GetMinimumFee(*m_wallet, tx_bytes, coin_control, &fee_calc);
     506         [ #  # ]:          0 :         if (returned_target) *returned_target = fee_calc.returnedTarget;
     507         [ #  # ]:          0 :         if (reason) *reason = fee_calc.reason;
     508                 :          0 :         return result;
     509                 :            :     }
     510                 :          0 :     unsigned int getConfirmTarget() override { return m_wallet->m_confirm_target; }
     511                 :          0 :     bool hdEnabled() override { return m_wallet->IsHDEnabled(); }
     512                 :          0 :     bool canGetAddresses() override { return m_wallet->CanGetAddresses(); }
     513                 :          0 :     bool hasExternalSigner() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_EXTERNAL_SIGNER); }
     514                 :          0 :     bool privateKeysDisabled() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS); }
     515                 :          0 :     bool taprootEnabled() override {
     516         [ #  # ]:          0 :         if (m_wallet->IsLegacy()) return false;
     517                 :          0 :         auto spk_man = m_wallet->GetScriptPubKeyMan(OutputType::BECH32M, /*internal=*/false);
     518                 :          0 :         return spk_man != nullptr;
     519                 :          0 :     }
     520                 :          0 :     OutputType getDefaultAddressType() override { return m_wallet->m_default_address_type; }
     521                 :          0 :     CAmount getDefaultMaxTxFee() override { return m_wallet->m_default_max_tx_fee; }
     522                 :          0 :     void remove() override
     523                 :            :     {
     524                 :          0 :         RemoveWallet(m_context, m_wallet, /*load_on_start=*/false);
     525                 :          0 :     }
     526                 :          0 :     bool isLegacy() override { return m_wallet->IsLegacy(); }
     527                 :          0 :     std::unique_ptr<Handler> handleUnload(UnloadFn fn) override
     528                 :            :     {
     529 [ #  # ][ #  # ]:          0 :         return MakeSignalHandler(m_wallet->NotifyUnload.connect(fn));
     530                 :          0 :     }
     531                 :          0 :     std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
     532                 :            :     {
     533 [ #  # ][ #  # ]:          0 :         return MakeSignalHandler(m_wallet->ShowProgress.connect(fn));
     534                 :          0 :     }
     535                 :          0 :     std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) override
     536                 :            :     {
     537 [ #  # ][ #  # ]:          0 :         return MakeSignalHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); }));
                 [ #  # ]
     538                 :          0 :     }
     539                 :          0 :     std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) override
     540                 :            :     {
     541 [ #  # ][ #  # ]:          0 :         return MakeSignalHandler(m_wallet->NotifyAddressBookChanged.connect(
     542         [ #  # ]:          0 :             [fn](const CTxDestination& address, const std::string& label, bool is_mine,
     543                 :          0 :                  AddressPurpose purpose, ChangeType status) { fn(address, label, is_mine, purpose, status); }));
     544                 :          0 :     }
     545                 :          0 :     std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) override
     546                 :            :     {
     547 [ #  # ][ #  # ]:          0 :         return MakeSignalHandler(m_wallet->NotifyTransactionChanged.connect(
     548         [ #  # ]:          0 :             [fn](const uint256& txid, ChangeType status) { fn(txid, status); }));
     549                 :          0 :     }
     550                 :          0 :     std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) override
     551                 :            :     {
     552 [ #  # ][ #  # ]:          0 :         return MakeSignalHandler(m_wallet->NotifyWatchonlyChanged.connect(fn));
     553                 :          0 :     }
     554                 :          0 :     std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) override
     555                 :            :     {
     556 [ #  # ][ #  # ]:          0 :         return MakeSignalHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn));
     557                 :          0 :     }
     558                 :          0 :     CWallet* wallet() override { return m_wallet.get(); }
     559                 :            : 
     560                 :            :     WalletContext& m_context;
     561                 :            :     std::shared_ptr<CWallet> m_wallet;
     562                 :            : };
     563                 :            : 
     564                 :            : class WalletLoaderImpl : public WalletLoader
     565                 :            : {
     566                 :            : public:
     567 [ #  # ][ #  # ]:          0 :     WalletLoaderImpl(Chain& chain, ArgsManager& args)
     568                 :          0 :     {
     569                 :          0 :         m_context.chain = &chain;
     570                 :          0 :         m_context.args = &args;
     571                 :          0 :     }
     572         [ #  # ]:          0 :     ~WalletLoaderImpl() override { UnloadWallets(m_context); }
     573                 :            : 
     574                 :            :     //! ChainClient methods
     575                 :          0 :     void registerRpcs() override
     576                 :            :     {
     577         [ #  # ]:          0 :         for (const CRPCCommand& command : GetWalletRPCCommands()) {
     578                 :          0 :             m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
     579                 :          0 :                 JSONRPCRequest wallet_request = request;
     580         [ #  # ]:          0 :                 wallet_request.context = &m_context;
     581         [ #  # ]:          0 :                 return command.actor(wallet_request, result, last_handler);
     582                 :          0 :             }, command.argNames, command.unique_id);
     583         [ #  # ]:          0 :             m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back()));
     584                 :            :         }
     585                 :          0 :     }
     586                 :          0 :     bool verify() override { return VerifyWallets(m_context); }
     587                 :          0 :     bool load() override { return LoadWallets(m_context); }
     588                 :          0 :     void start(CScheduler& scheduler) override { return StartWallets(m_context, scheduler); }
     589                 :          0 :     void flush() override { return FlushWallets(m_context); }
     590                 :          0 :     void stop() override { return StopWallets(m_context); }
     591                 :          0 :     void setMockTime(int64_t time) override { return SetMockTime(time); }
     592                 :            : 
     593                 :            :     //! WalletLoader methods
     594                 :          0 :     util::Result<std::unique_ptr<Wallet>> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, std::vector<bilingual_str>& warnings) override
     595                 :            :     {
     596                 :          0 :         DatabaseOptions options;
     597                 :            :         DatabaseStatus status;
     598         [ #  # ]:          0 :         ReadDatabaseArgs(*m_context.args, options);
     599                 :          0 :         options.require_create = true;
     600                 :          0 :         options.create_flags = wallet_creation_flags;
     601         [ #  # ]:          0 :         options.create_passphrase = passphrase;
     602                 :          0 :         bilingual_str error;
     603 [ #  # ][ #  # ]:          0 :         std::unique_ptr<Wallet> wallet{MakeWallet(m_context, CreateWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))};
     604         [ #  # ]:          0 :         if (wallet) {
     605         [ #  # ]:          0 :             return wallet;
     606                 :            :         } else {
     607 [ #  # ][ #  # ]:          0 :             return util::Error{error};
     608                 :            :         }
     609                 :          0 :     }
     610                 :          0 :     util::Result<std::unique_ptr<Wallet>> loadWallet(const std::string& name, std::vector<bilingual_str>& warnings) override
     611                 :            :     {
     612                 :          0 :         DatabaseOptions options;
     613                 :            :         DatabaseStatus status;
     614         [ #  # ]:          0 :         ReadDatabaseArgs(*m_context.args, options);
     615                 :          0 :         options.require_existing = true;
     616                 :          0 :         bilingual_str error;
     617 [ #  # ][ #  # ]:          0 :         std::unique_ptr<Wallet> wallet{MakeWallet(m_context, LoadWallet(m_context, name, /*load_on_start=*/true, options, status, error, warnings))};
     618         [ #  # ]:          0 :         if (wallet) {
     619         [ #  # ]:          0 :             return wallet;
     620                 :            :         } else {
     621 [ #  # ][ #  # ]:          0 :             return util::Error{error};
     622                 :            :         }
     623                 :          0 :     }
     624                 :          0 :     util::Result<std::unique_ptr<Wallet>> restoreWallet(const fs::path& backup_file, const std::string& wallet_name, std::vector<bilingual_str>& warnings) override
     625                 :            :     {
     626                 :            :         DatabaseStatus status;
     627                 :          0 :         bilingual_str error;
     628 [ #  # ][ #  # ]:          0 :         std::unique_ptr<Wallet> wallet{MakeWallet(m_context, RestoreWallet(m_context, backup_file, wallet_name, /*load_on_start=*/true, status, error, warnings))};
     629         [ #  # ]:          0 :         if (wallet) {
     630         [ #  # ]:          0 :             return wallet;
     631                 :            :         } else {
     632 [ #  # ][ #  # ]:          0 :             return util::Error{error};
     633                 :            :         }
     634                 :          0 :     }
     635                 :          0 :     util::Result<WalletMigrationResult> migrateWallet(const std::string& name, const SecureString& passphrase) override
     636                 :            :     {
     637                 :          0 :         auto res = wallet::MigrateLegacyToDescriptor(name, passphrase, m_context);
     638 [ #  # ][ #  # ]:          0 :         if (!res) return util::Error{util::ErrorString(res)};
                 [ #  # ]
     639                 :          0 :         WalletMigrationResult out{
     640 [ #  # ][ #  # ]:          0 :             .wallet = MakeWallet(m_context, res->wallet),
     641 [ #  # ][ #  # ]:          0 :             .watchonly_wallet_name = res->watchonly_wallet ? std::make_optional(res->watchonly_wallet->GetName()) : std::nullopt,
         [ #  # ][ #  # ]
     642 [ #  # ][ #  # ]:          0 :             .solvables_wallet_name = res->solvables_wallet ? std::make_optional(res->solvables_wallet->GetName()) : std::nullopt,
         [ #  # ][ #  # ]
     643 [ #  # ][ #  # ]:          0 :             .backup_path = res->backup_path,
     644                 :            :         };
     645         [ #  # ]:          0 :         return out;
     646                 :          0 :     }
     647                 :          0 :     std::string getWalletDir() override
     648                 :            :     {
     649         [ #  # ]:          0 :         return fs::PathToString(GetWalletDir());
     650                 :          0 :     }
     651                 :          0 :     std::vector<std::string> listWalletDir() override
     652                 :            :     {
     653                 :          0 :         std::vector<std::string> paths;
     654 [ #  # ][ #  # ]:          0 :         for (auto& path : ListDatabases(GetWalletDir())) {
                 [ #  # ]
     655 [ #  # ][ #  # ]:          0 :             paths.push_back(fs::PathToString(path));
     656                 :            :         }
     657                 :          0 :         return paths;
     658         [ #  # ]:          0 :     }
     659                 :          0 :     std::vector<std::unique_ptr<Wallet>> getWallets() override
     660                 :            :     {
     661                 :          0 :         std::vector<std::unique_ptr<Wallet>> wallets;
     662 [ #  # ][ #  # ]:          0 :         for (const auto& wallet : GetWallets(m_context)) {
     663 [ #  # ][ #  # ]:          0 :             wallets.emplace_back(MakeWallet(m_context, wallet));
     664                 :            :         }
     665                 :          0 :         return wallets;
     666         [ #  # ]:          0 :     }
     667                 :          0 :     std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override
     668                 :            :     {
     669         [ #  # ]:          0 :         return HandleLoadWallet(m_context, std::move(fn));
     670                 :          0 :     }
     671                 :          0 :     WalletContext* context() override  { return &m_context; }
     672                 :            : 
     673                 :            :     WalletContext m_context;
     674                 :            :     const std::vector<std::string> m_wallet_filenames;
     675                 :            :     std::vector<std::unique_ptr<Handler>> m_rpc_handlers;
     676                 :            :     std::list<CRPCCommand> m_rpc_commands;
     677                 :            : };
     678                 :            : } // namespace
     679                 :            : } // namespace wallet
     680                 :            : 
     681                 :            : namespace interfaces {
     682         [ #  # ]:          0 : std::unique_ptr<Wallet> MakeWallet(wallet::WalletContext& context, const std::shared_ptr<wallet::CWallet>& wallet) { return wallet ? std::make_unique<wallet::WalletImpl>(context, wallet) : nullptr; }
     683                 :            : 
     684                 :          0 : std::unique_ptr<WalletLoader> MakeWalletLoader(Chain& chain, ArgsManager& args)
     685                 :            : {
     686                 :          0 :     return std::make_unique<wallet::WalletLoaderImpl>(chain, args);
     687                 :            : }
     688                 :            : } // namespace interfaces

Generated by: LCOV version 1.14