Branch data Line data Source code
1 : : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : : // Copyright (c) 2009-present 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 <addresstype.h>
10 : : #include <consensus/amount.h>
11 : : #include <interfaces/chain.h>
12 : : #include <interfaces/handler.h>
13 : : #include <kernel/cs_main.h>
14 : : #include <logging.h>
15 : : #include <node/types.h>
16 : : #include <outputtype.h>
17 : : #include <policy/feerate.h>
18 : : #include <primitives/transaction.h>
19 : : #include <primitives/transaction_identifier.h>
20 : : #include <script/interpreter.h>
21 : : #include <script/script.h>
22 : : #include <support/allocators/secure.h>
23 : : #include <sync.h>
24 : : #include <tinyformat.h>
25 : : #include <uint256.h>
26 : : #include <util/fs.h>
27 : : #include <util/hasher.h>
28 : : #include <util/result.h>
29 : : #include <util/string.h>
30 : : #include <util/time.h>
31 : : #include <util/ui_change_type.h>
32 : : #include <wallet/crypter.h>
33 : : #include <wallet/db.h>
34 : : #include <wallet/scriptpubkeyman.h>
35 : : #include <wallet/transaction.h>
36 : : #include <wallet/types.h>
37 : : #include <wallet/walletutil.h>
38 : :
39 : : #include <atomic>
40 : : #include <cassert>
41 : : #include <cstddef>
42 : : #include <cstdint>
43 : : #include <functional>
44 : : #include <limits>
45 : : #include <map>
46 : : #include <memory>
47 : : #include <optional>
48 : : #include <set>
49 : : #include <string>
50 : : #include <unordered_map>
51 : : #include <utility>
52 : : #include <vector>
53 : :
54 : : #include <boost/signals2/signal.hpp>
55 : :
56 : : class CKey;
57 : : class CKeyID;
58 : : class CPubKey;
59 : : class Coin;
60 : : class SigningProvider;
61 : : enum class MemPoolRemovalReason;
62 : : enum class SigningResult;
63 : : namespace common {
64 : : enum class PSBTError;
65 : : } // namespace common
66 : : namespace interfaces {
67 : : class Wallet;
68 : : }
69 : : namespace wallet {
70 : : class CWallet;
71 : : class WalletBatch;
72 : : enum class DBErrors : int;
73 : : } // namespace wallet
74 : : struct CBlockLocator;
75 : : struct CExtKey;
76 : : struct FlatSigningProvider;
77 : : struct KeyOriginInfo;
78 : : struct PartiallySignedTransaction;
79 : : struct SignatureData;
80 : :
81 : : using LoadWalletFn = std::function<void(std::unique_ptr<interfaces::Wallet> wallet)>;
82 : :
83 : : struct bilingual_str;
84 : :
85 : : namespace wallet {
86 : : struct WalletContext;
87 : :
88 : : //! Explicitly delete the wallet.
89 : : //! Blocks the current thread until the wallet is destructed.
90 : : void WaitForDeleteWallet(std::shared_ptr<CWallet>&& wallet);
91 : :
92 : : bool AddWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet);
93 : : bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start, std::vector<bilingual_str>& warnings);
94 : : bool RemoveWallet(WalletContext& context, const std::shared_ptr<CWallet>& wallet, std::optional<bool> load_on_start);
95 : : std::vector<std::shared_ptr<CWallet>> GetWallets(WalletContext& context);
96 : : std::shared_ptr<CWallet> GetDefaultWallet(WalletContext& context, size_t& count);
97 : : std::shared_ptr<CWallet> GetWallet(WalletContext& context, const std::string& name);
98 : : std::shared_ptr<CWallet> LoadWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
99 : : std::shared_ptr<CWallet> CreateWallet(WalletContext& context, const std::string& name, std::optional<bool> load_on_start, DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings);
100 : : std::shared_ptr<CWallet> RestoreWallet(WalletContext& context, const fs::path& backup_file, const std::string& wallet_name, std::optional<bool> load_on_start, DatabaseStatus& status, bilingual_str& error, std::vector<bilingual_str>& warnings, bool load_after_restore = true);
101 : : std::unique_ptr<interfaces::Handler> HandleLoadWallet(WalletContext& context, LoadWalletFn load_wallet);
102 : : void NotifyWalletLoaded(WalletContext& context, const std::shared_ptr<CWallet>& wallet);
103 : : std::unique_ptr<WalletDatabase> MakeWalletDatabase(const std::string& name, const DatabaseOptions& options, DatabaseStatus& status, bilingual_str& error);
104 : :
105 : : //! -paytxfee default
106 : : constexpr CAmount DEFAULT_PAY_TX_FEE = 0;
107 : : //! -fallbackfee default
108 : : static const CAmount DEFAULT_FALLBACK_FEE = 0;
109 : : //! -discardfee default
110 : : static const CAmount DEFAULT_DISCARD_FEE = 10000;
111 : : //! -mintxfee default
112 : : static const CAmount DEFAULT_TRANSACTION_MINFEE = 1000;
113 : : //! -consolidatefeerate default
114 : : static const CAmount DEFAULT_CONSOLIDATE_FEERATE{10000}; // 10 sat/vbyte
115 : : /**
116 : : * maximum fee increase allowed to do partial spend avoidance, even for nodes with this feature disabled by default
117 : : *
118 : : * A value of -1 disables this feature completely.
119 : : * A value of 0 (current default) means to attempt to do partial spend avoidance, and use its results if the fees remain *unchanged*
120 : : * 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].
121 : : */
122 : : static const CAmount DEFAULT_MAX_AVOIDPARTIALSPEND_FEE = 0;
123 : : //! discourage APS fee higher than this amount
124 : : constexpr CAmount HIGH_APS_FEE{COIN / 10000};
125 : : //! minimum recommended increment for replacement txs
126 : : static const CAmount WALLET_INCREMENTAL_RELAY_FEE = 5000;
127 : : //! Default for -spendzeroconfchange
128 : : static const bool DEFAULT_SPEND_ZEROCONF_CHANGE = true;
129 : : //! Default for -walletrejectlongchains
130 : : static const bool DEFAULT_WALLET_REJECT_LONG_CHAINS{true};
131 : : //! -txconfirmtarget default
132 : : static const unsigned int DEFAULT_TX_CONFIRM_TARGET = 6;
133 : : //! -walletrbf default
134 : : static const bool DEFAULT_WALLET_RBF = true;
135 : : static const bool DEFAULT_WALLETBROADCAST = true;
136 : : static const bool DEFAULT_DISABLE_WALLET = false;
137 : : static const bool DEFAULT_WALLETCROSSCHAIN = false;
138 : : //! -maxtxfee default
139 : : constexpr CAmount DEFAULT_TRANSACTION_MAXFEE{COIN / 10};
140 : : //! Discourage users to set fees higher than this amount (in satoshis) per kB
141 : : constexpr CAmount HIGH_TX_FEE_PER_KB{COIN / 100};
142 : : //! -maxtxfee will warn if called with a higher fee than this amount (in satoshis)
143 : : constexpr CAmount HIGH_MAX_TX_FEE{100 * HIGH_TX_FEE_PER_KB};
144 : : //! Pre-calculated constants for input size estimation in *virtual size*
145 : : static constexpr size_t DUMMY_NESTED_P2WPKH_INPUT_SIZE = 91;
146 : :
147 : : class CCoinControl;
148 : :
149 : : //! Default for -addresstype
150 : : constexpr OutputType DEFAULT_ADDRESS_TYPE{OutputType::BECH32};
151 : :
152 : : static constexpr uint64_t KNOWN_WALLET_FLAGS =
153 : : WALLET_FLAG_AVOID_REUSE
154 : : | WALLET_FLAG_BLANK_WALLET
155 : : | WALLET_FLAG_KEY_ORIGIN_METADATA
156 : : | WALLET_FLAG_LAST_HARDENED_XPUB_CACHED
157 : : | WALLET_FLAG_DISABLE_PRIVATE_KEYS
158 : : | WALLET_FLAG_DESCRIPTORS
159 : : | WALLET_FLAG_EXTERNAL_SIGNER;
160 : :
161 : : static constexpr uint64_t MUTABLE_WALLET_FLAGS =
162 : : WALLET_FLAG_AVOID_REUSE;
163 : :
164 : : static const std::map<WalletFlags, std::string> WALLET_FLAG_TO_STRING{
165 : : {WALLET_FLAG_AVOID_REUSE, "avoid_reuse"},
166 : : {WALLET_FLAG_BLANK_WALLET, "blank"},
167 : : {WALLET_FLAG_KEY_ORIGIN_METADATA, "key_origin_metadata"},
168 : : {WALLET_FLAG_LAST_HARDENED_XPUB_CACHED, "last_hardened_xpub_cached"},
169 : : {WALLET_FLAG_DISABLE_PRIVATE_KEYS, "disable_private_keys"},
170 : : {WALLET_FLAG_DESCRIPTORS, "descriptor_wallet"},
171 : : {WALLET_FLAG_EXTERNAL_SIGNER, "external_signer"}
172 : : };
173 : :
174 : : static const std::map<std::string, WalletFlags> STRING_TO_WALLET_FLAG{
175 : : {WALLET_FLAG_TO_STRING.at(WALLET_FLAG_AVOID_REUSE), WALLET_FLAG_AVOID_REUSE},
176 : : {WALLET_FLAG_TO_STRING.at(WALLET_FLAG_BLANK_WALLET), WALLET_FLAG_BLANK_WALLET},
177 : : {WALLET_FLAG_TO_STRING.at(WALLET_FLAG_KEY_ORIGIN_METADATA), WALLET_FLAG_KEY_ORIGIN_METADATA},
178 : : {WALLET_FLAG_TO_STRING.at(WALLET_FLAG_LAST_HARDENED_XPUB_CACHED), WALLET_FLAG_LAST_HARDENED_XPUB_CACHED},
179 : : {WALLET_FLAG_TO_STRING.at(WALLET_FLAG_DISABLE_PRIVATE_KEYS), WALLET_FLAG_DISABLE_PRIVATE_KEYS},
180 : : {WALLET_FLAG_TO_STRING.at(WALLET_FLAG_DESCRIPTORS), WALLET_FLAG_DESCRIPTORS},
181 : : {WALLET_FLAG_TO_STRING.at(WALLET_FLAG_EXTERNAL_SIGNER), WALLET_FLAG_EXTERNAL_SIGNER}
182 : : };
183 : :
184 : : /** A wrapper to reserve an address from a wallet
185 : : *
186 : : * ReserveDestination is used to reserve an address.
187 : : * It is currently only used inside of CreateTransaction.
188 : : *
189 : : * Instantiating a ReserveDestination does not reserve an address. To do so,
190 : : * GetReservedDestination() needs to be called on the object. Once an address has been
191 : : * reserved, call KeepDestination() on the ReserveDestination object to make sure it is not
192 : : * returned. Call ReturnDestination() to return the address so it can be reused (for
193 : : * example, if the address was used in a new transaction
194 : : * and that transaction was not completed and needed to be aborted).
195 : : *
196 : : * If an address is reserved and KeepDestination() is not called, then the address will be
197 : : * returned when the ReserveDestination goes out of scope.
198 : : */
199 : : class ReserveDestination
200 : : {
201 : : protected:
202 : : //! The wallet to reserve from
203 : : const CWallet* const pwallet;
204 : : //! The ScriptPubKeyMan to reserve from. Based on type when GetReservedDestination is called
205 : : ScriptPubKeyMan* m_spk_man{nullptr};
206 : : OutputType const type;
207 : : //! The index of the address's key in the keypool
208 : : int64_t nIndex{-1};
209 : : //! The destination
210 : : CTxDestination address;
211 : : //! Whether this is from the internal (change output) keypool
212 : : bool fInternal{false};
213 : :
214 : : public:
215 : : //! Construct a ReserveDestination object. This does NOT reserve an address yet
216 : 3839 : explicit ReserveDestination(CWallet* pwallet, OutputType type)
217 : 3839 : : pwallet(pwallet)
218 [ + - ]: 3839 : , type(type) { }
219 : :
220 : : ReserveDestination(const ReserveDestination&) = delete;
221 : : ReserveDestination& operator=(const ReserveDestination&) = delete;
222 : :
223 : : //! Destructor. If a key has been reserved and not KeepKey'ed, it will be returned to the keypool
224 : 3839 : ~ReserveDestination()
225 : : {
226 : 3839 : ReturnDestination();
227 [ + - ]: 3839 : }
228 : :
229 : : //! Reserve an address
230 : : util::Result<CTxDestination> GetReservedDestination(bool internal);
231 : : //! Return reserved address
232 : : void ReturnDestination();
233 : : //! Keep the address. Do not return its key to the keypool when this object goes out of scope
234 : : void KeepDestination();
235 : : };
236 : :
237 : : /**
238 : : * Address book data.
239 : : */
240 : 32150 : struct CAddressBookData
241 : : {
242 : : /**
243 : : * Address label which is always nullopt for change addresses. For sending
244 : : * and receiving addresses, it will be set to an arbitrary label string
245 : : * provided by the user, or to "", which is the default label. The presence
246 : : * or absence of a label is used to distinguish change addresses from
247 : : * non-change addresses by wallet transaction listing and fee bumping code.
248 : : */
249 : : std::optional<std::string> label;
250 : :
251 : : /**
252 : : * Address purpose which was originally recorded for payment protocol
253 : : * support but now serves as a cached IsMine value. Wallet code should
254 : : * not rely on this field being set.
255 : : */
256 : : std::optional<AddressPurpose> purpose;
257 : :
258 : : /**
259 : : * Whether coins with this address have previously been spent. Set when the
260 : : * the wallet avoid_reuse option is enabled and this is an IsMine address
261 : : * that has already received funds and spent them. This is used during coin
262 : : * selection to increase privacy by not creating different transactions
263 : : * that spend from the same addresses.
264 : : */
265 : : bool previously_spent{false};
266 : :
267 : : /**
268 : : * Map containing data about previously generated receive requests
269 : : * requesting funds to be sent to this address. Only present for IsMine
270 : : * addresses. Map keys are decimal numbers uniquely identifying each
271 : : * request, and map values are serialized RecentRequestEntry objects
272 : : * containing BIP21 URI information including message and amount.
273 : : */
274 : : std::map<std::string, std::string> receive_requests{};
275 : :
276 : : /** Accessor methods. */
277 [ + - ]: 38188 : bool IsChange() const { return !label.has_value(); }
[ + + - + ]
278 [ + - - + ]: 37432 : std::string GetLabel() const { return label ? *label : std::string{}; }
279 : 32132 : void SetLabel(std::string name) { label = std::move(name); }
280 : : };
281 : :
282 : 29322 : inline std::string PurposeToString(AddressPurpose p)
283 : : {
284 [ + + - - ]: 29322 : switch(p) {
285 : 29311 : case AddressPurpose::RECEIVE: return "receive";
286 : 11 : case AddressPurpose::SEND: return "send";
287 : 0 : case AddressPurpose::REFUND: return "refund";
288 : : } // no default case so the compiler will warn when a new enum as added
289 : 0 : assert(false);
290 : : }
291 : :
292 : 2932 : inline std::optional<AddressPurpose> PurposeFromString(std::string_view s)
293 : : {
294 [ + + ]: 2932 : if (s == "receive") return AddressPurpose::RECEIVE;
295 [ + + ]: 39 : else if (s == "send") return AddressPurpose::SEND;
296 [ - + ]: 2 : else if (s == "refund") return AddressPurpose::REFUND;
297 : 2 : return {};
298 : : }
299 : :
300 [ + - ][ - - : 94202 : struct CRecipient
+ - + - +
- + - ][ +
- + - + -
# # # # ]
301 : : {
302 : : CTxDestination dest;
303 : : CAmount nAmount;
304 : : bool fSubtractFeeFromAmount;
305 : : };
306 : :
307 : : class WalletRescanReserver; //forward declarations for ScanForWalletTransactions/RescanFromTime
308 : : /**
309 : : * A CWallet maintains a set of transactions and balances, and provides the ability to create new transactions.
310 : : */
311 : : class CWallet final : public WalletStorage, public interfaces::Chain::Notifications
312 : : {
313 : : private:
314 : : CKeyingMaterial vMasterKey GUARDED_BY(cs_wallet);
315 : :
316 : : bool Unlock(const CKeyingMaterial& vMasterKeyIn);
317 : :
318 : : std::atomic<bool> fAbortRescan{false};
319 : : std::atomic<bool> fScanningWallet{false}; // controlled by WalletRescanReserver
320 : : std::atomic<bool> m_scanning_with_passphrase{false};
321 : : std::atomic<SteadyClock::time_point> m_scanning_start{SteadyClock::time_point{}};
322 : : std::atomic<double> m_scanning_progress{0};
323 : : friend class WalletRescanReserver;
324 : :
325 : : /** The next scheduled rebroadcast of wallet transactions. */
326 : : NodeClock::time_point m_next_resend{GetDefaultNextResend()};
327 : : /** Whether this wallet will submit newly created transactions to the node's mempool and
328 : : * prompt rebroadcasts (see ResendWalletTransactions()). */
329 : : bool fBroadcastTransactions = false;
330 : : // Local time that the tip block was received. Used to schedule wallet rebroadcasts.
331 : : std::atomic<int64_t> m_best_block_time {0};
332 : :
333 : : // First created key time. Used to skip blocks prior to this time.
334 : : // 'std::numeric_limits<int64_t>::max()' if wallet is blank.
335 : : std::atomic<int64_t> m_birth_time{std::numeric_limits<int64_t>::max()};
336 : :
337 : : /**
338 : : * Used to keep track of spent outpoints, and
339 : : * detect and report conflicts (double-spends or
340 : : * mutated transactions where the mutant gets mined).
341 : : */
342 : : typedef std::unordered_multimap<COutPoint, Txid, SaltedOutpointHasher> TxSpends;
343 : : TxSpends mapTxSpends GUARDED_BY(cs_wallet);
344 : : void AddToSpends(const COutPoint& outpoint, const Txid& txid) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
345 : : void AddToSpends(const CWalletTx& wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
346 : :
347 : : /**
348 : : * Add a transaction to the wallet, or update it. confirm.block_* should
349 : : * be set when the transaction was known to be included in a block. When
350 : : * block_hash.IsNull(), then wallet state is not updated in AddToWallet, but
351 : : * notifications happen and cached balances are marked dirty.
352 : : *
353 : : * If fUpdate is true, existing transactions will be updated.
354 : : * TODO: One exception to this is that the abandoned state is cleared under the
355 : : * assumption that any further notification of a transaction that was considered
356 : : * abandoned is an indication that it is not safe to be considered abandoned.
357 : : * Abandoned state should probably be more carefully tracked via different
358 : : * chain notifications or by checking mempool presence when necessary.
359 : : *
360 : : * Should be called with rescanning_old_block set to true, if the transaction is
361 : : * not discovered in real time, but during a rescan of old blocks.
362 : : */
363 : : bool AddToWalletIfInvolvingMe(const CTransactionRef& tx, const SyncTxState& state, bool fUpdate, bool rescanning_old_block) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
364 : :
365 : : /** Mark a transaction (and its in-wallet descendants) as conflicting with a particular block. */
366 : : void MarkConflicted(const uint256& hashBlock, int conflicting_height, const Txid& hashTx);
367 : :
368 : : enum class TxUpdate { UNCHANGED, CHANGED, NOTIFY_CHANGED };
369 : :
370 : : using TryUpdatingStateFn = std::function<TxUpdate(CWalletTx& wtx)>;
371 : :
372 : : /** Mark a transaction (and its in-wallet descendants) as a particular tx state. */
373 : : void RecursiveUpdateTxState(const Txid& tx_hash, const TryUpdatingStateFn& try_updating_state) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
374 : : void RecursiveUpdateTxState(WalletBatch* batch, const Txid& tx_hash, const TryUpdatingStateFn& try_updating_state) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
375 : :
376 : : /** Mark a transaction's inputs dirty, thus forcing the outputs to be recomputed */
377 : : void MarkInputsDirty(const CTransactionRef& tx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
378 : :
379 : : void SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator>) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
380 : :
381 : : bool SyncTransaction(const CTransactionRef& tx, const SyncTxState& state, bool update_tx = true, bool rescanning_old_block = false) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
382 : :
383 : : /** WalletFlags set on this wallet. */
384 : : std::atomic<uint64_t> m_wallet_flags{0};
385 : :
386 : : bool SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& address, const std::string& strName, const std::optional<AddressPurpose>& strPurpose);
387 : :
388 : : //! Unsets a wallet flag and saves it to disk
389 : : void UnsetWalletFlagWithDB(WalletBatch& batch, uint64_t flag);
390 : :
391 : : //! Unset the blank wallet flag and saves it to disk
392 : : void UnsetBlankWalletFlag(WalletBatch& batch) override;
393 : :
394 : : /** Interface for accessing chain state. */
395 : : interfaces::Chain* m_chain;
396 : :
397 : : /** Wallet name: relative directory name or "" for default wallet. */
398 : : std::string m_name;
399 : :
400 : : /** Internal database handle. */
401 : : std::unique_ptr<WalletDatabase> m_database;
402 : :
403 : : /**
404 : : * The following is used to keep track of how far behind the wallet is
405 : : * from the chain sync, and to allow clients to block on us being caught up.
406 : : *
407 : : * Processed hash is a pointer on node's tip and doesn't imply that the wallet
408 : : * has scanned sequentially all blocks up to this one.
409 : : */
410 : : uint256 m_last_block_processed GUARDED_BY(cs_wallet);
411 : :
412 : : /** Height of last block processed is used by wallet to know depth of transactions
413 : : * without relying on Chain interface beyond asynchronous updates. For safety, we
414 : : * initialize it to -1. Height is a pointer on node's tip and doesn't imply
415 : : * that the wallet has scanned sequentially all blocks up to this one.
416 : : */
417 : : int m_last_block_processed_height GUARDED_BY(cs_wallet) = -1;
418 : :
419 : : std::map<OutputType, ScriptPubKeyMan*> m_external_spk_managers;
420 : : std::map<OutputType, ScriptPubKeyMan*> m_internal_spk_managers;
421 : :
422 : : // Indexed by a unique identifier produced by each ScriptPubKeyMan using
423 : : // ScriptPubKeyMan::GetID. In many cases it will be the hash of an internal structure
424 : : std::map<uint256, std::unique_ptr<ScriptPubKeyMan>> m_spk_managers;
425 : :
426 : : // Appends spk managers into the main 'm_spk_managers'.
427 : : // Must be the only method adding data to it.
428 : : void AddScriptPubKeyMan(const uint256& id, std::unique_ptr<ScriptPubKeyMan> spkm_man);
429 : :
430 : : // Same as 'AddActiveScriptPubKeyMan' but designed for use within a batch transaction context
431 : : void AddActiveScriptPubKeyManWithDb(WalletBatch& batch, uint256 id, OutputType type, bool internal);
432 : :
433 : : /** Store wallet flags */
434 : : void SetWalletFlagWithDB(WalletBatch& batch, uint64_t flags);
435 : :
436 : : //! Cache of descriptor ScriptPubKeys used for IsMine. Maps ScriptPubKey to set of spkms
437 : : std::unordered_map<CScript, std::vector<ScriptPubKeyMan*>, SaltedSipHasher> m_cached_spks;
438 : :
439 : : //! Set of both spent and unspent transaction outputs owned by this wallet
440 : : std::unordered_map<COutPoint, WalletTXO, SaltedOutpointHasher> m_txos GUARDED_BY(cs_wallet);
441 : :
442 : : /**
443 : : * Catch wallet up to current chain, scanning new blocks, updating the best
444 : : * block locator and m_last_block_processed, and registering for
445 : : * notifications about new blocks and transactions.
446 : : */
447 : : static bool AttachChain(const std::shared_ptr<CWallet>& wallet, interfaces::Chain& chain, const bool rescan_required, bilingual_str& error, std::vector<bilingual_str>& warnings);
448 : :
449 : : static NodeClock::time_point GetDefaultNextResend();
450 : :
451 : : // Update last block processed in memory only
452 : : void SetLastBlockProcessedInMem(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
453 : :
454 : : //! Update mempool conflicts for TRUC sibling transactions
455 : : void UpdateTrucSiblingConflicts(const CWalletTx& parent_wtx, const Txid& child_txid, bool add_conflict) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
456 : :
457 : : public:
458 : : /**
459 : : * Main wallet lock.
460 : : * This lock protects all the fields added by CWallet.
461 : : */
462 : : mutable RecursiveMutex cs_wallet;
463 : :
464 : 172002 : WalletDatabase& GetDatabase() const override
465 : : {
466 [ - + ]: 172002 : assert(static_cast<bool>(m_database));
467 : 172002 : return *m_database;
468 : : }
469 : :
470 : : /** Get a name for this wallet for logging/debugging purposes.
471 : : */
472 [ + - + - : 117875 : const std::string& GetName() const { return m_name; }
# # # # #
# # # #
# ][ + + +
- + - + -
+ - + - +
- ][ # # #
# # # # #
# # # # #
# # # # #
# # # # #
# # # ][ +
- # # # #
# # ][ - +
- + + - +
- + - - -
- - - - +
- - + - +
+ + - + ]
473 : :
474 : : typedef std::map<unsigned int, CMasterKey> MasterKeyMap;
475 : : MasterKeyMap mapMasterKeys;
476 : : unsigned int nMasterKeyMaxID = 0;
477 : :
478 : : /** Construct wallet with specified name and database implementation. */
479 : 1010 : CWallet(interfaces::Chain* chain, const std::string& name, std::unique_ptr<WalletDatabase> database)
480 : 2020 : : m_chain(chain),
481 : 2020 : m_name(name),
482 [ + - + - : 2020 : m_database(std::move(database))
- + + - +
- + - + -
+ - + - +
- + - +
- ]
483 : : {
484 : 1010 : }
485 : :
486 : 1975 : ~CWallet()
487 : 1010 : {
488 : : // Should not have slots connected at this point.
489 [ - + ]: 1010 : assert(NotifyUnload.empty());
490 : 1975 : }
491 : :
492 : : bool IsCrypted() const;
493 : : bool IsLocked() const override;
494 : : bool Lock();
495 : :
496 : : /** Interface to assert chain access */
497 [ - + + + ]: 8249 : bool HaveChain() const { return m_chain ? true : false; }
498 : :
499 : : /** Map from txid to CWalletTx for all transactions this wallet is
500 : : * interested in, including received and sent transactions. */
501 : : std::unordered_map<Txid, CWalletTx, SaltedTxidHasher> mapWallet GUARDED_BY(cs_wallet);
502 : :
503 : : typedef std::multimap<int64_t, CWalletTx*> TxItems;
504 : : TxItems wtxOrdered;
505 : :
506 : : int64_t nOrderPosNext GUARDED_BY(cs_wallet) = 0;
507 : :
508 : : std::map<CTxDestination, CAddressBookData> m_address_book GUARDED_BY(cs_wallet);
509 : : const CAddressBookData* FindAddressBookEntry(const CTxDestination&, bool allow_change = false) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
510 : :
511 : : /** Set of Coins owned by this wallet that we won't try to spend from. A
512 : : * Coin may be locked if it has already been used to fund a transaction
513 : : * that hasn't confirmed yet. We wouldn't consider the Coin spent already,
514 : : * but also shouldn't try to use it again.
515 : : * bool to track whether this locked coin is persisted to disk.
516 : : */
517 : : std::map<COutPoint, bool> m_locked_coins GUARDED_BY(cs_wallet);
518 : :
519 : : /** Registered interfaces::Chain::Notifications handler. */
520 : : std::unique_ptr<interfaces::Handler> m_chain_notifications_handler;
521 : :
522 : : /** Interface for accessing chain state. */
523 [ - + ]: 1020869 : interfaces::Chain& chain() const { assert(m_chain); return *m_chain; }
524 : :
525 : : const CWalletTx* GetWalletTx(const Txid& hash) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
526 : :
527 : : std::set<Txid> GetTxConflicts(const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
528 : :
529 : 5155 : const std::unordered_map<COutPoint, WalletTXO, SaltedOutpointHasher>& GetTXOs() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet) { AssertLockHeld(cs_wallet); return m_txos; };
530 : : std::optional<WalletTXO> GetTXO(const COutPoint& outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
531 : :
532 : : /** Cache outputs that belong to the wallet from a single transaction */
533 : : void RefreshTXOsFromTx(const CWalletTx& wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
534 : : /** Cache outputs that belong to the wallet for all transactions in the wallet */
535 : : void RefreshAllTXOs() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
536 : :
537 : : /**
538 : : * Return depth of transaction in blockchain:
539 : : * <0 : conflicts with a transaction this deep in the blockchain
540 : : * 0 : in memory pool, waiting to be included in a block
541 : : * >=1 : this many blocks deep in the main chain
542 : : *
543 : : * Preconditions: it is only valid to call this function when the wallet is
544 : : * online and the block index is loaded. So this cannot be called by
545 : : * bitcoin-wallet tool code or by wallet migration code. If this is called
546 : : * without the wallet being online, it won't be able able to determine the
547 : : * the height of the last block processed, or the heights of blocks
548 : : * referenced in transaction, and might cause assert failures.
549 : : */
550 : : int GetTxDepthInMainChain(const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
551 : :
552 : : /**
553 : : * @return number of blocks to maturity for this transaction:
554 : : * 0 : is not a coinbase transaction, or is a mature coinbase transaction
555 : : * >0 : is a coinbase transaction which matures in this many blocks
556 : : */
557 : : int GetTxBlocksToMaturity(const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
558 : : bool IsTxImmatureCoinBase(const CWalletTx& wtx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
559 : :
560 : : bool IsSpent(const COutPoint& outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
561 : :
562 : : // Whether this or any known scriptPubKey with the same single key has been spent.
563 : : bool IsSpentKey(const CScript& scriptPubKey) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
564 : : void SetSpentKeyState(WalletBatch& batch, const Txid& hash, unsigned int n, bool used, std::set<CTxDestination>& tx_destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
565 : :
566 : : /** Display address on an external signer. */
567 : : util::Result<void> DisplayAddress(const CTxDestination& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
568 : :
569 : : bool IsLockedCoin(const COutPoint& output) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
570 : : void LoadLockedCoin(const COutPoint& coin, bool persistent) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
571 : : bool LockCoin(const COutPoint& output, bool persist) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
572 : : bool UnlockCoin(const COutPoint& output) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
573 : : bool UnlockAllCoins() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
574 : : void ListLockedCoins(std::vector<COutPoint>& vOutpts) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
575 : :
576 : : /*
577 : : * Rescan abort properties
578 : : */
579 [ # # ]: 0 : void AbortRescan() { fAbortRescan = true; }
580 [ - + ]: 589 : bool IsAbortingRescan() const { return fAbortRescan; }
581 [ - + ]: 427 : bool IsScanning() const { return fScanningWallet; }
582 [ - + + + : 48 : bool IsScanningWithPassphrase() const { return m_scanning_with_passphrase; }
+ + ]
583 [ # # ]: 0 : SteadyClock::duration ScanningDuration() const { return fScanningWallet ? SteadyClock::now() - m_scanning_start.load() : SteadyClock::duration{}; }
584 [ # # ]: 0 : double ScanningProgress() const { return fScanningWallet ? (double) m_scanning_progress : 0; }
585 : :
586 : : //! Upgrade DescriptorCaches
587 : : void UpgradeDescriptorCache() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
588 : :
589 : : //! Marks destination as previously spent.
590 : : void LoadAddressPreviouslySpent(const CTxDestination& dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
591 : : //! Appends payment request to destination.
592 : : void LoadAddressReceiveRequest(const CTxDestination& dest, const std::string& id, const std::string& request) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
593 : :
594 : : //! Holds a timestamp at which point the wallet is scheduled (externally) to be relocked. Caller must arrange for actual relocking to occur via Lock().
595 : : int64_t nRelockTime GUARDED_BY(cs_wallet){0};
596 : :
597 : : // Used to prevent concurrent calls to walletpassphrase RPC.
598 : : Mutex m_unlock_mutex;
599 : : // Used to prevent deleting the passphrase from memory when it is still in use.
600 : : RecursiveMutex m_relock_mutex;
601 : :
602 : : bool Unlock(const SecureString& strWalletPassphrase);
603 : : bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase);
604 : : bool EncryptWallet(const SecureString& strWalletPassphrase);
605 : :
606 : : unsigned int ComputeTimeSmart(const CWalletTx& wtx, bool rescanning_old_block) const;
607 : :
608 : : /**
609 : : * Increment the next transaction order id
610 : : * @return next transaction order id
611 : : */
612 : : int64_t IncOrderPosNext(WalletBatch *batch = nullptr) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
613 : : DBErrors ReorderTransactions();
614 : :
615 : : void MarkDirty();
616 : :
617 : : //! Callback for updating transaction metadata in mapWallet.
618 : : //!
619 : : //! @param wtx - reference to mapWallet transaction to update
620 : : //! @param new_tx - true if wtx is newly inserted, false if it previously existed
621 : : //!
622 : : //! @return true if wtx is changed and needs to be saved to disk, otherwise false
623 : : using UpdateWalletTxFn = std::function<bool(CWalletTx& wtx, bool new_tx)>;
624 : :
625 : : /**
626 : : * Add the transaction to the wallet, wrapping it up inside a CWalletTx
627 : : * @return the recently added wtx pointer or nullptr if there was a db write error.
628 : : */
629 : : CWalletTx* AddToWallet(CTransactionRef tx, const TxState& state, const UpdateWalletTxFn& update_wtx=nullptr, bool rescanning_old_block = false);
630 : : bool LoadToWallet(const Txid& hash, const UpdateWalletTxFn& fill_wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
631 : : void transactionAddedToMempool(const CTransactionRef& tx) override;
632 : : void blockConnected(ChainstateRole role, const interfaces::BlockInfo& block) override;
633 : : void blockDisconnected(const interfaces::BlockInfo& block) override;
634 : : void updatedBlockTip() override;
635 : : int64_t RescanFromTime(int64_t startTime, const WalletRescanReserver& reserver, bool update);
636 : :
637 : : struct ScanResult {
638 : : enum { SUCCESS, FAILURE, USER_ABORT } status = SUCCESS;
639 : :
640 : : //! Hash and height of most recent block that was successfully scanned.
641 : : //! Unset if no blocks were scanned due to read errors or the chain
642 : : //! being empty.
643 : : uint256 last_scanned_block;
644 : : std::optional<int> last_scanned_height;
645 : :
646 : : //! Height of the most recent block that could not be scanned due to
647 : : //! read errors or pruning. Will be set if status is FAILURE, unset if
648 : : //! status is SUCCESS, and may or may not be set if status is
649 : : //! USER_ABORT.
650 : : uint256 last_failed_block;
651 : : };
652 : : ScanResult ScanForWalletTransactions(const uint256& start_block, int start_height, std::optional<int> max_height, const WalletRescanReserver& reserver, bool fUpdate, const bool save_progress);
653 : : void transactionRemovedFromMempool(const CTransactionRef& tx, MemPoolRemovalReason reason) override;
654 : : /** Set the next time this wallet should resend transactions to 12-36 hours from now, ~1 day on average. */
655 [ + - ]: 3 : void SetNextResend() { m_next_resend = GetDefaultNextResend(); }
656 : : /** Return true if all conditions for periodically resending transactions are met. */
657 : : bool ShouldResend() const;
658 : : void ResubmitWalletTransactions(node::TxBroadcast broadcast_method, bool force);
659 : :
660 : : OutputType TransactionChangeType(const std::optional<OutputType>& change_type, const std::vector<CRecipient>& vecSend) const;
661 : :
662 : : /** Fetch the inputs and sign with SIGHASH_ALL. */
663 : : bool SignTransaction(CMutableTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
664 : : /** Sign the tx given the input coins and sighash. */
665 : : bool SignTransaction(CMutableTransaction& tx, const std::map<COutPoint, Coin>& coins, int sighash, std::map<int, bilingual_str>& input_errors) const;
666 : : SigningResult SignMessage(const std::string& message, const PKHash& pkhash, std::string& str_sig) const;
667 : :
668 : : /**
669 : : * Fills out a PSBT with information from the wallet. Fills in UTXOs if we have
670 : : * them. Tries to sign if sign=true. Sets `complete` if the PSBT is now complete
671 : : * (i.e. has all required signatures or signature-parts, and is ready to
672 : : * finalize.) Sets `error` and returns false if something goes wrong.
673 : : *
674 : : * @param[in] psbtx PartiallySignedTransaction to fill in
675 : : * @param[out] complete indicates whether the PSBT is now complete
676 : : * @param[in] sighash_type the sighash type to use when signing (if PSBT does not specify)
677 : : * @param[in] sign whether to sign or not
678 : : * @param[in] bip32derivs whether to fill in bip32 derivation information if available
679 : : * @param[out] n_signed the number of inputs signed by this wallet
680 : : * @param[in] finalize whether to create the final scriptSig or scriptWitness if possible
681 : : * return error
682 : : */
683 : : std::optional<common::PSBTError> FillPSBT(PartiallySignedTransaction& psbtx,
684 : : bool& complete,
685 : : std::optional<int> sighash_type = std::nullopt,
686 : : bool sign = true,
687 : : bool bip32derivs = true,
688 : : size_t* n_signed = nullptr,
689 : : bool finalize = true) const;
690 : :
691 : : /**
692 : : * Submit the transaction to the node's mempool and then relay to peers.
693 : : * Should be called after CreateTransaction unless you want to abort
694 : : * broadcasting the transaction.
695 : : *
696 : : * @param[in] tx The transaction to be broadcast.
697 : : * @param[in] mapValue key-values to be set on the transaction.
698 : : * @param[in] orderForm BIP 70 / BIP 21 order form details to be set on the transaction.
699 : : */
700 : : void CommitTransaction(CTransactionRef tx, mapValue_t mapValue, std::vector<std::pair<std::string, std::string>> orderForm);
701 : :
702 : : /** Pass this transaction to node for optional mempool insertion and relay to peers. */
703 : : bool SubmitTxMemoryPoolAndRelay(CWalletTx& wtx, std::string& err_string, node::TxBroadcast broadcast_method) const
704 : : EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
705 : :
706 : : /** Updates wallet birth time if 'time' is below it */
707 : : void MaybeUpdateBirthTime(int64_t time);
708 : :
709 : : CFeeRate m_pay_tx_fee{DEFAULT_PAY_TX_FEE};
710 : : unsigned int m_confirm_target{DEFAULT_TX_CONFIRM_TARGET};
711 : : /** Allow Coin Selection to pick unconfirmed UTXOs that were sent from our own wallet if it
712 : : * cannot fund the transaction otherwise. */
713 : : bool m_spend_zero_conf_change{DEFAULT_SPEND_ZEROCONF_CHANGE};
714 : : bool m_signal_rbf{DEFAULT_WALLET_RBF};
715 : : bool m_allow_fallback_fee{true}; //!< will be false if -fallbackfee=0
716 : : CFeeRate m_min_fee{DEFAULT_TRANSACTION_MINFEE}; //!< Override with -mintxfee
717 : : /**
718 : : * If fee estimation does not have enough data to provide estimates, use this fee instead.
719 : : * Has no effect if not using fee estimation
720 : : * Override with -fallbackfee
721 : : */
722 : : CFeeRate m_fallback_fee{DEFAULT_FALLBACK_FEE};
723 : :
724 : : /** If the cost to spend a change output at this feerate is greater than the value of the
725 : : * output itself, just drop it to fees. */
726 : : CFeeRate m_discard_rate{DEFAULT_DISCARD_FEE};
727 : :
728 : : /** When the actual feerate is less than the consolidate feerate, we will tend to make transactions which
729 : : * consolidate inputs. When the actual feerate is greater than the consolidate feerate, we will tend to make
730 : : * transactions which have the lowest fees.
731 : : */
732 : : CFeeRate m_consolidate_feerate{DEFAULT_CONSOLIDATE_FEERATE};
733 : :
734 : : /** The maximum fee amount we're willing to pay to prioritize partial spend avoidance. */
735 : : CAmount m_max_aps_fee{DEFAULT_MAX_AVOIDPARTIALSPEND_FEE}; //!< note: this is absolute fee, not fee rate
736 : : OutputType m_default_address_type{DEFAULT_ADDRESS_TYPE};
737 : : /**
738 : : * Default output type for change outputs. When unset, automatically choose type
739 : : * based on address type setting and the types other of non-change outputs
740 : : * (see -changetype option documentation and implementation in
741 : : * CWallet::TransactionChangeType for details).
742 : : */
743 : : std::optional<OutputType> m_default_change_type{};
744 : : /** Absolute maximum transaction fee (in satoshis) used by default for the wallet */
745 : : CAmount m_default_max_tx_fee{DEFAULT_TRANSACTION_MAXFEE};
746 : :
747 : : /** Number of pre-generated keys/scripts by each spkm (part of the look-ahead process, used to detect payments) */
748 : : int64_t m_keypool_size{DEFAULT_KEYPOOL_SIZE};
749 : :
750 : : /** Notify external script when a wallet transaction comes in or is updated (handled by -walletnotify) */
751 : : std::string m_notify_tx_changed_script;
752 : :
753 : : size_t KeypoolCountExternalKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
754 : : bool TopUpKeyPool(unsigned int kpSize = 0);
755 : :
756 : : // Filter struct for 'ListAddrBookAddresses'
757 [ + - ]: 120 : struct AddrBookFilter {
758 : : // Fetch addresses with the provided label
759 : : std::optional<std::string> m_op_label{std::nullopt};
760 : : // Don't include change addresses by default
761 : : bool ignore_change{true};
762 : : };
763 : :
764 : : /**
765 : : * Filter and retrieve destinations stored in the addressbook
766 : : */
767 : : std::vector<CTxDestination> ListAddrBookAddresses(const std::optional<AddrBookFilter>& filter) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
768 : :
769 : : /**
770 : : * Retrieve all the known labels in the address book
771 : : */
772 : : std::set<std::string> ListAddrBookLabels(const std::optional<AddressPurpose> purpose) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
773 : :
774 : : /**
775 : : * Walk-through the address book entries.
776 : : * Stops when the provided 'ListAddrBookFunc' returns false.
777 : : */
778 : : using ListAddrBookFunc = std::function<void(const CTxDestination& dest, const std::string& label, bool is_change, const std::optional<AddressPurpose> purpose)>;
779 : : void ForEachAddrBookEntry(const ListAddrBookFunc& func) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
780 : :
781 : : /**
782 : : * Marks all outputs in each one of the destinations dirty, so their cache is
783 : : * reset and does not return outdated information.
784 : : */
785 : : void MarkDestinationsDirty(const std::set<CTxDestination>& destinations) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
786 : :
787 : : util::Result<CTxDestination> GetNewDestination(const OutputType type, const std::string label);
788 : : util::Result<CTxDestination> GetNewChangeDestination(const OutputType type);
789 : :
790 : : bool IsMine(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
791 : : bool IsMine(const CScript& script) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
792 : : /** Returns amount of debit, i.e. the amount leaving this wallet due to this input */
793 : : CAmount GetDebit(const CTxIn& txin) const;
794 : : bool IsMine(const CTxOut& txout) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
795 : : bool IsMine(const CTransaction& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
796 : : bool IsMine(const COutPoint& outpoint) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
797 : : /** should probably be renamed to IsRelevantToMe */
798 : : bool IsFromMe(const CTransaction& tx) const;
799 : : CAmount GetDebit(const CTransaction& tx) const;
800 : :
801 : : DBErrors LoadWallet();
802 : :
803 : : /** Erases the provided transactions from the wallet. */
804 : : util::Result<void> RemoveTxs(std::vector<Txid>& txs_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
805 : : util::Result<void> RemoveTxs(WalletBatch& batch, std::vector<Txid>& txs_to_remove) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
806 : :
807 : : bool SetAddressBook(const CTxDestination& address, const std::string& strName, const std::optional<AddressPurpose>& purpose);
808 : :
809 : : bool DelAddressBook(const CTxDestination& address);
810 : : bool DelAddressBookWithDB(WalletBatch& batch, const CTxDestination& address);
811 : :
812 : : bool IsAddressPreviouslySpent(const CTxDestination& dest) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
813 : : bool SetAddressPreviouslySpent(WalletBatch& batch, const CTxDestination& dest, bool used) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
814 : :
815 : : std::vector<std::string> GetAddressReceiveRequests() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
816 : : bool SetAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id, const std::string& value) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
817 : : bool EraseAddressReceiveRequest(WalletBatch& batch, const CTxDestination& dest, const std::string& id) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
818 : :
819 : : unsigned int GetKeyPoolSize() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
820 : :
821 : : //! Get wallet transactions that conflict with given transaction (spend same outputs)
822 : : std::set<Txid> GetConflicts(const Txid& txid) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
823 : :
824 : : //! Check if a given transaction has any of its outputs spent by another transaction in the wallet
825 : : bool HasWalletSpend(const CTransactionRef& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
826 : :
827 : : //! Close wallet database
828 : : void Close();
829 : :
830 : : /** Wallet is about to be unloaded */
831 : : boost::signals2::signal<void ()> NotifyUnload;
832 : :
833 : : /**
834 : : * Address book entry changed.
835 : : * @note called without lock cs_wallet held.
836 : : */
837 : : boost::signals2::signal<void(const CTxDestination& address,
838 : : const std::string& label, bool isMine,
839 : : AddressPurpose purpose, ChangeType status)>
840 : : NotifyAddressBookChanged;
841 : :
842 : : /**
843 : : * Wallet transaction added, removed or updated.
844 : : * @note called with lock cs_wallet held.
845 : : */
846 : : boost::signals2::signal<void(const Txid& hashTx, ChangeType status)> NotifyTransactionChanged;
847 : :
848 : : /** Show progress e.g. for rescan */
849 : : boost::signals2::signal<void (const std::string &title, int nProgress)> ShowProgress;
850 : :
851 : : /** Keypool has new keys */
852 : : boost::signals2::signal<void ()> NotifyCanGetAddressesChanged;
853 : :
854 : : /**
855 : : * Wallet status (encrypted, locked) changed.
856 : : * Note: Called without locks held.
857 : : */
858 : : boost::signals2::signal<void (CWallet* wallet)> NotifyStatusChanged;
859 : :
860 : : /** Inquire whether this wallet broadcasts transactions. */
861 [ + - ]: 1646 : bool GetBroadcastTransactions() const { return fBroadcastTransactions; }
862 : : /** Set whether this wallet broadcasts transactions. */
863 : 918 : void SetBroadcastTransactions(bool broadcast) { fBroadcastTransactions = broadcast; }
864 : :
865 : : /** Return whether transaction can be abandoned */
866 : : bool TransactionCanBeAbandoned(const Txid& hashTx) const;
867 : :
868 : : /* Mark a transaction (and it in-wallet descendants) as abandoned so its inputs may be respent. */
869 : : bool AbandonTransaction(const Txid& hashTx);
870 : : bool AbandonTransaction(CWalletTx& tx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
871 : :
872 : : /** Mark a transaction as replaced by another transaction. */
873 : : bool MarkReplaced(const Txid& originalHash, const Txid& newHash);
874 : :
875 : : /* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
876 : : static std::shared_ptr<CWallet> Create(WalletContext& context, const std::string& name, std::unique_ptr<WalletDatabase> database, uint64_t wallet_creation_flags, bilingual_str& error, std::vector<bilingual_str>& warnings);
877 : :
878 : : /**
879 : : * Wallet post-init setup
880 : : * Gives the wallet a chance to register repetitive tasks and complete post-init tasks
881 : : */
882 : : void postInitProcess();
883 : :
884 : : bool BackupWallet(const std::string& strDest) const;
885 : :
886 : : /* Returns true if HD is enabled */
887 : : bool IsHDEnabled() const;
888 : :
889 : : /* Returns true if the wallet can give out new addresses. This means it has keys in the keypool or can generate new keys */
890 : : bool CanGetAddresses(bool internal = false) const;
891 : :
892 : : /* Returns the time of the first created key or, in case of an import, it could be the time of the first received transaction */
893 [ + + ]: 426 : int64_t GetBirthTime() const { return m_birth_time; }
894 : :
895 : : /**
896 : : * Blocks until the wallet state is up-to-date to /at least/ the current
897 : : * chain at the time this function is entered
898 : : * Obviously holding cs_main/cs_wallet when going into this call may cause
899 : : * deadlock
900 : : */
901 : : void BlockUntilSyncedToCurrentChain() const LOCKS_EXCLUDED(::cs_main) EXCLUSIVE_LOCKS_REQUIRED(!cs_wallet);
902 : :
903 : : /** set a single wallet flag */
904 : : void SetWalletFlag(uint64_t flags);
905 : :
906 : : /** Unsets a single wallet flag */
907 : : void UnsetWalletFlag(uint64_t flag);
908 : :
909 : : /** check if a certain wallet flag is set */
910 : : bool IsWalletFlagSet(uint64_t flag) const override;
911 : :
912 : : /** overwrite all flags by the given uint64_t
913 : : flags must be uninitialised (or 0)
914 : : only known flags may be present */
915 : : void InitWalletFlags(uint64_t flags);
916 : : /** Loads the flags into the wallet. (used by LoadWallet) */
917 : : bool LoadWalletFlags(uint64_t flags);
918 : : //! Retrieve all of the wallet's flags
919 : : uint64_t GetWalletFlags() const;
920 : :
921 : : /** Return wallet name for use in logs, will return "default wallet" if the wallet has no name. */
922 : 54030 : std::string LogName() const override
923 : : {
924 [ - + ]: 54030 : std::string name{GetName()};
925 [ + + + - ]: 107160 : return name.empty() ? "default wallet" : name;
926 : 54030 : };
927 : :
928 : : /** Return wallet name for display, like LogName() but translates "default wallet" string. */
929 : 2090 : std::string DisplayName() const
930 : : {
931 [ - + ]: 2090 : std::string name{GetName()};
932 [ + + + - ]: 4149 : return name.empty() ? _("default wallet") : name;
933 : 2090 : };
934 : :
935 : : /** Prepends the wallet name in logging output to ease debugging in multi-wallet use cases */
936 : : template <typename... Params>
937 : 53521 : void WalletLogPrintf(util::ConstevalFormatString<sizeof...(Params)> wallet_fmt, const Params&... params) const
938 : : {
939 [ + - + - ]: 107042 : LogInfo("[%s] %s", LogName(), tfm::format(wallet_fmt, params...));
940 : 53521 : };
941 : :
942 : : //! Returns all unique ScriptPubKeyMans in m_internal_spk_managers and m_external_spk_managers
943 : : std::set<ScriptPubKeyMan*> GetActiveScriptPubKeyMans() const;
944 : : bool IsActiveScriptPubKeyMan(const ScriptPubKeyMan& spkm) const;
945 : :
946 : : //! Returns all unique ScriptPubKeyMans
947 : : std::set<ScriptPubKeyMan*> GetAllScriptPubKeyMans() const;
948 : :
949 : : //! Get the ScriptPubKeyMan for the given OutputType and internal/external chain.
950 : : ScriptPubKeyMan* GetScriptPubKeyMan(const OutputType& type, bool internal) const;
951 : :
952 : : //! Get all the ScriptPubKeyMans for a script
953 : : std::set<ScriptPubKeyMan*> GetScriptPubKeyMans(const CScript& script) const;
954 : : //! Get the ScriptPubKeyMan by id
955 : : ScriptPubKeyMan* GetScriptPubKeyMan(const uint256& id) const;
956 : :
957 : : //! Get the SigningProvider for a script
958 : : std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script) const;
959 : : std::unique_ptr<SigningProvider> GetSolvingProvider(const CScript& script, SignatureData& sigdata) const;
960 : :
961 : : //! Get the wallet descriptors for a script.
962 : : std::vector<WalletDescriptor> GetWalletDescriptors(const CScript& script) const;
963 : :
964 : : //! Get the LegacyScriptPubKeyMan which is used for all types, internal, and external.
965 : : LegacyDataSPKM* GetLegacyDataSPKM() const;
966 : : LegacyDataSPKM* GetOrCreateLegacyDataSPKM();
967 : :
968 : : //! Make a Legacy(Data)SPKM and set it for all types, internal, and external.
969 : : void SetupLegacyScriptPubKeyMan();
970 : :
971 : : bool WithEncryptionKey(std::function<bool (const CKeyingMaterial&)> cb) const override;
972 : :
973 : : bool HasEncryptionKeys() const override;
974 : : bool HaveCryptedKeys() const;
975 : :
976 : : /** Get last block processed height */
977 : 1231158 : int GetLastBlockHeight() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
978 : : {
979 : 1231158 : AssertLockHeld(cs_wallet);
980 [ - + ]: 1231158 : assert(m_last_block_processed_height >= 0);
981 : 1231158 : return m_last_block_processed_height;
982 : : };
983 : 76308 : uint256 GetLastBlockHash() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
984 : : {
985 : 76308 : AssertLockHeld(cs_wallet);
986 [ - + ]: 76308 : assert(m_last_block_processed_height >= 0);
987 : 76308 : return m_last_block_processed;
988 : : }
989 : : /** Set last block processed height, and write to database */
990 : : void SetLastBlockProcessed(int block_height, uint256 block_hash) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
991 : : /** Write the current best block to database */
992 : : void WriteBestBlock() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
993 : :
994 : : //! Connect the signals from ScriptPubKeyMans to the signals in CWallet
995 : : void ConnectScriptPubKeyManNotifiers();
996 : :
997 : : //! Instantiate a descriptor ScriptPubKeyMan from the WalletDescriptor and load it
998 : : DescriptorScriptPubKeyMan& LoadDescriptorScriptPubKeyMan(uint256 id, WalletDescriptor& desc);
999 : :
1000 : : //! Adds the active ScriptPubKeyMan for the specified type and internal. Writes it to the wallet file
1001 : : //! @param[in] id The unique id for the ScriptPubKeyMan
1002 : : //! @param[in] type The OutputType this ScriptPubKeyMan provides addresses for
1003 : : //! @param[in] internal Whether this ScriptPubKeyMan provides change addresses
1004 : : void AddActiveScriptPubKeyMan(uint256 id, OutputType type, bool internal);
1005 : :
1006 : : //! Loads an active ScriptPubKeyMan for the specified type and internal. (used by LoadWallet)
1007 : : //! @param[in] id The unique id for the ScriptPubKeyMan
1008 : : //! @param[in] type The OutputType this ScriptPubKeyMan provides addresses for
1009 : : //! @param[in] internal Whether this ScriptPubKeyMan provides change addresses
1010 : : void LoadActiveScriptPubKeyMan(uint256 id, OutputType type, bool internal);
1011 : :
1012 : : //! Remove specified ScriptPubKeyMan from set of active SPK managers. Writes the change to the wallet file.
1013 : : //! @param[in] id The unique id for the ScriptPubKeyMan
1014 : : //! @param[in] type The OutputType this ScriptPubKeyMan provides addresses for
1015 : : //! @param[in] internal Whether this ScriptPubKeyMan provides change addresses
1016 : : void DeactivateScriptPubKeyMan(uint256 id, OutputType type, bool internal);
1017 : :
1018 : : //! Create new DescriptorScriptPubKeyMan and add it to the wallet
1019 : : DescriptorScriptPubKeyMan& SetupDescriptorScriptPubKeyMan(WalletBatch& batch, const CExtKey& master_key, const OutputType& output_type, bool internal) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1020 : : //! Create new DescriptorScriptPubKeyMans and add them to the wallet
1021 : : void SetupDescriptorScriptPubKeyMans(WalletBatch& batch, const CExtKey& master_key) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1022 : : void SetupDescriptorScriptPubKeyMans() EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1023 : :
1024 : : //! Create new seed and default DescriptorScriptPubKeyMans for this wallet
1025 : : void SetupOwnDescriptorScriptPubKeyMans(WalletBatch& batch) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1026 : :
1027 : : //! Return the DescriptorScriptPubKeyMan for a WalletDescriptor if it is already in the wallet
1028 : : DescriptorScriptPubKeyMan* GetDescriptorScriptPubKeyMan(const WalletDescriptor& desc) const;
1029 : :
1030 : : //! Returns whether the provided ScriptPubKeyMan is internal
1031 : : //! @param[in] spk_man The ScriptPubKeyMan to test
1032 : : //! @return contains value only for active DescriptorScriptPubKeyMan, otherwise undefined
1033 : : std::optional<bool> IsInternalScriptPubKeyMan(ScriptPubKeyMan* spk_man) const;
1034 : :
1035 : : //! Add a descriptor to the wallet, return a ScriptPubKeyMan & associated output type
1036 : : util::Result<std::reference_wrapper<DescriptorScriptPubKeyMan>> AddWalletDescriptor(WalletDescriptor& desc, const FlatSigningProvider& signing_provider, const std::string& label, bool internal) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1037 : :
1038 : : /** Move all records from the BDB database to a new SQLite database for storage.
1039 : : * The original BDB file will be deleted and replaced with a new SQLite file.
1040 : : * A backup is not created.
1041 : : * May crash if something unexpected happens in the filesystem.
1042 : : */
1043 : : bool MigrateToSQLite(bilingual_str& error) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1044 : :
1045 : : //! Get all of the descriptors from a legacy wallet
1046 : : std::optional<MigrationData> GetDescriptorsForLegacy(bilingual_str& error) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1047 : :
1048 : : //! Adds the ScriptPubKeyMans given in MigrationData to this wallet, removes LegacyScriptPubKeyMan,
1049 : : //! and where needed, moves tx and address book entries to watchonly_wallet or solvable_wallet
1050 : : util::Result<void> ApplyMigrationData(WalletBatch& local_wallet_batch, MigrationData& data) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1051 : :
1052 : : //! Whether the (external) signer performs R-value signature grinding
1053 : : bool CanGrindR() const;
1054 : :
1055 : : //! Add scriptPubKeys for this ScriptPubKeyMan into the scriptPubKey cache
1056 : : void CacheNewScriptPubKeys(const std::set<CScript>& spks, ScriptPubKeyMan* spkm);
1057 : :
1058 : : void TopUpCallback(const std::set<CScript>& spks, ScriptPubKeyMan* spkm) override;
1059 : :
1060 : : //! Retrieve the xpubs in use by the active descriptors
1061 : : std::set<CExtPubKey> GetActiveHDPubKeys() const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
1062 : :
1063 : : //! Find the private key for the given key id from the wallet's descriptors, if available
1064 : : //! Returns nullopt when no descriptor has the key or if the wallet is locked.
1065 : : std::optional<CKey> GetKey(const CKeyID& keyid) const;
1066 : : };
1067 : :
1068 : : /**
1069 : : * Called periodically by the schedule thread. Prompts individual wallets to resend
1070 : : * their transactions. Actual rebroadcast schedule is managed by the wallets themselves.
1071 : : */
1072 : : void MaybeResendWalletTxs(WalletContext& context);
1073 : :
1074 : : /** RAII object to check and reserve a wallet rescan */
1075 : : class WalletRescanReserver
1076 : : {
1077 : : private:
1078 : : using Clock = std::chrono::steady_clock;
1079 : : using NowFn = std::function<Clock::time_point()>;
1080 : : CWallet& m_wallet;
1081 : : bool m_could_reserve{false};
1082 : : NowFn m_now;
1083 : : public:
1084 : 976 : explicit WalletRescanReserver(CWallet& w) : m_wallet(w) {}
1085 : :
1086 : 976 : bool reserve(bool with_passphrase = false)
1087 : : {
1088 [ - + ]: 976 : assert(!m_could_reserve);
1089 [ + - ]: 976 : if (m_wallet.fScanningWallet.exchange(true)) {
1090 : : return false;
1091 : : }
1092 : 976 : m_wallet.m_scanning_with_passphrase.exchange(with_passphrase);
1093 : 976 : m_wallet.m_scanning_start = SteadyClock::now();
1094 : 976 : m_wallet.m_scanning_progress = 0;
1095 : 976 : m_could_reserve = true;
1096 : 976 : return true;
1097 : : }
1098 : :
1099 : 661 : bool isReserved() const
1100 : : {
1101 [ + - - + ]: 661 : return (m_could_reserve && m_wallet.fScanningWallet);
1102 : : }
1103 : :
1104 [ + + ]: 73183 : Clock::time_point now() const { return m_now ? m_now() : Clock::now(); };
1105 : :
1106 : 1 : void setNow(NowFn now) { m_now = std::move(now); }
1107 : :
1108 : 976 : ~WalletRescanReserver()
1109 : : {
1110 [ + - ]: 976 : if (m_could_reserve) {
1111 : 976 : m_wallet.fScanningWallet = false;
1112 : 976 : m_wallet.m_scanning_with_passphrase = false;
1113 : : }
1114 : 976 : }
1115 : : };
1116 : :
1117 : : //! Add wallet name to persistent configuration so it will be loaded on startup.
1118 : : bool AddWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
1119 : :
1120 : : //! Remove wallet name from persistent configuration so it will not be loaded on startup.
1121 : : bool RemoveWalletSetting(interfaces::Chain& chain, const std::string& wallet_name);
1122 : :
1123 : : struct MigrationResult {
1124 : : std::string wallet_name;
1125 : : std::shared_ptr<CWallet> wallet;
1126 : : std::shared_ptr<CWallet> watchonly_wallet;
1127 : : std::shared_ptr<CWallet> solvables_wallet;
1128 : : fs::path backup_path;
1129 : : };
1130 : :
1131 : : //! Do all steps to migrate a legacy wallet to a descriptor wallet
1132 : : [[nodiscard]] util::Result<MigrationResult> MigrateLegacyToDescriptor(const std::string& wallet_name, const SecureString& passphrase, WalletContext& context);
1133 : : //! Requirement: The wallet provided to this function must be isolated, with no attachment to the node's context.
1134 : : [[nodiscard]] util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet> local_wallet, const SecureString& passphrase, WalletContext& context);
1135 : : } // namespace wallet
1136 : :
1137 : : #endif // BITCOIN_WALLET_WALLET_H
|