Bitcoin Core 31.99.0
P2P Digital Currency
walletutil.h
Go to the documentation of this file.
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
13namespace wallet {
14
15enum 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
22
23 // Indicates that the metadata has already been upgraded to contain key origins
25
26 // Indicates that the descriptor cache has been upgraded to cache last hardened xpubs
28
29 // will enforce the rule that the wallet can't contain any private keys (only watch-only/pubkeys)
31
51
54
57};
58
60fs::path GetWalletDir();
61
64{
65private:
66 int32_t range_start = 0; // First item in range; start of range, inclusive, i.e. [range_start, range_end). This never changes.
67 int32_t next_index = 0; // Position of the next item to generate
68 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()
69public:
70 std::shared_ptr<Descriptor> descriptor;
71 uint256 id; // Descriptor ID (calculated once at descriptor initialization/deserialization)
72 uint64_t creation_time = 0;
74
75 int32_t GetStart() const { return range_start; }
76 int32_t GetNext() const { return next_index; }
77 int32_t GetEnd() const { return range_end; }
78
80 void IncNext()
81 {
82 next_index++;
83 }
84
86 void DecNext()
87 {
88 next_index--;
89 }
90
92 void SetEnd(int32_t end)
93 {
94 if (!descriptor->IsRange()) {
95 CHECK_NONFATAL(end == 1);
96 }
97 range_end = end;
98 }
99
100 void DeserializeDescriptor(const std::string& str)
101 {
102 std::string error;
104 auto descs = Parse(str, keys, error, true);
105 if (descs.empty()) {
106 throw std::ios_base::failure("Invalid descriptor: " + error);
107 }
108 if (descs.size() > 1) {
109 throw std::ios_base::failure("Can't load a multipath descriptor from databases");
110 }
111 descriptor = std::move(descs.at(0));
113 }
114
116 {
117 std::string descriptor_str;
118 SER_WRITE(obj, descriptor_str = obj.descriptor->ToString());
119 READWRITE(descriptor_str, obj.creation_time, obj.next_index, obj.range_start, obj.range_end);
120 SER_READ(obj, obj.DeserializeDescriptor(descriptor_str));
121 }
122
123 WalletDescriptor() = default;
124 WalletDescriptor(std::shared_ptr<Descriptor> descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index)
125 : range_start(descriptor->IsRange() ? range_start : 0),
127 range_end(descriptor->IsRange() ? range_end : 1),
131};
132
133WalletDescriptor GenerateWalletDescriptor(const CExtPubKey& master_key, const OutputType& output_type, bool internal);
134} // namespace wallet
135
136#endif // BITCOIN_WALLET_WALLETUTIL_H
#define CHECK_NONFATAL(condition)
Identity function.
Definition: check.h:112
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:29
256-bit opaque blob.
Definition: uint256.h:196
Descriptor with some wallet metadata.
Definition: walletutil.h:64
WalletDescriptor(std::shared_ptr< Descriptor > descriptor, uint64_t creation_time, int32_t range_start, int32_t range_end, int32_t next_index)
Definition: walletutil.h:124
int32_t GetStart() const
Definition: walletutil.h:75
int32_t GetNext() const
Definition: walletutil.h:76
void IncNext()
Increments the next_index of the descriptor.
Definition: walletutil.h:80
void SetEnd(int32_t end)
Sets the range_end of the descriptor.
Definition: walletutil.h:92
void DeserializeDescriptor(const std::string &str)
Definition: walletutil.h:100
SERIALIZE_METHODS(WalletDescriptor, obj)
Definition: walletutil.h:115
std::shared_ptr< Descriptor > descriptor
Definition: walletutil.h:70
int32_t GetEnd() const
Definition: walletutil.h:77
void DecNext()
Increments the next_index of the descriptor.
Definition: walletutil.h:86
DescriptorCache cache
Definition: walletutil.h:73
static UniValue Parse(std::string_view raw, ParamFormat format=ParamFormat::JSON)
Parse string to UniValue or throw runtime_error if string contains invalid JSON.
Definition: client.cpp:405
fs::path GetWalletDir()
Get the path of the wallet directory.
Definition: walletutil.cpp:13
WalletFlags
Definition: walletutil.h:15
@ WALLET_FLAG_EXTERNAL_SIGNER
Indicates that the wallet needs an external signer.
Definition: walletutil.h:56
@ WALLET_FLAG_LAST_HARDENED_XPUB_CACHED
Definition: walletutil.h:27
@ WALLET_FLAG_KEY_ORIGIN_METADATA
Definition: walletutil.h:24
@ WALLET_FLAG_AVOID_REUSE
Definition: walletutil.h:21
@ WALLET_FLAG_DESCRIPTORS
Indicate that this wallet supports DescriptorScriptPubKeyMan.
Definition: walletutil.h:53
@ WALLET_FLAG_DISABLE_PRIVATE_KEYS
Definition: walletutil.h:30
@ WALLET_FLAG_BLANK_WALLET
Flag set when a wallet contains no HD seed and no private keys, scripts, addresses,...
Definition: walletutil.h:50
WalletDescriptor GenerateWalletDescriptor(const CExtPubKey &master_key, const OutputType &addr_type, bool internal)
Definition: walletutil.cpp:35
OutputType
Definition: outputtype.h:18
uint256 DescriptorID(const Descriptor &desc)
Unique identifier that may not change over time, unless explicitly marked as not backwards compatible...
#define SER_WRITE(obj, code)
Definition: serialize.h:150
#define SER_READ(obj, code)
Definition: serialize.h:149
#define READWRITE(...)
Definition: serialize.h:148
std::vector< uint16_t > keys
Definition: dbwrapper.cpp:376