Branch data Line data Source code
# 1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
# 2 : : // Copyright (c) 2009-2020 The Bitcoin Core developers
# 3 : : // Distributed under the MIT software license, see the accompanying
# 4 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
# 5 : :
# 6 : : #ifndef BITCOIN_WALLET_WALLET_H
# 7 : : #define BITCOIN_WALLET_WALLET_H
# 8 : :
# 9 : : #include <amount.h>
# 10 : : #include <interfaces/chain.h>
# 11 : : #include <interfaces/handler.h>
# 12 : : #include <outputtype.h>
# 13 : : #include <policy/feerate.h>
# 14 : : #include <psbt.h>
# 15 : : #include <tinyformat.h>
# 16 : : #include <util/message.h>
# 17 : : #include <util/strencodings.h>
# 18 : : #include <util/string.h>
# 19 : : #include <util/system.h>
# 20 : : #include <util/ui_change_type.h>
# 21 : : #include <validationinterface.h>
# 22 : : #include <wallet/coinselection.h>
# 23 : : #include <wallet/crypter.h>
# 24 : : #include <external_signer.h>
# 25 : : #include <wallet/receive.h>
# 26 : : #include <wallet/scriptpubkeyman.h>
# 27 : : #include <wallet/spend.h>
# 28 : : #include <wallet/transaction.h>
# 29 : : #include <wallet/walletdb.h>
# 30 : : #include <wallet/walletutil.h>
# 31 : :
# 32 : : #include <algorithm>
# 33 : : #include <atomic>
# 34 : : #include <map>
# 35 : : #include <memory>
# 36 : : #include <optional>
# 37 : : #include <set>
# 38 : : #include <stdexcept>
# 39 : : #include <stdint.h>
# 40 : : #include <string>
# 41 : : #include <utility>
# 42 : : #include <vector>
# 43 : :
# 44 : : #include <boost/signals2/signal.hpp>
# 45 : :
# 46 : : using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wallet)>;
# 47 : :
# 48 : : struct bilingual_str;
# 49 : :
# 50 : : //! Explicitly unload and delete the wallet.
# 51 : : //! Blocks the current thread after signaling the unload intent so that all
# 52 : : //! wallet clients release the wallet.
# 53 : : //! Note that, when blocking is not required, the wallet is implicitly unloaded
# 54 : : //! by the shared pointer deleter.
# 55 : : void UnloadWallet(std::shared_ptr<CWallet>&& wallet);
# 56 : :
# 57 : : bool AddWallet(const std::shared_ptr<CWallet>& wallet);
# 58 : : bool RemoveWallet(const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start, std::vector<bilingual_str>& warnings);
# 59 : : bool RemoveWallet(const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start);
# 60 : : std::vector<std::shared_ptr<CWallet>> GetWallets();
# 61 : : std::shared_ptr<CWallet> GetWallet(const std::string& name);
# 62 : : std::shared_ptr<CWallet> LoadWallet(interfaces::Chain& chain, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
# 63 : : std::shared_ptr<CWallet> CreateWallet(interfaces::Chain& chain, const std::string& name, std::optional<bool> load_on_start, DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
# 64 : : std::unique_ptr<interfaces::Handler> HandleLoadWallet(LoadWalletFn load_wallet);
# 65 : : std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
# 66 : :
# 67 : : //! -paytxfee default
# 68 : : constexpr CAmount DEFAULT_PAY_TX_FEE = 0;
# 69 : : //! -fallbackfee default
# 70 : : static const CAmount DEFAULT_FALLBACK_FEE = 0;
# 71 : : //! -discardfee default
# 72 : : static const CAmount DEFAULT_DISCARD_FEE = 10000;
# 73 : : //! -mintxfee default
# 74 : : static const CAmount DEFAULT_TRANSACTION_MINFEE = 1000;
# 75 : : /**
# 76 : : * maximum fee increase allowed to do partial spend avoidance, even for nodes with this feature disabled by default
# 77 : : *
# 78 : : * A value of -1 disables this feature completely.
# 79 : : * A value of 0 (current default) means to attempt to do partial spend avoidance, and use its results if the fees remain *unchanged*
# 80 : : * A value > 0 means to do partial spend avoidance if the fee difference against a regular coin selection instance is in the range [0..value].
# 81 : : */
# 82 : : static const CAmount DEFAULT_MAX_AVOIDPARTIALSPEND_FEE = 0;
# 83 : : //! discourage APS fee higher than this amount
# 84 : : constexpr CAmount HIGH_APS_FEE{COIN / 10000};
# 85 : : //! minimum recommended increment for BIP 125 replacement txs
# 86 : : static const CAmount WALLET_INCREMENTAL_RELAY_FEE = 5000;
# 87 : : //! Default for -spendzeroconfchange
# 88 : : static const bool DEFAULT_SPEND_ZEROCONF_CHANGE = true;
# 89 : : //! Default for -walletrejectlongchains
# 90 : : static const bool DEFAULT_WALLET_REJECT_LONG_CHAINS = false;
# 91 : : //! -txconfirmtarget default
# 92 : : static const unsigned int DEFAULT_TX_CONFIRM_TARGET = 6;
# 93 : : //! -walletrbf default
# 94 : : static const bool DEFAULT_WALLET_RBF = false;
# 95 : : static const bool DEFAULT_WALLETBROADCAST = true;
# 96 : : static const bool DEFAULT_DISABLE_WALLET = false;
# 97 : : //! -maxtxfee default
# 98 : : constexpr CAmount DEFAULT_TRANSACTION_MAXFEE{COIN / 10};
# 99 : : //! Discourage users to set fees higher than this amount (in satoshis) per kB
# 100 : : constexpr CAmount HIGH_TX_FEE_PER_KB{COIN / 100};
# 101 : : //! -maxtxfee will warn if called with a higher fee than this amount (in satoshis)
# 102 : : constexpr CAmount HIGH_MAX_TX_FEE{100 * HIGH_TX_FEE_PER_KB};
# 103 : : //! Pre-calculated constants for input size estimation in *virtual size*
# 104 : : static constexpr size_t DUMMY_NESTED_P2WPKH_INPUT_SIZE = 91;
# 105 : :
# 106 : : class CCoinControl;
# 107 : : class COutput;
# 108 : : class CScript;
# 109 : : class CWalletTx;
# 110 : : struct FeeCalculation;
# 111 : : enum class FeeEstimateMode;
# 112 : : class ReserveDestination;
# 113 : :
# 114 : : //! Default for -addresstype
# 115 : : constexpr OutputType DEFAULT_ADDRESS_TYPE{OutputType::BECH32};
# 116 : :
# 117 : : static constexpr uint64_t KNOWN_WALLET_FLAGS =
# 118 : : WALLET_FLAG_AVOID_REUSE
# 119 : : | WALLET_FLAG_BLANK_WALLET
# 120 : : | WALLET_FLAG_KEY_ORIGIN_METADATA
# 121 : : | WALLET_FLAG_DISABLE_PRIVATE_KEYS
# 122 : : | WALLET_FLAG_DESCRIPTORS
# 123 : : | WALLET_FLAG_EXTERNAL_SIGNER;
# 124 : :
# 125 : : static constexpr uint64_t MUTABLE_WALLET_FLAGS =
# 126 : : WALLET_FLAG_AVOID_REUSE;
# 127 : :
# 128 : : static const std::map<std::string,WalletFlags> WALLET_FLAG_MAP{
# 129 : : {"avoid_reuse", WALLET_FLAG_AVOID_REUSE},
# 130 : : {"blank", WALLET_FLAG_BLANK_WALLET},
# 131 : : {"key_origin_metadata", WALLET_FLAG_KEY_ORIGIN_METADATA},
# 132 : : {"disable_private_keys", WALLET_FLAG_DISABLE_PRIVATE_KEYS},
# 133 : : {"descriptor_wallet", WALLET_FLAG_DESCRIPTORS},
# 134 : : {"external_signer", WALLET_FLAG_EXTERNAL_SIGNER}
# 135 : : };
# 136 : :
# 137 : : extern const std::map<uint64_t,std::string> WALLET_FLAG_CAVEATS;
# 138 : :
# 139 : : /** A wrapper to reserve an address from a wallet
# 140 : : *
# 141 : : * ReserveDestination is used to reserve an address.
# 142 : : * It is currently only used inside of CreateTransaction.
# 143 : : *
# 144 : : * Instantiating a ReserveDestination does not reserve an address. To do so,
# 145 : : * GetReservedDestination() needs to be called on the object. Once an address has been
# 146 : : * reserved, call KeepDestination() on the ReserveDestination object to make sure it is not
# 147 : : * returned. Call ReturnDestination() to return the address so it can be re-used (for
# 148 : : * example, if the address was used in a new transaction
# 149 : : * and that transaction was not completed and needed to be aborted).
# 150 : : *
# 151 : : * If an address is reserved and KeepDestination() is not called, then the address will be
# 152 : : * returned when the ReserveDestination goes out of scope.
# 153 : : */
# 154 : : class ReserveDestination
# 155 : : {
# 156 : : protected:
# 157 : : //! The wallet to reserve from
# 158 : : const CWallet* const pwallet;
# 159 : : //! The ScriptPubKeyMan to reserve from. Based on type when GetReservedDestination is called
# 160 : : ScriptPubKeyMan* m_spk_man{nullptr};
# 161 : : OutputType const type;
# 162 : : //! The index of the address's key in the keypool
# 163 : : int64_t nIndex{-1};
# 164 : : //! The destination
# 165 : : CTxDestination address;
# 166 : : //! Whether this is from the internal (change output) keypool
# 167 : : bool fInternal{false};
# 168 : :
# 169 : : public:
# 170 : : //! Construct a ReserveDestination object. This does NOT reserve an address yet
# 171 : : explicit ReserveDestination(CWallet* pwallet, OutputType type)
# 172 : : : pwallet(pwallet)
# 173 : 5204 : , type(type) { }
# 174 : :
# 175 : : ReserveDestination(const ReserveDestination&) = delete;
# 176 : : ReserveDestination& operator=(const ReserveDestination&) = delete;
# 177 : :
# 178 : : //! Destructor. If a key has been reserved and not KeepKey'ed, it will be returned to the keypool
# 179 : : ~ReserveDestination()
# 180 : 5204 : {
# 181 : 5204 : ReturnDestination();
# 182 : 5204 : }
# 183 : :
# 184 : : //! Reserve an address
# 185 : : bool GetReservedDestination(CTxDestination& pubkey, bool internal);
# 186 : : //! Return reserved address
# 187 : : void ReturnDestination();
# 188 : : //! Keep the address. Do not return it's key to the keypool when this object goes out of scope
# 189 : : void KeepDestination();
# 190 : : };
# 191 : :
# 192 : : /** Address book data */
# 193 : : class CAddressBookData
# 194 : : {
# 195 : : private:
# 196 : : bool m_change{true};
# 197 : : std::string m_label;
# 198 : : public:
# 199 : : std::string purpose;
# 200 : :
# 201 : 18558 : CAddressBookData() : purpose("unknown") {}
# 202 : :
# 203 : : typedef std::map<std::string, std::string> StringMap;
# 204 : : StringMap destdata;
# 205 : :
# 206 : 60221 : bool IsChange() const { return m_change; }
# 207 : 58951 : const std::string& GetLabel() const { return m_label; }
# 208 : 18631 : void SetLabel(const std::string& label) {
# 209 : 18631 : m_change = false;
# 210 : 18631 : m_label = label;
# 211 : 18631 : }
# 212 : : };
# 213 : :
# 214 : : struct CRecipient
# 215 : : {
# 216 : : CScript scriptPubKey;
# 217 : : CAmount nAmount;
# 218 : : bool fSubtractFeeFromAmount;
# 219 : : };
# 220 : :
# 221 : : class WalletRescanReserver; //forward declarations for ScanForWalletTransactions/RescanFromTime
# 222 : : /**
# 223 : : * A CWallet maintains a set of transactions and balances, and provides the ability to create new transactions.
# 224 : : */
# 225 : : class CWallet final : public WalletStorage, public interfaces::Chain::Notifications
# 226 : : {
# 227 : : private:
# 228 : : CKeyingMaterial vMasterKey GUARDED_BY(cs_wallet);
# 229 : :
# 230 : : bool Unlock(const CKeyingMaterial& vMasterKeyIn, bool accept_no_keys = false);
# 231 : :
# 232 : : std::atomic<bool> fAbortRescan{false};
# 233 : : std::atomic<bool> fScanningWallet{false}; // controlled by WalletRescanReserver
# 234 : : std::atomic<int64_t> m_scanning_start{0};
# 235 : : std::atomic<double> m_scanning_progress{0};
# 236 : : friend class WalletRescanReserver;
# 237 : :
# 238 : : //! the current wallet version: clients below this version are not able to load the wallet
# 239 : : int nWalletVersion GUARDED_BY(cs_wallet){FEATURE_BASE};
# 240 : :
# 241 : : /** The next scheduled rebroadcast of wallet transactions. */
# 242 : : int64_t nNextResend = 0;
# 243 : : /** Whether this wallet will submit newly created transactions to the node's mempool and
# 244 : : * prompt rebroadcasts (see ResendWalletTransactions()). */
# 245 : : bool fBroadcastTransactions = false;
# 246 : : // Local time that the tip block was received. Used to schedule wallet rebroadcasts.
# 247 : : std::atomic<int64_t> m_best_block_time {0};
# 248 : :
# 249 : : /**
# 250 : : * Used to keep track of spent outpoints, and
# 251 : : * detect and report conflicts (double-spends or
# 252 : : * mutated transactions where the mutant gets mined).
# 253 : : */
# 254 : : typedef std::multimap<COutPoint, uint256> TxSpends;
# 255 : : TxSpends mapTxSpends GUARDED_BY(cs_wallet);
# 256 : : void AddToSpends(const COutPoint& outpoint, const uint256& wtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 257 : : void AddToSpends(const uint256& wtxid) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 258 : :
# 259 : : /**
# 260 : : * Add a transaction to the wallet, or update it. pIndex and posInBlock should
# 261 : : * be set when the transaction was known to be included in a block. When
# 262 : : * pIndex == nullptr, then wallet state is not updated in AddToWallet, but
# 263 : : * notifications happen and cached balances are marked dirty.
# 264 : : *
# 265 : : * If fUpdate is true, existing transactions will be updated.
# 266 : : * TODO: One exception to this is that the abandoned state is cleared under the
# 267 : : * assumption that any further notification of a transaction that was considered
# 268 : : * abandoned is an indication that it is not safe to be considered abandoned.
# 269 : : * Abandoned state should probably be more carefully tracked via different
# 270 : : * posInBlock signals or by checking mempool presence when necessary.
# 271 : : */
# 272 : : bool AddToWalletIfInvolvingMe(const CTransactionRef& tx, CWalletTx::Confirmation confirm, bool fUpdate) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 273 : :
# 274 : : /** Mark a transaction (and its in-wallet descendants) as conflicting with a particular block. */
# 275 : : void MarkConflicted(const uint256& hashBlock, int conflicting_height, const uint256& hashTx);
# 276 : :
# 277 : : /** Mark a transaction's inputs dirty, thus forcing the outputs to be recomputed */
# 278 : : void MarkInputsDirty(const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 279 : :
# 280 : : void SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator>) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 281 : :
# 282 : : /* Used by TransactionAddedToMemorypool/BlockConnected/Disconnected/ScanForWalletTransactions.
# 283 : : * Should be called with non-zero block_hash and posInBlock if this is for a transaction that is included in a block. */
# 284 : : void SyncTransaction(const CTransactionRef& tx, CWalletTx::Confirmation confirm, bool update_tx = true) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 285 : :
# 286 : : /** WalletFlags set on this wallet. */
# 287 : : std::atomic<uint64_t> m_wallet_flags{0};
# 288 : :
# 289 : : bool SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& address, const std::string& strName, const std::string& strPurpose);
# 290 : :
# 291 : : //! Unsets a wallet flag and saves it to disk
# 292 : : void UnsetWalletFlagWithDB(WalletBatch& batch, uint64_t flag);
# 293 : :
# 294 : : //! Unset the blank wallet flag and saves it to disk
# 295 : : void UnsetBlankWalletFlag(WalletBatch& batch) override;
# 296 : :
# 297 : : /** Interface for accessing chain state. */
# 298 : : interfaces::Chain* m_chain;
# 299 : :
# 300 : : /** Wallet name: relative directory name or "" for default wallet. */
# 301 : : std::string m_name;
# 302 : :
# 303 : : /** Internal database handle. */
# 304 : : std::unique_ptr<WalletDatabase> const m_database;
# 305 : :
# 306 : : /**
# 307 : : * The following is used to keep track of how far behind the wallet is
# 308 : : * from the chain sync, and to allow clients to block on us being caught up.
# 309 : : *
# 310 : : * Processed hash is a pointer on node's tip and doesn't imply that the wallet
# 311 : : * has scanned sequentially all blocks up to this one.
# 312 : : */
# 313 : : uint256 m_last_block_processed GUARDED_BY(cs_wallet);
# 314 : :
# 315 : : /** Height of last block processed is used by wallet to know depth of transactions
# 316 : : * without relying on Chain interface beyond asynchronous updates. For safety, we
# 317 : : * initialize it to -1. Height is a pointer on node's tip and doesn't imply
# 318 : : * that the wallet has scanned sequentially all blocks up to this one.
# 319 : : */
# 320 : : int m_last_block_processed_height GUARDED_BY(cs_wallet) = -1;
# 321 : :
# 322 : : std::map<OutputType, ScriptPubKeyMan*> m_external_spk_managers;
# 323 : : std::map<OutputType, ScriptPubKeyMan*> m_internal_spk_managers;
# 324 : :
# 325 : : // Indexed by a unique identifier produced by each ScriptPubKeyMan using
# 326 : : // ScriptPubKeyMan::GetID. In many cases it will be the hash of an internal structure
# 327 : : std::map<uint256, std::unique_ptr<ScriptPubKeyMan>> m_spk_managers;
# 328 : :
# 329 : : bool CreateTransactionInternal(const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet, int& nChangePosInOut, bilingual_str& error, const CCoinControl& coin_control, FeeCalculation& fee_calc_out, bool sign);
# 330 : :
# 331 : : /**
# 332 : : * Catch wallet up to current chain, scanning new blocks, updating the best
# 333 : : * block locator and m_last_block_processed, and registering for
# 334 : : * notifications about new blocks and transactions.
# 335 : : */
# 336 : : static bool AttachChain(const std::shared_ptr<CWallet>& wallet, interfaces::Chain& chain, bilingual_str& error, std::vector<bilingual_str>& warnings);
# 337 : :
# 338 : : public:
# 339 : : /**
# 340 : : * Main wallet lock.
# 341 : : * This lock protects all the fields added by CWallet.
# 342 : : */
# 343 : : mutable RecursiveMutex cs_wallet;
# 344 : :
# 345 : : WalletDatabase& GetDatabase() const override
# 346 : 307336 : {
# 347 : 307336 : assert(static_cast<bool>(m_database));
# 348 : 307336 : return *m_database;
# 349 : 307336 : }
# 350 : :
# 351 : : /**
# 352 : : * Select a set of coins such that nValueRet >= nTargetValue and at least
# 353 : : * all coins from coin_control are selected; never select unconfirmed coins if they are not ours
# 354 : : * param@[out] setCoinsRet Populated with inputs including pre-selected inputs from
# 355 : : * coin_control and Coin Selection if successful.
# 356 : : * param@[out] nValueRet Total value of selected coins including pre-selected ones
# 357 : : * from coin_control and Coin Selection if successful.
# 358 : : */
# 359 : : bool SelectCoins(const std::vector<COutput>& vAvailableCoins, const CAmount& nTargetValue, std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet,
# 360 : : const CCoinControl& coin_control, CoinSelectionParams& coin_selection_params) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 361 : :
# 362 : : /** Get a name for this wallet for logging/debugging purposes.
# 363 : : */
# 364 : 278925 : const std::string& GetName() const { return m_name; }
# 365 : :
# 366 : : typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
# 367 : : MasterKeyMap mapMasterKeys;
# 368 : : unsigned int nMasterKeyMaxID = 0;
# 369 : :
# 370 : : /** Construct wallet with specified name and database implementation. */
# 371 : : CWallet(interfaces::Chain* chain, const std::string& name, std::unique_ptr<WalletDatabase> database)
# 372 : : : m_chain(chain),
# 373 : : m_name(name),
# 374 : : m_database(std::move(database))
# 375 : 819 : {
# 376 : 819 : }
# 377 : :
# 378 : : ~CWallet()
# 379 : 817 : {
# 380 : : // Should not have slots connected at this point.
# 381 : 817 : assert(NotifyUnload.empty());
# 382 : 817 : }
# 383 : :
# 384 : : bool IsCrypted() const;
# 385 : : bool IsLocked() const override;
# 386 : : bool Lock();
# 387 : :
# 388 : : /** Interface to assert chain access */
# 389 [ + + ]: 9892 : bool HaveChain() const { return m_chain ? true : false; }
# 390 : :
# 391 : : /** Map from txid to CWalletTx for all transactions this wallet is
# 392 : : * interested in, including received and sent transactions. */
# 393 : : std::map<uint256, CWalletTx> mapWallet GUARDED_BY(cs_wallet);
# 394 : :
# 395 : : typedef std::multimap<int64_t, CWalletTx*> TxItems;
# 396 : : TxItems wtxOrdered;
# 397 : :
# 398 : : int64_t nOrderPosNext GUARDED_BY(cs_wallet) = 0;
# 399 : : uint64_t nAccountingEntryNumber = 0;
# 400 : :
# 401 : : std::map<CTxDestination, CAddressBookData> m_address_book GUARDED_BY(cs_wallet);
# 402 : : const CAddressBookData* FindAddressBookEntry(const CTxDestination&, bool allow_change = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 403 : :
# 404 : : /** Set of Coins owned by this wallet that we won't try to spend from. A
# 405 : : * Coin may be locked if it has already been used to fund a transaction
# 406 : : * that hasn't confirmed yet. We wouldn't consider the Coin spent already,
# 407 : : * but also shouldn't try to use it again. */
# 408 : : std::set<COutPoint> setLockedCoins GUARDED_BY(cs_wallet);
# 409 : :
# 410 : : /** Registered interfaces::Chain::Notifications handler. */
# 411 : : std::unique_ptr<interfaces::Handler> m_chain_notifications_handler;
# 412 : :
# 413 : : /** Interface for accessing chain state. */
# 414 : 4764974 : interfaces::Chain& chain() const { assert(m_chain); return *m_chain; }
# 415 : :
# 416 : : const CWalletTx* GetWalletTx(const uint256& hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 417 : : bool IsTrusted(const CWalletTx& wtx, std::set<uint256>& trusted_parents) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 418 : :
# 419 : : //! check whether we support the named feature
# 420 : 89187 : bool CanSupportFeature(enum WalletFeature wf) const override EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); return IsFeatureSupported(nWalletVersion, wf); }
# 421 : :
# 422 : : /**
# 423 : : * populate vCoins with vector of available COutputs.
# 424 : : */
# 425 : : void AvailableCoins(std::vector<COutput>& vCoins, const CCoinControl* coinControl = nullptr, const CAmount& nMinimumAmount = 1, const CAmount& nMaximumAmount = MAX_MONEY, const CAmount& nMinimumSumAmount = MAX_MONEY, const uint64_t nMaximumCount = 0) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 426 : :
# 427 : : /**
# 428 : : * Return list of available coins and locked coins grouped by non-change output address.
# 429 : : */
# 430 : : std::map<CTxDestination, std::vector<COutput>> ListCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 431 : :
# 432 : : /**
# 433 : : * Find non-change parent output.
# 434 : : */
# 435 : : const CTxOut& FindNonChangeParentOutput(const CTransaction& tx, int output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 436 : :
# 437 : : /**
# 438 : : * Shuffle and select coins until nTargetValue is reached while avoiding
# 439 : : * small change; This method is stochastic for some inputs and upon
# 440 : : * completion the coin set and corresponding actual target value is
# 441 : : * assembled
# 442 : : * param@[in] coins Set of UTXOs to consider. These will be categorized into
# 443 : : * OutputGroups and filtered using eligibility_filter before
# 444 : : * selecting coins.
# 445 : : * param@[out] setCoinsRet Populated with the coins selected if successful.
# 446 : : * param@[out] nValueRet Used to return the total value of selected coins.
# 447 : : */
# 448 : : bool SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibilityFilter& eligibility_filter, std::vector<COutput> coins,
# 449 : : std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CoinSelectionParams& coin_selection_params) const;
# 450 : :
# 451 : : bool IsSpent(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 452 : :
# 453 : : // Whether this or any known UTXO with the same single key has been spent.
# 454 : : bool IsSpentKey(const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 455 : : void SetSpentKeyState(WalletBatch& batch, const uint256& hash, unsigned int n, bool used, std::set<CTxDestination>& tx_destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 456 : :
# 457 : : std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, const CoinSelectionParams& coin_sel_params, const CoinEligibilityFilter& filter, bool positive_only) const;
# 458 : :
# 459 : : /** Display address on an external signer. Returns false if external signer support is not compiled */
# 460 : : bool DisplayAddress(const CTxDestination& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 461 : :
# 462 : : bool IsLockedCoin(uint256 hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 463 : : void LockCoin(const COutPoint& output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 464 : : void UnlockCoin(const COutPoint& output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 465 : : void UnlockAllCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 466 : : void ListLockedCoins(std::vector<COutPoint>& vOutpts) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 467 : :
# 468 : : /*
# 469 : : * Rescan abort properties
# 470 : : */
# 471 : 0 : void AbortRescan() { fAbortRescan = true; }
# 472 : 537 : bool IsAbortingRescan() const { return fAbortRescan; }
# 473 : 978 : bool IsScanning() const { return fScanningWallet; }
# 474 [ # # ]: 0 : int64_t ScanningDuration() const { return fScanningWallet ? GetTimeMillis() - m_scanning_start : 0; }
# 475 [ # # ]: 0 : double ScanningProgress() const { return fScanningWallet ? (double) m_scanning_progress : 0; }
# 476 : :
# 477 : : //! Upgrade stored CKeyMetadata objects to store key origin info as KeyOriginInfo
# 478 : : void UpgradeKeyMetadata() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 479 : :
# 480 : 341 : bool LoadMinVersion(int nVersion) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); nWalletVersion = nVersion; return true; }
# 481 : :
# 482 : : /**
# 483 : : * Adds a destination data tuple to the store, and saves it to disk
# 484 : : * When adding new fields, take care to consider how DelAddressBook should handle it!
# 485 : : */
# 486 : : bool AddDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 487 : : //! Erases a destination data tuple in the store and on disk
# 488 : : bool EraseDestData(WalletBatch& batch, const CTxDestination& dest, const std::string& key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 489 : : //! Adds a destination data tuple to the store, without saving it to disk
# 490 : : void LoadDestData(const CTxDestination& dest, const std::string& key, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 491 : : //! Look up a destination data tuple in the store, return true if found false otherwise
# 492 : : bool GetDestData(const CTxDestination& dest, const std::string& key, std::string* value) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 493 : : //! Get all destination values matching a prefix.
# 494 : : std::vector<std::string> GetDestValues(const std::string& prefix) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 495 : :
# 496 : : //! Holds a timestamp at which point the wallet is scheduled (externally) to be relocked. Caller must arrange for actual relocking to occur via Lock().
# 497 : : int64_t nRelockTime GUARDED_BY(cs_wallet){0};
# 498 : :
# 499 : : // Used to prevent concurrent calls to walletpassphrase RPC.
# 500 : : Mutex m_unlock_mutex;
# 501 : : bool Unlock(const SecureString& strWalletPassphrase, bool accept_no_keys = false);
# 502 : : bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
# 503 : : bool EncryptWallet(const SecureString& strWalletPassphrase);
# 504 : :
# 505 : : void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 506 : : unsigned int ComputeTimeSmart(const CWalletTx& wtx) const;
# 507 : :
# 508 : : /**
# 509 : : * Increment the next transaction order id
# 510 : : * @return next transaction order id
# 511 : : */
# 512 : : int64_t IncOrderPosNext(WalletBatch *batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 513 : : DBErrors ReorderTransactions();
# 514 : :
# 515 : : void MarkDirty();
# 516 : :
# 517 : : //! Callback for updating transaction metadata in mapWallet.
# 518 : : //!
# 519 : : //! @param wtx - reference to mapWallet transaction to update
# 520 : : //! @param new_tx - true if wtx is newly inserted, false if it previously existed
# 521 : : //!
# 522 : : //! @return true if wtx is changed and needs to be saved to disk, otherwise false
# 523 : : using UpdateWalletTxFn = std::function<bool(CWalletTx& wtx, bool new_tx)>;
# 524 : :
# 525 : : CWalletTx* AddToWallet(CTransactionRef tx, const CWalletTx::Confirmation& confirm, const UpdateWalletTxFn& update_wtx=nullptr, bool fFlushOnClose=true);
# 526 : : bool LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 527 : : void transactionAddedToMempool(const CTransactionRef& tx, uint64_t mempool_sequence) override;
# 528 : : void blockConnected(const CBlock& block, int height) override;
# 529 : : void blockDisconnected(const CBlock& block, int height) override;
# 530 : : void updatedBlockTip() override;
# 531 : : int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update);
# 532 : :
# 533 : : struct ScanResult {
# 534 : : enum { SUCCESS, FAILURE, USER_ABORT } status = SUCCESS;
# 535 : :
# 536 : : //! Hash and height of most recent block that was successfully scanned.
# 537 : : //! Unset if no blocks were scanned due to read errors or the chain
# 538 : : //! being empty.
# 539 : : uint256 last_scanned_block;
# 540 : : std::optional<int> last_scanned_height;
# 541 : :
# 542 : : //! Height of the most recent block that could not be scanned due to
# 543 : : //! read errors or pruning. Will be set if status is FAILURE, unset if
# 544 : : //! status is SUCCESS, and may or may not be set if status is
# 545 : : //! USER_ABORT.
# 546 : : uint256 last_failed_block;
# 547 : : };
# 548 : : ScanResult ScanForWalletTransactions(const uint256& start_block, int start_height, std::optional<int> max_height, const WalletRescanReserver& reserver, bool fUpdate);
# 549 : : void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason, uint64_t mempool_sequence) override;
# 550 : : void ReacceptWalletTransactions() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 551 : : void ResendWalletTransactions();
# 552 : : struct Balance {
# 553 : : CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more
# 554 : : CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending)
# 555 : : CAmount m_mine_immature{0}; //!< Immature coinbases in the main chain
# 556 : : CAmount m_watchonly_trusted{0};
# 557 : : CAmount m_watchonly_untrusted_pending{0};
# 558 : : CAmount m_watchonly_immature{0};
# 559 : : };
# 560 : : Balance GetBalance(int min_depth = 0, bool avoid_reuse = true) const;
# 561 : : CAmount GetAvailableBalance(const CCoinControl* coinControl = nullptr) const;
# 562 : :
# 563 : : OutputType TransactionChangeType(const std::optional<OutputType>& change_type, const std::vector<CRecipient>& vecSend) const;
# 564 : :
# 565 : : /**
# 566 : : * Insert additional inputs into the transaction by
# 567 : : * calling CreateTransaction();
# 568 : : */
# 569 : : bool FundTransaction(CMutableTransaction& tx, CAmount& nFeeRet, int& nChangePosInOut, bilingual_str& error, bool lockUnspents, const std::set<int>& setSubtractFeeFromOutputs, CCoinControl);
# 570 : : /** Fetch the inputs and sign with SIGHASH_ALL. */
# 571 : : bool SignTransaction(CMutableTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 572 : : /** Sign the tx given the input coins and sighash. */
# 573 : : bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, std::string>& input_errors) const;
# 574 : : SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const;
# 575 : :
# 576 : : /**
# 577 : : * Fills out a PSBT with information from the wallet. Fills in UTXOs if we have
# 578 : : * them. Tries to sign if sign=true. Sets `complete` if the PSBT is now complete
# 579 : : * (i.e. has all required signatures or signature-parts, and is ready to
# 580 : : * finalize.) Sets `error` and returns false if something goes wrong.
# 581 : : *
# 582 : : * @param[in] psbtx PartiallySignedTransaction to fill in
# 583 : : * @param[out] complete indicates whether the PSBT is now complete
# 584 : : * @param[in] sighash_type the sighash type to use when signing (if PSBT does not specify)
# 585 : : * @param[in] sign whether to sign or not
# 586 : : * @param[in] bip32derivs whether to fill in bip32 derivation information if available
# 587 : : * return error
# 588 : : */
# 589 : : TransactionError FillPSBT(PartiallySignedTransaction& psbtx,
# 590 : : bool& complete,
# 591 : : int sighash_type = 1 /* SIGHASH_ALL */,
# 592 : : bool sign = true,
# 593 : : bool bip32derivs = true,
# 594 : : size_t* n_signed = nullptr) const;
# 595 : :
# 596 : : /**
# 597 : : * Create a new transaction paying the recipients with a set of coins
# 598 : : * selected by SelectCoins(); Also create the change output, when needed
# 599 : : * @note passing nChangePosInOut as -1 will result in setting a random position
# 600 : : */
# 601 : : bool CreateTransaction(const std::vector<CRecipient>& vecSend, CTransactionRef& tx, CAmount& nFeeRet, int& nChangePosInOut, bilingual_str& error, const CCoinControl& coin_control, FeeCalculation& fee_calc_out, bool sign = true);
# 602 : : /**
# 603 : : * Submit the transaction to the node's mempool and then relay to peers.
# 604 : : * Should be called after CreateTransaction unless you want to abort
# 605 : : * broadcasting the transaction.
# 606 : : *
# 607 : : * @param[in] tx The transaction to be broadcast.
# 608 : : * @param[in] mapValue key-values to be set on the transaction.
# 609 : : * @param[in] orderForm BIP 70 / BIP 21 order form details to be set on the transaction.
# 610 : : */
# 611 : : void CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm);
# 612 : :
# 613 : : bool DummySignTx(CMutableTransaction &txNew, const std::set<CTxOut> &txouts, bool use_max_sig = false) const
# 614 : 0 : {
# 615 : 0 : std::vector<CTxOut> v_txouts(txouts.size());
# 616 : 0 : std::copy(txouts.begin(), txouts.end(), v_txouts.begin());
# 617 : 0 : return DummySignTx(txNew, v_txouts, use_max_sig);
# 618 : 0 : }
# 619 : : bool DummySignTx(CMutableTransaction &txNew, const std::vector<CTxOut> &txouts, bool use_max_sig = false) const;
# 620 : : bool DummySignInput(CTxIn &tx_in, const CTxOut &txout, bool use_max_sig = false) const;
# 621 : :
# 622 : : bool ImportScripts(const std::set<CScript> scripts, int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 623 : : bool ImportPrivKeys(const std::map<CKeyID, CKey>& privkey_map, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 624 : : bool ImportPubKeys(const std::vector<CKeyID>& ordered_pubkeys, const std::map<CKeyID, CPubKey>& pubkey_map, const std::map<CKeyID, std::pair<CPubKey, KeyOriginInfo>>& key_origins, const bool add_keypool, const bool internal, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 625 : : bool ImportScriptPubKeys(const std::string& label, const std::set<CScript>& script_pub_keys, const bool have_solving_data, const bool apply_label, const int64_t timestamp) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 626 : :
# 627 : : CFeeRate m_pay_tx_fee{DEFAULT_PAY_TX_FEE};
# 628 : : unsigned int m_confirm_target{DEFAULT_TX_CONFIRM_TARGET};
# 629 : : /** Allow Coin Selection to pick unconfirmed UTXOs that were sent from our own wallet if it
# 630 : : * cannot fund the transaction otherwise. */
# 631 : : bool m_spend_zero_conf_change{DEFAULT_SPEND_ZEROCONF_CHANGE};
# 632 : : bool m_signal_rbf{DEFAULT_WALLET_RBF};
# 633 : : bool m_allow_fallback_fee{true}; //!< will be false if -fallbackfee=0
# 634 : : CFeeRate m_min_fee{DEFAULT_TRANSACTION_MINFEE}; //!< Override with -mintxfee
# 635 : : /**
# 636 : : * If fee estimation does not have enough data to provide estimates, use this fee instead.
# 637 : : * Has no effect if not using fee estimation
# 638 : : * Override with -fallbackfee
# 639 : : */
# 640 : : CFeeRate m_fallback_fee{DEFAULT_FALLBACK_FEE};
# 641 : :
# 642 : : /** If the cost to spend a change output at this feerate is greater than the value of the
# 643 : : * output itself, just drop it to fees. */
# 644 : : CFeeRate m_discard_rate{DEFAULT_DISCARD_FEE};
# 645 : :
# 646 : : /** The maximum fee amount we're willing to pay to prioritize partial spend avoidance. */
# 647 : : CAmount m_max_aps_fee{DEFAULT_MAX_AVOIDPARTIALSPEND_FEE}; //!< note: this is absolute fee, not fee rate
# 648 : : OutputType m_default_address_type{DEFAULT_ADDRESS_TYPE};
# 649 : : /**
# 650 : : * Default output type for change outputs. When unset, automatically choose type
# 651 : : * based on address type setting and the types other of non-change outputs
# 652 : : * (see -changetype option documentation and implementation in
# 653 : : * CWallet::TransactionChangeType for details).
# 654 : : */
# 655 : : std::optional<OutputType> m_default_change_type{};
# 656 : : /** Absolute maximum transaction fee (in satoshis) used by default for the wallet */
# 657 : : CAmount m_default_max_tx_fee{DEFAULT_TRANSACTION_MAXFEE};
# 658 : :
# 659 : : size_t KeypoolCountExternalKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 660 : : bool TopUpKeyPool(unsigned int kpSize = 0);
# 661 : :
# 662 : : int64_t GetOldestKeyPoolTime() const;
# 663 : :
# 664 : : std::set<std::set<CTxDestination>> GetAddressGroupings() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 665 : : std::map<CTxDestination, CAmount> GetAddressBalances() const;
# 666 : :
# 667 : : std::set<CTxDestination> GetLabelAddresses(const std::string& label) const;
# 668 : :
# 669 : : /**
# 670 : : * Marks all outputs in each one of the destinations dirty, so their cache is
# 671 : : * reset and does not return outdated information.
# 672 : : */
# 673 : : void MarkDestinationsDirty(const std::set<CTxDestination>& destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 674 : :
# 675 : : bool GetNewDestination(const OutputType type, const std::string label, CTxDestination& dest, std::string& error);
# 676 : : bool GetNewChangeDestination(const OutputType type, CTxDestination& dest, std::string& error);
# 677 : :
# 678 : : isminetype IsMine(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 679 : : isminetype IsMine(const CScript& script) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 680 : : isminetype IsMine(const CTxIn& txin) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 681 : : /**
# 682 : : * Returns amount of debit if the input matches the
# 683 : : * filter, otherwise returns 0
# 684 : : */
# 685 : : CAmount GetDebit(const CTxIn& txin, const isminefilter& filter) const;
# 686 : : isminetype IsMine(const CTxOut& txout) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 687 : : CAmount GetCredit(const CTxOut& txout, const isminefilter& filter) const;
# 688 : : bool IsChange(const CTxOut& txout) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 689 : : bool IsChange(const CScript& script) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 690 : : CAmount GetChange(const CTxOut& txout) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 691 : : bool IsMine(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 692 : : /** should probably be renamed to IsRelevantToMe */
# 693 : : bool IsFromMe(const CTransaction& tx) const;
# 694 : : CAmount GetDebit(const CTransaction& tx, const isminefilter& filter) const;
# 695 : : /** Returns whether all of the inputs match the filter */
# 696 : : bool IsAllFromMe(const CTransaction& tx, const isminefilter& filter) const;
# 697 : : CAmount GetCredit(const CTransaction& tx, const isminefilter& filter) const;
# 698 : : CAmount GetChange(const CTransaction& tx) const;
# 699 : : void chainStateFlushed(const CBlockLocator& loc) override;
# 700 : :
# 701 : : DBErrors LoadWallet();
# 702 : : DBErrors ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 703 : :
# 704 : : bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& purpose);
# 705 : :
# 706 : : bool DelAddressBook(const CTxDestination& address);
# 707 : :
# 708 : : unsigned int GetKeyPoolSize() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 709 : :
# 710 : : //! signify that a particular wallet feature is now used.
# 711 : : void SetMinVersion(enum WalletFeature, WalletBatch* batch_in = nullptr) override;
# 712 : :
# 713 : : //! get the current wallet format (the oldest client version guaranteed to understand this wallet)
# 714 : 1758 : int GetVersion() const { LOCK(cs_wallet); return nWalletVersion; }
# 715 : :
# 716 : : //! Get wallet transactions that conflict with given transaction (spend same outputs)
# 717 : : std::set<uint256> GetConflicts(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 718 : :
# 719 : : //! Check if a given transaction has any of its outputs spent by another transaction in the wallet
# 720 : : bool HasWalletSpend(const uint256& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 721 : :
# 722 : : //! Flush wallet (bitdb flush)
# 723 : : void Flush();
# 724 : :
# 725 : : //! Close wallet database
# 726 : : void Close();
# 727 : :
# 728 : : /** Wallet is about to be unloaded */
# 729 : : boost::signals2::signal<void ()> NotifyUnload;
# 730 : :
# 731 : : /**
# 732 : : * Address book entry changed.
# 733 : : * @note called with lock cs_wallet held.
# 734 : : */
# 735 : : boost::signals2::signal<void (CWallet *wallet, const CTxDestination
# 736 : : &address, const std::string &label, bool isMine,
# 737 : : const std::string &purpose,
# 738 : : ChangeType status)> NotifyAddressBookChanged;
# 739 : :
# 740 : : /**
# 741 : : * Wallet transaction added, removed or updated.
# 742 : : * @note called with lock cs_wallet held.
# 743 : : */
# 744 : : boost::signals2::signal<void (CWallet *wallet, const uint256 &hashTx,
# 745 : : ChangeType status)> NotifyTransactionChanged;
# 746 : :
# 747 : : /** Show progress e.g. for rescan */
# 748 : : boost::signals2::signal<void (const std::string &title, int nProgress)> ShowProgress;
# 749 : :
# 750 : : /** Watch-only address added */
# 751 : : boost::signals2::signal<void (bool fHaveWatchOnly)> NotifyWatchonlyChanged;
# 752 : :
# 753 : : /** Keypool has new keys */
# 754 : : boost::signals2::signal<void ()> NotifyCanGetAddressesChanged;
# 755 : :
# 756 : : /**
# 757 : : * Wallet status (encrypted, locked) changed.
# 758 : : * Note: Called without locks held.
# 759 : : */
# 760 : : boost::signals2::signal<void (CWallet* wallet)> NotifyStatusChanged;
# 761 : :
# 762 : : /** Inquire whether this wallet broadcasts transactions. */
# 763 : 2621 : bool GetBroadcastTransactions() const { return fBroadcastTransactions; }
# 764 : : /** Set whether this wallet broadcasts transactions. */
# 765 : 739 : void SetBroadcastTransactions(bool broadcast) { fBroadcastTransactions = broadcast; }
# 766 : :
# 767 : : /** Return whether transaction can be abandoned */
# 768 : : bool TransactionCanBeAbandoned(const uint256& hashTx) const;
# 769 : :
# 770 : : /* Mark a transaction (and it in-wallet descendants) as abandoned so its inputs may be respent. */
# 771 : : bool AbandonTransaction(const uint256& hashTx);
# 772 : :
# 773 : : /** Mark a transaction as replaced by another transaction (e.g., BIP 125). */
# 774 : : bool MarkReplaced(const uint256& originalHash, const uint256& newHash);
# 775 : :
# 776 : : /* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
# 777 : : static std::shared_ptr<CWallet> Create(interfaces::Chain* chain, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings);
# 778 : :
# 779 : : /**
# 780 : : * Wallet post-init setup
# 781 : : * Gives the wallet a chance to register repetitive tasks and complete post-init tasks
# 782 : : */
# 783 : : void postInitProcess();
# 784 : :
# 785 : : bool BackupWallet(const std::string& strDest) const;
# 786 : :
# 787 : : /* Returns true if HD is enabled */
# 788 : : bool IsHDEnabled() const;
# 789 : :
# 790 : : /* Returns true if the wallet can give out new addresses. This means it has keys in the keypool or can generate new keys */
# 791 : : bool CanGetAddresses(bool internal = false) const;
# 792 : :
# 793 : : /**
# 794 : : * Blocks until the wallet state is up-to-date to /at least/ the current
# 795 : : * chain at the time this function is entered
# 796 : : * Obviously holding cs_main/cs_wallet when going into this call may cause
# 797 : : * deadlock
# 798 : : */
# 799 : : void BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main) EXCLUSIVE_LOCKS_REQUIRED(!cs_wallet);
# 800 : :
# 801 : : /** set a single wallet flag */
# 802 : : void SetWalletFlag(uint64_t flags);
# 803 : :
# 804 : : /** Unsets a single wallet flag */
# 805 : : void UnsetWalletFlag(uint64_t flag);
# 806 : :
# 807 : : /** check if a certain wallet flag is set */
# 808 : : bool IsWalletFlagSet(uint64_t flag) const override;
# 809 : :
# 810 : : /** overwrite all flags by the given uint64_t
# 811 : : returns false if unknown, non-tolerable flags are present */
# 812 : : bool AddWalletFlags(uint64_t flags);
# 813 : : /** Loads the flags into the wallet. (used by LoadWallet) */
# 814 : : bool LoadWalletFlags(uint64_t flags);
# 815 : :
# 816 : : /** Determine if we are a legacy wallet */
# 817 : : bool IsLegacy() const;
# 818 : :
# 819 : : /** Returns a bracketed wallet name for displaying in logs, will return [default wallet] if the wallet has no name */
# 820 : 228252 : const std::string GetDisplayName() const override {
# 821 [ + + ]: 228252 : std::string wallet_name = GetName().length() == 0 ? "default wallet" : GetName();
# 822 : 228252 : return strprintf("[%s]", wallet_name);
# 823 : 228252 : };
# 824 : :
# 825 : : /** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
# 826 : : template<typename... Params>
# 827 : 189002 : void WalletLogPrintf(std::string fmt, Params... parameters) const {
# 828 : 189002 : LogPrintf(("%s " + fmt).c_str(), GetDisplayName(), parameters...);
# 829 : 189002 : };
# 830 : :
# 831 : : /** Upgrade the wallet */
# 832 : : bool UpgradeWallet(int version, bilingual_str& error);
# 833 : :
# 834 : : //! Returns all unique ScriptPubKeyMans in m_internal_spk_managers and m_external_spk_managers
# 835 : : std::set<ScriptPubKeyMan*> GetActiveScriptPubKeyMans() const;
# 836 : :
# 837 : : //! Returns all unique ScriptPubKeyMans
# 838 : : std::set<ScriptPubKeyMan*> GetAllScriptPubKeyMans() const;
# 839 : :
# 840 : : //! Get the ScriptPubKeyMan for the given OutputType and internal/external chain.
# 841 : : ScriptPubKeyMan* GetScriptPubKeyMan(const OutputType& type, bool internal) const;
# 842 : :
# 843 : : //! Get the ScriptPubKeyMan for a script
# 844 : : ScriptPubKeyMan* GetScriptPubKeyMan(const CScript& script) const;
# 845 : : //! Get the ScriptPubKeyMan by id
# 846 : : ScriptPubKeyMan* GetScriptPubKeyMan(const uint256& id) const;
# 847 : :
# 848 : : //! Get all of the ScriptPubKeyMans for a script given additional information in sigdata (populated by e.g. a psbt)
# 849 : : std::set<ScriptPubKeyMan*> GetScriptPubKeyMans(const CScript& script, SignatureData& sigdata) const;
# 850 : :
# 851 : : //! Get the SigningProvider for a script
# 852 : : std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script) const;
# 853 : : std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script, SignatureData& sigdata) const;
# 854 : :
# 855 : : //! Get the LegacyScriptPubKeyMan which is used for all types, internal, and external.
# 856 : : LegacyScriptPubKeyMan* GetLegacyScriptPubKeyMan() const;
# 857 : : LegacyScriptPubKeyMan* GetOrCreateLegacyScriptPubKeyMan();
# 858 : :
# 859 : : //! Make a LegacyScriptPubKeyMan and set it for all types, internal, and external.
# 860 : : void SetupLegacyScriptPubKeyMan();
# 861 : :
# 862 : : const CKeyingMaterial& GetEncryptionKey() const override;
# 863 : : bool HasEncryptionKeys() const override;
# 864 : :
# 865 : : /** Get last block processed height */
# 866 : : int GetLastBlockHeight() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
# 867 : 3817738 : {
# 868 : 3817738 : AssertLockHeld(cs_wallet);
# 869 : 3817738 : assert(m_last_block_processed_height >= 0);
# 870 : 3817738 : return m_last_block_processed_height;
# 871 : 3817738 : };
# 872 : : uint256 GetLastBlockHash() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
# 873 : 84235 : {
# 874 : 84235 : AssertLockHeld(cs_wallet);
# 875 : 84235 : assert(m_last_block_processed_height >= 0);
# 876 : 84235 : return m_last_block_processed;
# 877 : 84235 : }
# 878 : : /** Set last block processed height, currently only use in unit test */
# 879 : : void SetLastBlockProcessed(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
# 880 : 10 : {
# 881 : 10 : AssertLockHeld(cs_wallet);
# 882 : 10 : m_last_block_processed_height = block_height;
# 883 : 10 : m_last_block_processed = block_hash;
# 884 : 10 : };
# 885 : :
# 886 : : //! Connect the signals from ScriptPubKeyMans to the signals in CWallet
# 887 : : void ConnectScriptPubKeyManNotifiers();
# 888 : :
# 889 : : //! Instantiate a descriptor ScriptPubKeyMan from the WalletDescriptor and load it
# 890 : : void LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc);
# 891 : :
# 892 : : //! Adds the active ScriptPubKeyMan for the specified type and internal. Writes it to the wallet file
# 893 : : //! @param[in] id The unique id for the ScriptPubKeyMan
# 894 : : //! @param[in] type The OutputType this ScriptPubKeyMan provides addresses for
# 895 : : //! @param[in] internal Whether this ScriptPubKeyMan provides change addresses
# 896 : : void AddActiveScriptPubKeyMan(uint256 id, OutputType type, bool internal);
# 897 : :
# 898 : : //! Loads an active ScriptPubKeyMan for the specified type and internal. (used by LoadWallet)
# 899 : : //! @param[in] id The unique id for the ScriptPubKeyMan
# 900 : : //! @param[in] type The OutputType this ScriptPubKeyMan provides addresses for
# 901 : : //! @param[in] internal Whether this ScriptPubKeyMan provides change addresses
# 902 : : void LoadActiveScriptPubKeyMan(uint256 id, OutputType type, bool internal);
# 903 : :
# 904 : : //! Create new DescriptorScriptPubKeyMans and add them to the wallet
# 905 : : void SetupDescriptorScriptPubKeyMans() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
# 906 : :
# 907 : : //! Return the DescriptorScriptPubKeyMan for a WalletDescriptor if it is already in the wallet
# 908 : : DescriptorScriptPubKeyMan* GetDescriptorScriptPubKeyMan(const WalletDescriptor& desc) const;
# 909 : :
# 910 : : //! Add a descriptor to the wallet, return a ScriptPubKeyMan & associated output type
# 911 : : ScriptPubKeyMan* AddWalletDescriptor(WalletDescriptor& desc, const FlatSigningProvider& signing_provider, const std::string& label, bool internal);
# 912 : : };
# 913 : :
# 914 : : /**
# 915 : : * Called periodically by the schedule thread. Prompts individual wallets to resend
# 916 : : * their transactions. Actual rebroadcast schedule is managed by the wallets themselves.
# 917 : : */
# 918 : : void MaybeResendWalletTxs();
# 919 : :
# 920 : : /** RAII object to check and reserve a wallet rescan */
# 921 : : class WalletRescanReserver
# 922 : : {
# 923 : : private:
# 924 : : CWallet& m_wallet;
# 925 : : bool m_could_reserve;
# 926 : : public:
# 927 : 817 : explicit WalletRescanReserver(CWallet& w) : m_wallet(w), m_could_reserve(false) {}
# 928 : :
# 929 : : bool reserve()
# 930 : 693 : {
# 931 : 693 : assert(!m_could_reserve);
# 932 [ - + ]: 693 : if (m_wallet.fScanningWallet.exchange(true)) {
# 933 : 0 : return false;
# 934 : 0 : }
# 935 : 693 : m_wallet.m_scanning_start = GetTimeMillis();
# 936 : 693 : m_wallet.m_scanning_progress = 0;
# 937 : 693 : m_could_reserve = true;
# 938 : 693 : return true;
# 939 : 693 : }
# 940 : :
# 941 : : bool isReserved() const
# 942 : 650 : {
# 943 [ + - ][ + - ]: 650 : return (m_could_reserve && m_wallet.fScanningWallet);
# 944 : 650 : }
# 945 : :
# 946 : : ~WalletRescanReserver()
# 947 : 817 : {
# 948 [ + + ]: 817 : if (m_could_reserve) {
# 949 : 693 : m_wallet.fScanningWallet = false;
# 950 : 693 : }
# 951 : 817 : }
# 952 : : };
# 953 : :
# 954 : : struct TxSize {
# 955 : : int64_t vsize{-1};
# 956 : : int64_t weight{-1};
# 957 : : };
# 958 : :
# 959 : : /** Calculate the size of the transaction assuming all signatures are max size
# 960 : : * Use DummySignatureCreator, which inserts 71 byte signatures everywhere.
# 961 : : * NOTE: this requires that all inputs must be in mapWallet (eg the tx should
# 962 : : * be IsAllFromMe). */
# 963 : : TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, bool use_max_sig = false) EXCLUSIVE_LOCKS_REQUIRED(wallet->cs_wallet);
# 964 : : TxSize CalculateMaximumSignedTxSize(const CTransaction& tx, const CWallet* wallet, const std::vector<CTxOut>& txouts, bool use_max_sig = false);
# 965 : :
# 966 : : //! Add wallet name to persistent configuration so it will be loaded on startup.
# 967 : : bool AddWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
# 968 : :
# 969 : : //! Remove wallet name from persistent configuration so it will not be loaded on startup.
# 970 : : bool RemoveWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
# 971 : :
# 972 : : #endif // BITCOIN_WALLET_WALLET_H
|