LCOV - code coverage report
Current view: top level - src/wallet - interfaces.cpp (source / functions) Coverage Total Hit
Test: fuzz_coverage.info Lines: 0.0 % 420 0
Test Date: 2025-08-01 04:15:35 Functions: 0.0 % 95 0
Branches: 0.0 % 450 0

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

Generated by: LCOV version 2.0-1