descriptor.h raw

   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      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      /** Whether the descriptor can be used to produce its address(es) without needing a cache or private keys. */
 144      virtual bool CanSelfExpand() const = 0;
 145  
 146      /** Expand a descriptor at a specified position.
 147       *
 148       * @param[in] pos The position at which to expand the descriptor. If IsRange() is false, this is ignored.
 149       * @param[in] provider The provider to query for private keys in case of hardened derivation.
 150       * @param[out] output_scripts The expanded scriptPubKeys.
 151       * @param[out] out Scripts and public keys necessary for solving the expanded scriptPubKeys (may be equal to `provider`).
 152       * @param[out] write_cache Cache data necessary to evaluate the descriptor at this point without access to private keys.
 153       */
 154      virtual bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, DescriptorCache* write_cache = nullptr) const = 0;
 155  
 156      /** Expand a descriptor at a specified position using cached expansion data.
 157       *
 158       * @param[in] pos The position at which to expand the descriptor. If IsRange() is false, this is ignored.
 159       * @param[in] read_cache Cached expansion data.
 160       * @param[out] output_scripts The expanded scriptPubKeys.
 161       * @param[out] out Scripts and public keys necessary for solving the expanded scriptPubKeys (may be equal to `provider`).
 162       */
 163      virtual bool ExpandFromCache(int pos, const DescriptorCache& read_cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const = 0;
 164  
 165      /** Expand the private key for a descriptor at a specified position, if possible.
 166       *
 167       * @param[in] pos The position at which to expand the descriptor. If IsRange() is false, this is ignored.
 168       * @param[in] provider The provider to query for the private keys.
 169       * @param[out] out Any private keys available for the specified `pos`.
 170       */
 171      virtual void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const = 0;
 172  
 173      /** @return The OutputType of the scriptPubKey(s) produced by this descriptor. Or nullopt if indeterminate (multiple or none) */
 174      virtual std::optional<OutputType> GetOutputType() const = 0;
 175  
 176      /** Get the size of the scriptPubKey for this descriptor. */
 177      virtual std::optional<int64_t> ScriptSize() const = 0;
 178  
 179      /** Get the maximum size of a satisfaction for this descriptor, in weight units.
 180       *
 181       * @param use_max_sig Whether to assume ECDSA signatures will have a high-r.
 182       */
 183      virtual std::optional<int64_t> MaxSatisfactionWeight(bool use_max_sig) const = 0;
 184  
 185      /** Get the maximum size number of stack elements for satisfying this descriptor. */
 186      virtual std::optional<int64_t> MaxSatisfactionElems() const = 0;
 187  
 188      /** Return all (extended) public keys for this descriptor, including any from subdescriptors.
 189       *
 190       * @param[out] pubkeys Any public keys
 191       * @param[out] ext_pubs Any extended public keys
 192       */
 193      virtual void GetPubKeys(std::set<CPubKey>& pubkeys, std::set<CExtPubKey>& ext_pubs) const = 0;
 194  
 195      /** Whether this descriptor produces any scripts with the Expand functions */
 196      virtual bool HasScripts() const = 0;
 197  
 198      /** Semantic/safety warnings (includes subdescriptors). */
 199      virtual std::vector<std::string> Warnings() const = 0;
 200  
 201      /** Get the maximum key expression index. Used only for tests */
 202      virtual uint32_t GetMaxKeyExpr() const = 0;
 203  
 204      /** Get the number of key expressions in this descriptor. Used only for tests */
 205      virtual size_t GetKeyCount() const = 0;
 206  };
 207  
 208  /** Parse a `descriptor` string. Included private keys are put in `out`.
 209   *
 210   * If the descriptor has a checksum, it must be valid. If `require_checksum`
 211   * is set, the checksum is mandatory - otherwise it is optional.
 212   *
 213   * If a parse error occurs, or the checksum is missing/invalid, or anything
 214   * else is wrong, an empty vector is returned.
 215   */
 216  std::vector<std::unique_ptr<Descriptor>> Parse(std::string_view descriptor, FlatSigningProvider& out, std::string& error, bool require_checksum = false);
 217  
 218  /** Get the checksum for a `descriptor`.
 219   *
 220   * - If it already has one, and it is correct, return the checksum in the input.
 221   * - If it already has one that is wrong, return "".
 222   * - If it does not already have one, return the checksum that would need to be added.
 223   */
 224  std::string GetDescriptorChecksum(const std::string& descriptor);
 225  
 226  /** Find a descriptor for the specified `script`, using information from `provider` where possible.
 227   *
 228   * A non-ranged descriptor which only generates the specified script will be returned in all
 229   * circumstances.
 230   *
 231   * For public keys with key origin information, this information will be preserved in the returned
 232   * descriptor.
 233   *
 234   * - If all information for solving `script` is present in `provider`, a descriptor will be returned
 235   *   which is IsSolvable() and encapsulates said information.
 236   * - Failing that, if `script` corresponds to a known address type, an "addr()" descriptor will be
 237   *   returned (which is not IsSolvable()).
 238   * - Failing that, a "raw()" descriptor is returned.
 239   */
 240  std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const SigningProvider& provider);
 241  
 242  /** Unique identifier that may not change over time, unless explicitly marked as not backwards compatible.
 243  *   This is not part of BIP 380, not guaranteed to be interoperable and should not be exposed to the user.
 244  */
 245  uint256 DescriptorID(const Descriptor& desc);
 246  
 247  #endif // BITCOIN_SCRIPT_DESCRIPTOR_H
 248