Skip to content

Commit b59b450

Browse files
committed
have GenerateNewKey and DeriveNewChildKey take a CHDChain as an argument
1 parent 844d207 commit b59b450

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

src/wallet/scriptpubkeyman.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ bool LegacyScriptPubKeyMan::SetupGeneration(bool force)
357357

358358
bool LegacyScriptPubKeyMan::IsHDEnabled() const
359359
{
360-
return !hdChain.seed_id.IsNull();
360+
return !m_hd_chain.seed_id.IsNull();
361361
}
362362

363363
bool LegacyScriptPubKeyMan::CanGetAddresses(bool internal) const
@@ -842,7 +842,7 @@ void LegacyScriptPubKeyMan::SetHDChain(const CHDChain& chain, bool memonly)
842842
if (!memonly && !WalletBatch(m_storage.GetDatabase()).WriteHDChain(chain))
843843
throw std::runtime_error(std::string(__func__) + ": writing chain failed");
844844

845-
hdChain = chain;
845+
m_hd_chain = chain;
846846
}
847847

848848
bool LegacyScriptPubKeyMan::HaveKey(const CKeyID &address) const
@@ -921,7 +921,7 @@ bool LegacyScriptPubKeyMan::GetPubKey(const CKeyID &address, CPubKey& vchPubKeyO
921921
return GetWatchPubKey(address, vchPubKeyOut);
922922
}
923923

924-
CPubKey LegacyScriptPubKeyMan::GenerateNewKey(WalletBatch &batch, bool internal)
924+
CPubKey LegacyScriptPubKeyMan::GenerateNewKey(WalletBatch &batch, CHDChain& hd_chain, bool internal)
925925
{
926926
assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS));
927927
assert(!m_storage.IsWalletFlagSet(WALLET_FLAG_BLANK_WALLET));
@@ -936,7 +936,7 @@ CPubKey LegacyScriptPubKeyMan::GenerateNewKey(WalletBatch &batch, bool internal)
936936

937937
// use HD key derivation if HD was enabled during wallet creation and a seed is present
938938
if (IsHDEnabled()) {
939-
DeriveNewChildKey(batch, metadata, secret, (m_storage.CanSupportFeature(FEATURE_HD_SPLIT) ? internal : false));
939+
DeriveNewChildKey(batch, metadata, secret, hd_chain, (m_storage.CanSupportFeature(FEATURE_HD_SPLIT) ? internal : false));
940940
} else {
941941
secret.MakeNewKey(fCompressed);
942942
}
@@ -960,7 +960,7 @@ CPubKey LegacyScriptPubKeyMan::GenerateNewKey(WalletBatch &batch, bool internal)
960960

961961
const uint32_t BIP32_HARDENED_KEY_LIMIT = 0x80000000;
962962

