Changeset View
Changeset View
Standalone View
Standalone View
src/script/descriptor.h
// Copyright (c) 2018 The Bitcoin Core developers | // Copyright (c) 2018 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_SCRIPT_DESCRIPTOR_H | #ifndef BITCOIN_SCRIPT_DESCRIPTOR_H | ||||
#define BITCOIN_SCRIPT_DESCRIPTOR_H | #define BITCOIN_SCRIPT_DESCRIPTOR_H | ||||
#include <script/script.h> | #include <script/script.h> | ||||
#include <script/sign.h> | #include <script/sign.h> | ||||
#include <script/signingprovider.h> | #include <script/signingprovider.h> | ||||
#include <vector> | #include <vector> | ||||
// Descriptors are strings that describe a set of scriptPubKeys, together with | /** | ||||
// all information necessary to solve them. By combining all information into | * \brief Interface for parsed descriptor objects. | ||||
// one, they avoid the need to separately import keys and scripts. | * | ||||
// | * Descriptors are strings that describe a set of scriptPubKeys, together with | ||||
// Descriptors may be ranged, which occurs when the public keys inside are | * all information necessary to solve them. By combining all information into | ||||
// specified in the form of HD chains (xpubs). | * one, they avoid the need to separately import keys and scripts. | ||||
// | * | ||||
// Descriptors always represent public information - public keys and scripts - | * Descriptors may be ranged, which occurs when the public keys inside are | ||||
// but in cases where private keys need to be conveyed along with a descriptor, | * specified in the form of HD chains (xpubs). | ||||
// they can be included inside by changing public keys to private keys (WIF | * | ||||
// format), and changing xpubs by xprvs. | * Descriptors always represent public information - public keys and scripts - | ||||
// | * but in cases where private keys need to be conveyed along with a descriptor, | ||||
// Reference documentation about the descriptor language can be found in | * they can be included inside by changing public keys to private keys (WIF | ||||
// doc/descriptors.md. | * format), and changing xpubs by xprvs. | ||||
* | |||||
/** Interface for parsed descriptor objects. */ | * Reference documentation about the descriptor language can be found in | ||||
* doc/descriptors.md. | |||||
*/ | |||||
struct Descriptor { | struct Descriptor { | ||||
virtual ~Descriptor() = default; | virtual ~Descriptor() = default; | ||||
/** Whether the expansion of this descriptor depends on the position. */ | /** Whether the expansion of this descriptor depends on the position. */ | ||||
virtual bool IsRange() const = 0; | virtual bool IsRange() const = 0; | ||||
/** | /** | ||||
* Whether this descriptor has all information about signing ignoring lack | * Whether this descriptor has all information about signing ignoring lack | ||||
Show All 10 Lines | struct Descriptor { | ||||
* provider does not have the relevant private keys. | * provider does not have the relevant private keys. | ||||
*/ | */ | ||||
virtual bool ToPrivateString(const SigningProvider &provider, | virtual bool ToPrivateString(const SigningProvider &provider, | ||||
std::string &out) const = 0; | std::string &out) const = 0; | ||||
/** | /** | ||||
* Expand a descriptor at a specified position. | * Expand a descriptor at a specified position. | ||||
* | * | ||||
* pos: the position at which to expand the descriptor. If IsRange() is | * @param[in] pos: The position at which to expand the descriptor. If | ||||
* false, this is ignored. | * IsRange() is false, this is ignored. | ||||
* provider: the provider to query for private keys in case of hardened | * @param[in] provider: The provider to query for private keys in case of | ||||
* derivation. | * hardened derivation. | ||||
* output_scripts: the expanded scriptPubKeys will be put here. | * @param[out] output_scripts: The expanded scriptPubKeys. | ||||
* out: scripts and public keys necessary for solving the expanded | * @param[out] out: Scripts and public keys necessary for solving the | ||||
* scriptPubKeys will be put here (may be equal to provider). | * expanded scriptPubKeys (may be equal to `provider`). | ||||
* cache: vector which will be overwritten with cache data necessary to | * @param[out] cache: Cache data necessary to evaluate the descriptor at | ||||
* evaluate the descriptor at this point without access to private | * this point without access to private keys. | ||||
* keys. | |||||
*/ | */ | ||||
virtual bool Expand(int pos, const SigningProvider &provider, | virtual bool Expand(int pos, const SigningProvider &provider, | ||||
std::vector<CScript> &output_scripts, | std::vector<CScript> &output_scripts, | ||||
FlatSigningProvider &out, | FlatSigningProvider &out, | ||||
std::vector<uint8_t> *cache = nullptr) const = 0; | std::vector<uint8_t> *cache = nullptr) const = 0; | ||||
/** | /** | ||||
* Expand a descriptor at a specified position using cached expansion data. | * Expand a descriptor at a specified position using cached expansion data. | ||||
* | * | ||||
* pos: the position at which to expand the descriptor. If IsRange() is | * @param[in] pos: The position at which to expand the descriptor. If | ||||
* false, this is ignored. | * IsRange() is false, this is ignored. | ||||
* cache: vector from which cached expansion data will be read. | * @param[in] cache: Cached expansion data. | ||||
* output_scripts: the expanded scriptPubKeys will be put here. | * @param[out] output_scripts: The expanded scriptPubKeys. | ||||
* out: scripts and public keys necessary for solving the expanded | * @param[out] out: Scripts and public keys necessary for solving the | ||||
* scriptPubKeys will be put here (may be equal to provider). | * expanded scriptPubKeys (may be equal to `provider`). | ||||
*/ | */ | ||||
virtual bool ExpandFromCache(int pos, const std::vector<uint8_t> &cache, | virtual bool ExpandFromCache(int pos, const std::vector<uint8_t> &cache, | ||||
std::vector<CScript> &output_scripts, | std::vector<CScript> &output_scripts, | ||||
FlatSigningProvider &out) const = 0; | FlatSigningProvider &out) const = 0; | ||||
/** | /** | ||||
* Expand the private key for a descriptor at a specified position, if | * Expand the private key for a descriptor at a specified position, if | ||||
* possible. | * possible. | ||||
* | * | ||||
* pos: the position at which to expand the descriptor. If IsRange() is | * @param[in] pos: The position at which to expand the descriptor. If | ||||
* false, this is ignored. | * IsRange() is false, this is ignored. | ||||
* provider: the provider to query for the private keys. | * @param[in] provider: The provider to query for the private keys. | ||||
* out: any private keys available for the specified pos will be placed | * @param[out] out: Any private keys available for the specified `pos`. | ||||
* here. | |||||
*/ | */ | ||||
virtual void ExpandPrivate(int pos, const SigningProvider &provider, | virtual void ExpandPrivate(int pos, const SigningProvider &provider, | ||||
FlatSigningProvider &out) const = 0; | FlatSigningProvider &out) const = 0; | ||||
}; | }; | ||||
/** | /** | ||||
* Parse a descriptor string. Included private keys are put in out. | * Parse a `descriptor` string. Included private keys are put in `out`. | ||||
* | * | ||||
* If the descriptor has a checksum, it must be valid. If require_checksum | * If the descriptor has a checksum, it must be valid. If `require_checksum` | ||||
* is set, the checksum is mandatory - otherwise it is optional. | * is set, the checksum is mandatory - otherwise it is optional. | ||||
* | * | ||||
* If a parse error occurs, or the checksum is missing/invalid, or anything | * If a parse error occurs, or the checksum is missing/invalid, or anything | ||||
* else is wrong, nullptr is returned. | * else is wrong, `nullptr` is returned. | ||||
*/ | */ | ||||
std::unique_ptr<Descriptor> Parse(const std::string &descriptor, | std::unique_ptr<Descriptor> Parse(const std::string &descriptor, | ||||
FlatSigningProvider &out, std::string &error, | FlatSigningProvider &out, std::string &error, | ||||
bool require_checksum = false); | bool require_checksum = false); | ||||
/** | /** | ||||
* Get the checksum for a descriptor. | * Get the checksum for a `descriptor`. | ||||
* | * | ||||
* If it already has one, and it is correct, return the checksum in the input. | * - If it already has one, and it is correct, return the checksum in the input. | ||||
* If it already has one that is wrong, return "". | * - If it already has one that is wrong, return "". | ||||
* If it does not already have one, return the checksum that would need to be | * - If it does not already have one, return the checksum that would need to be | ||||
* added. | * added. | ||||
*/ | */ | ||||
std::string GetDescriptorChecksum(const std::string &descriptor); | std::string GetDescriptorChecksum(const std::string &descriptor); | ||||
/** | /** | ||||
* Find a descriptor for the specified script, using information from provider | * Find a descriptor for the specified `script`, using information from | ||||
* where possible. | * `provider` where possible. | ||||
* | * | ||||
* A non-ranged descriptor which only generates the specified script will be | * A non-ranged descriptor which only generates the specified script will be | ||||
* returned in all circumstances. | * returned in all circumstances. | ||||
* | * | ||||
* For public keys with key origin information, this information will be | * For public keys with key origin information, this information will be | ||||
* preserved in the returned descriptor. | * preserved in the returned descriptor. | ||||
* | * | ||||
* - If all information for solving `script` is present in `provider`, a | * - If all information for solving `script` is present in `provider`, a | ||||
* descriptor will be returned which is `IsSolvable()` and encapsulates said | * descriptor will be returned which is IsSolvable() and encapsulates said | ||||
* information. | * information. | ||||
* - Failing that, if `script` corresponds to a known address type, an "addr()" | * - Failing that, if `script` corresponds to a known address type, an "addr()" | ||||
* descriptor will be returned (which is not `IsSolvable()`). | * descriptor will be returned (which is not IsSolvable()). | ||||
* - Failing that, a "raw()" descriptor is returned. | * - Failing that, a "raw()" descriptor is returned. | ||||
*/ | */ | ||||
std::unique_ptr<Descriptor> InferDescriptor(const CScript &script, | std::unique_ptr<Descriptor> InferDescriptor(const CScript &script, | ||||
const SigningProvider &provider); | const SigningProvider &provider); | ||||
#endif // BITCOIN_SCRIPT_DESCRIPTOR_H | #endif // BITCOIN_SCRIPT_DESCRIPTOR_H |