From 960a966b254eee4a3a1ada8610c040e87cde00e1 Mon Sep 17 00:00:00 2001 From: riordant Date: Mon, 22 Jul 2019 14:47:08 +0700 Subject: [PATCH 1/4] Fix issue with parsing legacy hd keypath --- src/wallet/walletdb.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 2836b958f3..652220cc54 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -26,6 +26,7 @@ #include "libzerocoin/Zerocoin.h" #include +#include #include #include @@ -124,9 +125,11 @@ class CKeyMetadata std::vector nComponents; if(hdKeypath=="m") return false; - boost::split(nComponents, hdKeypath, boost::is_any_of("/"), boost::token_compress_on); - nChange = boost::lexical_cast(nComponents[4]); - nChild = boost::lexical_cast(nComponents[5]); + std::string hdKeypathNonHardened = hdKeypath; + boost::erase_all(hdKeypathNonHardened, "'"); + boost::split(nComponents, hdKeypathNonHardened, boost::is_any_of("/"), boost::token_compress_on); + nChange = boost::lexical_cast(nComponents[nComponents.size()-2]); + nChild = boost::lexical_cast(nComponents[nComponents.size()-1]); return true; } From c03cfa0b7ae9a9a7b9fd37f6ca20a484614160d3 Mon Sep 17 00:00:00 2001 From: riordant Date: Thu, 25 Jul 2019 18:00:33 +0700 Subject: [PATCH 2/4] Add hardened value in CKeyMetadata --- src/wallet/rpcdump.cpp | 4 ++-- src/wallet/wallet.cpp | 4 ++-- src/wallet/walletdb.h | 28 +++++++++++++++++++--------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index f7dfb0b2a7..d24b920da7 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -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; } } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 61f4f1fb43..f8be1fa3b6 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -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 regTest = GetOptBoolArg("-regtest") , testNet = GetOptBoolArg("-testnet"); @@ -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())); diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 652220cc54..d6fbd9becf 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -58,6 +58,9 @@ enum DBErrors DB_NEED_REWRITE }; +// {value, isHardened} +typedef pair Component; + /* simple HD chain data model */ class CHDChain { @@ -107,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() @@ -125,11 +128,18 @@ class CKeyMetadata std::vector nComponents; if(hdKeypath=="m") return false; - std::string hdKeypathNonHardened = hdKeypath; - boost::erase_all(hdKeypathNonHardened, "'"); - boost::split(nComponents, hdKeypathNonHardened, boost::is_any_of("/"), boost::token_compress_on); - nChange = boost::lexical_cast(nComponents[nComponents.size()-2]); - nChild = boost::lexical_cast(nComponents[nComponents.size()-1]); + boost::split(nComponents, hdKeypath, boost::is_any_of("/"), boost::token_compress_on); + 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(nChangeStr); + + nChild.second = (nChildStr.find("'") != string::npos); + boost::erase_all(nChildStr, "'"); + nChild.first = boost::lexical_cast(nChildStr); + return true; } @@ -152,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(); } }; From b835086fb99a6b2cff5befdf9a12810fbe034e9f Mon Sep 17 00:00:00 2001 From: riordant Date: Thu, 25 Jul 2019 22:48:40 +0700 Subject: [PATCH 3/4] Update type in component --- src/wallet/walletdb.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index d6fbd9becf..bbde6a9d0b 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -59,7 +59,7 @@ enum DBErrors }; // {value, isHardened} -typedef pair Component; +typedef pair Component; /* simple HD chain data model */ class CHDChain From 2d24daa5c54402539da997a964e6192d01dd2911 Mon Sep 17 00:00:00 2001 From: riordant Date: Fri, 26 Jul 2019 12:54:24 +0700 Subject: [PATCH 4/4] update cast type --- src/wallet/walletdb.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index bbde6a9d0b..7b7ff1b8a7 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -134,11 +134,11 @@ class CKeyMetadata nChange.second = (nChangeStr.find("'") != string::npos); boost::erase_all(nChangeStr, "'"); - nChange.first = boost::lexical_cast(nChangeStr); + nChange.first = boost::lexical_cast(nChangeStr); nChild.second = (nChildStr.find("'") != string::npos); boost::erase_all(nChildStr, "'"); - nChild.first = boost::lexical_cast(nChildStr); + nChild.first = boost::lexical_cast(nChildStr); return true; }