963-
void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, bool internal)
963+
void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata& metadata, CKey& secret, CHDChain& hd_chain, bool internal)
964964
{
965965
// for now we use a fixed keypath scheme of m/0'/0'/k
966966
CKey seed; //seed (256bit)
@@ -970,7 +970,7 @@ void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata&
970970
CExtKey childKey; //key at m/0'/0'/<n>'
971971

972972
// try to get the seed
973-
if (!GetKey(hdChain.seed_id, seed))
973+
if (!GetKey(hd_chain.seed_id, seed))
974974
throw std::runtime_error(std::string(__func__) + ": seed not found");
975975

976976
masterKey.SetSeed(seed.begin(), seed.size());
@@ -989,29 +989,29 @@ void LegacyScriptPubKeyMan::DeriveNewChildKey(WalletBatch &batch, CKeyMetadata&
989989
// childIndex | BIP32_HARDENED_KEY_LIMIT = derive childIndex in hardened child-index-range
990990
// example: 1 | BIP32_HARDENED_KEY_LIMIT == 0x80000001 == 2147483649
991991
if (internal) {
992-
chainChildKey.Derive(childKey, hdChain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
993-
metadata.hdKeypath = "m/0'/1'/" + ToString(hdChain.nInternalChainCounter) + "'";
992+
chainChildKey.Derive(childKey, hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
993+
metadata.hdKeypath = "m/0'/1'/" + ToString(hd_chain.nInternalChainCounter) + "'";
994994
metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
995995
metadata.key_origin.path.push_back(1 | BIP32_HARDENED_KEY_LIMIT);
996-
metadata.key_origin.path.push_back(hdChain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
997-
hdChain.nInternalChainCounter++;
996+
metadata.key_origin.path.push_back(hd_chain.nInternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
997+
hd_chain.nInternalChainCounter++;
998998
}
999999
else {
1000-
chainChildKey.Derive(childKey, hdChain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1001-
metadata.hdKeypath = "m/0'/0'/" + ToString(hdChain.nExternalChainCounter) + "'";
1000+
chainChildKey.Derive(childKey, hd_chain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1001+
metadata.hdKeypath = "m/0'/0'/" + ToString(hd_chain.nExternalChainCounter) + "'";
10021002
metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
10031003
metadata.key_origin.path.push_back(0 | BIP32_HARDENED_KEY_LIMIT);
1004-
metadata.key_origin.path.push_back(hdChain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1005-
hdChain.nExternalChainCounter++;
1004+
metadata.key_origin.path.push_back(hd_chain.nExternalChainCounter | BIP32_HARDENED_KEY_LIMIT);
1005+
hd_chain.nExternalChainCounter++;
10061006
}
10071007
} while (HaveKey(childKey.key.GetPubKey().GetID()));
10081008
secret = childKey.key;
1009-
metadata.hd_seed_id = hdChain.seed_id;
1009+
metadata.hd_seed_id = hd_chain.seed_id;
10101010
CKeyID master_id = masterKey.key.GetPubKey().GetID();
10111011
std::copy(master_id.begin(), master_id.begin() + 4, metadata.key_origin.fingerprint);
10121012
metadata.has_key_origin = true;
10131013
// update the chain model in the database
1014-
if (!batch.WriteHDChain(hdChain))
1014+
if (!batch.WriteHDChain(hd_chain))
10151015
throw std::runtime_error(std::string(__func__) + ": Writing HD chain model failed");
10161016
}
10171017

@@ -1167,7 +1167,7 @@ bool LegacyScriptPubKeyMan::TopUp(unsigned int kpSize)
11671167
internal = true;
11681168
}
11691169

1170-
CPubKey pubkey(GenerateNewKey(batch, internal));
1170+
CPubKey pubkey(GenerateNewKey(batch, m_hd_chain, internal));
11711171
AddKeypoolPubkeyWithDB(pubkey, internal, batch);
11721172
}
11731173
if (missingInternal + missingExternal > 0) {
@@ -1240,7 +1240,7 @@ bool LegacyScriptPubKeyMan::GetKeyFromPool(CPubKey& result, const OutputType typ
12401240
if (!ReserveKeyFromKeyPool(nIndex, keypool, internal) && !m_storage.IsWalletFlagSet(WALLET_FLAG_DISABLE_PRIVATE_KEYS)) {
12411241
if (m_storage.IsLocked()) return false;
12421242
WalletBatch batch(m_storage.GetDatabase());
1243-
result = GenerateNewKey(batch, internal);
1243+
result = GenerateNewKey(batch, m_hd_chain, internal);
12441244
return true;
12451245
}
12461246
KeepDestination(nIndex, type);

src/wallet/scriptpubkeyman.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,10 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
287287
bool AddKeyOriginWithDB(WalletBatch& batch, const CPubKey& pubkey, const KeyOriginInfo& info);
288288

289289
/* the HD chain data model (external chain counters) */
290-
CHDChain hdChain;
290+
CHDChain m_hd_chain;
291291

292292
/* HD derive new child key (on internal or external chain) */
293-
void DeriveNewChildKey(WalletBatch& batch, CKeyMetadata& metadata, CKey& secret, bool internal = false) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
293+
void DeriveNewChildKey(WalletBatch& batch, CKeyMetadata& metadata, CKey& secret, CHDChain& hd_chain, bool internal = false) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
294294

295295
std::set<int64_t> setInternalKeyPool GUARDED_BY(cs_KeyStore);
296296
std::set<int64_t> setExternalKeyPool GUARDED_BY(cs_KeyStore);
@@ -392,11 +392,11 @@ class LegacyScriptPubKeyMan : public ScriptPubKeyMan, public FillableSigningProv
392392
void LoadKeyMetadata(const CKeyID& keyID, const CKeyMetadata &metadata);
393393
void LoadScriptMetadata(const CScriptID& script_id, const CKeyMetadata &metadata);
394394
//! Generate a new key
395-
CPubKey GenerateNewKey(WalletBatch& batch, bool internal = false) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
395+
CPubKey GenerateNewKey(WalletBatch& batch, CHDChain& hd_chain, bool internal = false) EXCLUSIVE_LOCKS_REQUIRED(cs_KeyStore);
396396

397397
/* Set the HD chain model (chain child index counters) */
398398
void SetHDChain(const CHDChain& chain, bool memonly);
399-
const CHDChain& GetHDChain() const { return hdChain; }
399+
const CHDChain& GetHDChain() const { return m_hd_chain; }
400400

401401
//! Adds a watch-only address to the store, without saving it to disk (used by LoadWallet)
402402
bool LoadWatchOnly(const CScript &dest);

0 commit comments

Comments
 (0)