diff --git a/src/util/strencodings.h b/src/util/strencodings.h --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -205,6 +205,10 @@ return true; } +/** Parse an HD keypaths like "m/7/0'/2000". */ +bool ParseHDKeypath(const std::string &keypath_str, + std::vector &keypath); + /** * Converts the given character to its lowercase equivalent. * This function is locale independent. It only converts uppercase diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -801,6 +801,46 @@ return true; } +bool ParseHDKeypath(const std::string &keypath_str, + std::vector &keypath) { + std::stringstream ss(keypath_str); + std::string item; + bool first = true; + while (std::getline(ss, item, '/')) { + if (item.compare("m") == 0) { + if (first) { + first = false; + continue; + } + return false; + } + // Finds whether it is hardened + uint32_t path = 0; + size_t pos = item.find("'"); + if (pos != std::string::npos) { + // The hardened tick can only be in the last index of the string + if (pos != item.size() - 1) { + return false; + } + path |= 0x80000000; + // Drop the last character which is the hardened tick + item = item.substr(0, item.size() - 1); + } + + // Ensure this is only numbers + if (item.find_first_not_of("0123456789") != std::string::npos) { + return false; + } + uint32_t number; + ParseUInt32(item, &number); + path |= number; + + keypath.push_back(path); + first = false; + } + return true; +} + void Downcase(std::string &str) { std::transform(str.begin(), str.end(), str.begin(), [](uint8_t c) { return ToLower(c); }); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5084,46 +5084,6 @@ return NullUniValue; } -static bool ParseHDKeypath(std::string keypath_str, - std::vector &keypath) { - std::stringstream ss(keypath_str); - std::string item; - bool first = true; - while (std::getline(ss, item, '/')) { - if (item.compare("m") == 0) { - if (first) { - first = false; - continue; - } - return false; - } - // Finds whether it is hardened - uint32_t path = 0; - size_t pos = item.find("'"); - if (pos != std::string::npos) { - // The hardened tick can only be in the last index of the string - if (pos != item.size() - 1) { - return false; - } - path |= 0x80000000; - // Drop the last character which is the hardened tick - item = item.substr(0, item.size() - 1); - } - - // Ensure this is only numbers - if (item.find_first_not_of("0123456789") != std::string::npos) { - return false; - } - uint32_t number; - ParseUInt32(item, &number); - path |= number; - - keypath.push_back(path); - first = false; - } - return true; -} - static void AddKeypathToMap(const CWallet *pwallet, const CKeyID &keyID, std::map &hd_keypaths) { CPubKey vchPubKey; diff --git a/src/wallet/test/psbt_wallet_tests.cpp b/src/wallet/test/psbt_wallet_tests.cpp --- a/src/wallet/test/psbt_wallet_tests.cpp +++ b/src/wallet/test/psbt_wallet_tests.cpp @@ -5,7 +5,6 @@ #include #include