Bitcoin Core  0.20.99
P2P Digital Currency
descriptor.h
Go to the documentation of this file.
1 // Copyright (c) 2018-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_SCRIPT_DESCRIPTOR_H
6 #define BITCOIN_SCRIPT_DESCRIPTOR_H
7 
8 #include <optional.h>
9 #include <outputtype.h>
10 #include <script/script.h>
11 #include <script/sign.h>
12 #include <script/signingprovider.h>
13 
14 #include <vector>
15 
16 using ExtPubKeyMap = std::unordered_map<uint32_t, CExtPubKey>;
17 
20 private:
22  std::unordered_map<uint32_t, ExtPubKeyMap> m_derived_xpubs;
25 
26 public:
32  void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey& xpub);
38  bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey& xpub) const;
45  void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey& xpub);
52  bool GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey& xpub) const;
53 
57  const std::unordered_map<uint32_t, ExtPubKeyMap> GetCachedDerivedExtPubKeys() const;
58 };
59 
77 struct Descriptor {
78  virtual ~Descriptor() = default;
79 
81  virtual bool IsRange() const = 0;
82 
85  virtual bool IsSolvable() const = 0;
86 
88  virtual std::string ToString() const = 0;
89 
91  virtual bool IsSingleType() const = 0;
92 
94  virtual bool ToPrivateString(const SigningProvider& provider, std::string& out) const = 0;
95 
104  virtual bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const = 0;
105 
113  virtual bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const = 0;
114 
121  virtual void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const = 0;
122 
124  virtual Optional<OutputType> GetOutputType() const = 0;
125 };
126 
135 std::unique_ptr<Descriptor> Parse(const std::string& descriptor, FlatSigningProvider& out, std::string& error, bool require_checksum = false);
136 
143 std::string GetDescriptorChecksum(const std::string& descriptor);
144 
159 std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const SigningProvider& provider);
160 
161 #endif // BITCOIN_SCRIPT_DESCRIPTOR_H
const std::unordered_map< uint32_t, ExtPubKeyMap > GetCachedDerivedExtPubKeys() const
Retrieve all cached derived xpubs.
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum=false)
Parse a descriptor string.
ExtPubKeyMap m_parent_xpubs
Map key expression index -> parent xpub.
Definition: descriptor.h:24
bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey &xpub) const
Retrieve a cached parent xpub.
std::string ToString(const T &t)
Locale-independent version of std::to_string.
Definition: string.h:71
bool IsSolvable(const SigningProvider &provider, const CScript &script)
Definition: sign.cpp:437
An interface to be implemented by keystores that support signing.
std::string GetDescriptorChecksum(const std::string &descriptor)
Get the checksum for a descriptor.
std::unordered_map< uint32_t, CExtPubKey > ExtPubKeyMap
Definition: descriptor.h:16
Cache for single descriptor&#39;s derived extended pubkeys.
Definition: descriptor.h:19
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:404
void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey &xpub)
Cache a parent xpub.
std::unique_ptr< Descriptor > InferDescriptor(const CScript &script, const SigningProvider &provider)
Find a descriptor for the specified script, using information from provider where possible...
boost::optional< T > Optional
Substitute for C++17 std::optional.
Definition: optional.h:14
const ExtPubKeyMap GetCachedParentExtPubKeys() const
Retrieve all cached parent xpubs.
bool error(const char *fmt, const Args &... args)
Definition: system.h:52
void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey &xpub)
Cache an 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:22
Interface for parsed descriptor objects.
Definition: descriptor.h:77
bool GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, CExtPubKey &xpub) const
Retrieve a cached xpub derived at an index.