Bitcoin Core 32.99.0
P2P Digital Currency
descriptor.h
Go to the documentation of this file.
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#include <util/expected.h>
12
13#include <cstddef>
14#include <cstdint>
15#include <memory>
16#include <optional>
17#include <set>
18#include <string>
19#include <string_view>
20#include <unordered_map>
21#include <vector>
22
23class CScript;
24class SigningProvider;
26
27using ExtPubKeyMap = std::unordered_map<uint32_t, CExtPubKey>;
28
31private:
33 std::unordered_map<uint32_t, ExtPubKeyMap> m_derived_xpubs;
38
39public:
45 void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub);
51 bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey& xpub) const;
58 void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey& xpub);
65 bool GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey& xpub) const;
71 void CacheLastHardenedExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub);
77 bool GetCachedLastHardenedExtPubKey(uint32_t key_exp_pos, CExtPubKey& xpub) const;
78
82 std::unordered_map<uint32_t, ExtPubKeyMap> GetCachedDerivedExtPubKeys() const;
85
90};
91
110 virtual ~Descriptor() = default;
111
113 virtual bool IsRange() const = 0;
114
117 virtual bool IsSolvable() const = 0;
118
120 virtual std::string ToString(bool compat_format=false) const = 0;
121
125 virtual std::string ToCanonicalString() const = 0;
126
128 virtual bool IsSingleType() const = 0;
129
135 virtual bool HavePrivateKeys(const SigningProvider& provider) const = 0;
136
144 virtual bool ToPrivateString(const SigningProvider& provider, std::string& out) const = 0;
145
147 virtual bool ToNormalizedString(const SigningProvider& provider, std::string& out, const DescriptorCache* cache = nullptr) const = 0;
148
150 virtual bool CanSelfExpand() const = 0;
151
160 virtual bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const = 0;
161
169 virtual bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const = 0;
170
177 virtual void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const = 0;
178
180 virtual std::optional<OutputType> GetOutputType() const = 0;
181
183 virtual std::optional<int64_t> ScriptSize() const = 0;
184
189 virtual std::optional<int64_t> MaxSatisfactionWeight(bool use_max_sig) const = 0;
190
192 virtual std::optional<int64_t> MaxSatisfactionElems() const = 0;
193
199 virtual void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const = 0;
200
202 virtual bool HasScripts() const = 0;
203
205 virtual std::vector<std::string> Warnings() const = 0;
206
208 virtual uint32_t GetMaxKeyExpr() const = 0;
209
211 virtual size_t GetKeyCount() const = 0;
212};
213
220
229std::vector<std::unique_ptr<Descriptor>> Parse(std::string_view descriptor, FlatSigningProvider& out, std::string& error, bool require_checksum = false);
230
237std::string GetDescriptorChecksum(const std::string& descriptor);
238
253std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const SigningProvider& provider);
254
262
263#endif // BITCOIN_SCRIPT_DESCRIPTOR_H
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:406
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:30
bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey &xpub) const
Retrieve a cached parent xpub.
std::unordered_map< uint32_t, ExtPubKeyMap > GetCachedDerivedExtPubKeys() const
Retrieve all cached derived xpubs.
ExtPubKeyMap m_last_hardened_xpubs
Map key expression index -> last hardened xpub.
Definition: descriptor.h:37
void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey &xpub)
Cache an xpub derived at an index.
DescriptorCache MergeAndDiff(const DescriptorCache &other)
Combine another DescriptorCache into this one.
ExtPubKeyMap GetCachedParentExtPubKeys() const
Retrieve all cached parent xpubs.
ExtPubKeyMap GetCachedLastHardenedExtPubKeys() const
Retrieve all cached last hardened xpubs.
void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey &xpub)
Cache a parent xpub.
void CacheLastHardenedExtPubKey(uint32_t key_exp_pos, const CExtPubKey &xpub)
Cache a last hardened xpub.
bool GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey &xpub) const
Retrieve a cached xpub derived at an index.
std::unordered_map< uint32_t, ExtPubKeyMap > m_derived_xpubs
Map key expression index -> map of (key derivation index -> xpub)
Definition: descriptor.h:33
bool GetCachedLastHardenedExtPubKey(uint32_t key_exp_pos, CExtPubKey &xpub) const
Retrieve a cached last hardened xpub.
ExtPubKeyMap m_parent_xpubs
Map key expression index -> parent xpub.
Definition: descriptor.h:35
An interface to be implemented by keystores that support signing.
256-bit opaque blob.
Definition: uint256.h:196
The util::Expected class provides a standard way for low-level functions to return either error value...
Definition: expected.h:44
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible.
std::vector< std::unique_ptr< Descriptor > > Parse(std::string_view descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum=false)
Parse a descriptor string.
std::string GetDescriptorChecksum(const std::string &descriptor)
Get the checksum for a descriptor.
uint256 CompatDescriptorHash(const Descriptor &desc)
Hash of the COMPAT string representation of the descriptor that is not supposed to change over time.
std::unordered_map< uint32_t, CExtPubKey > ExtPubKeyMap
Definition: descriptor.h:27
util::Expected< void, std::string > CheckDescriptorRangeBounds(int64_t low, int64_t high)
Validate the numeric bounds of a descriptor key-expression range [low, high] (high inclusive).
Definition: descriptor.cpp:52
Interface for parsed descriptor objects.
Definition: descriptor.h:109
virtual size_t GetKeyCount() const =0
Get the number of key expressions in this descriptor.
virtual bool IsSingleType() const =0
Whether this descriptor will return at most one scriptPubKey or multiple (aka is or is not combo)
virtual std::optional< int64_t > MaxSatisfactionElems() const =0
Get the maximum size number of stack elements for satisfying this descriptor.
virtual ~Descriptor()=default
virtual void GetPubKeys(std::set< CPubKey > &pubkeys, std::set< CExtPubKey > &ext_pubs) const =0
Return all (extended) public keys for this descriptor, including any from subdescriptors.
virtual bool ToNormalizedString(const SigningProvider &provider, std::string &out, const DescriptorCache *cache=nullptr) const =0
Convert the descriptor to a normalized string.
virtual std::optional< int64_t > MaxSatisfactionWeight(bool use_max_sig) const =0
Get the maximum size of a satisfaction for this descriptor, in weight units.
virtual std::vector< std::string > Warnings() const =0
Semantic/safety warnings (includes subdescriptors).
virtual std::string ToCanonicalString() const =0
Convert the descriptor to the canonical string.
virtual std::string ToString(bool compat_format=false) const =0
Convert the descriptor back to a string, undoing parsing.
virtual bool CanSelfExpand() const =0
Whether the descriptor can be used to produce its address(es) without needing a cache or private keys...
virtual std::optional< OutputType > GetOutputType() const =0
virtual bool HasScripts() const =0
Whether this descriptor produces any scripts with the Expand functions.
virtual bool Expand(int pos, const SigningProvider &provider, std::vector< CScript > &output_scripts, FlatSigningProvider &out, DescriptorCache *write_cache=nullptr) const =0
Expand a descriptor at a specified position.
virtual bool IsRange() const =0
Whether the expansion of this descriptor depends on the position.
virtual std::optional< int64_t > ScriptSize() const =0
Get the size of the scriptPubKey for this descriptor.
virtual bool IsSolvable() const =0
Whether this descriptor has all information about signing ignoring lack of private keys.
virtual void ExpandPrivate(int pos, const SigningProvider &provider, FlatSigningProvider &out) const =0
Expand the private key for a descriptor at a specified position, if possible.
virtual uint32_t GetMaxKeyExpr() const =0
Get the maximum key expression index.
virtual bool ToPrivateString(const SigningProvider &provider, std::string &out) const =0
Convert the descriptor to a private string.
virtual bool HavePrivateKeys(const SigningProvider &provider) const =0
Whether the given provider has all private keys required by this descriptor.
virtual bool ExpandFromCache(int pos, const DescriptorCache &read_cache, std::vector< CScript > &output_scripts, FlatSigningProvider &out) const =0
Expand a descriptor at a specified position using cached expansion data.
FuzzedDataProvider provider
Definition: dbwrapper.cpp:366