Skip to content

Commit 3b01efa

Browse files
committed
[MOVEONLY] Move ParseHDKeypath to utilstrencodings
1 parent 81e1dd5 commit 3b01efa

File tree

4 files changed

+43
-43
lines changed

4 files changed

+43
-43
lines changed

src/utilstrencodings.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,3 +544,43 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out)
544544
return true;
545545
}
546546

547+
bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath)
548+
{
549+
std::stringstream ss(keypath_str);
550+
std::string item;
551+
bool first = true;
552+
while (std::getline(ss, item, '/')) {
553+
if (item.compare("m") == 0) {
554+
if (first) {
555+
first = false;
556+
continue;
557+
}
558+
return false;
559+
}
560+
// Finds whether it is hardened
561+
uint32_t path = 0;
562+
size_t pos = item.find("'");
563+
if (pos != std::string::npos) {
564+
// The hardened tick can only be in the last index of the string
565+
if (pos != item.size() - 1) {
566+
return false;
567+
}
568+
path |= 0x80000000;
569+
item = item.substr(0, item.size() - 1); // Drop the last character which is the hardened tick
570+
}
571+
572+
// Ensure this is only numbers
573+
if (item.find_first_not_of( "0123456789" ) != std::string::npos) {
574+
return false;
575+
}
576+
uint32_t number;
577+
if (!ParseUInt32(item, &number)) {
578+
return false;
579+
}
580+
path |= number;
581+
582+
keypath.push_back(path);
583+
first = false;
584+
}
585+
return true;
586+
}

src/utilstrencodings.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,7 @@ bool ConvertBits(const O& outfn, I it, I end) {
183183
return true;
184184
}
185185

186+
/** Parse an HD keypaths like "m/7/0'/2000". */
187+
bool ParseHDKeypath(const std::string& keypath_str, std::vector<uint32_t>& keypath);
188+
186189
#endif // BITCOIN_UTILSTRENCODINGS_H

src/wallet/rpcwallet.cpp

Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4423,47 +4423,6 @@ UniValue sethdseed(const JSONRPCRequest& request)
44234423
return NullUniValue;
44244424
}
44254425

4426-
bool ParseHDKeypath(std::string keypath_str, std::vector<uint32_t>& keypath)
4427-
{
4428-
std::stringstream ss(keypath_str);
4429-
std::string item;
4430-
bool first = true;
4431-
while (std::getline(ss, item, '/')) {
4432-
if (item.compare("m") == 0) {
4433-
if (first) {
4434-
first = false;
4435-
continue;
4436-
}
4437-
return false;
4438-
}
4439-
// Finds whether it is hardened
4440-
uint32_t path = 0;
4441-
size_t pos = item.find("'");
4442-
if (pos != std::string::npos) {
4443-
// The hardened tick can only be in the last index of the string
4444-
if (pos != item.size() - 1) {
4445-
return false;
4446-
}
4447-
path |= 0x80000000;
4448-
item = item.substr(0, item.size() - 1); // Drop the last character which is the hardened tick
4449-
}
4450-
4451-
// Ensure this is only numbers
4452-
if (item.find_first_not_of( "0123456789" ) != std::string::npos) {
4453-
return false;
4454-
}
4455-
uint32_t number;
4456-
if (!ParseUInt32(item, &number)) {
4457-
return false;
4458-
}
4459-
path |= number;
4460-
4461-
keypath.push_back(path);
4462-
first = false;
4463-
}
4464-
return true;
4465-
}
4466-
44674426
void AddKeypathToMap(const CWallet* pwallet, const CKeyID& keyID, std::map<CPubKey, KeyOriginInfo>& hd_keypaths)
44684427
{
44694428
CPubKey vchPubKey;

src/wallet/test/psbt_wallet_tests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#include <test/test_bitcoin.h>
1414
#include <wallet/test/wallet_test_fixture.h>
1515

16-
extern bool ParseHDKeypath(std::string keypath_str, std::vector<uint32_t>& keypath);
17-
1816
BOOST_FIXTURE_TEST_SUITE(psbt_wallet_tests, WalletTestingSetup)
1917

2018
BOOST_AUTO_TEST_CASE(psbt_updater_test)

0 commit comments

Comments
 (0)