Skip to content

Commit 5a6dedf

Browse files
committed
[Ed25519] Add ability to import mnemonic and restore when wallet is not synced. WIP
1 parent 1e6ce40 commit 5a6dedf

File tree

2 files changed

+117
-13
lines changed

2 files changed

+117
-13
lines changed

src/wallet/wallet.cpp

Lines changed: 111 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,7 +1310,7 @@ bool CWallet::LoadToWallet(const CWalletTx& wtxIn)
13101310
* Abandoned state should probably be more carefuly tracked via different
13111311
* posInBlock signals or by checking mempool presence when necessary.
13121312
*/
1313-
bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate, bool fUpdateKeyPool)
1313+
bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate)
13141314
{
13151315
{
13161316
AssertLockHeld(cs_wallet);
@@ -1344,9 +1344,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlockIndex
13441344
std::vector<std::vector<unsigned char>> vvchOpParameters;
13451345
if (GetBDAPOpScript(ptx, bdapOpScript, vvchOpParameters, op1, op2)) {
13461346
std::string strOpType = GetBDAPOpTypeString(op1, op2);
1347-
if (fUpdateKeyPool && vvchOpParameters.size() > 1) {
1348-
UpdateKeyPoolsFromTransactions(strOpType,vvchOpParameters);
1349-
}
1347+
UpdateKeyPoolsFromTransactions(strOpType,vvchOpParameters);
13501348
if (GetOpCodeType(strOpType) == "link" && vvchOpParameters.size() > 1) {
13511349
uint64_t nExpireTime = 0;
13521350
if (vvchOpParameters.size() > 2) {
@@ -1545,6 +1543,19 @@ void CWallet::SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex,
15451543

15461544
fAnonymizableTallyCached = false;
15471545
fAnonymizableTallyCachedNonDenom = false;
1546+
1547+
if (fNeedToUpdateKeyPools) {
1548+
LogPrintf("DEBUGGER ED %s - fNeedToUpdateKeyPools is TRUE\n",__func__);
1549+
TopUpKeyPoolCombo();
1550+
fNeedToUpdateKeyPools = false;
1551+
}
1552+
1553+
if (fNeedToUpdateLinks) {
1554+
LogPrintf("DEBUGGER ED %s - fNeedToUpdateLinks is TRUE\n",__func__);
1555+
ProcessLinkQueue();
1556+
fNeedToUpdateLinks = false;
1557+
}
1558+
15481559
}
15491560

15501561

@@ -2061,7 +2072,19 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
20612072
CBlock block;
20622073
if (ReadBlockFromDisk(block, pindex, Params().GetConsensus())) {
20632074
for (size_t posInBlock = 0; posInBlock < block.vtx.size(); ++posInBlock) {
2064-
AddToWalletIfInvolvingMe(*block.vtx[posInBlock], pindex, posInBlock, fUpdate, true); //need to also update keypool
2075+
AddToWalletIfInvolvingMe(*block.vtx[posInBlock], pindex, posInBlock, fUpdate);
2076+
2077+
if (fNeedToUpdateKeyPools) {
2078+
LogPrintf("DEBUGGER ED %s - fNeedToUpdateKeyPools is TRUE\n",__func__);
2079+
TopUpKeyPoolCombo();
2080+
fNeedToUpdateKeyPools = false;
2081+
}
2082+
2083+
if (fNeedToUpdateLinks) {
2084+
LogPrintf("DEBUGGER ED %s - fNeedToUpdateLinks is TRUE\n",__func__);
2085+
ProcessLinkQueue();
2086+
fNeedToUpdateLinks = false;
2087+
}
20652088
}
20662089
if (!ret) {
20672090
ret = pindex;
@@ -2070,10 +2093,30 @@ CBlockIndex* CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool f
20702093
ret = nullptr;
20712094
}
20722095
pindex = chainActive.Next(pindex);
2096+
2097+
20732098
}
2074-
ReserveEdKeyForTransactions();
2075-
TopUpKeyPoolCombo();
2076-
ProcessLinkQueue();
2099+
//ReserveEdKeyForTransactions();
2100+
2101+
// if (fNeedToUpdatePoolsAndLinks) {
2102+
// LogPrintf("DEBUGGER ED %s - fNeedToUpdatePoolsAndLinks is TRUE\n",__func__);
2103+
// TopUpKeyPoolCombo();
2104+
// ProcessLinkQueue();
2105+
// fNeedToUpdatePoolsAndLinks = false;
2106+
// }
2107+
2108+
// if (fNeedToUpdateKeyPools) {
2109+
// LogPrintf("DEBUGGER ED %s - fNeedToUpdateKeyPools is TRUE\n",__func__);
2110+
// TopUpKeyPoolCombo();
2111+
// fNeedToUpdateKeyPools = false;
2112+
// }
2113+
2114+
// if (fNeedToUpdateLinks) {
2115+
// LogPrintf("DEBUGGER ED %s - fNeedToUpdateLinks is TRUE\n",__func__);
2116+
// ProcessLinkQueue();
2117+
// fNeedToUpdateLinks = false;
2118+
// }
2119+
20772120
ShowProgress(_("Rescanning..."), 100); // hide progress dialog in GUI
20782121
}
20792122
return ret;
@@ -4648,10 +4691,15 @@ void CWallet::UpdateKeyPoolsFromTransactions(const std::string& strOpType, const
46484691
std::vector<unsigned char> key0 = vvchOpParameters[0];
46494692
std::vector<unsigned char> key1 = vvchOpParameters[1];
46504693

4651-
if (strOpType == "bdap_new_account")
4652-
reservedEd25519PubKeys.push_back(key1);
4653-
else if (strOpType == "bdap_new_link_request" || strOpType == "bdap_new_link_accept")
4654-
reservedEd25519PubKeys.push_back(key0);
4694+
if (strOpType == "bdap_new_account") {
4695+
//reservedEd25519PubKeys.push_back(key1);
4696+
ReserveEdKeyForTransactions(key1);
4697+
}
4698+
else if (strOpType == "bdap_new_link_request" || strOpType == "bdap_new_link_accept") {
4699+
//reservedEd25519PubKeys.push_back(key0);
4700+
ReserveEdKeyForTransactions(key0);
4701+
fNeedToUpdateLinks = true;
4702+
}
46554703

46564704
} //UpdateKeyPoolsFromTransactions
46574705

@@ -4739,6 +4787,8 @@ void CWallet::ReserveEdKeyForTransactions()
47394787
KeepKey(nIndex);
47404788
KeepEdKey(nIndex);
47414789
keypoolIndexes.push_back(nIndex);
4790+
4791+
LogPrintf("DEBUGGER ED %s - reserving index [%d]\n",__func__,nIndex);
47424792
}
47434793
}
47444794

@@ -4755,9 +4805,58 @@ void CWallet::ReserveEdKeyForTransactions()
47554805
setInternalKeyPool.erase(*setInternalKeyPool.begin());
47564806
setInternalEdKeyPool.erase(*setInternalEdKeyPool.begin());
47574807
}
4808+
LogPrintf("DEBUGGER ED %s - keypool now pointing at [%d]\n",__func__,*setInternalKeyPool.begin());
4809+
LogPrintf("DEBUGGER ED %s - edkeypool now pointing at [%d]\n",__func__,*setInternalEdKeyPool.begin());
4810+
4811+
47584812

