2018-07-04 21:08:19 -04:00
|
|
|
// Copyright (c) 2018 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_SCRIPT_DESCRIPTOR_H
|
|
|
|
#define BITCOIN_SCRIPT_DESCRIPTOR_H
|
|
|
|
|
|
|
|
#include <script/script.h>
|
|
|
|
#include <script/sign.h>
|
2019-06-06 16:52:24 -04:00
|
|
|
#include <script/signingprovider.h>
|
2018-07-04 21:08:19 -04:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// Descriptors are strings that describe a set of scriptPubKeys, together with
|
|
|
|
// all information necessary to solve them. By combining all information into
|
|
|
|
// one, they avoid the need to separately import keys and scripts.
|
|
|
|
//
|
|
|
|
// Descriptors may be ranged, which occurs when the public keys inside are
|
|
|
|
// specified in the form of HD chains (xpubs).
|
|
|
|
//
|
|
|
|
// Descriptors always represent public information - public keys and scripts -
|
|
|
|
// but in cases where private keys need to be conveyed along with a descriptor,
|
|
|
|
// they can be included inside by changing public keys to private keys (WIF
|
|
|
|
// format), and changing xpubs by xprvs.
|
|
|
|
//
|
2018-09-05 14:59:02 -03:00
|
|
|
// Reference documentation about the descriptor language can be found in
|
|
|
|
// doc/descriptors.md.
|
2018-07-04 21:08:19 -04:00
|
|
|
|
|
|
|
/** Interface for parsed descriptor objects. */
|
|
|
|
struct Descriptor {
|
|
|
|
virtual ~Descriptor() = default;
|
|
|
|
|
|
|
|
/** Whether the expansion of this descriptor depends on the position. */
|
|
|
|
virtual bool IsRange() const = 0;
|
|
|
|
|
2018-10-12 22:27:00 -03:00
|
|
|
/** Whether this descriptor has all information about signing ignoring lack of private keys.
|
|
|
|
* This is true for all descriptors except ones that use `raw` or `addr` constructions. */
|
|
|
|
virtual bool IsSolvable() const = 0;
|
|
|
|
|
2018-07-04 21:08:19 -04:00
|
|
|
/** Convert the descriptor back to a string, undoing parsing. */
|
|
|
|
virtual std::string ToString() const = 0;
|
|
|
|
|
|
|
|
/** Convert the descriptor to a private string. This fails if the provided provider does not have the relevant private keys. */
|
|
|
|
virtual bool ToPrivateString(const SigningProvider& provider, std::string& out) const = 0;
|
|
|
|
|
|
|
|
/** Expand a descriptor at a specified position.
|
|
|
|
*
|
|
|
|
* pos: the position at which to expand the descriptor. If IsRange() is false, this is ignored.
|
|
|
|
* provider: the provider to query for private keys in case of hardened derivation.
|
|
|
|
* output_script: the expanded scriptPubKeys will be put here.
|
|
|
|
* out: scripts and public keys necessary for solving the expanded scriptPubKeys will be put here (may be equal to provider).
|
2018-11-02 23:19:39 -03:00
|
|
|
* cache: vector which will be overwritten with cache data necessary to-evaluate the descriptor at this point without access to private keys.
|
2018-07-04 21:08:19 -04:00
|
|
|
*/
|
2018-11-02 23:19:39 -03:00
|
|
|
virtual bool Expand(int pos, const SigningProvider& provider, std::vector<CScript>& output_scripts, FlatSigningProvider& out, std::vector<unsigned char>* cache = nullptr) const = 0;
|
|
|
|
|
|
|
|
/** Expand a descriptor at a specified position using cached expansion data.
|
|
|
|
*
|
|
|
|
* pos: the position at which to expand the descriptor. If IsRange() is false, this is ignored.
|
|
|
|
* cache: vector from which cached expansion data will be read.
|
|
|
|
* output_script: the expanded scriptPubKeys will be put here.
|
|
|
|
* out: scripts and public keys necessary for solving the expanded scriptPubKeys will be put here (may be equal to provider).
|
|
|
|
*/
|
|
|
|
virtual bool ExpandFromCache(int pos, const std::vector<unsigned char>& cache, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const = 0;
|
2018-12-18 23:54:40 -03:00
|
|
|
|
|
|
|
/** Expand the private key for a descriptor at a specified position, if possible.
|
|
|
|
*
|
|
|
|
* pos: the position at which to expand the descriptor. If IsRange() is false, this is ignored.
|
|
|
|
* provider: the provider to query for the private keys.
|
|
|
|
* out: any private keys available for the specified pos will be placed here.
|
|
|
|
*/
|
|
|
|
virtual void ExpandPrivate(int pos, const SigningProvider& provider, FlatSigningProvider& out) const = 0;
|
2018-07-04 21:08:19 -04:00
|
|
|
};
|
|
|
|
|
2019-02-06 23:08:21 -03:00
|
|
|
/** Parse a descriptor string. Included private keys are put in out.
|
|
|
|
*
|
|
|
|
* If the descriptor has a checksum, it must be valid. If require_checksum
|
|
|
|
* is set, the checksum is mandatory - otherwise it is optional.
|
|
|
|
*
|
|
|
|
* If a parse error occurs, or the checksum is missing/invalid, or anything
|
|
|
|
* else is wrong, nullptr is returned.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<Descriptor> Parse(const std::string& descriptor, FlatSigningProvider& out, bool require_checksum = false);
|
2018-07-04 21:08:19 -04:00
|
|
|
|
2018-10-12 22:22:22 -03:00
|
|
|
/** Find a descriptor for the specified script, using information from provider where possible.
|
|
|
|
*
|
|
|
|
* A non-ranged descriptor which only generates the specified script will be returned in all
|
|
|
|
* circumstances.
|
|
|
|
*
|
|
|
|
* For public keys with key origin information, this information will be preserved in the returned
|
|
|
|
* descriptor.
|
|
|
|
*
|
|
|
|
* - If all information for solving `script` is present in `provider`, a descriptor will be returned
|
|
|
|
* which is `IsSolvable()` and encapsulates said information.
|
|
|
|
* - Failing that, if `script` corresponds to a known address type, an "addr()" descriptor will be
|
|
|
|
* returned (which is not `IsSolvable()`).
|
|
|
|
* - Failing that, a "raw()" descriptor is returned.
|
|
|
|
*/
|
|
|
|
std::unique_ptr<Descriptor> InferDescriptor(const CScript& script, const SigningProvider& provider);
|
2018-07-04 21:08:19 -04:00
|
|
|
|
2018-10-12 22:22:22 -03:00
|
|
|
#endif // BITCOIN_SCRIPT_DESCRIPTOR_H
|