Skip to content

Commit 7a26ff1

Browse files
committed
Change DescriptorImpl::ToStringHelper to use an enum
Instead of having multiple, possibly conflicting, bools controlling the flow of ToStringHelper, use an enum.
1 parent 75530c9 commit 7a26ff1

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/script/descriptor.cpp

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,13 @@ class DescriptorImpl : public Descriptor
507507
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))) {}
508508
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)) {}
509509

510+
enum class StringType
511+
{
512+
PUBLIC,
513+
PRIVATE,
514+
NORMALIZED,
515+
};
516+
510517
bool IsSolvable() const override
511518
{
512519
for (const auto& arg : m_subdescriptor_args) {
@@ -526,37 +533,41 @@ class DescriptorImpl : public Descriptor
526533
return false;
527534
}
528535

529-
virtual bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, bool priv, bool normalized) const
536+
virtual bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, const StringType type) const
530537
{
531538
size_t pos = 0;
532539
for (const auto& scriptarg : m_subdescriptor_args) {
533540
if (pos++) ret += ",";
534541
std::string tmp;
535-
if (!scriptarg->ToStringHelper(arg, tmp, priv, normalized)) return false;
542+
if (!scriptarg->ToStringHelper(arg, tmp, type)) return false;
536543
ret += std::move(tmp);
537544
}
538545
return true;
539546
}
540547

541-
bool ToStringHelper(const SigningProvider* arg, std::string& out, bool priv, bool normalized) const
548+
bool ToStringHelper(const SigningProvider* arg, std::string& out, const StringType type) const
542549
{
543550
std::string extra = ToStringExtra();
544551
size_t pos = extra.size() > 0 ? 1 : 0;
545552
std::string ret = m_name + "(" + extra;
546553
for (const auto& pubkey : m_pubkey_args) {
547554
if (pos++) ret += ",";
548555
std::string tmp;
549-
if (normalized) {
550-
if (!pubkey->ToNormalizedString(*arg, tmp)) return false;
551-
} else if (priv) {
552-
if (!pubkey->ToPrivateString(*arg, tmp)) return false;
553-
} else {
554-
tmp = pubkey->ToString();
556+
switch (type) {
557+
case StringType::NORMALIZED:
558+
if (!pubkey->ToNormalizedString(*arg, tmp)) return false;
559+
break;
560+
case StringType::PRIVATE:
561+
if (!pubkey->ToPrivateString(*arg, tmp)) return false;
562+
break;
563+
case StringType::PUBLIC:
564+
tmp = pubkey->ToString();
565+
break;
555566
}
556567
ret += std::move(tmp);
557568
}
558569
std::string subscript;
559-
if (!ToStringSubScriptHelper(arg, subscript, priv, normalized)) return false;
570+
if (!ToStringSubScriptHelper(arg, subscript, type)) return false;
560571
if (pos && subscript.size()) ret += ',';
561572
out = std::move(ret) + std::move(subscript) + ")";
562573
return true;
@@ -565,20 +576,20 @@ class DescriptorImpl : public Descriptor
565576
std::string ToString() const final
566577
{
567578
std::string ret;
568-
ToStringHelper(nullptr, ret, false, false);
579+
ToStringHelper(nullptr, ret, StringType::PUBLIC);
569580
return AddChecksum(ret);
570581
}
571582

572583
bool ToPrivateString(const SigningProvider& arg, std::string& out) const final
573584
{
574-
bool ret = ToStringHelper(&arg, out, true, false);
585+
bool ret = ToStringHelper(&arg, out, StringType::PRIVATE);
575586
out = AddChecksum(out);
576587
return ret;
577588
}
578589

579590
bool ToNormalizedString(const SigningProvider& arg, std::string& out) const override final
580591
{
581-
bool ret = ToStringHelper(&arg, out, false, true);
592+
bool ret = ToStringHelper(&arg, out, StringType::NORMALIZED);
582593
out = AddChecksum(out);
583594
return ret;
584595
}
@@ -832,7 +843,7 @@ class TRDescriptor final : public DescriptorImpl
832843
out.tr_spenddata[output].Merge(builder.GetSpendData());
833844
return Vector(GetScriptForDestination(output));
834845
}
835-
bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, bool priv, bool normalized) const override
846+
bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, const StringType type) const override
836847
{
837848
if (m_depths.empty()) return true;
838849
std::vector<bool> path;
@@ -843,7 +854,7 @@ class TRDescriptor final : public DescriptorImpl
843854
path.push_back(false);
844855
}
845856
std::string tmp;
846-
if (!m_subdescriptor_args[pos]->ToStringHelper(arg, tmp, priv, normalized)) return false;
857+
if (!m_subdescriptor_args[pos]->ToStringHelper(arg, tmp, type)) return false;
847858
ret += std::move(tmp);
848859
while (!path.empty() && path.back()) {
849860
if (path.size() > 1) ret += '}';

0 commit comments

Comments
 (0)