Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with parsing legacy HD keypath #536

Merged
merged 4 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/wallet/rpcdump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,8 @@ UniValue importwallet(const UniValue& params, bool fHelp)

if(fHd){
// If change component in HD path is 2, this is a mint seed key. Add to mintpool. (Have to call after key addition)
if(pwalletMain->mapKeyMetadata[keyid].nChange==2){
zwalletMain->RegenerateMintPoolEntry(hdMasterKeyID, keyid, pwalletMain->mapKeyMetadata[keyid].nChild);
if(pwalletMain->mapKeyMetadata[keyid].nChange.first==2){
zwalletMain->RegenerateMintPoolEntry(hdMasterKeyID, keyid, pwalletMain->mapKeyMetadata[keyid].nChild.first);
fMintUpdate = true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ CPubKey CWallet::GenerateNewKey(uint32_t nChange) {
// Create new metadata
int64_t nCreationTime = GetTime();
CKeyMetadata metadata(nCreationTime);
metadata.nChange = nChange;
metadata.nChange = Component(nChange, false);

boost::optional<bool> regTest = GetOptBoolArg("-regtest")
, testNet = GetOptBoolArg("-testnet");
Expand Down Expand Up @@ -187,7 +187,7 @@ CPubKey CWallet::GenerateNewKey(uint32_t nChange) {
externalChainChildKey.Derive(childKey, hdChain.nExternalChainCounters[nChange]);
metadata.hdKeypath = "m/44'/" + std::to_string(nIndex) + "'/0'/" + std::to_string(nChange) + "/" + std::to_string(hdChain.nExternalChainCounters[nChange]);
metadata.hdMasterKeyID = hdChain.masterKeyID;
metadata.nChild = hdChain.nExternalChainCounters[nChange];
metadata.nChild = Component(hdChain.nExternalChainCounters[nChange], false);
// increment childkey index
hdChain.nExternalChainCounters[nChange]++;
} while (HaveKey(childKey.key.GetPubKey().GetID()));
Expand Down
25 changes: 19 additions & 6 deletions src/wallet/walletdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "libzerocoin/Zerocoin.h"

#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/erase.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/lexical_cast.hpp>

Expand Down Expand Up @@ -57,6 +58,9 @@ enum DBErrors
DB_NEED_REWRITE
};

// {value, isHardened}
typedef pair<uint32_t,bool> Component;

/* simple HD chain data model */
class CHDChain
{
Expand Down Expand Up @@ -106,8 +110,8 @@ class CKeyMetadata
int nVersion;
int64_t nCreateTime; // 0 means unknown
std::string hdKeypath; //optional HD/bip32 keypath
int64_t nChange; // HD/bip32 keypath change counter
int64_t nChild; // HD/bip32 keypath child counter
Component nChange; // HD/bip32 keypath change counter
Component nChild; // HD/bip32 keypath child counter
CKeyID hdMasterKeyID; //id of the HD masterkey used to derive this key

CKeyMetadata()
Expand All @@ -125,8 +129,17 @@ class CKeyMetadata
if(hdKeypath=="m")
return false;
boost::split(nComponents, hdKeypath, boost::is_any_of("/"), boost::token_compress_on);
nChange = boost::lexical_cast<int64_t>(nComponents[4]);
nChild = boost::lexical_cast<int64_t>(nComponents[5]);
std::string nChangeStr = nComponents[nComponents.size()-2];
std::string nChildStr = nComponents[nComponents.size()-1];

nChange.second = (nChangeStr.find("'") != string::npos);
boost::erase_all(nChangeStr, "'");
nChange.first = boost::lexical_cast<uint32_t>(nChangeStr);

nChild.second = (nChildStr.find("'") != string::npos);
boost::erase_all(nChildStr, "'");
nChild.first = boost::lexical_cast<uint32_t>(nChildStr);

return true;
}

Expand All @@ -149,8 +162,8 @@ class CKeyMetadata
nVersion = CKeyMetadata::CURRENT_VERSION;
nCreateTime = 0;
hdKeypath.clear();
nChild = 0;
nChange = 0;
nChild = Component(0, false);
nChange = Component(0, false);
hdMasterKeyID.SetNull();
}
};
Expand Down