diff --git a/src/spark/sparkwallet.cpp b/src/spark/sparkwallet.cpp index 942f9d04b4..5b9372308e 100644 --- a/src/spark/sparkwallet.cpp +++ b/src/spark/sparkwallet.cpp @@ -23,12 +23,20 @@ CSparkWallet::CSparkWallet(const std::string& strWalletFile) { fullViewKey = spark::FullViewKey(params); viewKey = spark::IncomingViewKey(params); + bool fWalletJustUnlocked = false; + // try to get incoming view key from db, if it fails, that means it is first start if (!walletdb.readFullViewKey(fullViewKey)) { if (pwalletMain->IsLocked()) { - LogPrintf("Spark wallet creation FAILED, wallet is locked\n"); - return; + pwalletMain->RequestUnlock(); + if (pwalletMain->WaitForUnlock()) { + fWalletJustUnlocked = true; + } else { + throw std::runtime_error("Spark wallet creation FAILED, wallet could not be unlocked\n"); + return; + } } + // Generating spark key set first time spark::SpendKey spendKey = generateSpendKey(params); fullViewKey = generateFullViewKey(spendKey); @@ -66,6 +74,9 @@ CSparkWallet::CSparkWallet(const std::string& strWalletFile) { } threadPool = new ParallelOpThreadPool(boost::thread::hardware_concurrency()); + + if (fWalletJustUnlocked) + pwalletMain->Lock(); } CSparkWallet::~CSparkWallet() { diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 11c2589559..0c37c9364b 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -496,13 +496,34 @@ bool CWallet::Unlock(const SecureString &strWalletPassphrase, const bool& fFirst return false; if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey)) continue; // try another master key - if (CCryptoKeyStore::Unlock(vMasterKey, fFirstUnlock)) + if (CCryptoKeyStore::Unlock(vMasterKey, fFirstUnlock)) { + fUnlockRequested.store(false); return true; + } } } return false; } +void CWallet::RequestUnlock() { + if (!IsLocked()) + return; + + LogPrintf("Requesting wallet unlock\n"); + fUnlockRequested.store(true); +} + +bool CWallet::WaitForUnlock() { + while (IsLocked()) { + MilliSleep(100); + + if (ShutdownRequested()) + return false; + } + + return true; +} + bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase) { bool fWasLocked = IsLocked(); diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 58ddbe2d8b..d5a4e7b4af 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -758,6 +758,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface std::unique_ptr sparkWallet; + std::atomic fUnlockRequested; + CWallet() { SetNull(); @@ -897,6 +899,9 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface bool ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase); bool EncryptWallet(const SecureString& strWalletPassphrase); + void RequestUnlock(); + bool WaitForUnlock(); + void GetKeyBirthTimes(std::map &mapKeyBirth) const; /**