Skip to content

Commit 9473e96

Browse files
committed
descriptors: Move DeriveType parsing into its own function
1 parent 4af0dca commit 9473e96

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/script/descriptor.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,20 @@ std::optional<uint32_t> ParseKeyPathNum(std::span<const char> elem, bool& apostr
17121712
return true;
17131713
}
17141714

1715+
static DeriveType ParseDeriveType(std::vector<std::span<const char>>& split, bool& apostrophe)
1716+
{
1717+
DeriveType type = DeriveType::NO;
1718+
if (std::ranges::equal(split.back(), std::span{"*"}.first(1))) {
1719+
split.pop_back();
1720+
type = DeriveType::UNHARDENED;
1721+
} else if (std::ranges::equal(split.back(), std::span{"*'"}.first(2)) || std::ranges::equal(split.back(), std::span{"*h"}.first(2))) {
1722+
apostrophe = std::ranges::equal(split.back(), std::span{"*'"}.first(2));
1723+
split.pop_back();
1724+
type = DeriveType::HARDENED;
1725+
}
1726+
return type;
1727+
}
1728+
17151729
/** Parse a public key that excludes origin information. */
17161730
std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkeyInner(uint32_t key_exp_index, const std::span<const char>& sp, ParseScriptContext ctx, FlatSigningProvider& out, bool& apostrophe, std::string& error)
17171731
{
@@ -1775,15 +1789,7 @@ std::vector<std::unique_ptr<PubkeyProvider>> ParsePubkeyInner(uint32_t key_exp_i
17751789
return {};
17761790
}
17771791
std::vector<KeyPath> paths;
1778-
DeriveType type = DeriveType::NO;
1779-
if (std::ranges::equal(split.back(), std::span{"*"}.first(1))) {
1780-
split.pop_back();
1781-
type = DeriveType::UNHARDENED;
1782-
} else if (std::ranges::equal(split.back(), std::span{"*'"}.first(2)) || std::ranges::equal(split.back(), std::span{"*h"}.first(2))) {
1783-
apostrophe = std::ranges::equal(split.back(), std::span{"*'"}.first(2));
1784-
split.pop_back();
1785-
type = DeriveType::HARDENED;
1786-
}
1792+
DeriveType type = ParseDeriveType(split, apostrophe);
17871793
if (!ParseKeyPath(split, paths, apostrophe, error, /*allow_multipath=*/true)) return {};
17881794
if (extkey.key.IsValid()) {
17891795
extpubkey = extkey.Neuter();

0 commit comments

Comments
 (0)