Branch data Line data Source code
# 1 : : // Copyright (c) 2018-2020 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 <amount.h>
# 8 : : #include <interfaces/chain.h>
# 9 : : #include <interfaces/handler.h>
# 10 : : #include <policy/fees.h>
# 11 : : #include <primitives/transaction.h>
# 12 : : #include <rpc/server.h>
# 13 : : #include <script/standard.h>
# 14 : : #include <support/allocators/secure.h>
# 15 : : #include <sync.h>
# 16 : : #include <uint256.h>
# 17 : : #include <util/check.h>
# 18 : : #include <util/system.h>
# 19 : : #include <util/ui_change_type.h>
# 20 : : #include <wallet/context.h>
# 21 : : #include <wallet/feebumper.h>
# 22 : : #include <wallet/fees.h>
# 23 : : #include <wallet/ismine.h>
# 24 : : #include <wallet/load.h>
# 25 : : #include <wallet/rpcwallet.h>
# 26 : : #include <wallet/wallet.h>
# 27 : :
# 28 : : #include <memory>
# 29 : : #include <string>
# 30 : : #include <utility>
# 31 : : #include <vector>
# 32 : :
# 33 : : using interfaces::Chain;
# 34 : : using interfaces::FoundBlock;
# 35 : : using interfaces::Handler;
# 36 : : using interfaces::MakeHandler;
# 37 : : using interfaces::Wallet;
# 38 : : using interfaces::WalletAddress;
# 39 : : using interfaces::WalletBalances;
# 40 : : using interfaces::WalletClient;
# 41 : : using interfaces::WalletOrderForm;
# 42 : : using interfaces::WalletTx;
# 43 : : using interfaces::WalletTxOut;
# 44 : : using interfaces::WalletTxStatus;
# 45 : : using interfaces::WalletValueMap;
# 46 : :
# 47 : : namespace wallet {
# 48 : : namespace {
# 49 : : //! Construct wallet tx struct.
# 50 : : WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
# 51 : 0 : {
# 52 : 0 : LOCK(wallet.cs_wallet);
# 53 : 0 : WalletTx result;
# 54 : 0 : result.tx = wtx.tx;
# 55 : 0 : result.txin_is_mine.reserve(wtx.tx->vin.size());
# 56 [ # # ]: 0 : for (const auto& txin : wtx.tx->vin) {
# 57 : 0 : result.txin_is_mine.emplace_back(wallet.IsMine(txin));
# 58 : 0 : }
# 59 : 0 : result.txout_is_mine.reserve(wtx.tx->vout.size());
# 60 : 0 : result.txout_address.reserve(wtx.tx->vout.size());
# 61 : 0 : result.txout_address_is_mine.reserve(wtx.tx->vout.size());
# 62 [ # # ]: 0 : for (const auto& txout : wtx.tx->vout) {
# 63 : 0 : result.txout_is_mine.emplace_back(wallet.IsMine(txout));
# 64 : 0 : result.txout_address.emplace_back();
# 65 [ # # ]: 0 : result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ?
# 66 : 0 : wallet.IsMine(result.txout_address.back()) :
# 67 : 0 : ISMINE_NO);
# 68 : 0 : }
# 69 : 0 : result.credit = wtx.GetCredit(ISMINE_ALL);
# 70 : 0 : result.debit = wtx.GetDebit(ISMINE_ALL);
# 71 : 0 : result.change = wtx.GetChange();
# 72 : 0 : result.time = wtx.GetTxTime();
# 73 : 0 : result.value_map = wtx.mapValue;
# 74 : 0 : result.is_coinbase = wtx.IsCoinBase();
# 75 : 0 : return result;
# 76 : 0 : }
# 77 : :
# 78 : : //! Construct wallet tx status struct.
# 79 : : WalletTxStatus MakeWalletTxStatus(const CWallet& wallet, const CWalletTx& wtx)
# 80 : 0 : {
# 81 : 0 : WalletTxStatus result;
# 82 [ # # ]: 0 : result.block_height = wtx.m_confirm.block_height > 0 ? wtx.m_confirm.block_height : std::numeric_limits<int>::max();
# 83 : 0 : result.blocks_to_maturity = wtx.GetBlocksToMaturity();
# 84 : 0 : result.depth_in_main_chain = wtx.GetDepthInMainChain();
# 85 : 0 : result.time_received = wtx.nTimeReceived;
# 86 : 0 : result.lock_time = wtx.tx->nLockTime;
# 87 : 0 : result.is_final = wallet.chain().checkFinalTx(*wtx.tx);
# 88 : 0 : result.is_trusted = wtx.IsTrusted();
# 89 : 0 : result.is_abandoned = wtx.isAbandoned();
# 90 : 0 : result.is_coinbase = wtx.IsCoinBase();
# 91 : 0 : result.is_in_main_chain = wtx.IsInMainChain();
# 92 : 0 : return result;
# 93 : 0 : }
# 94 : :
# 95 : : //! Construct wallet TxOut struct.
# 96 : : WalletTxOut MakeWalletTxOut(const CWallet& wallet,
# 97 : : const CWalletTx& wtx,
# 98 : : int n,
# 99 : : int depth) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
# 100 : 0 : {
# 101 : 0 : WalletTxOut result;
# 102 : 0 : result.txout = wtx.tx->vout[n];
# 103 : 0 : result.time = wtx.GetTxTime();
# 104 : 0 : result.depth_in_main_chain = depth;
# 105 : 0 : result.is_spent = wallet.IsSpent(wtx.GetHash(), n);
# 106 : 0 : return result;
# 107 : 0 : }
# 108 : :
# 109 : : class WalletImpl : public Wallet
# 110 : : {
# 111 : : public:
# 112 : 1 : explicit WalletImpl(const std::shared_ptr<CWallet>& wallet) : m_wallet(wallet) {}
# 113 : :
# 114 : : bool encryptWallet(const SecureString& wallet_passphrase) override
# 115 : 0 : {
# 116 : 0 : return m_wallet->EncryptWallet(wallet_passphrase);
# 117 : 0 : }
# 118 : 0 : bool isCrypted() override { return m_wallet->IsCrypted(); }
# 119 : 0 : bool lock() override { return m_wallet->Lock(); }
# 120 : 0 : bool unlock(const SecureString& wallet_passphrase) override { return m_wallet->Unlock(wallet_passphrase); }
# 121 : 0 : bool isLocked() override { return m_wallet->IsLocked(); }
# 122 : : bool changeWalletPassphrase(const SecureString& old_wallet_passphrase,
# 123 : : const SecureString& new_wallet_passphrase) override
# 124 : 0 : {
# 125 : 0 : return m_wallet->ChangeWalletPassphrase(old_wallet_passphrase, new_wallet_passphrase);
# 126 : 0 : }
# 127 : 0 : void abortRescan() override { m_wallet->AbortRescan(); }
# 128 : 0 : bool backupWallet(const std::string& filename) override { return m_wallet->BackupWallet(filename); }
# 129 : 0 : std::string getWalletName() override { return m_wallet->GetName(); }
# 130 : : bool getNewDestination(const OutputType type, const std::string label, CTxDestination& dest) override
# 131 : 0 : {
# 132 : 0 : LOCK(m_wallet->cs_wallet);
# 133 : 0 : std::string error;
# 134 : 0 : return m_wallet->GetNewDestination(type, label, dest, error);
# 135 : 0 : }
# 136 : : bool getPubKey(const CScript& script, const CKeyID& address, CPubKey& pub_key) override
# 137 : 0 : {
# 138 : 0 : std::unique_ptr<SigningProvider> provider = m_wallet->GetSolvingProvider(script);
# 139 [ # # ]: 0 : if (provider) {
# 140 : 0 : return provider->GetPubKey(address, pub_key);
# 141 : 0 : }
# 142 : 0 : return false;
# 143 : 0 : }
# 144 : : SigningResult signMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) override
# 145 : 0 : {
# 146 : 0 : return m_wallet->SignMessage(message, pkhash, str_sig);
# 147 : 0 : }
# 148 : : bool isSpendable(const CTxDestination& dest) override
# 149 : 0 : {
# 150 : 0 : LOCK(m_wallet->cs_wallet);
# 151 : 0 : return m_wallet->IsMine(dest) & ISMINE_SPENDABLE;
# 152 : 0 : }
# 153 : : bool haveWatchOnly() override
# 154 : 0 : {
# 155 : 0 : auto spk_man = m_wallet->GetLegacyScriptPubKeyMan();
# 156 [ # # ]: 0 : if (spk_man) {
# 157 : 0 : return spk_man->HaveWatchOnly();
# 158 : 0 : }
# 159 : 0 : return false;
# 160 : 0 : };
# 161 : : bool setAddressBook(const CTxDestination& dest, const std::string& name, const std::string& purpose) override
# 162 : 0 : {
# 163 : 0 : return m_wallet->SetAddressBook(dest, name, purpose);
# 164 : 0 : }
# 165 : : bool delAddressBook(const CTxDestination& dest) override
# 166 : 0 : {
# 167 : 0 : return m_wallet->DelAddressBook(dest);
# 168 : 0 : }
# 169 : : bool getAddress(const CTxDestination& dest,
# 170 : : std::string* name,
# 171 : : isminetype* is_mine,
# 172 : : std::string* purpose) override
# 173 : 0 : {
# 174 : 0 : LOCK(m_wallet->cs_wallet);
# 175 : 0 : auto it = m_wallet->m_address_book.find(dest);
# 176 [ # # ][ # # ]: 0 : if (it == m_wallet->m_address_book.end() || it->second.IsChange()) {
# [ # # ]
# 177 : 0 : return false;
# 178 : 0 : }
# 179 [ # # ]: 0 : if (name) {
# 180 : 0 : *name = it->second.GetLabel();
# 181 : 0 : }
# 182 [ # # ]: 0 : if (is_mine) {
# 183 : 0 : *is_mine = m_wallet->IsMine(dest);
# 184 : 0 : }
# 185 [ # # ]: 0 : if (purpose) {
# 186 : 0 : *purpose = it->second.purpose;
# 187 : 0 : }
# 188 : 0 : return true;
# 189 : 0 : }
# 190 : : std::vector<WalletAddress> getAddresses() override
# 191 : 0 : {
# 192 : 0 : LOCK(m_wallet->cs_wallet);
# 193 : 0 : std::vector<WalletAddress> result;
# 194 [ # # ]: 0 : for (const auto& item : m_wallet->m_address_book) {
# 195 [ # # ]: 0 : if (item.second.IsChange()) continue;
# 196 : 0 : result.emplace_back(item.first, m_wallet->IsMine(item.first), item.second.GetLabel(), item.second.purpose);
# 197 : 0 : }
# 198 : 0 : return result;
# 199 : 0 : }
# 200 : : bool addDestData(const CTxDestination& dest, const std::string& key, const std::string& value) override
# 201 : 0 : {
# 202 : 0 : LOCK(m_wallet->cs_wallet);
# 203 : 0 : WalletBatch batch{m_wallet->GetDatabase()};
# 204 : 0 : return m_wallet->AddDestData(batch, dest, key, value);
# 205 : 0 : }
# 206 : : bool eraseDestData(const CTxDestination& dest, const std::string& key) override
# 207 : 0 : {
# 208 : 0 : LOCK(m_wallet->cs_wallet);
# 209 : 0 : WalletBatch batch{m_wallet->GetDatabase()};
# 210 : 0 : return m_wallet->EraseDestData(batch, dest, key);
# 211 : 0 : }
# 212 : : std::vector<std::string> getDestValues(const std::string& prefix) override
# 213 : 0 : {
# 214 : 0 : LOCK(m_wallet->cs_wallet);
# 215 : 0 : return m_wallet->GetDestValues(prefix);
# 216 : 0 : }
# 217 : : void lockCoin(const COutPoint& output) override
# 218 : 0 : {
# 219 : 0 : LOCK(m_wallet->cs_wallet);
# 220 : 0 : return m_wallet->LockCoin(output);
# 221 : 0 : }
# 222 : : void unlockCoin(const COutPoint& output) override
# 223 : 0 : {
# 224 : 0 : LOCK(m_wallet->cs_wallet);
# 225 : 0 : return m_wallet->UnlockCoin(output);
# 226 : 0 : }
# 227 : : bool isLockedCoin(const COutPoint& output) override
# 228 : 0 : {
# 229 : 0 : LOCK(m_wallet->cs_wallet);
# 230 : 0 : return m_wallet->IsLockedCoin(output.hash, output.n);
# 231 : 0 : }
# 232 : : void listLockedCoins(std::vector<COutPoint>& outputs) override
# 233 : 0 : {
# 234 : 0 : LOCK(m_wallet->cs_wallet);
# 235 : 0 : return m_wallet->ListLockedCoins(outputs);
# 236 : 0 : }
# 237 : : CTransactionRef createTransaction(const std::vector<CRecipient>& recipients,
# 238 : : const CCoinControl& coin_control,
# 239 : : bool sign,
# 240 : : int& change_pos,
# 241 : : CAmount& fee,
# 242 : : bilingual_str& fail_reason) override
# 243 : 0 : {
# 244 : 0 : LOCK(m_wallet->cs_wallet);
# 245 : 0 : CTransactionRef tx;
# 246 : 0 : FeeCalculation fee_calc_out;
# 247 [ # # ]: 0 : if (!m_wallet->CreateTransaction(recipients, tx, fee, change_pos,
# 248 : 0 : fail_reason, coin_control, fee_calc_out, sign)) {
# 249 : 0 : return {};
# 250 : 0 : }
# 251 : 0 : return tx;
# 252 : 0 : }
# 253 : : void commitTransaction(CTransactionRef tx,
# 254 : : WalletValueMap value_map,
# 255 : : WalletOrderForm order_form) override
# 256 : 0 : {
# 257 : 0 : LOCK(m_wallet->cs_wallet);
# 258 : 0 : m_wallet->CommitTransaction(std::move(tx), std::move(value_map), std::move(order_form));
# 259 : 0 : }
# 260 : 0 : bool transactionCanBeAbandoned(const uint256& txid) override { return m_wallet->TransactionCanBeAbandoned(txid); }
# 261 : : bool abandonTransaction(const uint256& txid) override
# 262 : 0 : {
# 263 : 0 : LOCK(m_wallet->cs_wallet);
# 264 : 0 : return m_wallet->AbandonTransaction(txid);
# 265 : 0 : }
# 266 : : bool transactionCanBeBumped(const uint256& txid) override
# 267 : 0 : {
# 268 : 0 : return feebumper::TransactionCanBeBumped(*m_wallet.get(), txid);
# 269 : 0 : }
# 270 : : bool createBumpTransaction(const uint256& txid,
# 271 : : const CCoinControl& coin_control,
# 272 : : std::vector<bilingual_str>& errors,
# 273 : : CAmount& old_fee,
# 274 : : CAmount& new_fee,
# 275 : : CMutableTransaction& mtx) override
# 276 : 0 : {
# 277 : 0 : return feebumper::CreateRateBumpTransaction(*m_wallet.get(), txid, coin_control, errors, old_fee, new_fee, mtx) == feebumper::Result::OK;
# 278 : 0 : }
# 279 : 0 : bool signBumpTransaction(CMutableTransaction& mtx) override { return feebumper::SignTransaction(*m_wallet.get(), mtx); }
# 280 : : bool commitBumpTransaction(const uint256& txid,
# 281 : : CMutableTransaction&& mtx,
# 282 : : std::vector<bilingual_str>& errors,
# 283 : : uint256& bumped_txid) override
# 284 : 0 : {
# 285 : 0 : return feebumper::CommitTransaction(*m_wallet.get(), txid, std::move(mtx), errors, bumped_txid) ==
# 286 : 0 : feebumper::Result::OK;
# 287 : 0 : }
# 288 : : CTransactionRef getTx(const uint256& txid) override
# 289 : 0 : {
# 290 : 0 : LOCK(m_wallet->cs_wallet);
# 291 : 0 : auto mi = m_wallet->mapWallet.find(txid);
# 292 [ # # ]: 0 : if (mi != m_wallet->mapWallet.end()) {
# 293 : 0 : return mi->second.tx;
# 294 : 0 : }
# 295 : 0 : return {};
# 296 : 0 : }
# 297 : : WalletTx getWalletTx(const uint256& txid) override
# 298 : 0 : {
# 299 : 0 : LOCK(m_wallet->cs_wallet);
# 300 : 0 : auto mi = m_wallet->mapWallet.find(txid);
# 301 [ # # ]: 0 : if (mi != m_wallet->mapWallet.end()) {
# 302 : 0 : return MakeWalletTx(*m_wallet, mi->second);
# 303 : 0 : }
# 304 : 0 : return {};
# 305 : 0 : }
# 306 : : std::vector<WalletTx> getWalletTxs() override
# 307 : 0 : {
# 308 : 0 : LOCK(m_wallet->cs_wallet);
# 309 : 0 : std::vector<WalletTx> result;
# 310 : 0 : result.reserve(m_wallet->mapWallet.size());
# 311 [ # # ]: 0 : for (const auto& entry : m_wallet->mapWallet) {
# 312 : 0 : result.emplace_back(MakeWalletTx(*m_wallet, entry.second));
# 313 : 0 : }
# 314 : 0 : return result;
# 315 : 0 : }
# 316 : : bool tryGetTxStatus(const uint256& txid,
# 317 : : interfaces::WalletTxStatus& tx_status,
# 318 : : int& num_blocks,
# 319 : : int64_t& block_time) override
# 320 : 0 : {
# 321 : 0 : TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
# 322 [ # # ]: 0 : if (!locked_wallet) {
# 323 : 0 : return false;
# 324 : 0 : }
# 325 : 0 : auto mi = m_wallet->mapWallet.find(txid);
# 326 [ # # ]: 0 : if (mi == m_wallet->mapWallet.end()) {
# 327 : 0 : return false;
# 328 : 0 : }
# 329 : 0 : num_blocks = m_wallet->GetLastBlockHeight();
# 330 : 0 : block_time = -1;
# 331 [ # # ]: 0 : CHECK_NONFATAL(m_wallet->chain().findBlock(m_wallet->GetLastBlockHash(), FoundBlock().time(block_time)));
# 332 : 0 : tx_status = MakeWalletTxStatus(*m_wallet, mi->second);
# 333 : 0 : return true;
# 334 : 0 : }
# 335 : : WalletTx getWalletTxDetails(const uint256& txid,
# 336 : : WalletTxStatus& tx_status,
# 337 : : WalletOrderForm& order_form,
# 338 : : bool& in_mempool,
# 339 : : int& num_blocks) override
# 340 : 0 : {
# 341 : 0 : LOCK(m_wallet->cs_wallet);
# 342 : 0 : auto mi = m_wallet->mapWallet.find(txid);
# 343 [ # # ]: 0 : if (mi != m_wallet->mapWallet.end()) {
# 344 : 0 : num_blocks = m_wallet->GetLastBlockHeight();
# 345 : 0 : in_mempool = mi->second.InMempool();
# 346 : 0 : order_form = mi->second.vOrderForm;
# 347 : 0 : tx_status = MakeWalletTxStatus(*m_wallet, mi->second);
# 348 : 0 : return MakeWalletTx(*m_wallet, mi->second);
# 349 : 0 : }
# 350 : 0 : return {};
# 351 : 0 : }
# 352 : : TransactionError fillPSBT(int sighash_type,
# 353 : : bool sign,
# 354 : : bool bip32derivs,
# 355 : : PartiallySignedTransaction& psbtx,
# 356 : : bool& complete,
# 357 : : size_t* n_signed) override
# 358 : 0 : {
# 359 : 0 : return m_wallet->FillPSBT(psbtx, complete, sighash_type, sign, bip32derivs, n_signed);
# 360 : 0 : }
# 361 : : WalletBalances getBalances() override
# 362 : 0 : {
# 363 : 0 : const auto bal = m_wallet->GetBalance();
# 364 : 0 : WalletBalances result;
# 365 : 0 : result.balance = bal.m_mine_trusted;
# 366 : 0 : result.unconfirmed_balance = bal.m_mine_untrusted_pending;
# 367 : 0 : result.immature_balance = bal.m_mine_immature;
# 368 : 0 : result.have_watch_only = haveWatchOnly();
# 369 [ # # ]: 0 : if (result.have_watch_only) {
# 370 : 0 : result.watch_only_balance = bal.m_watchonly_trusted;
# 371 : 0 : result.unconfirmed_watch_only_balance = bal.m_watchonly_untrusted_pending;
# 372 : 0 : result.immature_watch_only_balance = bal.m_watchonly_immature;
# 373 : 0 : }
# 374 : 0 : return result;
# 375 : 0 : }
# 376 : : bool tryGetBalances(WalletBalances& balances, uint256& block_hash) override
# 377 : 0 : {
# 378 : 0 : TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
# 379 [ # # ]: 0 : if (!locked_wallet) {
# 380 : 0 : return false;
# 381 : 0 : }
# 382 : 0 : block_hash = m_wallet->GetLastBlockHash();
# 383 : 0 : balances = getBalances();
# 384 : 0 : return true;
# 385 : 0 : }
# 386 : 0 : CAmount getBalance() override { return m_wallet->GetBalance().m_mine_trusted; }
# 387 : : CAmount getAvailableBalance(const CCoinControl& coin_control) override
# 388 : 0 : {
# 389 : 0 : return m_wallet->GetAvailableBalance(&coin_control);
# 390 : 0 : }
# 391 : : isminetype txinIsMine(const CTxIn& txin) override
# 392 : 0 : {
# 393 : 0 : LOCK(m_wallet->cs_wallet);
# 394 : 0 : return m_wallet->IsMine(txin);
# 395 : 0 : }
# 396 : : isminetype txoutIsMine(const CTxOut& txout) override
# 397 : 0 : {
# 398 : 0 : LOCK(m_wallet->cs_wallet);
# 399 : 0 : return m_wallet->IsMine(txout);
# 400 : 0 : }
# 401 : : CAmount getDebit(const CTxIn& txin, isminefilter filter) override
# 402 : 0 : {
# 403 : 0 : LOCK(m_wallet->cs_wallet);
# 404 : 0 : return m_wallet->GetDebit(txin, filter);
# 405 : 0 : }
# 406 : : CAmount getCredit(const CTxOut& txout, isminefilter filter) override
# 407 : 0 : {
# 408 : 0 : LOCK(m_wallet->cs_wallet);
# 409 : 0 : return m_wallet->GetCredit(txout, filter);
# 410 : 0 : }
# 411 : : CoinsList listCoins() override
# 412 : 0 : {
# 413 : 0 : LOCK(m_wallet->cs_wallet);
# 414 : 0 : CoinsList result;
# 415 [ # # ]: 0 : for (const auto& entry : m_wallet->ListCoins()) {
# 416 : 0 : auto& group = result[entry.first];
# 417 [ # # ]: 0 : for (const auto& coin : entry.second) {
# 418 : 0 : group.emplace_back(COutPoint(coin.tx->GetHash(), coin.i),
# 419 : 0 : MakeWalletTxOut(*m_wallet, *coin.tx, coin.i, coin.nDepth));
# 420 : 0 : }
# 421 : 0 : }
# 422 : 0 : return result;
# 423 : 0 : }
# 424 : : std::vector<WalletTxOut> getCoins(const std::vector<COutPoint>& outputs) override
# 425 : 0 : {
# 426 : 0 : LOCK(m_wallet->cs_wallet);
# 427 : 0 : std::vector<WalletTxOut> result;
# 428 : 0 : result.reserve(outputs.size());
# 429 [ # # ]: 0 : for (const auto& output : outputs) {
# 430 : 0 : result.emplace_back();
# 431 : 0 : auto it = m_wallet->mapWallet.find(output.hash);
# 432 [ # # ]: 0 : if (it != m_wallet->mapWallet.end()) {
# 433 : 0 : int depth = it->second.GetDepthInMainChain();
# 434 [ # # ]: 0 : if (depth >= 0) {
# 435 : 0 : result.back() = MakeWalletTxOut(*m_wallet, it->second, output.n, depth);
# 436 : 0 : }
# 437 : 0 : }
# 438 : 0 : }
# 439 : 0 : return result;
# 440 : 0 : }
# 441 : 0 : CAmount getRequiredFee(unsigned int tx_bytes) override { return GetRequiredFee(*m_wallet, tx_bytes); }
# 442 : : CAmount getMinimumFee(unsigned int tx_bytes,
# 443 : : const CCoinControl& coin_control,
# 444 : : int* returned_target,
# 445 : : FeeReason* reason) override
# 446 : 0 : {
# 447 : 0 : FeeCalculation fee_calc;
# 448 : 0 : CAmount result;
# 449 : 0 : result = GetMinimumFee(*m_wallet, tx_bytes, coin_control, &fee_calc);
# 450 [ # # ]: 0 : if (returned_target) *returned_target = fee_calc.returnedTarget;
# 451 [ # # ]: 0 : if (reason) *reason = fee_calc.reason;
# 452 : 0 : return result;
# 453 : 0 : }
# 454 : 0 : unsigned int getConfirmTarget() override { return m_wallet->m_confirm_target; }
# 455 : 0 : bool hdEnabled() override { return m_wallet->IsHDEnabled(); }
# 456 : 0 : bool canGetAddresses() override { return m_wallet->CanGetAddresses(); }
# 457 : 0 : bool privateKeysDisabled() override { return m_wallet->IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS); }
# 458 : 0 : OutputType getDefaultAddressType() override { return m_wallet->m_default_address_type; }
# 459 : 0 : CAmount getDefaultMaxTxFee() override { return m_wallet->m_default_max_tx_fee; }
# 460 : : void remove() override
# 461 : 0 : {
# 462 : 0 : RemoveWallet(m_wallet, false /* load_on_start */);
# 463 : 0 : }
# 464 : 0 : bool isLegacy() override { return m_wallet->IsLegacy(); }
# 465 : : std::unique_ptr<Handler> handleUnload(UnloadFn fn) override
# 466 : 0 : {
# 467 : 0 : return MakeHandler(m_wallet->NotifyUnload.connect(fn));
# 468 : 0 : }
# 469 : : std::unique_ptr<Handler> handleShowProgress(ShowProgressFn fn) override
# 470 : 0 : {
# 471 : 0 : return MakeHandler(m_wallet->ShowProgress.connect(fn));
# 472 : 0 : }
# 473 : : std::unique_ptr<Handler> handleStatusChanged(StatusChangedFn fn) override
# 474 : 0 : {
# 475 : 0 : return MakeHandler(m_wallet->NotifyStatusChanged.connect([fn](CWallet*) { fn(); }));
# 476 : 0 : }
# 477 : : std::unique_ptr<Handler> handleAddressBookChanged(AddressBookChangedFn fn) override
# 478 : 0 : {
# 479 : 0 : return MakeHandler(m_wallet->NotifyAddressBookChanged.connect(
# 480 : 0 : [fn](CWallet*, const CTxDestination& address, const std::string& label, bool is_mine,
# 481 : 0 : const std::string& purpose, ChangeType status) { fn(address, label, is_mine, purpose, status); }));
# 482 : 0 : }
# 483 : : std::unique_ptr<Handler> handleTransactionChanged(TransactionChangedFn fn) override
# 484 : 0 : {
# 485 : 0 : return MakeHandler(m_wallet->NotifyTransactionChanged.connect(
# 486 : 0 : [fn](CWallet*, const uint256& txid, ChangeType status) { fn(txid, status); }));
# 487 : 0 : }
# 488 : : std::unique_ptr<Handler> handleWatchOnlyChanged(WatchOnlyChangedFn fn) override
# 489 : 0 : {
# 490 : 0 : return MakeHandler(m_wallet->NotifyWatchonlyChanged.connect(fn));
# 491 : 0 : }
# 492 : : std::unique_ptr<Handler> handleCanGetAddressesChanged(CanGetAddressesChangedFn fn) override
# 493 : 0 : {
# 494 : 0 : return MakeHandler(m_wallet->NotifyCanGetAddressesChanged.connect(fn));
# 495 : 0 : }
# 496 : 4 : CWallet* wallet() override { return m_wallet.get(); }
# 497 : :
# 498 : : std::shared_ptr<CWallet> m_wallet;
# 499 : : };
# 500 : :
# 501 : : class WalletClientImpl : public WalletClient
# 502 : : {
# 503 : : public:
# 504 : : WalletClientImpl(Chain& chain, ArgsManager& args)
# 505 : 1521 : {
# 506 : 1521 : m_context.chain = &chain;
# 507 : 1521 : m_context.args = &args;
# 508 : 1521 : }
# 509 : 1520 : ~WalletClientImpl() override { UnloadWallets(); }
# 510 : :
# 511 : : //! ChainClient methods
# 512 : : void registerRpcs() override
# 513 : 664 : {
# 514 [ + + ]: 40504 : for (const CRPCCommand& command : GetWalletRPCCommands()) {
# 515 : 40504 : m_rpc_commands.emplace_back(command.category, command.name, [this, &command](const JSONRPCRequest& request, UniValue& result, bool last_handler) {
# 516 : 25542 : JSONRPCRequest wallet_request = request;
# 517 : 25542 : wallet_request.context = &m_context;
# 518 : 25542 : return command.actor(wallet_request, result, last_handler);
# 519 : 25542 : }, command.argNames, command.unique_id);
# 520 : 40504 : m_rpc_handlers.emplace_back(m_context.chain->handleRpc(m_rpc_commands.back()));
# 521 : 40504 : }
# 522 : 664 : }
# 523 : 659 : bool verify() override { return VerifyWallets(*m_context.chain); }
# 524 : 617 : bool load() override { return LoadWallets(*m_context.chain); }
# 525 : 612 : void start(CScheduler& scheduler) override { return StartWallets(scheduler, *Assert(m_context.args)); }
# 526 : 657 : void flush() override { return FlushWallets(); }
# 527 : 657 : void stop() override { return StopWallets(); }
# 528 : 506 : void setMockTime(int64_t time) override { return SetMockTime(time); }
# 529 : :
# 530 : : //! WalletClient methods
# 531 : : std::unique_ptr<Wallet> createWallet(const std::string& name, const SecureString& passphrase, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings) override
# 532 : 0 : {
# 533 : 0 : std::shared_ptr<CWallet> wallet;
# 534 : 0 : DatabaseOptions options;
# 535 : 0 : DatabaseStatus status;
# 536 : 0 : options.require_create = true;
# 537 : 0 : options.create_flags = wallet_creation_flags;
# 538 : 0 : options.create_passphrase = passphrase;
# 539 : 0 : return MakeWallet(CreateWallet(*m_context.chain, name, true /* load_on_start */, options, status, error, warnings));
# 540 : 0 : }
# 541 : : std::unique_ptr<Wallet> loadWallet(const std::string& name, bilingual_str& error, std::vector<bilingual_str>& warnings) override
# 542 : 0 : {
# 543 : 0 : DatabaseOptions options;
# 544 : 0 : DatabaseStatus status;
# 545 : 0 : options.require_existing = true;
# 546 : 0 : return MakeWallet(LoadWallet(*m_context.chain, name, true /* load_on_start */, options, status, error, warnings));
# 547 : 0 : }
# 548 : : std::string getWalletDir() override
# 549 : 0 : {
# 550 : 0 : return GetWalletDir().string();
# 551 : 0 : }
# 552 : : std::vector<std::string> listWalletDir() override
# 553 : 0 : {
# 554 : 0 : std::vector<std::string> paths;
# 555 [ # # ]: 0 : for (auto& path : ListDatabases(GetWalletDir())) {
# 556 : 0 : paths.push_back(path.string());
# 557 : 0 : }
# 558 : 0 : return paths;
# 559 : 0 : }
# 560 : : std::vector<std::unique_ptr<Wallet>> getWallets() override
# 561 : 0 : {
# 562 : 0 : std::vector<std::unique_ptr<Wallet>> wallets;
# 563 [ # # ]: 0 : for (const auto& wallet : GetWallets()) {
# 564 : 0 : wallets.emplace_back(MakeWallet(wallet));
# 565 : 0 : }
# 566 : 0 : return wallets;
# 567 : 0 : }
# 568 : : std::unique_ptr<Handler> handleLoadWallet(LoadWalletFn fn) override
# 569 : 0 : {
# 570 : 0 : return HandleLoadWallet(std::move(fn));
# 571 : 0 : }
# 572 : :
# 573 : : WalletContext m_context;
# 574 : : const std::vector<std::string> m_wallet_filenames;
# 575 : : std::vector<std::unique_ptr<Handler>> m_rpc_handlers;
# 576 : : std::list<CRPCCommand> m_rpc_commands;
# 577 : : };
# 578 : : } // namespace
# 579 : : } // namespace wallet
# 580 : :
# 581 : : namespace interfaces {
# 582 [ + - ]: 1 : std::unique_ptr<Wallet> MakeWallet(const std::shared_ptr<CWallet>& wallet) { return wallet ? std::make_unique<wallet::WalletImpl>(wallet) : nullptr; }
# 583 : :
# 584 : : std::unique_ptr<WalletClient> MakeWalletClient(Chain& chain, ArgsManager& args)
# 585 : 1521 : {
# 586 : 1521 : return std::make_unique<wallet::WalletClientImpl>(chain, args);
# 587 : 1521 : }
# 588 : : } // namespace interfaces
|