Skip to content

Commit

Permalink
Add a method to request wallet unlock.
Browse files Browse the repository at this point in the history
  • Loading branch information
sproxet authored and levonpetrosyan93 committed Aug 10, 2023
1 parent 9506e7a commit 30bcc53
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
15 changes: 13 additions & 2 deletions src/spark/sparkwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -66,6 +74,9 @@ CSparkWallet::CSparkWallet(const std::string& strWalletFile) {

}
threadPool = new ParallelOpThreadPool<void>(boost::thread::hardware_concurrency());

if (fWalletJustUnlocked)
pwalletMain->Lock();
}

CSparkWallet::~CSparkWallet() {
Expand Down
23 changes: 22 additions & 1 deletion src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -758,6 +758,8 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface

std::unique_ptr<CSparkWallet> sparkWallet;

std::atomic<bool> fUnlockRequested;

CWallet()
{
SetNull();
Expand Down Expand Up @@ -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<CTxDestination, int64_t> &mapKeyBirth) const;

/**
Expand Down

0 comments on commit 30bcc53

Please sign in to comment.