From 7132b27723602c202254f3d82f96e082365b3007 Mon Sep 17 00:00:00 2001 From: Kris Nuttycombe Date: Tue, 21 Sep 2021 18:24:39 -0600 Subject: [PATCH] Persist network id string instead of bip44 coin type. --- src/wallet/wallet.cpp | 21 +++++++++++++-------- src/wallet/wallet.h | 7 ++++--- src/wallet/walletdb.cpp | 13 +++++++------ src/wallet/walletdb.h | 2 +- 4 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 2e98c2f61..7d26262c4 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -130,14 +130,14 @@ SaplingPaymentAddress CWallet::GenerateNewSaplingZKey() // Derive m/32' auto m_32h = m.Derive(32 | ZIP32_HARDENED_KEY_LIMIT); // Derive m/32'/coin_type' - auto m_32h_cth = m_32h.Derive(bip44CoinType | ZIP32_HARDENED_KEY_LIMIT); + auto m_32h_cth = m_32h.Derive(BIP44CoinType() | ZIP32_HARDENED_KEY_LIMIT); // Derive account key at next index, skip keys already known to the wallet libzcash::SaplingExtendedSpendingKey xsk; do { xsk = m_32h_cth.Derive(hdChain.saplingAccountCounter | ZIP32_HARDENED_KEY_LIMIT); - metadata.hdKeypath = "m/32'/" + std::to_string(bip44CoinType) + "'/" + std::to_string(hdChain.saplingAccountCounter) + "'"; + metadata.hdKeypath = "m/32'/" + std::to_string(BIP44CoinType()) + "'/" + std::to_string(hdChain.saplingAccountCounter) + "'"; metadata.seedFp = hdChain.seedFp; // Increment childkey index hdChain.saplingAccountCounter++; @@ -2250,7 +2250,7 @@ bool CWallet::SetHDSeed(const HDSeed& seed) { LOCK(cs_wallet); - CWalletDB(strWalletFile).WriteBIP44CoinType(bip44CoinType); + CWalletDB(strWalletFile).WriteNetworkInfo(networkIdString); if (!IsCrypted()) { return CWalletDB(strWalletFile).WriteHDSeed(seed); } @@ -2295,18 +2295,23 @@ void CWallet::SetHDChain(const CHDChain& chain, bool memonly) hdChain = chain; } -void CWallet::CheckBIP44CoinType(uint32_t coinType) +void CWallet::CheckNetworkInfo(std::pair readNetworkInfo) { LOCK(cs_wallet); - if (bip44CoinType != coinType) + std::pair networkInfo(PACKAGE_NAME, networkIdString); + if (readNetworkInfo != networkInfo) throw std::runtime_error( - strprintf("%s: this wallet is for a different BIP 44 coin type (%i) than the node is configured for (%i)", + strprintf("%s: this wallet is for a different network (%s, %s) than the node is configured for (%s, %s)", std::string(__func__), - coinType, - bip44CoinType) + readNetworkInfo.first, readNetworkInfo.second, + networkInfo.first, networkInfo.second) ); } +uint32_t CWallet::BIP44CoinType() { + return Params(networkIdString).BIP44CoinType(); +} + bool CWallet::LoadHDSeed(const HDSeed& seed) { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 3641970ec..1bd9f1682 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -806,7 +806,7 @@ protected: /* the hd chain data model (chain counters) */ CHDChain hdChain; - uint32_t bip44CoinType; + std::string networkIdString; public: /* @@ -864,7 +864,7 @@ public: nTimeFirstKey = 0; fBroadcastTransactions = false; nWitnessCacheSize = 0; - bip44CoinType = params.BIP44CoinType(); + networkIdString = params.NetworkIDString(); } /** @@ -1298,7 +1298,8 @@ public: void SetHDChain(const CHDChain& chain, bool memonly); const CHDChain& GetHDChain() const { return hdChain; } - void CheckBIP44CoinType(uint32_t coinType); + void CheckNetworkInfo(std::pair networkInfo); + uint32_t BIP44CoinType(); /* Set the current HD seed, without saving it to disk (used by LoadWallet) */ bool LoadHDSeed(const HDSeed& key); diff --git a/src/wallet/walletdb.cpp b/src/wallet/walletdb.cpp index f07eae4c1..7ac13f32a 100644 --- a/src/wallet/walletdb.cpp +++ b/src/wallet/walletdb.cpp @@ -724,11 +724,11 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue, ssValue >> chain; pwallet->SetHDChain(chain, true); } - else if (strType == "bip44cointype") + else if (strType == "networkinfo") { - uint32_t bip44CoinType; - ssValue >> bip44CoinType; - pwallet->CheckBIP44CoinType(bip44CoinType); + std::pair networkInfo; + ssValue >> networkInfo; + pwallet->CheckNetworkInfo(networkInfo); } } catch (...) { @@ -1148,10 +1148,11 @@ bool CWalletDB::EraseDestData(const std::string &address, const std::string &key return Erase(std::make_pair(std::string("destdata"), std::make_pair(address, key))); } -bool CWalletDB::WriteBIP44CoinType(uint32_t bip44CoinType) +bool CWalletDB::WriteNetworkInfo(const std::string& networkId) { nWalletDBUpdateCounter++; - return Write(std::string("bip44cointype"), bip44CoinType); + std::pair networkInfo(PACKAGE_NAME, networkId); + return Write(std::string("networkinfo"), networkInfo); } bool CWalletDB::WriteHDSeed(const HDSeed& seed) diff --git a/src/wallet/walletdb.h b/src/wallet/walletdb.h index 825297cbd..5d0a2d0de 100644 --- a/src/wallet/walletdb.h +++ b/src/wallet/walletdb.h @@ -168,7 +168,7 @@ public: static bool Recover(CDBEnv& dbenv, const std::string& filename, bool fOnlyKeys); static bool Recover(CDBEnv& dbenv, const std::string& filename); - bool WriteBIP44CoinType(uint32_t bip44CoinType); + bool WriteNetworkInfo(const std::string& networkId); bool WriteHDSeed(const HDSeed& seed); bool WriteCryptedHDSeed(const uint256& seedFp, const std::vector& vchCryptedSecret); //! write the hdchain model (external chain child index counter)