Skip to content

Commit 1eda33a

Browse files
committed
[refactor] Combine the ToString and ToPrivateString implementations
1 parent 24d3a7b commit 1eda33a

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

src/script/descriptor.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -251,21 +251,25 @@ class DescriptorImpl : public Descriptor
251251
return false;
252252
}
253253

254-
bool ToPrivateString(const SigningProvider& arg, std::string& out) const final
254+
bool ToStringHelper(const SigningProvider* arg, std::string& out, bool priv) const
255255
{
256256
std::string extra = ToStringExtra();
257257
size_t pos = extra.size() > 0 ? 1 : 0;
258258
std::string ret = m_name + "(" + extra;
259259
for (const auto& pubkey : m_pubkey_args) {
260260
if (pos++) ret += ",";
261261
std::string tmp;
262-
if (!pubkey->ToPrivateString(arg, tmp)) return false;
262+
if (priv) {
263+
if (!pubkey->ToPrivateString(*arg, tmp)) return false;
264+
} else {
265+
tmp = pubkey->ToString();
266+
}
263267
ret += std::move(tmp);
264268
}
265269
if (m_script_arg) {
266270
if (pos++) ret += ",";
267271
std::string tmp;
268-
if (!m_script_arg->ToPrivateString(arg, tmp)) return false;
272+
if (!m_script_arg->ToStringHelper(arg, tmp, priv)) return false;
269273
ret += std::move(tmp);
270274
}
271275
out = std::move(ret) + ")";
@@ -274,20 +278,13 @@ class DescriptorImpl : public Descriptor
274278

275279
std::string ToString() const final
276280
{
277-
std::string extra = ToStringExtra();
278-
size_t pos = extra.size() > 0 ? 1 : 0;
279-
std::string ret = m_name + "(" + extra;
280-
for (const auto& pubkey : m_pubkey_args) {
281-
if (pos++) ret += ",";
282-
ret += pubkey->ToString();
283-
}
284-
if (m_script_arg) {
285-
if (pos++) ret += ",";
286-
ret += m_script_arg->ToString();
287-
}
288-
return std::move(ret) + ")";
281+
std::string ret;
282+
ToStringHelper(nullptr, ret, false);
283+
return ret;
289284
}
290285

286+
bool ToPrivateString(const SigningProvider& arg, std::string& out) const override final { return ToStringHelper(&arg, out, true); }
287+
291288
bool Expand(int pos, const SigningProvider& arg, std::vector<CScript>& output_scripts, FlatSigningProvider& out) const final
292289
{
293290
std::vector<std::pair<CPubKey, KeyOriginInfo>> entries;

0 commit comments

Comments
 (0)