Skip to content

Commit

Permalink
descriptors: Add DescriptorImpl::Clone
Browse files Browse the repository at this point in the history
  • Loading branch information
achow101 committed Aug 7, 2024
1 parent 7e86541 commit 0d55dea
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions src/script/descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,8 @@ class DescriptorImpl : public Descriptor
arg->GetPubKeys(pubkeys, ext_pubs);
}
}

virtual std::unique_ptr<DescriptorImpl> Clone() const = 0;
};

/** A parsed addr(A) descriptor. */
Expand All @@ -807,6 +809,10 @@ class AddressDescriptor final : public DescriptorImpl
bool ToPrivateString(const SigningProvider& arg, std::string& out) const final { return false; }

std::optional<int64_t> ScriptSize() const override { return GetScriptForDestination(m_destination).size(); }
std::unique_ptr<DescriptorImpl> Clone() const override
{
return std::make_unique<AddressDescriptor>(m_destination);
}
};

/** A parsed raw(H) descriptor. */
Expand All @@ -830,6 +836,11 @@ class RawDescriptor final : public DescriptorImpl
bool ToPrivateString(const SigningProvider& arg, std::string& out) const final { return false; }

std::optional<int64_t> ScriptSize() const override { return m_script.size(); }

std::unique_ptr<DescriptorImpl> Clone() const override
{
return std::make_unique<RawDescriptor>(m_script);
}
};

/** A parsed pk(P) descriptor. */
Expand Down Expand Up @@ -865,6 +876,11 @@ class PKDescriptor final : public DescriptorImpl
}

std::optional<int64_t> MaxSatisfactionElems() const override { return 1; }

std::unique_ptr<DescriptorImpl> Clone() const override
{
return std::make_unique<PKDescriptor>(m_pubkey_args.at(0)->Clone(), m_xonly);
}
};

/** A parsed pkh(P) descriptor. */
Expand Down Expand Up @@ -894,6 +910,11 @@ class PKHDescriptor final : public DescriptorImpl
}

std::optional<int64_t> MaxSatisfactionElems() const override { return 2; }

std::unique_ptr<DescriptorImpl> Clone() const override
{
return std::make_unique<PKHDescriptor>(m_pubkey_args.at(0)->Clone());
}
};

/** A parsed wpkh(P) descriptor. */
Expand Down Expand Up @@ -923,6 +944,11 @@ class WPKHDescriptor final : public DescriptorImpl
}

std::optional<int64_t> MaxSatisfactionElems() const override { return 2; }

std::unique_ptr<DescriptorImpl> Clone() const override
{
return std::make_unique<WPKHDescriptor>(m_pubkey_args.at(0)->Clone());
}
};

/** A parsed combo(P) descriptor. */
Expand All @@ -947,6 +973,10 @@ class ComboDescriptor final : public DescriptorImpl
public:
ComboDescriptor(std::unique_ptr<PubkeyProvider> prov) : DescriptorImpl(Vector(std::move(prov)), "combo") {}
bool IsSingleType() const final { return false; }
std::unique_ptr<DescriptorImpl> Clone() const override
{
return std::make_unique<ComboDescriptor>(m_pubkey_args.at(0)->Clone());
}
};

/** A parsed multi(...) or sortedmulti(...) descriptor */
Expand Down Expand Up @@ -985,6 +1015,14 @@ class MultisigDescriptor final : public DescriptorImpl
}

std::optional<int64_t> MaxSatisfactionElems() const override { return 1 + m_threshold; }

std::unique_ptr<DescriptorImpl> Clone() const override
{
std::vector<std::unique_ptr<PubkeyProvider>> providers;
providers.reserve(m_pubkey_args.size());
std::transform(m_pubkey_args.begin(), m_pubkey_args.end(), providers.begin(), [](const std::unique_ptr<PubkeyProvider>& p) { return p->Clone(); });
return std::make_unique<MultisigDescriptor>(m_threshold, std::move(providers), m_sorted);
}
};

/** A parsed (sorted)multi_a(...) descriptor. Always uses x-only pubkeys. */
Expand Down Expand Up @@ -1021,6 +1059,16 @@ class MultiADescriptor final : public DescriptorImpl
}

std::optional<int64_t> MaxSatisfactionElems() const override { return m_pubkey_args.size(); }

std::unique_ptr<DescriptorImpl> Clone() const override
{
std::vector<std::unique_ptr<PubkeyProvider>> providers;
providers.reserve(m_pubkey_args.size());
for (const auto& arg : m_pubkey_args) {
providers.push_back(arg->Clone());
}
return std::make_unique<MultiADescriptor>(m_threshold, std::move(providers), m_sorted);
}
};

/** A parsed sh(...) descriptor. */
Expand Down Expand Up @@ -1066,6 +1114,11 @@ class SHDescriptor final : public DescriptorImpl
if (const auto sub_elems = m_subdescriptor_args[0]->MaxSatisfactionElems()) return 1 + *sub_elems;
return {};
}

std::unique_ptr<DescriptorImpl> Clone() const override
{
return std::make_unique<SHDescriptor>(m_subdescriptor_args.at(0)->Clone());
}
};

/** A parsed wsh(...) descriptor. */
Expand Down Expand Up @@ -1102,6 +1155,11 @@ class WSHDescriptor final : public DescriptorImpl
if (const auto sub_elems = m_subdescriptor_args[0]->MaxSatisfactionElems()) return 1 + *sub_elems;
return {};
}

std::unique_ptr<DescriptorImpl> Clone() const override
{
return std::make_unique<WSHDescriptor>(m_subdescriptor_args.at(0)->Clone());
}
};

/** A parsed tr(...) descriptor. */
Expand Down Expand Up @@ -1167,6 +1225,14 @@ class TRDescriptor final : public DescriptorImpl
// FIXME: See above, we assume keypath spend.
return 1;
}

std::unique_ptr<DescriptorImpl> Clone() const override
{
std::vector<std::unique_ptr<DescriptorImpl>> subdescs;
subdescs.reserve(m_subdescriptor_args.size());
std::transform(m_subdescriptor_args.begin(), m_subdescriptor_args.end(), subdescs.begin(), [](const std::unique_ptr<DescriptorImpl>& d) { return d->Clone(); });
return std::make_unique<TRDescriptor>(m_pubkey_args.at(0)->Clone(), std::move(subdescs), m_depths);
}
};

/* We instantiate Miniscript here with a simple integer as key type.
Expand Down Expand Up @@ -1285,6 +1351,16 @@ class MiniscriptDescriptor final : public DescriptorImpl
std::optional<int64_t> MaxSatisfactionElems() const override {
return m_node->GetStackSize();
}

std::unique_ptr<DescriptorImpl> Clone() const override
{
std::vector<std::unique_ptr<PubkeyProvider>> providers;
providers.reserve(m_pubkey_args.size());
for (const auto& arg : m_pubkey_args) {
providers.push_back(arg->Clone());
}
return std::make_unique<MiniscriptDescriptor>(std::move(providers), miniscript::MakeNodeRef<uint32_t>(*m_node));
}
};

/** A parsed rawtr(...) descriptor. */
Expand Down Expand Up @@ -1315,6 +1391,11 @@ class RawTRDescriptor final : public DescriptorImpl
// See above, we assume keypath spend.
return 1;
}

std::unique_ptr<DescriptorImpl> Clone() const override
{
return std::make_unique<RawTRDescriptor>(m_pubkey_args.at(0)->Clone());
}
};

////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 0d55dea

Please sign in to comment.