From fb1ba320db81f898ba83d1190c813e7e56f1445d Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Thu, 4 Feb 2021 16:11:05 -0800 Subject: [PATCH] refactor: move population of out.scripts from ExpandHelper to MakeScripts There are currently two DescriptorImpl subclasses that rely on the functionality that ExpandHelper automatically adds subscripts to the output SigningProvider. Taproot descriptors will have subscripts, but we don't want them in the SigningProvider's bare script field. To avoid them ending up there, move this functionality into the specific classes' MakeScripts implementation. --- src/script/descriptor.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/script/descriptor.cpp b/src/script/descriptor.cpp index ed382f2a5435ae..d15cba1fa8a630 100644 --- a/src/script/descriptor.cpp +++ b/src/script/descriptor.cpp @@ -497,7 +497,7 @@ class DescriptorImpl : public Descriptor * @param pubkeys The evaluations of the m_pubkey_args field. * @param script The evaluation of m_subdescriptor_arg (or nullptr when m_subdescriptor_arg is nullptr). * @param out A FlatSigningProvider to put scripts or public keys in that are necessary to the solver. - * The script arguments to this function are automatically added, as is the origin info of the provided pubkeys. + * The origin info of the provided pubkeys is automatically added. * @return A vector with scriptPubKeys for this descriptor. */ virtual std::vector MakeScripts(const std::vector& pubkeys, const CScript* script, FlatSigningProvider& out) const = 0; @@ -597,7 +597,6 @@ class DescriptorImpl : public Descriptor out.origins.emplace(entry.first.GetID(), std::make_pair(CPubKey(entry.first), std::move(entry.second))); } if (m_subdescriptor_arg) { - out.scripts.emplace(CScriptID(subscripts[0]), subscripts[0]); output_scripts = MakeScripts(pubkeys, &subscripts[0], out); } else { output_scripts = MakeScripts(pubkeys, nullptr, out); @@ -776,7 +775,12 @@ class MultisigDescriptor final : public DescriptorImpl class SHDescriptor final : public DescriptorImpl { protected: - std::vector MakeScripts(const std::vector&, const CScript* script, FlatSigningProvider&) const override { return Vector(GetScriptForDestination(ScriptHash(*script))); } + std::vector MakeScripts(const std::vector&, const CScript* script, FlatSigningProvider& out) const override + { + auto ret = Vector(GetScriptForDestination(ScriptHash(*script))); + if (ret.size()) out.scripts.emplace(CScriptID(*script), *script); + return ret; + } public: SHDescriptor(std::unique_ptr desc) : DescriptorImpl({}, std::move(desc), "sh") {} @@ -793,7 +797,12 @@ class SHDescriptor final : public DescriptorImpl class WSHDescriptor final : public DescriptorImpl { protected: - std::vector MakeScripts(const std::vector&, const CScript* script, FlatSigningProvider&) const override { return Vector(GetScriptForDestination(WitnessV0ScriptHash(*script))); } + std::vector MakeScripts(const std::vector&, const CScript* script, FlatSigningProvider& out) const override + { + auto ret = Vector(GetScriptForDestination(WitnessV0ScriptHash(*script))); + if (ret.size()) out.scripts.emplace(CScriptID(*script), *script); + return ret; + } public: WSHDescriptor(std::unique_ptr desc) : DescriptorImpl({}, std::move(desc), "wsh") {} std::optional GetOutputType() const override { return OutputType::BECH32; }