descriptor: refactor ToPrivateString for providers
This commit modifies the Pubkey providers to return the public string if private data is not available. This is setup for a future commit to make Descriptor::ToPrivateString return strings with missing private key information. Co-authored-by: rkrux <rkrux.connect@gmail.com>
This commit is contained in:
1 parent
2dc74e3f4e
commit
5c4db25b61
1 file changed
+16
-9
@@ -204,7 +204,11 @@ public:
|
|||||||
/** Get the descriptor string form. */
|
/** Get the descriptor string form. */
|
||||||
virtual std::string ToString(StringType type=StringType::PUBLIC) const = 0;
|
virtual std::string ToString(StringType type=StringType::PUBLIC) const = 0;
|
||||||
|
|
||||||
/** Get the descriptor string form including private data (if available in arg). */
|
/** Get the descriptor string form including private data (if available in arg).
|
||||||
|
* If the private data is not available, the output string in the "out" parameter
|
||||||
|
* will not contain any private key information,
|
||||||
|
* and this function will return "false".
|
||||||
|
*/
|
||||||
virtual bool ToPrivateString(const SigningProvider& arg, std::string& out) const = 0;
|
virtual bool ToPrivateString(const SigningProvider& arg, std::string& out) const = 0;
|
||||||
|
|
||||||
/** Get the descriptor string form with the xpub at the last hardened derivation,
|
/** Get the descriptor string form with the xpub at the last hardened derivation,
|
||||||
@@ -260,9 +264,9 @@ public:
|
|||||||
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
|
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
|
||||||
{
|
{
|
||||||
std::string sub;
|
std::string sub;
|
||||||
if (!m_provider->ToPrivateString(arg, sub)) return false;
|
bool has_priv_key{m_provider->ToPrivateString(arg, sub)};
|
||||||
ret = "[" + OriginString(StringType::PUBLIC) + "]" + std::move(sub);
|
ret = "[" + OriginString(StringType::PUBLIC) + "]" + std::move(sub);
|
||||||
return true;
|
return has_priv_key;
|
||||||
}
|
}
|
||||||
bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override
|
bool ToNormalizedString(const SigningProvider& arg, std::string& ret, const DescriptorCache* cache) const override
|
||||||
{
|
{
|
||||||
@@ -329,7 +333,10 @@ public:
|
|||||||
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
|
bool ToPrivateString(const SigningProvider& arg, std::string& ret) const override
|
||||||
{
|
{
|
||||||
std::optional<CKey> key = GetPrivKey(arg);
|
std::optional<CKey> key = GetPrivKey(arg);
|
||||||
if (!key) return false;
|
if (!key) {
|
||||||
|
ret = ToString(StringType::PUBLIC);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
ret = EncodeSecret(*key);
|
ret = EncodeSecret(*key);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -492,7 +499,10 @@ public:
|
|||||||
bool ToPrivateString(const SigningProvider& arg, std::string& out) const override
|
bool ToPrivateString(const SigningProvider& arg, std::string& out) const override
|
||||||
{
|
{
|
||||||
CExtKey key;
|
CExtKey key;
|
||||||
if (!GetExtKey(arg, key)) return false;
|
if (!GetExtKey(arg, key)) {
|
||||||
|
out = ToString(StringType::PUBLIC);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
out = EncodeExtKey(key) + FormatHDKeypath(m_path, /*apostrophe=*/m_apostrophe);
|
out = EncodeExtKey(key) + FormatHDKeypath(m_path, /*apostrophe=*/m_apostrophe);
|
||||||
if (IsRange()) {
|
if (IsRange()) {
|
||||||
out += "/*";
|
out += "/*";
|
||||||
@@ -710,17 +720,14 @@ public:
|
|||||||
std::string tmp;
|
std::string tmp;
|
||||||
if (pubkey->ToPrivateString(arg, tmp)) {
|
if (pubkey->ToPrivateString(arg, tmp)) {
|
||||||
any_privkeys = true;
|
any_privkeys = true;
|
||||||
out += tmp;
|
|
||||||
} else {
|
|
||||||
out += pubkey->ToString();
|
|
||||||
}
|
}
|
||||||
|
out += tmp;
|
||||||
}
|
}
|
||||||
out += ")";
|
out += ")";
|
||||||
out += FormatHDKeypath(m_path);
|
out += FormatHDKeypath(m_path);
|
||||||
if (IsRangedDerivation()) {
|
if (IsRangedDerivation()) {
|
||||||
out += "/*";
|
out += "/*";
|
||||||
}
|
}
|
||||||
if (!any_privkeys) out.clear();
|
|
||||||
return any_privkeys;
|
return any_privkeys;
|
||||||
}
|
}
|
||||||
bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr) const override
|
bool ToNormalizedString(const SigningProvider& arg, std::string& out, const DescriptorCache* cache = nullptr) const override
|
||||||
|
|||||||
Reference in new issue
Block a user