@@ -114,6 +114,13 @@ const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
114
114
return &(it->second );
115
115
}
116
116
117
+ void CWallet::GenerateEdandStealthKey (CKey& keyIn)
118
+ {
119
+ CKeyEd25519 secretEdRet;
120
+ DeriveEd25519ChildKey (keyIn,secretEdRet); // Derive Ed25519 key
121
+ DeriveChildStealthKey (keyIn); // Derive stealth key
122
+ }
123
+
117
124
CPubKey CWallet::GenerateNewKey (uint32_t nAccountIndex, bool fInternal )
118
125
{
119
126
AssertLockHeld (cs_wallet); // mapKeyMetadata
@@ -4570,69 +4577,6 @@ bool CWallet::SyncEdKeyPool()
4570
4577
4571
4578
} // SyncEdKeyPool
4572
4579
4573
-
4574
-
4575
-
4576
- bool CWallet::TopUpKeyPool (unsigned int kpSize)
4577
- {
4578
- {
4579
- LOCK (cs_wallet);
4580
-
4581
- if (IsLocked (true ))
4582
- return false ;
4583
-
4584
- // Top up key pool
4585
- unsigned int nTargetSize;
4586
- if (kpSize > 0 )
4587
- nTargetSize = kpSize;
4588
- else
4589
- nTargetSize = std::max (GetArg (" -keypool" , DEFAULT_KEYPOOL_SIZE), (int64_t )0 );
4590
-
4591
- // count amount of available keys (internal, external)
4592
- // make sure the keypool of external and internal keys fits the user selected target (-keypool)
4593
- int64_t amountExternal = setExternalKeyPool.size ();
4594
- int64_t amountInternal = setInternalKeyPool.size ();
4595
- int64_t missingExternal = std::max (std::max ((int64_t )nTargetSize, (int64_t )1 ) - amountExternal, (int64_t )0 );
4596
- int64_t missingInternal = std::max (std::max ((int64_t )nTargetSize, (int64_t )1 ) - amountInternal, (int64_t )0 );
4597
-
4598
- if (!IsHDEnabled ()) {
4599
- // don't create extra internal keys
4600
- missingInternal = 0 ;
4601
- } else {
4602
- nTargetSize *= 2 ;
4603
- }
4604
- bool fInternal = false ;
4605
- CWalletDB walletdb (strWalletFile);
4606
- for (int64_t i = missingInternal + missingExternal; i--;) {
4607
- int64_t nEnd = 1 ;
4608
- if (i < missingInternal) {
4609
- fInternal = true ;
4610
- }
4611
- if (!setInternalKeyPool.empty ()) {
4612
- nEnd = *(--setInternalKeyPool.end ()) + 1 ;
4613
- }
4614
- if (!setExternalKeyPool.empty ()) {
4615
- nEnd = std::max (nEnd, *(--setExternalKeyPool.end ()) + 1 );
4616
- }
4617
- // TODO: implement keypools for all accounts?
4618
- if (!walletdb.WritePool (nEnd, CKeyPool (GenerateNewKey (0 , fInternal ), fInternal )))
4619
- throw std::runtime_error (" TopUpKeyPool(): writing generated key failed" );
4620
-
4621
- if (fInternal ) {
4622
- setInternalKeyPool.insert (nEnd);
4623
- } else {
4624
- setExternalKeyPool.insert (nEnd);
4625
- }
4626
- LogPrintf (" keypool added key %d, size=%u, internal=%d\n " , nEnd, setInternalKeyPool.size () + setExternalKeyPool.size (), fInternal );
4627
-
4628
- double dProgress = 100 .f * nEnd / (nTargetSize + 1 );
4629
- std::string strMsg = strprintf (_ (" Loading wallet... (%3.2f %%)" ), dProgress);
4630
- uiInterface.InitMessage (strMsg);
4631
- }
4632
- }
4633
- return true ;
4634
- }
4635
-
4636
4580
bool CWallet::TopUpKeyPoolCombo (unsigned int kpSize)
4637
4581
{
4638
4582
{
@@ -4696,7 +4640,7 @@ bool CWallet::TopUpKeyPoolCombo(unsigned int kpSize)
4696
4640
4697
4641
// TODO: implement keypools for all accounts?
4698
4642
if (!walletdb.WriteEdPool (nEnd, CEdKeyPool (GenerateNewEdKey (0 , fInternal , retrievedKey), fInternal )))
4699
- throw std::runtime_error (" TopUpEdKeyPool (): writing generated key failed" );
4643
+ throw std::runtime_error (" TopUpKeyPoolCombo (): writing generated key failed" );
4700
4644
4701
4645
if (fInternal ) {
4702
4646
setInternalEdKeyPool.insert (nEnd);
@@ -4713,63 +4657,6 @@ bool CWallet::TopUpKeyPoolCombo(unsigned int kpSize)
4713
4657
return true ;
4714
4658
} // TopUpKeyPoolCombo
4715
4659
4716
- bool CWallet::TopUpEdKeyPool (unsigned int kpSize)
4717
- {
4718
- {
4719
- LOCK (cs_wallet);
4720
-
4721
- if (IsLocked (true ))
4722
- return false ;
4723
-
4724
- // Top up key pool
4725
- unsigned int nTargetSize;
4726
- if (kpSize > 0 )
4727
- nTargetSize = kpSize;
4728
- else
4729
- nTargetSize = std::max (GetArg (" -keypool" , DEFAULT_KEYPOOL_SIZE), (int64_t )0 );
4730
-
4731
- // count amount of available keys (internal, external)
4732
- // make sure the keypool of external and internal keys fits the user selected target (-keypool)
4733
- int64_t amountExternal = setExternalEdKeyPool.size ();
4734
- int64_t amountInternal = setInternalEdKeyPool.size ();
4735
- int64_t missingExternal = std::max (std::max ((int64_t )nTargetSize, (int64_t )1 ) - amountExternal, (int64_t )0 );
4736
- int64_t missingInternal = std::max (std::max ((int64_t )nTargetSize, (int64_t )1 ) - amountInternal, (int64_t )0 );
4737
-
4738
- if (!IsHDEnabled ()) {
4739
- // don't create extra internal keys
4740
- missingInternal = 0 ;
4741
- } else {
4742
- nTargetSize *= 2 ;
4743
- }
4744
- bool fInternal = false ;
4745
- CWalletDB walletdb (strWalletFile);
4746
- for (int64_t i = missingInternal + missingExternal; i--;) {
4747
- int64_t nEnd = 1 ;
4748
- if (i < missingInternal) {
4749
- fInternal = true ;
4750
- }
4751
- if (!setInternalEdKeyPool.empty ()) {
4752
- nEnd = *(--setInternalEdKeyPool.end ()) + 1 ;
4753
- }
4754
- if (!setExternalEdKeyPool.empty ()) {
4755
- nEnd = std::max (nEnd, *(--setExternalEdKeyPool.end ()) + 1 );
4756
- }
4757
- // TODO: implement keypools for all accounts?
4758
- if (!walletdb.WriteEdPool (nEnd, CEdKeyPool (GenerateNewEdKey (0 , fInternal ), fInternal )))
4759
- throw std::runtime_error (" TopUpEdKeyPool(): writing generated key failed" );
4760
-
4761
- if (fInternal ) {
4762
- setInternalEdKeyPool.insert (nEnd);
4763
- } else {
4764
- setExternalEdKeyPool.insert (nEnd);
4765
- }
4766
- LogPrintf (" edkeypool added key %d, size=%u, internal=%d\n " , nEnd, setInternalEdKeyPool.size () + setExternalEdKeyPool.size (), fInternal );
4767
-
4768
- }
4769
- }
4770
- return true ;
4771
- } // TopUpEdKeyPool
4772
-
4773
4660
void CWallet::UpdateKeyPoolsFromTransactions (const std::string& strOpType, const std::vector<std::vector<unsigned char >>& vvchOpParameters)
4774
4661
{
4775
4662
std::vector<unsigned char > key0 = vvchOpParameters[0 ];
@@ -4785,9 +4672,11 @@ void CWallet::UpdateKeyPoolsFromTransactions(const std::string& strOpType, const
4785
4672
4786
4673
} // UpdateKeyPoolsFromTransactions
4787
4674
4788
- void CWallet::ReserveKeyFromKeyPool (int64_t & nIndex, CKeyPool& keypool, bool fInternal )
4675
+
4676
+ void CWallet::ReserveKeysFromKeyPools (int64_t & nIndex, CKeyPool& keypool, CEdKeyPool& edkeypool, bool fInternal )
4789
4677
{
4790
4678
nIndex = -1 ;
4679
+ int64_t nEdIndex = nIndex;
4791
4680
keypool.vchPubKey = CPubKey ();
4792
4681
{
4793
4682
LOCK (cs_wallet);
@@ -4797,6 +4686,7 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fIn
4797
4686
4798
4687
fInternal = fInternal && IsHDEnabled ();
4799
4688
std::set<int64_t >& setKeyPool = fInternal ? setInternalKeyPool : setExternalKeyPool;
4689
+ std::set<int64_t >& setEdKeyPool = fInternal ? setInternalEdKeyPool : setExternalEdKeyPool;
4800
4690
4801
4691
// Get the oldest key
4802
4692
if (setKeyPool.empty ())
@@ -4818,39 +4708,20 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool fIn
4818
4708
4819
4709
assert (keypool.vchPubKey .IsValid ());
4820
4710
LogPrintf (" keypool reserve %d\n " , nIndex);
4821
- }
4822
- }
4823
-
4824
- void CWallet::ReserveEdKeyFromKeyPool (int64_t & nIndex, CEdKeyPool& edkeypool, bool fInternal )
4825
- {
4826
- nIndex = -1 ;
4827
- {
4828
- LOCK (cs_wallet);
4829
-
4830
- if (!IsLocked (true ))
4831
- TopUpKeyPoolCombo (); // TopUpEdKeyPool();
4832
-
4833
- fInternal = fInternal && IsHDEnabled ();
4834
- std::set<int64_t >& setEdKeyPool = fInternal ? setInternalEdKeyPool : setExternalEdKeyPool;
4835
4711
4836
- // Get the oldest key
4837
- if (setEdKeyPool.empty ())
4838
- return ;
4839
-
4840
- CWalletDB walletdb (strWalletFile);
4841
-
4842
- nIndex = *setEdKeyPool.begin ();
4843
- setEdKeyPool.erase (nIndex);
4844
- if (!walletdb.ReadEdPool (nIndex, edkeypool)) {
4712
+ nEdIndex = *setEdKeyPool.begin ();
4713
+ setEdKeyPool.erase (nEdIndex);
4714
+ if (!walletdb.ReadEdPool (nEdIndex, edkeypool)) {
4845
4715
throw std::runtime_error (std::string (__func__) + " : read failed" );
4846
4716
}
4847
4717
if (edkeypool.fInternal != fInternal ) {
4848
4718
throw std::runtime_error (std::string (__func__) + " : keypool entry misclassified" );
4849
4719
}
4850
4720
4851
- LogPrintf (" edkeypool reserve %d\n " , nIndex);
4721
+ LogPrintf (" edkeypool reserve %d\n " , nEdIndex);
4722
+
4852
4723
}
4853
- } // ReserveEdKeyFromKeyPool
4724
+ } // ReserveKeysFromKeyPools
4854
4725
4855
4726
void CWallet::ReserveEdKeyForTransactions (const std::vector<unsigned char >& pubKeyToReserve)
4856
4727
{
@@ -4872,7 +4743,6 @@ void CWallet::ReserveEdKeyForTransactions(const std::vector<unsigned char>& pubK
4872
4743
4873
4744
if (pubKeyToReserve == edPubKey) {
4874
4745
KeepKey (nIndex);
4875
- KeepEdKey (nIndex);
4876
4746
fNeedToUpdateKeyPools = true ;
4877
4747
EraseIndex = true ;
4878
4748
IndexToErase = nIndex;
@@ -4897,18 +4767,10 @@ void CWallet::KeepKey(int64_t nIndex)
4897
4767
if (fFileBacked ) {
4898
4768
CWalletDB walletdb (strWalletFile);
4899
4769
walletdb.ErasePool (nIndex);
4770
+ walletdb.EraseEdPool (nIndex);
4900
4771
nKeysLeftSinceAutoBackup = nWalletBackups ? nKeysLeftSinceAutoBackup - 1 : 0 ;
4901
4772
}
4902
4773
LogPrintf (" keypool keep %d\n " , nIndex);
4903
- }
4904
-
4905
- void CWallet::KeepEdKey (int64_t nIndex)
4906
- {
4907
- // Remove from key pool
4908
- if (fFileBacked ) {
4909
- CWalletDB walletdb (strWalletFile);
4910
- walletdb.EraseEdPool (nIndex);
4911
- }
4912
4774
LogPrintf (" edkeypool keep %d\n " , nIndex);
4913
4775
}
4914
4776
@@ -4919,11 +4781,14 @@ void CWallet::ReturnKey(int64_t nIndex, bool fInternal)
4919
4781
LOCK (cs_wallet);
4920
4782
if (fInternal ) {
4921
4783
setInternalKeyPool.insert (nIndex);
4784
+ setInternalEdKeyPool.insert (nIndex);
4922
4785
} else {
4923
4786
setExternalKeyPool.insert (nIndex);
4787
+ setExternalEdKeyPool.insert (nIndex);
4924
4788
}
4925
4789
}
4926
4790
LogPrintf (" keypool return %d\n " , nIndex);
4791
+ LogPrintf (" edkeypool return %d\n " , nIndex);
4927
4792
}
4928
4793
4929
4794
bool CWallet::GetKeysFromPool (CPubKey& pubkeyWallet, std::vector<unsigned char >& vchEd25519PubKey, CStealthAddress& sxAddr, bool fInternal )
@@ -4952,17 +4817,19 @@ bool CWallet::GetKeysFromPool(CPubKey& pubkeyWallet, std::vector<unsigned char>&
4952
4817
int64_t nEdIndex = 0 ;
4953
4818
CKeyPool keypool;
4954
4819
CEdKeyPool edkeypool;
4820
+ bool keptAlready = false ;
4955
4821
{
4956
4822
LOCK (cs_wallet);
4957
- ReserveKeyFromKeyPool (nIndex, keypool, fInternal );
4958
- ReserveEdKeyFromKeyPool ( nEdIndex, edkeypool, fInternal ) ;
4823
+ ReserveKeysFromKeyPools (nIndex, keypool, edkeypool , fInternal );
4824
+ nEdIndex = nIndex ;
4959
4825
if (nIndex == -1 ) {
4960
4826
if (IsLocked (true ))
4961
4827
return false ;
4962
4828
// TODO: implement keypool for all accouts?
4963
4829
pubkeyWallet = GenerateNewKey (0 , fInternal );
4964
4830
}
4965
4831
else {
4832
+ keptAlready = true ;
4966
4833
KeepKey (nIndex);
4967
4834
pubkeyWallet = keypool.vchPubKey ;
4968
4835
}
@@ -4976,7 +4843,8 @@ bool CWallet::GetKeysFromPool(CPubKey& pubkeyWallet, std::vector<unsigned char>&
4976
4843
vchEd25519PubKey = GenerateNewEdKey (0 , fInternal , keyRetrieved);
4977
4844
}
4978
4845
else {
4979
- KeepEdKey (nEdIndex);
4846
+ if (!keptAlready)
4847
+ KeepKey (nIndex);
4980
4848
vchEd25519PubKey = edkeypool.edPubKey ;
4981
4849
}
4982
4850
}
@@ -5183,7 +5051,8 @@ bool CReserveKey::GetReservedKey(CPubKey& pubkey, bool fInternalIn)
5183
5051
{
5184
5052
if (nIndex == -1 ) {
5185
5053
CKeyPool keypool;
5186
- pwallet->ReserveKeyFromKeyPool (nIndex, keypool, fInternalIn );
5054
+ CEdKeyPool edkeypool;
5055
+ pwallet->ReserveKeysFromKeyPools (nIndex, keypool, edkeypool, fInternalIn );
5187
5056
if (nIndex != -1 ) {
5188
5057
vchPubKey = keypool.vchPubKey ;
5189
5058
} else {
0 commit comments