mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-08 10:31:50 -05:00
Add tr() descriptor (derivation only, no signing)
This adds a new descriptor with syntax e.g. tr(KEY,{S1,{{S2,S3},S4}) where KEY is a key expression for the internal key and S_i are script expression for the leaves. They have to be organized in nested {A,B} groups, with exactly two elements. tr() only exists at the top level, and inside the script expressions only pk() scripts are allowed for now.
This commit is contained in:
parent
90fcac365e
commit
7cedafc541
4 changed files with 163 additions and 15 deletions
|
@ -30,6 +30,7 @@ Output descriptors currently support:
|
|||
- Pay-to-witness-pubkey-hash scripts (P2WPKH), through the `wpkh` function.
|
||||
- Pay-to-script-hash scripts (P2SH), through the `sh` function.
|
||||
- Pay-to-witness-script-hash scripts (P2WSH), through the `wsh` function.
|
||||
- Pay-to-taproot outputs (P2TR), through the `tr` function.
|
||||
- Multisig scripts, through the `multi` function.
|
||||
- Multisig scripts where the public keys are sorted lexicographically, through the `sortedmulti` function.
|
||||
- Any type of supported address through the `addr` function.
|
||||
|
@ -54,6 +55,7 @@ Output descriptors currently support:
|
|||
- `pkh([d34db33f/44'/0'/0']xpub6ERApfZwUNrhLCkDtcHTcxd75RbzS1ed54G1LkBUHQVHQKqhMkhgbmJbZRkrgZw4koxb5JaHWkY4ALHY2grBGRjaDMzQLcgJvLJuZZvRcEL/1/*)` describes a set of P2PKH outputs, but additionally specifies that the specified xpub is a child of a master with fingerprint `d34db33f`, and derived using path `44'/0'/0'`.
|
||||
- `wsh(multi(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*))` describes a set of *1-of-2* P2WSH multisig outputs where the first multisig key is the *1/0/`i`* child of the first specified xpub and the second multisig key is the *0/0/`i`* child of the second specified xpub, and `i` is any number in a configurable range (`0-1000` by default).
|
||||
- `wsh(sortedmulti(1,xpub661MyMwAqRbcFW31YEwpkMuc5THy2PSt5bDMsktWQcFF8syAmRUapSCGu8ED9W6oDMSgv6Zz8idoc4a6mr8BDzTJY47LJhkJ8UB7WEGuduB/1/0/*,xpub69H7F5d8KSRgmmdJg2KhpAK8SR3DjMwAdkxj3ZuxV27CprR9LgpeyGmXUbC6wb7ERfvrnKZjXoUmmDznezpbZb7ap6r1D3tgFxHmwMkQTPH/0/0/*))` describes a set of *1-of-2* P2WSH multisig outputs where one multisig key is the *1/0/`i`* child of the first specified xpub and the other multisig key is the *0/0/`i`* child of the second specified xpub, and `i` is any number in a configurable range (`0-1000` by default). The order of public keys in the resulting witnessScripts is determined by the lexicographic order of the public keys at that index.
|
||||
- `tr(c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5,{pk(fff97bd5755eeea420453a14355235d382f6472f8568a18b2f057a1460297556),pk(e493dbf1c10d80f3581e4904930b1404cc6c13900ee0758474fa94abe8c4cd13)})` describes a P2TR output with the `c6...` x-only pubkey as internal key, and two script paths.
|
||||
|
||||
## Reference
|
||||
|
||||
|
@ -61,13 +63,14 @@ Descriptors consist of several types of expressions. The top level expression is
|
|||
|
||||
`SCRIPT` expressions:
|
||||
- `sh(SCRIPT)` (top level only): P2SH embed the argument.
|
||||
- `wsh(SCRIPT)` (not inside another 'wsh'): P2WSH embed the argument.
|
||||
- `wsh(SCRIPT)` (top level or inside `sh` only): P2WSH embed the argument.
|
||||
- `pk(KEY)` (anywhere): P2PK output for the given public key.
|
||||
- `pkh(KEY)` (anywhere): P2PKH output for the given public key (use `addr` if you only know the pubkey hash).
|
||||
- `wpkh(KEY)` (not inside `wsh`): P2WPKH output for the given compressed pubkey.
|
||||
- `pkh(KEY)` (not inside `tr`): P2PKH output for the given public key (use `addr` if you only know the pubkey hash).
|
||||
- `wpkh(KEY)` (top level or inside `sh` only): P2WPKH output for the given compressed pubkey.
|
||||
- `combo(KEY)` (top level only): an alias for the collection of `pk(KEY)` and `pkh(KEY)`. If the key is compressed, it also includes `wpkh(KEY)` and `sh(wpkh(KEY))`.
|
||||
- `multi(k,KEY_1,KEY_2,...,KEY_n)` (anywhere): k-of-n multisig script.
|
||||
- `sortedmulti(k,KEY_1,KEY_2,...,KEY_n)` (anywhere): k-of-n multisig script with keys sorted lexicographically in the resulting script.
|
||||
- `multi(k,KEY_1,KEY_2,...,KEY_n)` (not inside `tr`): k-of-n multisig script.
|
||||
- `sortedmulti(k,KEY_1,KEY_2,...,KEY_n)` (not inside `tr`): k-of-n multisig script with keys sorted lexicographically in the resulting script.
|
||||
- `tr(KEY)` or `tr(KEY,TREE)` (top level only): P2TR output with the specified key as internal key, and optionally a tree of script paths.
|
||||
- `addr(ADDR)` (top level only): the script which ADDR expands to.
|
||||
- `raw(HEX)` (top level only): the script whose hex encoding is HEX.
|
||||
|
||||
|
@ -80,12 +83,17 @@ Descriptors consist of several types of expressions. The top level expression is
|
|||
- Followed by the actual key, which is either:
|
||||
- Hex encoded public keys (either 66 characters starting with `02` or `03` for a compressed pubkey, or 130 characters starting with `04` for an uncompressed pubkey).
|
||||
- Inside `wpkh` and `wsh`, only compressed public keys are permitted.
|
||||
- Inside `tr`, x-only pubkeys are also permitted (64 hex characters).
|
||||
- [WIF](https://en.bitcoin.it/wiki/Wallet_import_format) encoded private keys may be specified instead of the corresponding public key, with the same meaning.
|
||||
- `xpub` encoded extended public key or `xprv` encoded extended private key (as defined in [BIP 32](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)).
|
||||
- Followed by zero or more `/NUM` unhardened and `/NUM'` hardened BIP32 derivation steps.
|
||||
- Optionally followed by a single `/*` or `/*'` final step to denote all (direct) unhardened or hardened children.
|
||||
- The usage of hardened derivation steps requires providing the private key.
|
||||
|
||||
`TREE` expressions:
|
||||
- any `SCRIPT` expression
|
||||
- An open brace `{`, a `TREE` expression, a comma `,`, a `TREE` expression, and a closing brace `}`
|
||||
|
||||
(Anywhere a `'` suffix is permitted to denote hardened derivation, the suffix `h` can be used instead.)
|
||||
|
||||
`ADDR` expressions are any type of supported address:
|
||||
|
|
|
@ -237,6 +237,9 @@ public:
|
|||
/** Construct an x-only pubkey from exactly 32 bytes. */
|
||||
explicit XOnlyPubKey(Span<const unsigned char> bytes);
|
||||
|
||||
/** Construct an x-only pubkey from a normal pubkey. */
|
||||
explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(Span<const unsigned char>(pubkey.begin() + 1, pubkey.begin() + 33)) {}
|
||||
|
||||
/** Verify a Schnorr signature against this public key.
|
||||
*
|
||||
* sigbytes must be exactly 64 bytes.
|
||||
|
|
|
@ -241,9 +241,10 @@ public:
|
|||
class ConstPubkeyProvider final : public PubkeyProvider
|
||||
{
|
||||
CPubKey m_pubkey;
|
||||
bool m_xonly;
|
||||
|
||||
public:
|
||||
ConstPubkeyProvider(uint32_t exp_index, const CPubKey& pubkey) : PubkeyProvider(exp_index), m_pubkey(pubkey) {}
|
||||
ConstPubkeyProvider(uint32_t exp_index, const CPubKey& pubkey, bool xonly = false) : PubkeyProvider(exp_index), m_pubkey(pubkey), m_xonly(xonly) {}
|
||||
bool GetPubKey(int pos, const SigningProvider& arg, CPubKey& key, KeyOriginInfo& info, const DescriptorCache* read_cache = nullptr, DescriptorCache* write_cache = nullptr) override
|
||||
{
|
||||
key = m_pubkey;
|
||||
|
@ -254,7 +255,7 @@ public:
|
|||
}
|
||||
bool IsRange() const override { return false; }
|
||||
size_t GetSize() const override { return m_pubkey.size(); }
|
||||
std::string ToString() const override { return HexStr(m_pubkey); }
|
||||
std::string ToString() const override { return m_xonly ? HexStr(m_pubkey).substr(2) : HexStr(m_pubkey); }
|
||||
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
|
||||
{
|
||||
CKey key;
|
||||
|
@ -505,6 +506,7 @@ protected:
|
|||
public:
|
||||
DescriptorImpl(std::vector<std::unique_ptr<PubkeyProvider>> pubkeys, const std::string& name) : m_pubkey_args(std::move(pubkeys)), m_name(name), m_subdescriptor_args() {}
|
||||
DescriptorImpl(std::vector<std::unique_ptr<PubkeyProvider>> pubkeys, std::unique_ptr<DescriptorImpl> script, const std::string& name) : m_pubkey_args(std::move(pubkeys)), m_name(name), m_subdescriptor_args(Vector(std::move(script))) {}
|
||||
DescriptorImpl(std::vector<std::unique_ptr<PubkeyProvider>> pubkeys, std::vector<std::unique_ptr<DescriptorImpl>> scripts, const std::string& name) : m_pubkey_args(std::move(pubkeys)), m_name(name), m_subdescriptor_args(std::move(scripts)) {}
|
||||
|
||||
bool IsSolvable() const override
|
||||
{
|
||||
|
@ -693,10 +695,20 @@ public:
|
|||
/** A parsed pk(P) descriptor. */
|
||||
class PKDescriptor final : public DescriptorImpl
|
||||
{
|
||||
private:
|
||||
const bool m_xonly;
|
||||
protected:
|
||||
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider&) const override { return Vector(GetScriptForRawPubKey(keys[0])); }
|
||||
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript>, FlatSigningProvider&) const override
|
||||
{
|
||||
if (m_xonly) {
|
||||
CScript script = CScript() << ToByteVector(XOnlyPubKey(keys[0])) << OP_CHECKSIG;
|
||||
return Vector(std::move(script));
|
||||
} else {
|
||||
return Vector(GetScriptForRawPubKey(keys[0]));
|
||||
}
|
||||
}
|
||||
public:
|
||||
PKDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), "pk") {}
|
||||
PKDescriptor(std::unique_ptr<PubkeyProvider> prov, bool xonly = false) : DescriptorImpl(Vector(std::move(prov)), "pk"), m_xonly(xonly) {}
|
||||
bool IsSingleType() const final { return true; }
|
||||
};
|
||||
|
||||
|
@ -814,6 +826,56 @@ public:
|
|||
bool IsSingleType() const final { return true; }
|
||||
};
|
||||
|
||||
/** A parsed tr(...) descriptor. */
|
||||
class TRDescriptor final : public DescriptorImpl
|
||||
{
|
||||
std::vector<int> m_depths;
|
||||
protected:
|
||||
std::vector<CScript> MakeScripts(const std::vector<CPubKey>& keys, Span<const CScript> scripts, FlatSigningProvider& out) const override
|
||||
{
|
||||
TaprootBuilder builder;
|
||||
assert(m_depths.size() == scripts.size());
|
||||
for (size_t pos = 0; pos < m_depths.size(); ++pos) {
|
||||
builder.Add(m_depths[pos], scripts[pos], TAPROOT_LEAF_TAPSCRIPT);
|
||||
}
|
||||
if (!builder.IsComplete()) return {};
|
||||
assert(keys.size() == 1);
|
||||
XOnlyPubKey xpk(keys[0]);
|
||||
if (!xpk.IsFullyValid()) return {};
|
||||
builder.Finalize(xpk);
|
||||
return Vector(GetScriptForDestination(builder.GetOutput()));
|
||||
}
|
||||
bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, bool priv, bool normalized) const override
|
||||
{
|
||||
if (m_depths.empty()) return true;
|
||||
std::vector<bool> path;
|
||||
for (size_t pos = 0; pos < m_depths.size(); ++pos) {
|
||||
if (pos) ret += ',';
|
||||
while ((int)path.size() <= m_depths[pos]) {
|
||||
if (path.size()) ret += '{';
|
||||
path.push_back(false);
|
||||
}
|
||||
std::string tmp;
|
||||
if (!m_subdescriptor_args[pos]->ToStringHelper(arg, tmp, priv, normalized)) return false;
|
||||
ret += std::move(tmp);
|
||||
while (!path.empty() && path.back()) {
|
||||
if (path.size() > 1) ret += '}';
|
||||
path.pop_back();
|
||||
}
|
||||
if (!path.empty()) path.back() = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
public:
|
||||
TRDescriptor(std::unique_ptr<PubkeyProvider> internal_key, std::vector<std::unique_ptr<DescriptorImpl>> descs, std::vector<int> depths) :
|
||||
DescriptorImpl(Vector(std::move(internal_key)), std::move(descs), "tr"), m_depths(std::move(depths))
|
||||
{
|
||||
assert(m_subdescriptor_args.size() == m_depths.size());
|
||||
}
|
||||
std::optional<OutputType> GetOutputType() const override { return OutputType::BECH32; }
|
||||
bool IsSingleType() const final { return true; }
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
// Parser //
|
||||
////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -823,6 +885,7 @@ enum class ParseScriptContext {
|
|||
P2SH, //!< Inside sh() (script becomes P2SH redeemScript)
|
||||
P2WPKH, //!< Inside wpkh() (no script, pubkey only)
|
||||
P2WSH, //!< Inside wsh() (script becomes v0 witness script)
|
||||
P2TR, //!< Inside tr() (either internal key, or BIP342 script leaf)
|
||||
};
|
||||
|
||||
/** Parse a key path, being passed a split list of elements (the first element is ignored). */
|
||||
|
@ -871,6 +934,13 @@ std::unique_ptr<PubkeyProvider> ParsePubkeyInner(uint32_t key_exp_index, const S
|
|||
error = "Uncompressed keys are not allowed";
|
||||
return nullptr;
|
||||
}
|
||||
} else if (data.size() == 32 && ctx == ParseScriptContext::P2TR) {
|
||||
unsigned char fullkey[33] = {0x02};
|
||||
std::copy(data.begin(), data.end(), fullkey + 1);
|
||||
pubkey.Set(std::begin(fullkey), std::end(fullkey));
|
||||
if (pubkey.IsFullyValid()) {
|
||||
return std::make_unique<ConstPubkeyProvider>(key_exp_index, pubkey, true);
|
||||
}
|
||||
}
|
||||
error = strprintf("Pubkey '%s' is invalid", str);
|
||||
return nullptr;
|
||||
|
@ -958,13 +1028,16 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
|
|||
auto pubkey = ParsePubkey(key_exp_index, expr, ctx, out, error);
|
||||
if (!pubkey) return nullptr;
|
||||
++key_exp_index;
|
||||
return std::make_unique<PKDescriptor>(std::move(pubkey));
|
||||
return std::make_unique<PKDescriptor>(std::move(pubkey), ctx == ParseScriptContext::P2TR);
|
||||
}
|
||||
if (Func("pkh", expr)) {
|
||||
if ((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH) && Func("pkh", expr)) {
|
||||
auto pubkey = ParsePubkey(key_exp_index, expr, ctx, out, error);
|
||||
if (!pubkey) return nullptr;
|
||||
++key_exp_index;
|
||||
return std::make_unique<PKHDescriptor>(std::move(pubkey));
|
||||
} else if (Func("pkh", expr)) {
|
||||
error = "Can only have pkh at top level, in sh(), or in wsh()";
|
||||
return nullptr;
|
||||
}
|
||||
if (ctx == ParseScriptContext::TOP && Func("combo", expr)) {
|
||||
auto pubkey = ParsePubkey(key_exp_index, expr, ctx, out, error);
|
||||
|
@ -975,7 +1048,7 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
|
|||
error = "Can only have combo() at top level";
|
||||
return nullptr;
|
||||
}
|
||||
if ((sorted_multi = Func("sortedmulti", expr)) || Func("multi", expr)) {
|
||||
if ((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH || ctx == ParseScriptContext::P2WSH) && ((sorted_multi = Func("sortedmulti", expr)) || Func("multi", expr))) {
|
||||
auto threshold = Expr(expr);
|
||||
uint32_t thres;
|
||||
std::vector<std::unique_ptr<PubkeyProvider>> providers;
|
||||
|
@ -1020,6 +1093,9 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
|
|||
}
|
||||
}
|
||||
return std::make_unique<MultisigDescriptor>(thres, std::move(providers), sorted_multi);
|
||||
} else if (Func("sortedmulti", expr) || Func("multi", expr)) {
|
||||
error = "Can only have multi/sortedmulti at top level, in sh(), or in wsh()";
|
||||
return nullptr;
|
||||
}
|
||||
if ((ctx == ParseScriptContext::TOP || ctx == ParseScriptContext::P2SH) && Func("wpkh", expr)) {
|
||||
auto pubkey = ParsePubkey(key_exp_index, expr, ParseScriptContext::P2WPKH, out, error);
|
||||
|
@ -1057,6 +1133,67 @@ std::unique_ptr<DescriptorImpl> ParseScript(uint32_t& key_exp_index, Span<const
|
|||
error = "Can only have addr() at top level";
|
||||
return nullptr;
|
||||
}
|
||||
if (ctx == ParseScriptContext::TOP && Func("tr", expr)) {
|
||||
auto arg = Expr(expr);
|
||||
auto internal_key = ParsePubkey(key_exp_index, arg, ParseScriptContext::P2TR, out, error);
|
||||
if (!internal_key) return nullptr;
|
||||
++key_exp_index;
|
||||
std::vector<std::unique_ptr<DescriptorImpl>> subscripts; //!< list of script subexpressions
|
||||
std::vector<int> depths; //!< depth in the tree of each subexpression (same length subscripts)
|
||||
if (expr.size()) {
|
||||
if (!Const(",", expr)) {
|
||||
error = strprintf("tr: expected ',', got '%c'", expr[0]);
|
||||
return nullptr;
|
||||
}
|
||||
/** The path from the top of the tree to what we're currently processing.
|
||||
* branches[i] == false: left branch in the i'th step from the top; true: right branch.
|
||||
*/
|
||||
std::vector<bool> branches;
|
||||
// Loop over all provided scripts. In every iteration exactly one script will be processed.
|
||||
// Use a do-loop because inside this if-branch we expect at least one script.
|
||||
do {
|
||||
// First process all open braces.
|
||||
while (Const("{", expr)) {
|
||||
branches.push_back(false); // new left branch
|
||||
if (branches.size() > TAPROOT_CONTROL_MAX_NODE_COUNT) {
|
||||
error = strprintf("tr() supports at most %i nesting levels", TAPROOT_CONTROL_MAX_NODE_COUNT);
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
// Process the actual script expression.
|
||||
auto sarg = Expr(expr);
|
||||
subscripts.emplace_back(ParseScript(key_exp_index, sarg, ParseScriptContext::P2TR, out, error));
|
||||
if (!subscripts.back()) return nullptr;
|
||||
depths.push_back(branches.size());
|
||||
// Process closing braces; one is expected for every right branch we were in.
|
||||
while (branches.size() && branches.back()) {
|
||||
if (!Const("}", expr)) {
|
||||
error = strprintf("tr(): expected '}' after script expression");
|
||||
return nullptr;
|
||||
}
|
||||
branches.pop_back(); // move up one level after encountering '}'
|
||||
}
|
||||
// If after that, we're at the end of a left branch, expect a comma.
|
||||
if (branches.size() && !branches.back()) {
|
||||
if (!Const(",", expr)) {
|
||||
error = strprintf("tr(): expected ',' after script expression");
|
||||
return nullptr;
|
||||
}
|
||||
branches.back() = true; // And now we're in a right branch.
|
||||
}
|
||||
} while (branches.size());
|
||||
// After we've explored a whole tree, we must be at the end of the expression.
|
||||
if (expr.size()) {
|
||||
error = strprintf("tr(): expected ')' after script expression");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
assert(TaprootBuilder::ValidDepths(depths));
|
||||
return std::make_unique<TRDescriptor>(std::move(internal_key), std::move(subscripts), std::move(depths));
|
||||
} else if (Func("tr", expr)) {
|
||||
error = "Can only have tr at top level";
|
||||
return nullptr;
|
||||
}
|
||||
if (ctx == ParseScriptContext::TOP && Func("raw", expr)) {
|
||||
std::string str(expr.begin(), expr.end());
|
||||
if (!IsHex(str)) {
|
||||
|
|
|
@ -34,11 +34,11 @@ Span<const char> Expr(Span<const char>& sp)
|
|||
int level = 0;
|
||||
auto it = sp.begin();
|
||||
while (it != sp.end()) {
|
||||
if (*it == '(') {
|
||||
if (*it == '(' || *it == '{') {
|
||||
++level;
|
||||
} else if (level && *it == ')') {
|
||||
} else if (level && (*it == ')' || *it == '}')) {
|
||||
--level;
|
||||
} else if (level == 0 && (*it == ')' || *it == ',')) {
|
||||
} else if (level == 0 && (*it == ')' || *it == '}' || *it == ',')) {
|
||||
break;
|
||||
}
|
||||
++it;
|
||||
|
|
Loading…
Add table
Reference in a new issue