Bitcoin ABC 0.30.7
P2P Digital Currency
descriptor.h
Go to the documentation of this file.
1// Copyright (c) 2018 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 <script/script.h>
10#include <script/sign.h>
12
13#include <optional>
14#include <vector>
15
16using ExtPubKeyMap = std::unordered_map<uint32_t, CExtPubKey>;
17
20private:
22 std::unordered_map<uint32_t, ExtPubKeyMap> m_derived_xpubs;
25
26public:
34 void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey &xpub);
42 bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey &xpub) const;
51 void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index,
52 const CExtPubKey &xpub);
61 bool GetCachedDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index,
62 CExtPubKey &xpub) const;
63
67 const std::unordered_map<uint32_t, ExtPubKeyMap>
69};
70
89struct Descriptor {
90 virtual ~Descriptor() = default;
91
93 virtual bool IsRange() const = 0;
94
100 virtual bool IsSolvable() const = 0;
101
103 virtual std::string ToString() const = 0;
104
109 virtual bool IsSingleType() const = 0;
110
115 virtual bool ToPrivateString(const SigningProvider &provider,
116 std::string &out) const = 0;
117
131 virtual bool Expand(int pos, const SigningProvider &provider,
132 std::vector<CScript> &output_scripts,
134 DescriptorCache *write_cache = nullptr) const = 0;
135
146 virtual bool ExpandFromCache(int pos, const DescriptorCache &read_cache,
147 std::vector<CScript> &output_scripts,
148 FlatSigningProvider &out) const = 0;
149
159 virtual void ExpandPrivate(int pos, const SigningProvider &provider,
160 FlatSigningProvider &out) const = 0;
161
166 virtual std::optional<OutputType> GetOutputType() const = 0;
167};
168
178std::unique_ptr<Descriptor> Parse(const std::string &descriptor,
179 FlatSigningProvider &out, std::string &error,
180 bool require_checksum = false);
181
190std::string GetDescriptorChecksum(const std::string &descriptor);
191
209std::unique_ptr<Descriptor> InferDescriptor(const CScript &script,
210 const SigningProvider &provider);
211
212#endif // BITCOIN_SCRIPT_DESCRIPTOR_H
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:424
Cache for single descriptor's derived extended pubkeys.
Definition: descriptor.h:19
bool GetCachedParentExtPubKey(uint32_t key_exp_pos, CExtPubKey &xpub) const
Retrieve a cached parent xpub.
void CacheDerivedExtPubKey(uint32_t key_exp_pos, uint32_t der_index, const CExtPubKey &xpub)
Cache an xpub derived at an index.
void CacheParentExtPubKey(uint32_t key_exp_pos, const CExtPubKey &xpub)
Cache a parent 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:22
const ExtPubKeyMap GetCachedParentExtPubKeys() const
Retrieve all cached parent xpubs.
const std::unordered_map< uint32_t, ExtPubKeyMap > GetCachedDerivedExtPubKeys() const
Retrieve all cached derived xpubs.
ExtPubKeyMap m_parent_xpubs
Map key expression index -> parent xpub.
Definition: descriptor.h:24
An interface to be implemented by keystores that support signing.
std::unique_ptr< Descriptor > Parse(const std::string &descriptor, FlatSigningProvider &out, std::string &error, bool require_checksum=false)
Parse a descriptor string.
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::string GetDescriptorChecksum(const std::string &descriptor)
Get the checksum for a descriptor.
std::unordered_map< uint32_t, CExtPubKey > ExtPubKeyMap
Definition: descriptor.h:16
bool error(const char *fmt, const Args &...args)
Definition: logging.h:263
Interface for parsed descriptor objects.
Definition: descriptor.h:89
virtual bool IsSingleType() const =0
Whether this descriptor will return one scriptPubKey or multiple (aka is or is not combo)
virtual std::string ToString() const =0
Convert the descriptor back to a string, undoing parsing.
virtual ~Descriptor()=default
virtual std::optional< OutputType > GetOutputType() const =0
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 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 bool ToPrivateString(const SigningProvider &provider, std::string &out) const =0
Convert the descriptor to a private string.
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.