From 8d9670ccb756592bddb2a269bf5078d62658537b Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Tue, 9 Nov 2021 23:08:16 -0500 Subject: Add rawtr() descriptor for P2TR with unknown tweak --- src/script/descriptor.cpp | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'src/script') diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index 34a4da74f8..db386c9ab8 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -1015,6 +1015,24 @@ public: bool IsSingleType() const final { return true; } }; +/** A parsed rawtr(...) descriptor. */ +class RawTRDescriptor final : public DescriptorImpl +{ +protected: + std::vector MakeScripts(const std::vector& keys, Span scripts, FlatSigningProvider& out) const override + { + assert(keys.size() == 1); + XOnlyPubKey xpk(keys[0]); + if (!xpk.IsFullyValid()) return {}; + WitnessV1Taproot output{xpk}; + return Vector(GetScriptForDestination(output)); + } +public: + RawTRDescriptor(std::unique_ptr output_key) : DescriptorImpl(Vector(std::move(output_key)), "rawtr") {} + std::optional GetOutputType() const override { return OutputType::BECH32M; } + bool IsSingleType() const final { return true; } +}; + //////////////////////////////////////////////////////////////////////////// // Parser // //////////////////////////////////////////////////////////////////////////// @@ -1453,6 +1471,16 @@ std::unique_ptr ParseScript(uint32_t& key_exp_index, Span(std::move(output_key)); + } else if (Func("rawtr", expr)) { + error = "Can only have rawtr at top level"; + return nullptr; + } if (ctx == ParseScriptContext::TOP && Func("raw", expr)) { std::string str(expr.begin(), expr.end()); if (!IsHex(str)) { @@ -1626,6 +1654,13 @@ std::unique_ptr InferScript(const CScript& script, ParseScriptCo } } } + // If the above doesn't work, construct a rawtr() descriptor with just the encoded x-only pubkey. + if (pubkey.IsFullyValid()) { + auto key = InferXOnlyPubkey(pubkey, ParseScriptContext::P2TR, provider); + if (key) { + return std::make_unique(std::move(key)); + } + } } if (ctx == ParseScriptContext::P2WSH) { -- cgit v1.2.3