/tmp/bitcoin/src/wallet/walletutil.h
Line | Count | Source |
1 | | // Copyright (c) 2017-present The Bitcoin Core developers |
2 | | // Distributed under the MIT software license, see the accompanying |
3 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
4 | | |
5 | | #ifndef BITCOIN_WALLET_WALLETUTIL_H |
6 | | #define BITCOIN_WALLET_WALLETUTIL_H |
7 | | |
8 | | #include <script/descriptor.h> |
9 | | #include <util/fs.h> |
10 | | |
11 | | #include <vector> |
12 | | |
13 | | namespace wallet { |
14 | | |
15 | | enum WalletFlags : uint64_t { |
16 | | // wallet flags in the upper section (> 1 << 31) will lead to not opening the wallet if flag is unknown |
17 | | // unknown wallet flags in the lower section <= (1 << 31) will be tolerated |
18 | | |
19 | | // will categorize coins as clean (not reused) and dirty (reused), and handle |
20 | | // them with privacy considerations in mind |
21 | | WALLET_FLAG_AVOID_REUSE = (1ULL << 0), |
22 | | |
23 | | // Indicates that the metadata has already been upgraded to contain key origins |
24 | | WALLET_FLAG_KEY_ORIGIN_METADATA = (1ULL << 1), |
25 | | |
26 | | // Indicates that the descriptor cache has been upgraded to cache last hardened xpubs |
27 | | WALLET_FLAG_LAST_HARDENED_XPUB_CACHED = (1ULL << 2), |
28 | | |
29 | | // will enforce the rule that the wallet can't contain any private keys (only watch-only/pubkeys) |
30 | | WALLET_FLAG_DISABLE_PRIVATE_KEYS = (1ULL << 32), |
31 | | |
32 | | //! Flag set when a wallet contains no HD seed and no private keys, scripts, |
33 | | //! addresses, and other watch only things, and is therefore "blank." |
34 | | //! |
35 | | //! The main function this flag serves is to distinguish a blank wallet from |
36 | | //! a newly created wallet when the wallet database is loaded, to avoid |
37 | | //! initialization that should only happen on first run. |
38 | | //! |
39 | | //! A secondary function of this flag, which applies to descriptor wallets |
40 | | //! only, is to serve as an ongoing indication that descriptors in the |
41 | | //! wallet should be created manually, and that the wallet should not |
42 | | //! generate automatically generate new descriptors if it is later |
43 | | //! encrypted. To support this behavior, descriptor wallets unlike legacy |
44 | | //! wallets do not automatically unset the BLANK flag when things are |
45 | | //! imported. |
46 | | //! |
47 | | //! This flag is also a mandatory flag to prevent previous versions of |
48 | | //! bitcoin from opening the wallet, thinking it was newly created, and |
49 | | //! then improperly reinitializing it. |
50 | | WALLET_FLAG_BLANK_WALLET = (1ULL << 33), |
51 | | |
52 | | //! Indicate that this wallet supports DescriptorScriptPubKeyMan |
53 | | WALLET_FLAG_DESCRIPTORS = (1ULL << 34), |
54 | | |
55 | | //! Indicates that the wallet needs an external signer |
56 | | WALLET_FLAG_EXTERNAL_SIGNER = (1ULL << 35), |
57 | | }; |
58 | | |
59 | | // Version numbers for the wallet client that opens a wallet |
60 | | // These numbers will be written as the last client version in the "version" record and can be used to detect |
61 | | // when an upgrade-downgrade-upgrade was performed. However, we should prefer to use LastClientFeatures rather |
62 | | // than new version numbers. |
63 | | // New version numbers must be greater than 329900 which is guaranteed by setting bit 19 |
64 | | enum WalletClientVersion : int32_t { |
65 | | MIN_VERSION = (1L << 19), |
66 | | |
67 | | // The wallet client supports the records for LastClientFeatures |
68 | | VERSION_LAST_CLIENT_FEATURES = MIN_VERSION + 1, |
69 | | |
70 | | VERSION_LATEST = VERSION_LAST_CLIENT_FEATURES |
71 | | }; |
72 | | |
73 | | enum LastClientFeatures : uint64_t { |
74 | | // Flags indicating the automatic upgrade features supported by the wallet client that last opened a wallet file |
75 | | // New automatic upgrades must define a flag here so that upgrade-downgrade-upgrade can be detected to determine whether |
76 | | // an automatic upgrade should be performed. |
77 | | |
78 | | WALLET_CLIENT_FEATURES = 0 |
79 | | }; |
80 | | |
81 | | //! Get the path of the wallet directory. |
82 | | fs::path GetWalletDir(); |
83 | | |
84 | | /** Descriptor with some wallet metadata */ |
85 | | class WalletDescriptor |
86 | | { |
87 | | private: |
88 | | int32_t range_start = 0; // First item in range; start of range, inclusive, i.e. [range_start, range_end). This never changes. |
89 | | int32_t next_index = 0; // Position of the next item to generate |
90 | | int32_t range_end = 0; // Item after the last; end of range, exclusive, i.e. [range_start, range_end). This will increment with each TopUp() |
91 | | |
92 | | mutable std::optional<uint256> m_canonical_hash; // Hash of the canonical string, used as a shortcut for comparing canonical strings |
93 | | |
94 | | uint256 GetCanonicalHash() const; |
95 | | |
96 | | public: |
97 | | const std::shared_ptr<const Descriptor> descriptor; |
98 | | uint64_t creation_time = 0; |
99 | | DescriptorCache cache; |
100 | | |
101 | 5.20k | int32_t GetStart() const { return range_start; } |
102 | 229k | int32_t GetNext() const { return next_index; } |
103 | 282k | int32_t GetEnd() const { return range_end; } |
104 | | |
105 | | //! Increments the next_index of the descriptor. |
106 | | void IncNext() |
107 | 43.9k | { |
108 | 43.9k | next_index++; |
109 | 43.9k | } |
110 | | |
111 | | //! Increments the next_index of the descriptor. |
112 | | void DecNext() |
113 | 107 | { |
114 | 107 | next_index--; |
115 | 107 | } |
116 | | |
117 | | //! Sets the range_end of the descriptor. |
118 | | void SetEnd(int32_t end) |
119 | 78.1k | { |
120 | 78.1k | if (!descriptor->IsRange()) { |
121 | 12.3k | CHECK_NONFATAL(end == 1); |
122 | 12.3k | } |
123 | 78.1k | range_end = end; |
124 | 78.1k | } |
125 | | |
126 | | template <typename Stream> |
127 | | void Serialize(Stream& s) const |
128 | 102k | { |
129 | 102k | std::string descriptor_str = descriptor->ToString(); |
130 | 102k | s << descriptor_str << creation_time << next_index << range_start << range_end; |
131 | 102k | } |
132 | | |
133 | | template <typename Stream> |
134 | | static WalletDescriptor FromStream(deserialize_type, Stream& s) |
135 | 3.17k | { |
136 | 3.17k | std::string descriptor_str; |
137 | 3.17k | uint64_t creation_time; |
138 | 3.17k | int32_t next_index, range_start, range_end; |
139 | 3.17k | s >> descriptor_str >> creation_time >> next_index >> range_start >> range_end; |
140 | | |
141 | 3.17k | std::string error; |
142 | 3.17k | FlatSigningProvider keys; |
143 | 3.17k | auto descs = Parse(descriptor_str, keys, error, true); |
144 | 3.17k | if (descs.empty()) { |
145 | 2 | throw std::ios_base::failure("Invalid descriptor: " + error); |
146 | 2 | } |
147 | 3.16k | if (descs.size() > 1) { |
148 | 0 | throw std::ios_base::failure("Can't load a multipath descriptor from databases"); |
149 | 0 | } |
150 | 3.16k | return WalletDescriptor(std::move(descs.at(0)), creation_time, range_start, range_end, next_index); |
151 | 3.16k | } wallet::WalletDescriptor wallet::WalletDescriptor::FromStream<SpanReader>(deserialize_type, SpanReader&) Line | Count | Source | 135 | 1 | { | 136 | 1 | std::string descriptor_str; | 137 | 1 | uint64_t creation_time; | 138 | 1 | int32_t next_index, range_start, range_end; | 139 | 1 | s >> descriptor_str >> creation_time >> next_index >> range_start >> range_end; | 140 | | | 141 | 1 | std::string error; | 142 | 1 | FlatSigningProvider keys; | 143 | 1 | auto descs = Parse(descriptor_str, keys, error, true); | 144 | 1 | if (descs.empty()) { | 145 | 1 | throw std::ios_base::failure("Invalid descriptor: " + error); | 146 | 1 | } | 147 | 0 | if (descs.size() > 1) { | 148 | 0 | throw std::ios_base::failure("Can't load a multipath descriptor from databases"); | 149 | 0 | } | 150 | 0 | return WalletDescriptor(std::move(descs.at(0)), creation_time, range_start, range_end, next_index); | 151 | 0 | } |
wallet::WalletDescriptor wallet::WalletDescriptor::FromStream<DataStream>(deserialize_type, DataStream&) Line | Count | Source | 135 | 3.16k | { | 136 | 3.16k | std::string descriptor_str; | 137 | 3.16k | uint64_t creation_time; | 138 | 3.16k | int32_t next_index, range_start, range_end; | 139 | 3.16k | s >> descriptor_str >> creation_time >> next_index >> range_start >> range_end; | 140 | | | 141 | 3.16k | std::string error; | 142 | 3.16k | FlatSigningProvider keys; | 143 | 3.16k | auto descs = Parse(descriptor_str, keys, error, true); | 144 | 3.16k | if (descs.empty()) { | 145 | 1 | throw std::ios_base::failure("Invalid descriptor: " + error); | 146 | 1 | } | 147 | 3.16k | if (descs.size() > 1) { | 148 | 0 | throw std::ios_base::failure("Can't load a multipath descriptor from databases"); | 149 | 0 | } | 150 | 3.16k | return WalletDescriptor(std::move(descs.at(0)), creation_time, range_start, range_end, next_index); | 151 | 3.16k | } |
|
152 | | |
153 | | WalletDescriptor() = delete; |
154 | | WalletDescriptor(std::shared_ptr<Descriptor> descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index) |
155 | 8.54k | : range_start(descriptor->IsRange() ? range_start : 0), |
156 | 8.54k | next_index(next_index), |
157 | 8.54k | range_end(descriptor->IsRange() ? range_end : 1), |
158 | 8.54k | descriptor(descriptor), |
159 | 8.54k | creation_time(creation_time) |
160 | 8.54k | {} |
161 | | |
162 | | /** Replaces all metadata (range, start, end, creation time), and cache from another WalletDescriptor if it has the same canonical descriptor string. |
163 | | * The descriptor itself is not replaced to preserve existing serialization to maintain compatibility with previous software versions that expect |
164 | | * specific serialization. |
165 | | */ |
166 | | void UpdateFrom(const WalletDescriptor& other); |
167 | | |
168 | | // Compare by using the canonical string to make the hardened indicators consistent for comparison |
169 | | bool IsCanonicallyEquivalent(const WalletDescriptor& other) const; |
170 | | }; |
171 | | |
172 | | WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& output_type, bool internal); |
173 | | } // namespace wallet |
174 | | |
175 | | #endif // BITCOIN_WALLET_WALLETUTIL_H |