wallet: fix metadata updates on HD derivation (#4819)

* wallet: Use temporary structure to update metadata correctly while generating new hd keys

* wallet: Make sure to never update an already existing key_origin while deriving hd keys
This commit is contained in:
UdjinM6 2022-04-30 23:33:45 +03:00 committed by pasta
parent 1d2c9da931
commit 070ef357c0
2 changed files with 9 additions and 1 deletions

View File

@ -187,6 +187,9 @@ void CHDChain::DeriveChildExtKey(uint32_t nAccountIndex, bool fInternal, uint32_
changeKey.Derive(extKeyRet, nChildIndex);
#ifdef ENABLE_WALLET
// We should never ever update an already existing key_origin here
assert(!metadata.has_key_origin);
assert(metadata.key_origin.path.empty());
metadata.key_origin.path.push_back(44 | 0x80000000);
metadata.key_origin.path.push_back(Params().ExtCoinType() | 0x80000000);
metadata.key_origin.path.push_back(nAccountIndex | 0x80000000);

View File

@ -365,12 +365,17 @@ void CWallet::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey
// derive child key at next index, skip keys already known to the wallet
CExtKey childKey;
CKeyMetadata metadataTmp;
uint32_t nChildIndex = fInternal ? acc.nInternalChainCounter : acc.nExternalChainCounter;
do {
hdChainTmp.DeriveChildExtKey(nAccountIndex, fInternal, nChildIndex, childKey, metadata);
// NOTE: DeriveChildExtKey updates metadata, use temporary structure to make sure
// we start with the original (non-updated) data each time.
metadataTmp = metadata;
hdChainTmp.DeriveChildExtKey(nAccountIndex, fInternal, nChildIndex, childKey, metadataTmp);
// increment childkey index
nChildIndex++;
} while (HaveKey(childKey.key.GetPubKey().GetID()));
metadata = metadataTmp;
secretRet = childKey.key;
CPubKey pubkey = secretRet.GetPubKey();