Coverage Report

Created: 2026-05-30 09:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/script/descriptor.h
Line
Count
Source
1
// Copyright (c) 2018-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_SCRIPT_DESCRIPTOR_H
6
#define BITCOIN_SCRIPT_DESCRIPTOR_H
7
8
#include <outputtype.h>
9
#include <pubkey.h>
10
#include <uint256.h>
11
12
#include <cstddef>
13
#include <cstdint>
14
#include <memory>
15
#include <optional>
16
#include <set>
17
#include <string>
18
#include <string_view>
19
#include <unordered_map>
20
#include <vector>
21
22
class CScript;
23
class SigningProvider;
24
struct FlatSigningProvider;
25
26
using ExtPubKeyMap = std::unordered_map<uint32_t, CExtPubKey>;
27
28
/** Cache for single descriptor's derived extended pubkeys */
29
class DescriptorCache {
30
private:
31
    /** Map key expression index -> map of (key derivation index -> xpub) */
32
    std::unordered_map<uint32_t, ExtPubKeyMap> m_derived_xpubs;
33
    /** Map key expression index -> parent xpub */
34
    ExtPubKeyMap m_parent_xpubs;
35
    /** Map key expression index -> last hardened xpub */
36
    ExtPubKeyMap m_last_hardened_xpubs;
37
38
public:
39
    /** Cache a parent xpub
40
     *
41
     * @param[in] key_exp_pos Position of the key expression within the descriptor
42
     * @param[in] xpub The CExtPubKey to cache
43
     */
44
    void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub);
45
    /** Retrieve a cached parent xpub
46
     *
47
     * @param[in] key_exp_pos Position of the key expression within the descriptor
48
     * @param[out] xpub The CExtPubKey to get from cache
49
     */
50
    bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey& xpub) const;
51
    /** Cache an xpub derived at an index
52
     *
53
     * @param[in] key_exp_pos Position of the key expression within the descriptor
54
     * @param[in] der_index Derivation index of the xpub
55
     * @param[in] xpub The CExtPubKey to cache
56
     */
57
    void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey& xpub);
58
    /** Retrieve a cached xpub derived at an index
59
     *
60
     * @param[in] key_exp_pos Position of the key expression within the descriptor
61
     * @param[in] der_index Derivation index of the xpub
62
     * @param[out] xpub The CExtPubKey to get from cache
63
     */
64
    bool GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey& xpub) const;
65
    /** Cache a last hardened xpub
66
     *
67
     * @param[in] key_exp_pos Position of the key expression within the descriptor
68
     * @param[in] xpub The CExtPubKey to cache
69
     */
70
    void CacheLastHardenedExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub);
71
    /** Retrieve a cached last hardened xpub
72
     *
73
     * @param[in] key_exp_pos Position of the key expression within the descriptor
74
     * @param[out] xpub The CExtPubKey to get from cache
75
     */
76
    bool GetCachedLastHardenedExtPubKey(uint32_t key_exp_pos, CExtPubKey& xpub) const;
77
78
    /** Retrieve all cached parent xpubs */
79
    ExtPubKeyMap GetCachedParentExtPubKeys() const;
80
    /** Retrieve all cached derived xpubs */
81
    std::unordered_map<uint32_t, ExtPubKeyMap> GetCachedDerivedExtPubKeys() const;
82
    /** Retrieve all cached last hardened xpubs */
83
    ExtPubKeyMap GetCachedLastHardenedExtPubKeys() const;
84
85
    /** Combine another DescriptorCache into this one.
86
     * Returns a cache containing the items from the other cache unknown to current cache
87
     */
88
    DescriptorCache MergeAndDiff(const DescriptorCache& other);
89
};
90
91
/** \brief Interface for parsed descriptor objects.
92
 *
93
 * Descriptors are strings that describe a set of scriptPubKeys, together with
94
 * all information necessary to solve them. By combining all information into
95
 * one, they avoid the need to separately import keys and scripts.
96
 *
97
 * Descriptors may be ranged, which occurs when the public keys inside are
98
 * specified in the form of HD chains (xpubs).
99
 *
100
 * Descriptors always represent public information - public keys and scripts -
101
 * but in cases where private keys need to be conveyed along with a descriptor,
102
 * they can be included inside by changing public keys to private keys (WIF
103
 * format), and changing xpubs by xprvs.
104
 *
105
 * Reference documentation about the descriptor language can be found in
106
 * doc/descriptors.md.
107
 */
