0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

Change DescriptorImpl::ToStringHelper to use an enum

Instead of having multiple, possibly conflicting, bools controlling the
flow of ToStringHelper, use an enum.
This commit is contained in:
Andrew Chow 2021-04-22 15:03:31 -04:00
parent 75530c93a8
commit 7a26ff10c2

View file

@ -507,6 +507,13 @@ public:
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))) {} 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))) {}
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)) {} 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)) {}
enum class StringType
{
PUBLIC,
PRIVATE,
NORMALIZED,
};
bool IsSolvable() const override bool IsSolvable() const override
{ {
for (const auto& arg : m_subdescriptor_args) { for (const auto& arg : m_subdescriptor_args) {
@ -526,19 +533,19 @@ public:
return false; return false;
} }
virtual bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, bool priv, bool normalized) const virtual bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, const StringType type) const
{ {
size_t pos = 0; size_t pos = 0;
for (const auto& scriptarg : m_subdescriptor_args) { for (const auto& scriptarg : m_subdescriptor_args) {
if (pos++) ret += ","; if (pos++) ret += ",";
std::string tmp; std::string tmp;
if (!scriptarg->ToStringHelper(arg, tmp, priv, normalized)) return false; if (!scriptarg->ToStringHelper(arg, tmp, type)) return false;
ret += std::move(tmp); ret += std::move(tmp);
} }
return true; return true;
} }
bool ToStringHelper(const SigningProvider* arg, std::string& out, bool priv, bool normalized) const bool ToStringHelper(const SigningProvider* arg, std::string& out, const StringType type) const
{ {
std::string extra = ToStringExtra(); std::string extra = ToStringExtra();
size_t pos = extra.size() > 0 ? 1 : 0; size_t pos = extra.size() > 0 ? 1 : 0;
@ -546,17 +553,21 @@ public:
for (const auto& pubkey : m_pubkey_args) { for (const auto& pubkey : m_pubkey_args) {
if (pos++) ret += ","; if (pos++) ret += ",";
std::string tmp; std::string tmp;
if (normalized) { switch (type) {
case StringType::NORMALIZED:
if (!pubkey->ToNormalizedString(*arg, tmp)) return false; if (!pubkey->ToNormalizedString(*arg, tmp)) return false;
} else if (priv) { break;
case StringType::PRIVATE:
if (!pubkey->ToPrivateString(*arg, tmp)) return false; if (!pubkey->ToPrivateString(*arg, tmp)) return false;
} else { break;
case StringType::PUBLIC:
tmp = pubkey->ToString(); tmp = pubkey->ToString();
break;
} }
ret += std::move(tmp); ret += std::move(tmp);
} }
std::string subscript; std::string subscript;
if (!ToStringSubScriptHelper(arg, subscript, priv, normalized)) return false; if (!ToStringSubScriptHelper(arg, subscript, type)) return false;
if (pos && subscript.size()) ret += ','; if (pos && subscript.size()) ret += ',';
out = std::move(ret) + std::move(subscript) + ")"; out = std::move(ret) + std::move(subscript) + ")";
return true; return true;
@ -565,20 +576,20 @@ public:
std::string ToString() const final std::string ToString() const final
{ {
std::string ret; std::string ret;
ToStringHelper(nullptr, ret, false, false); ToStringHelper(nullptr, ret, StringType::PUBLIC);
return AddChecksum(ret); return AddChecksum(ret);
} }
bool ToPrivateString(const SigningProvider& arg, std::string& out) const final bool ToPrivateString(const SigningProvider& arg, std::string& out) const final
{ {
bool ret = ToStringHelper(&arg, out, true, false); bool ret = ToStringHelper(&arg, out, StringType::PRIVATE);
out = AddChecksum(out); out = AddChecksum(out);
return ret; return ret;
} }
bool ToNormalizedString(const SigningProvider& arg, std::string& out) const override final bool ToNormalizedString(const SigningProvider& arg, std::string& out) const override final
{ {
bool ret = ToStringHelper(&arg, out, false, true); bool ret = ToStringHelper(&arg, out, StringType::NORMALIZED);
out = AddChecksum(out); out = AddChecksum(out);
return ret; return ret;
} }
@ -832,7 +843,7 @@ protected:
out.tr_spenddata[output].Merge(builder.GetSpendData()); out.tr_spenddata[output].Merge(builder.GetSpendData());
return Vector(GetScriptForDestination(output)); return Vector(GetScriptForDestination(output));
} }
bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, bool priv, bool normalized) const override bool ToStringSubScriptHelper(const SigningProvider* arg, std::string& ret, const StringType type) const override
{ {
if (m_depths.empty()) return true; if (m_depths.empty()) return true;
std::vector<bool> path; std::vector<bool> path;
@ -843,7 +854,7 @@ protected:
path.push_back(false); path.push_back(false);
} }
std::string tmp; std::string tmp;
if (!m_subdescriptor_args[pos]->ToStringHelper(arg, tmp, priv, normalized)) return false; if (!m_subdescriptor_args[pos]->ToStringHelper(arg, tmp, type)) return false;
ret += std::move(tmp); ret += std::move(tmp);
while (!path.empty() && path.back()) { while (!path.empty() && path.back()) {
if (path.size() > 1) ret += '}'; if (path.size() > 1) ret += '}';