Branch data Line data Source code
1 : : // Copyright (c) 2017-2021 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 : : /** (client) version numbers for particular wallet features */
15 : : enum WalletFeature
16 : : {
17 : : FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getwalletinfo's clientversion output)
18 : :
19 : : FEATURE_WALLETCRYPT = 40000, // wallet encryption
20 : : FEATURE_COMPRPUBKEY = 60000, // compressed public keys
21 : :
22 : : FEATURE_HD = 130000, // Hierarchical key derivation after BIP32 (HD Wallet)
23 : :
24 : : FEATURE_HD_SPLIT = 139900, // Wallet with HD chain split (change outputs will use m/0'/1'/k)
25 : :
26 : : FEATURE_NO_DEFAULT_KEY = 159900, // Wallet without a default key written
27 : :
28 : : FEATURE_PRE_SPLIT_KEYPOOL = 169900, // Upgraded to HD SPLIT and can have a pre-split keypool
29 : :
30 : : FEATURE_LATEST = FEATURE_PRE_SPLIT_KEYPOOL
31 : : };
32 : :
33 : : bool IsFeatureSupported(int wallet_version, int feature_version);
34 : : WalletFeature GetClosestWalletFeature(int version);
35 : :
36 : : enum WalletFlags : uint64_t {
37 : : // wallet flags in the upper section (> 1 << 31) will lead to not opening the wallet if flag is unknown
38 : : // unknown wallet flags in the lower section <= (1 << 31) will be tolerated
39 : :
40 : : // will categorize coins as clean (not reused) and dirty (reused), and handle
41 : : // them with privacy considerations in mind
42 : : WALLET_FLAG_AVOID_REUSE = (1ULL << 0),
43 : :
44 : : // Indicates that the metadata has already been upgraded to contain key origins
45 : : WALLET_FLAG_KEY_ORIGIN_METADATA = (1ULL << 1),
46 : :
47 : : // Indicates that the descriptor cache has been upgraded to cache last hardened xpubs
48 : : WALLET_FLAG_LAST_HARDENED_XPUB_CACHED = (1ULL << 2),
49 : :
50 : : // will enforce the rule that the wallet can't contain any private keys (only watch-only/pubkeys)
51 : : WALLET_FLAG_DISABLE_PRIVATE_KEYS = (1ULL << 32),
52 : :
53 : : //! Flag set when a wallet contains no HD seed and no private keys, scripts,
54 : : //! addresses, and other watch only things, and is therefore "blank."
55 : : //!
56 : : //! The main function this flag serves is to distinguish a blank wallet from
57 : : //! a newly created wallet when the wallet database is loaded, to avoid
58 : : //! initialization that should only happen on first run.
59 : : //!
60 : : //! A secondary function of this flag, which applies to descriptor wallets
61 : : //! only, is to serve as an ongoing indication that descriptors in the
62 : : //! wallet should be created manually, and that the wallet should not
63 : : //! generate automatically generate new descriptors if it is later
64 : : //! encrypted. To support this behavior, descriptor wallets unlike legacy
65 : : //! wallets do not automatically unset the BLANK flag when things are
66 : : //! imported.
67 : : //!
68 : : //! This flag is also a mandatory flag to prevent previous versions of
69 : : //! bitcoin from opening the wallet, thinking it was newly created, and
70 : : //! then improperly reinitializing it.
71 : : WALLET_FLAG_BLANK_WALLET = (1ULL << 33),
72 : :
73 : : //! Indicate that this wallet supports DescriptorScriptPubKeyMan
74 : : WALLET_FLAG_DESCRIPTORS = (1ULL << 34),
75 : :
76 : : //! Indicates that the wallet needs an external signer
77 : : WALLET_FLAG_EXTERNAL_SIGNER = (1ULL << 35),
78 : : };
79 : :
80 : : //! Get the path of the wallet directory.
81 : : fs::path GetWalletDir();
82 : :
83 : : /** Descriptor with some wallet metadata */
84 : : class WalletDescriptor
85 : : {
86 : : public:
87 : : std::shared_ptr<Descriptor> descriptor;
88 : : uint256 id; // Descriptor ID (calculated once at descriptor initialization/deserialization)
89 : : uint64_t creation_time = 0;
90 : : int32_t range_start = 0; // First item in range; start of range, inclusive, i.e. [range_start, range_end). This never changes.
91 : : 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()
92 : : int32_t next_index = 0; // Position of the next item to generate
93 : : DescriptorCache cache;
94 : :
95 : 21 : void DeserializeDescriptor(const std::string& str)
96 : : {
97 : 21 : std::string error;
98 : 21 : FlatSigningProvider keys;
99 [ + - ]: 21 : auto descs = Parse(str, keys, error, true);
100 [ + + ]: 21 : if (descs.empty()) {
101 [ + - + - ]: 4 : throw std::ios_base::failure("Invalid descriptor: " + error);
102 : : }
103 [ - + ]: 19 : if (descs.size() > 1) {
104 [ # # ]: 0 : throw std::ios_base::failure("Can't load a multipath descriptor from databases");
105 : : }
106 [ + - + - ]: 19 : descriptor = std::move(descs.at(0));
107 [ + - ]: 19 : id = DescriptorID(*descriptor);
108 : 23 : }
109 : :
110 [ + - ]: 26904 : SERIALIZE_METHODS(WalletDescriptor, obj)
111 : : {
112 : 13452 : std::string descriptor_str;
113 [ + - ]: 26883 : SER_WRITE(obj, descriptor_str = obj.descriptor->ToString());
114 [ + - ]: 13452 : READWRITE(descriptor_str, obj.creation_time, obj.next_index, obj.range_start, obj.range_end);
115 [ + + ]: 13471 : SER_READ(obj, obj.DeserializeDescriptor(descriptor_str));
116 : 13450 : }
117 : :
118 : 293 : WalletDescriptor() = default;
119 [ + - + - : 600 : WalletDescriptor(std::shared_ptr<Descriptor> descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index) : descriptor(descriptor), id(DescriptorID(*descriptor)), creation_time(creation_time), range_start(range_start), range_end(range_end), next_index(next_index) { }
- - ]
120 : : };
121 : :
122 : : WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& output_type, bool internal);
123 : : } // namespace wallet
124 : :
125 : : #endif // BITCOIN_WALLET_WALLETUTIL_H
|