108
struct Descriptor {
109
404k
    virtual ~Descriptor() = default;
110
111
    /** Whether the expansion of this descriptor depends on the position. */
112
    virtual bool IsRange() const = 0;
113
114
    /** Whether this descriptor has all information about signing ignoring lack of private keys.
115
     *  This is true for all descriptors except ones that use `raw` or `addr` constructions. */
116
    virtual bool IsSolvable() const = 0;
117
118
    /** Convert the descriptor back to a string, undoing parsing. */
119
    virtual std::string ToString(bool compat_format=false) const = 0;
120
121
    /** Whether this descriptor will return at most one scriptPubKey or multiple (aka is or is not combo) */
122
    virtual bool IsSingleType() const = 0;
123
124
    /** Whether the given provider has all private keys required by this descriptor.
125
     * @return `false` if the descriptor doesn't have any keys or subdescriptors,
126
     *         or if the provider does not have all private keys required by
127
     *         the descriptor.
128
     */
129
    virtual bool HavePrivateKeys(const SigningProvider& provider) const = 0;
130
131
    /** Convert the descriptor to a private string. This uses public keys if the relevant private keys are not in the SigningProvider.
132
     *  If none of the relevant private keys are available, the output string in the "out" parameter will not contain any private key information,
133
     *  and this function will return "false".
134
     *  @param[in] provider The SigningProvider to query for private keys.
135
     *  @param[out] out The resulting descriptor string, containing private keys if available.
136
     *  @returns true if at least one private key available.
137
     */
138
    virtual bool ToPrivateString(const SigningProvider& provider, std::string& out) const = 0;
139
140
    /** Convert the descriptor to a normalized string. Normalized descriptors have the xpub at the last hardened step. This fails if the provided provider does not have the private keys to derive that xpub. */
141
    virtual bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const = 0;
142
143
    /** Expand a descriptor at a specified position.
144
     *
145
     * @param[in] pos The position at which to expand the descriptor. If IsRange() is false, this is ignored.
146
     * @param[in] provider The provider to query for private keys in case of hardened derivation.
147
     * @param[out] output_scripts The expanded scriptPubKeys.
148
     * @param[out] out Scripts and public keys necessary for solving the expanded scriptPubKeys (may be equal to `provider`).
149
     * @param[out] write_cache Cache data necessary to evaluate the descriptor at this point without access to private keys.
150
     */
151
    virtual bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const = 0;
152
153
    /** Expand a descriptor at a specified position using cached expansion data.
154
     *
155
     * @param[in] pos The position at which to expand the descriptor. If IsRange() is false, this is ignored.
156
     * @param[in] read_cache Cached expansion data.
157
     * @param[out] output_scripts The expanded scriptPubKeys.
158
     * @param[out] out Scripts and public keys necessary for solving the expanded scriptPubKeys (may be equal to `provider`).
159
     */
160
    virtual bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const = 0;
161
162
    /** Expand the private key for a descriptor at a specified position, if possible.
163
     *
164
     * @param[in] pos The position at which to expand the descriptor. If IsRange() is false, this is ignored.
165
     * @param[in] provider The provider to query for the private keys.
166
     * @param[out] out Any private keys available for the specified `pos`.
167
     */
168
    virtual void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const = 0;
169
170
    /** @return The OutputType of the scriptPubKey(s) produced by this descriptor. Or nullopt if indeterminate (multiple or none) */
171
    virtual std::optional<OutputType> GetOutputType() const = 0;
172
173
    /** Get the size of the scriptPubKey for this descriptor. */
174
    virtual std::optional<int64_t> ScriptSize() const = 0;
175
176
    /** Get the maximum size of a satisfaction for this descriptor, in weight units.
177
     *
178
     * @param use_max_sig Whether to assume ECDSA signatures will have a high-r.
179
     */
180
    virtual std::optional<int64_t> MaxSatisfactionWeight(bool use_max_sig) const = 0;
181
182
    /** Get the maximum size number of stack elements for satisfying this descriptor. */
183
    virtual std::optional<int64_t> MaxSatisfactionElems() const = 0;
184
185
    /** Return all (extended) public keys for this descriptor, including any from subdescriptors.
186
     *
187
     * @param[out] pubkeys Any public keys
188
     * @param[out] ext_pubs Any extended public keys
189
     */
190
    virtual void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const = 0;
191
192
    /** Whether this descriptor produces any scripts with the Expand functions */
193
    virtual bool HasScripts() const = 0;
194
195
    /** Semantic/safety warnings (includes subdescriptors). */
196
    virtual std::vector<std::string> Warnings() const = 0;
197
198
    /** Get the maximum key expression index. Used only for tests */
199
    virtual uint32_t GetMaxKeyExpr() const = 0;
200
201
    /** Get the number of key expressions in this descriptor. Used only for tests */
202
    virtual size_t GetKeyCount() const = 0;
203
};
204
205
/** Parse a `descriptor` string. Included private keys are put in `out`.
206
 *
207
 * If the descriptor has a checksum, it must be valid. If `require_checksum`
208
 * is set, the checksum is mandatory - otherwise it is optional.
209
 *
210
 * If a parse error occurs, or the checksum is missing/invalid, or anything
211
 * else is wrong, an empty vector is returned.
212
 */
213
std::vector<std::unique_ptr<Descriptor>> Parse(std::string_view descriptor, FlatSigningProvider& out, std::string& error, bool require_checksum = false);
214
215
/** Get the checksum for a `descriptor`.
216
 *
217
 * - If it already has one, and it is correct, return the checksum in the input.
218
 * - If it already has one that is wrong, return "".
219
 * - If it does not already have one, return the checksum that would need to be added.
220
 */
221
std::string GetDescriptorChecksum(const std::string& descriptor);
222
223
/** Find a descriptor for the specified `script`, using information from `provider` where possible.
224
 *
225
 * A non-ranged descriptor which only generates the specified script will be returned in all
226
 * circumstances.
227
 *
228
 * For public keys with key origin information, this information will be preserved in the returned
229
 * descriptor.
230
 *
231
 * - If all information for solving `script` is present in `provider`, a descriptor will be returned
232
 *   which is IsSolvable() and encapsulates said information.
233
 * - Failing that, if `script` corresponds to a known address type, an "addr()" descriptor will be
234
 *   returned (which is not IsSolvable()).
235
 * - Failing that, a "raw()" descriptor is returned.
236
 */
237
std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const SigningProvider& provider);
238
239
/** Unique identifier that may not change over time, unless explicitly marked as not backwards compatible.
240
*   This is not part of BIP 380, not guaranteed to be interoperable and should not be exposed to the user.
241
*/
242
uint256 DescriptorID(const Descriptor& desc);
243
244
#endif // BITCOIN_SCRIPT_DESCRIPTOR_H