1 // Copyright (c) 2017-2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #ifndef BITCOIN_WALLET_WALLETUTIL_H
6 #define BITCOIN_WALLET_WALLETUTIL_H
7 
8 #include <fs.h>
9 #include <script/descriptor.h>
10 
11 #include <vector>
12 
13 /** (client) version numbers for particular wallet features */
14 enum WalletFeature
15 {
16     FEATURE_BASE = 10500, // the earliest version new wallets supports (only useful for getwalletinfo's clientversion output)
17 
18     FEATURE_WALLETCRYPT = 40000, // wallet encryption
19     FEATURE_COMPRPUBKEY = 60000, // compressed public keys
20 
21     FEATURE_HD = 130000, // Hierarchical key derivation after BIP32 (HD Wallet)
22 
23     FEATURE_HD_SPLIT = 139900, // Wallet with HD chain split (change outputs will use m/0'/1'/k)
24 
25     FEATURE_NO_DEFAULT_KEY = 159900, // Wallet without a default key written
26 
27     FEATURE_PRE_SPLIT_KEYPOOL = 169900, // Upgraded to HD SPLIT and can have a pre-split keypool
28 
29     FEATURE_LATEST = FEATURE_PRE_SPLIT_KEYPOOL
30 };
31 
32 bool IsFeatureSupported(int wallet_version, int feature_version);
33 WalletFeature GetClosestWalletFeature(int version);
34 
35 enum WalletFlags : uint64_t {
36     // wallet flags in the upper section (> 1 << 31) will lead to not opening the wallet if flag is unknown
37     // unknown wallet flags in the lower section <= (1 << 31) will be tolerated
38 
39     // will categorize coins as clean (not reused) and dirty (reused), and handle
40     // them with privacy considerations in mind
41     WALLET_FLAG_AVOID_REUSE = (1ULL << 0),
42 
43     // Indicates that the metadata has already been upgraded to contain key origins
44     WALLET_FLAG_KEY_ORIGIN_METADATA = (1ULL << 1),
45 
46     // Indicates that the descriptor cache has been upgraded to cache last hardened xpubs
47     WALLET_FLAG_LAST_HARDENED_XPUB_CACHED = (1ULL << 2),
48 
49     // will enforce the rule that the wallet can't contain any private keys (only watch-only/pubkeys)
50     WALLET_FLAG_DISABLE_PRIVATE_KEYS = (1ULL << 32),
51 
52     //! Flag set when a wallet contains no HD seed and no private keys, scripts,
53     //! addresses, and other watch only things, and is therefore "blank."
54     //!
55     //! The only function this flag serves is to distinguish a blank wallet from
56     //! a newly created wallet when the wallet database is loaded, to avoid
57     //! initialization that should only happen on first run.
58     //!
59     //! This flag is also a mandatory flag to prevent previous versions of
60     //! bitcoin from opening the wallet, thinking it was newly created, and
61     //! then improperly reinitializing it.
62     WALLET_FLAG_BLANK_WALLET = (1ULL << 33),
63 
64     //! Indicate that this wallet supports DescriptorScriptPubKeyMan
65     WALLET_FLAG_DESCRIPTORS = (1ULL << 34),
66 
67     //! Indicates that the wallet needs an external signer
68     WALLET_FLAG_EXTERNAL_SIGNER = (1ULL << 35),
69 };
70 
71 //! Get the path of the wallet directory.
72 fs::path GetWalletDir();
73 
74 /** Descriptor with some wallet metadata */
75 class WalletDescriptor
76 {
77 public:
78     std::shared_ptr<Descriptor> descriptor;
79     uint64_t creation_time = 0;
80     int32_t range_start = 0; // First item in range; start of range, inclusive, i.e. [range_start, range_end). This never changes.
81     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()
82     int32_t next_index = 0; // Position of the next item to generate
83     DescriptorCache cache;
84 
85     void DeserializeDescriptor(const std::string& str)
86     {
87         std::string error;
88         FlatSigningProvider keys;
89         descriptor = Parse(str, keys, error, true);
90         if (!descriptor) {
91             throw std::ios_base::failure("Invalid descriptor: " + error);
92         }
93     }
94 
95     SERIALIZE_METHODS(WalletDescriptor, obj)
96     {
97         std::string descriptor_str;
98         SER_WRITE(obj, descriptor_str = obj.descriptor->ToString());
99         READWRITE(descriptor_str, obj.creation_time, obj.next_index, obj.range_start, obj.range_end);
100         SER_READ(obj, obj.DeserializeDescriptor(descriptor_str));
101     }
102 
103     WalletDescriptor() {}
104     WalletDescriptor(std::shared_ptr<Descriptor> descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index) : descriptor(descriptor), creation_time(creation_time), range_start(range_start), range_end(range_end), next_index(next_index) {}
105 };
106 
107 #endif // BITCOIN_WALLET_WALLETUTIL_H
108