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