Minor cleanup of the code that searches for a valid transparent key.

This commit is contained in:
Kris Nuttycombe 2021-11-04 21:36:40 -05:00
parent de57da38ce
commit f65d6f63ee
1 changed files with 21 additions and 22 deletions

View File

@ -281,35 +281,34 @@ CPubKey CWallet::GenerateNewKey()
BIP44CoinType(),
ZCASH_LEGACY_ACCOUNT).value();
while (true) {
auto extKey = accountChains.DeriveExternal(hdChain.GetLegacyTKeyCounter());
std::optional<std::pair<CExtKey, HDKeyPath>> extKey = std::nullopt;
do {
extKey = accountChains.DeriveExternal(hdChain.GetLegacyTKeyCounter());
hdChain.IncrementLegacyTKeyCounter();
// if we did not successfully generate a key, try again.
if (extKey.has_value()) {
CKey secret = extKey.value().first.key;
CPubKey pubkey = secret.GetPubKey();
assert(secret.VerifyPubKey(pubkey));
} while (!extKey.has_value());
// Create new metadata
CKeyMetadata keyMeta(GetTime());
keyMeta.hdKeypath = extKey.value().second;
keyMeta.seedFp = seed.Fingerprint();
mapKeyMetadata[pubkey.GetID()] = keyMeta;
if (nTimeFirstKey == 0 || keyMeta.nCreateTime < nTimeFirstKey)
nTimeFirstKey = keyMeta.nCreateTime;
CKey secret = extKey.value().first.key;
CPubKey pubkey = secret.GetPubKey();
assert(secret.VerifyPubKey(pubkey));
if (!AddKeyPubKey(secret, pubkey))
throw std::runtime_error("CWallet::GenerateNewKey(): AddKey failed");
// Create new metadata
CKeyMetadata keyMeta(GetTime());
keyMeta.hdKeypath = extKey.value().second;
keyMeta.seedFp = seed.Fingerprint();
mapKeyMetadata[pubkey.GetID()] = keyMeta;
if (nTimeFirstKey == 0 || keyMeta.nCreateTime < nTimeFirstKey)
nTimeFirstKey = keyMeta.nCreateTime;
// Update the persisted chain information
if (fFileBacked && !CWalletDB(strWalletFile).WriteMnemonicHDChain(hdChain)) {
throw std::runtime_error("CWallet::GenerateNewKey(): Writing HD chain model failed");
}
if (!AddKeyPubKey(secret, pubkey))
throw std::runtime_error("CWallet::GenerateNewKey(): AddKey failed");
return pubkey;
}
// Update the persisted chain information
if (fFileBacked && !CWalletDB(strWalletFile).WriteMnemonicHDChain(hdChain)) {
throw std::runtime_error("CWallet::GenerateNewKey(): Writing HD chain model failed");
}
return pubkey;
}
bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)