Skip to content

Commit

Permalink
refactor: move population of out.scripts from ExpandHelper to MakeScr…
Browse files Browse the repository at this point in the history
…ipts

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.
  • Loading branch information
sipa authored and pull[bot] committed Feb 15, 2022
1 parent 811fe69 commit fb1ba32
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<CScript> MakeScripts(const std::vector<CPubKey>& pubkeys, const CScript* script, FlatSigningProvider& out) const = 0;
Expand Down Expand Up @@ -597,7 +597,6 @@ class DescriptorImpl : public Descriptor
out.origins.emplace(entry.first.GetID(), std::make_pair<CPubKey, KeyOriginInfo>(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);
Expand Down Expand Up @@ -776,7 +775,12 @@ class MultisigDescriptor final : public DescriptorImpl
class SHDescriptor final : public DescriptorImpl
{
protected:
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript* script, FlatSigningProvider&) const override { return Vector(GetScriptForDestination(ScriptHash(*script))); }
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, 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<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), "sh") {}

Expand All @@ -793,7 +797,12 @@ class SHDescriptor final : public DescriptorImpl
class WSHDescriptor final : public DescriptorImpl
{
protected:
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, const CScript* script, FlatSigningProvider&) const override { return Vector(GetScriptForDestination(WitnessV0ScriptHash(*script))); }
std::vector<CScript> MakeScripts(const std::vector<CPubKey>&, 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<DescriptorImpl> desc) : DescriptorImpl({}, std::move(desc), "wsh") {}
std::optional<OutputType> GetOutputType() const override { return OutputType::BECH32; }
Expand Down

0 comments on commit fb1ba32

Please sign in to comment.