47594813
} //ReserveEdKeyForTransactions
47604814

4815+
void CWallet::ReserveEdKeyForTransactions(const std::vector<unsigned char>& pubKeyToReserve)
4816+
{
4817+
CWalletDB walletdb(strWalletFile);
4818+
CEdKeyPool edkeypool;
4819+
std::vector<unsigned char> edPubKey;
4820+
std::vector<int64_t> keypoolIndexes;
4821+
bool EraseIndex = false;
4822+
int64_t IndexToErase = 0;
4823+
int64_t nIndex = 0;
4824+
std::set<std::int64_t>::iterator it = setInternalEdKeyPool.begin();
4825+
4826+
while ((it != setInternalEdKeyPool.end()) && (!EraseIndex)) {
4827+
//for (int64_t nIndex : setInternalEdKeyPool) {
4828+
nIndex = *it;
4829+
if (!walletdb.ReadEdPool(nIndex, edkeypool)) {
4830+
throw std::runtime_error(std::string(__func__) + ": read failed");
4831+
}
4832+
edPubKey = edkeypool.edPubKey;
4833+
4834+
if(pubKeyToReserve == edPubKey) {
4835+
KeepKey(nIndex);
4836+
KeepEdKey(nIndex);
4837+
fNeedToUpdateKeyPools = true;
4838+
EraseIndex = true;
4839+
IndexToErase = nIndex;
4840+
LogPrintf("DEBUGGER ED %s - reserving index [%d]\n",__func__,nIndex);
4841+
}
4842+
it++;
4843+
}
4844+
4845+
if (EraseIndex) {
4846+
std::set<int64_t>::iterator eraseIndexEd = setInternalEdKeyPool.find(IndexToErase);
4847+
std::set<int64_t>::iterator eraseIndex = setInternalKeyPool.find(IndexToErase);
4848+
4849+
setInternalEdKeyPool.erase(eraseIndexEd);
4850+
setInternalKeyPool.erase(eraseIndex);
4851+
4852+
}
4853+
LogPrintf("DEBUGGER ED %s - keypool now pointing at [%d]\n",__func__,*setInternalKeyPool.begin());
4854+
LogPrintf("DEBUGGER ED %s - edkeypool now pointing at [%d]\n",__func__,*setInternalEdKeyPool.begin());
4855+
4856+
4857+
} //ReserveEdKeyForTransactions OVERLOAD
4858+
4859+
47614860
void CWallet::KeepKey(int64_t nIndex)
47624861
{
47634862
// Remove from key pool

src/wallet/wallet.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,6 +753,10 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
753753
int64_t nLastResend;
754754
bool fBroadcastTransactions;
755755

756+
//bool fNeedToUpdatePoolsAndLinks = false;
757+
bool fNeedToUpdateKeyPools = false;
758+
bool fNeedToUpdateLinks = false;
759+
756760
mutable bool fAnonymizableTallyCached;
757761
mutable std::vector<CompactTallyItem> vecAnonymizableTallyCached;
758762
mutable bool fAnonymizableTallyCachedNonDenom;
@@ -783,6 +787,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
783787
void ReserveEdKeyFromKeyPool(int64_t& nIndex, CEdKeyPool& edkeypool, bool fInternal);
784788

785789
void ReserveEdKeyForTransactions();
790+
void ReserveEdKeyForTransactions(const std::vector<unsigned char>& pubKeyToReserve); //overload
786791

787792
std::array<char, 32> ConvertSecureVector32ToArray(const std::vector<unsigned char, secure_allocator<unsigned char> >& vIn);
788793

@@ -1064,7 +1069,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
10641069
bool AddToWallet(const CWalletTx& wtxIn, bool fFlushOnClose = true);
10651070
bool LoadToWallet(const CWalletTx& wtxIn);
10661071
void SyncTransaction(const CTransaction& tx, const CBlockIndex* pindex, int posInBlock) override;
1067-
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate, bool fUpdateKeyPool = false);
1072+
bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlockIndex* pIndex, int posInBlock, bool fUpdate);
10681073
CBlockIndex* ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
10691074
void ReacceptWalletTransactions();
10701075
void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) override;

0 commit comments

Comments
 (0)