Persist network id string instead of bip44 coin type.

This commit is contained in:
Kris Nuttycombe 2021-09-21 18:24:39 -06:00
parent 68c3bd8eaa
commit 7132b27723
4 changed files with 25 additions and 18 deletions

View File

@ -130,14 +130,14 @@ SaplingPaymentAddress CWallet::GenerateNewSaplingZKey()
// Derive m/32' // Derive m/32'
auto m_32h = m.Derive(32 | ZIP32_HARDENED_KEY_LIMIT); auto m_32h = m.Derive(32 | ZIP32_HARDENED_KEY_LIMIT);
// Derive m/32'/coin_type' // 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 // Derive account key at next index, skip keys already known to the wallet
libzcash::SaplingExtendedSpendingKey xsk; libzcash::SaplingExtendedSpendingKey xsk;
do do
{ {
xsk = m_32h_cth.Derive(hdChain.saplingAccountCounter | ZIP32_HARDENED_KEY_LIMIT); 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; metadata.seedFp = hdChain.seedFp;
// Increment childkey index // Increment childkey index
hdChain.saplingAccountCounter++; hdChain.saplingAccountCounter++;
@ -2250,7 +2250,7 @@ bool CWallet::SetHDSeed(const HDSeed& seed)
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
CWalletDB(strWalletFile).WriteBIP44CoinType(bip44CoinType); CWalletDB(strWalletFile).WriteNetworkInfo(networkIdString);
if (!IsCrypted()) { if (!IsCrypted()) {
return CWalletDB(strWalletFile).WriteHDSeed(seed); return CWalletDB(strWalletFile).WriteHDSeed(seed);
} }
@ -2295,18 +2295,23 @@ void CWallet::SetHDChain(const CHDChain& chain, bool memonly)
hdChain = chain; hdChain = chain;
} }
void CWallet::CheckBIP44CoinType(uint32_t coinType) void CWallet::CheckNetworkInfo(std::pair<std::string, std::string> readNetworkInfo)
{ {
LOCK(cs_wallet); LOCK(cs_wallet);
if (bip44CoinType != coinType) std::pair<string, string> networkInfo(PACKAGE_NAME, networkIdString);
if (readNetworkInfo != networkInfo)
throw std::runtime_error( 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__), std::string(__func__),
coinType, readNetworkInfo.first, readNetworkInfo.second,
bip44CoinType) networkInfo.first, networkInfo.second)
); );
} }
uint32_t CWallet::BIP44CoinType() {
return Params(networkIdString).BIP44CoinType();
}
bool CWallet::LoadHDSeed(const HDSeed& seed) bool CWallet::LoadHDSeed(const HDSeed& seed)
{ {

View File

@ -806,7 +806,7 @@ protected:
/* the hd chain data model (chain counters) */ /* the hd chain data model (chain counters) */
CHDChain hdChain; CHDChain hdChain;
uint32_t bip44CoinType; std::string networkIdString;
public: public:
/* /*
@ -864,7 +864,7 @@ public:
nTimeFirstKey = 0; nTimeFirstKey = 0;
fBroadcastTransactions = false; fBroadcastTransactions = false;
nWitnessCacheSize = 0; nWitnessCacheSize = 0;
bip44CoinType = params.BIP44CoinType(); networkIdString = params.NetworkIDString();
} }
/** /**
@ -1298,7 +1298,8 @@ public:
void SetHDChain(const CHDChain& chain, bool memonly); void SetHDChain(const CHDChain& chain, bool memonly);
const CHDChain& GetHDChain() const { return hdChain; } const CHDChain& GetHDChain() const { return hdChain; }
void CheckBIP44CoinType(uint32_t coinType); void CheckNetworkInfo(std::pair<std::string, std::string> networkInfo);
uint32_t BIP44CoinType();
/* Set the current HD seed, without saving it to disk (used by LoadWallet) */ /* Set the current HD seed, without saving it to disk (used by LoadWallet) */
bool LoadHDSeed(const HDSeed& key); bool LoadHDSeed(const HDSeed& key);

View File

@ -724,11 +724,11 @@ ReadKeyValue(CWallet* pwallet, CDataStream& ssKey, CDataStream& ssValue,
ssValue >> chain; ssValue >> chain;
pwallet->SetHDChain(chain, true); pwallet->SetHDChain(chain, true);
} }
else if (strType == "bip44cointype") else if (strType == "networkinfo")
{ {
uint32_t bip44CoinType; std::pair<std::string, std::string> networkInfo;
ssValue >> bip44CoinType; ssValue >> networkInfo;
pwallet->CheckBIP44CoinType(bip44CoinType); pwallet->CheckNetworkInfo(networkInfo);
} }
} catch (...) } 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))); 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++; nWalletDBUpdateCounter++;
return Write(std::string("bip44cointype"), bip44CoinType); std::pair<std::string, std::string> networkInfo(PACKAGE_NAME, networkId);
return Write(std::string("networkinfo"), networkInfo);
} }
bool CWalletDB::WriteHDSeed(const HDSeed& seed) bool CWalletDB::WriteHDSeed(const HDSeed& seed)

View File

@ -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 fOnlyKeys);
static bool Recover(CDBEnv& dbenv, const std::string& filename); 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 WriteHDSeed(const HDSeed& seed);
bool WriteCryptedHDSeed(const uint256& seedFp, const std::vector<unsigned char>& vchCryptedSecret); bool WriteCryptedHDSeed(const uint256& seedFp, const std::vector<unsigned char>& vchCryptedSecret);
//! write the hdchain model (external chain child index counter) //! write the hdchain model (external chain child index counter)