LCOV - code coverage report
Current view: top level - src/wallet - interfaces.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 6.5 % 414 27
Test Date: 2025-08-25 05:11:47 Functions: 14.7 % 95 14
Branches: 1.8 % 448 8

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

Generated by: LCOV version 2.0-1