Skip to content

Commit

Permalink
Locked Wallet case covered
Browse files Browse the repository at this point in the history
  • Loading branch information
levonpetrosyan93 committed Aug 2, 2023
1 parent 4490b38 commit ddb04d8
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/spark/sparkwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ CSparkWallet::CSparkWallet(const std::string& strWalletFile) {

fullViewKey = spark::FullViewKey(params);
viewKey = spark::IncomingViewKey(params);
isSet = false;

// try to get incoming view key from db, if it fails, that means it is first start
if (!walletdb.readFullViewKey(fullViewKey)) {
Expand All @@ -31,6 +32,7 @@ CSparkWallet::CSparkWallet(const std::string& strWalletFile) {
}
// Generating spark key set first time
spark::SpendKey spendKey = generateSpendKey(params);
isSet = true;
fullViewKey = generateFullViewKey(spendKey);
viewKey = generateIncomingViewKey(fullViewKey);

Expand All @@ -42,6 +44,7 @@ CSparkWallet::CSparkWallet(const std::string& strWalletFile) {
// set 0 as last diversifier into db, we will update it later, in case coin comes, or user manually generates new address
walletdb.writeDiversifier(lastDiversifier);
} else {
isSet = true;
viewKey = generateIncomingViewKey(fullViewKey);
int32_t diversifierInDB = 0;
// read diversifier from db
Expand All @@ -63,6 +66,7 @@ CSparkWallet::CSparkWallet(const std::string& strWalletFile) {

}
}

}
threadPool = new ParallelOpThreadPool<void>(boost::thread::hardware_concurrency());
}
Expand All @@ -79,6 +83,10 @@ void CSparkWallet::updatetDiversifierInDB(CWalletDB& walletdb) {
walletdb.writeDiversifier(lastDiversifier);
}

bool CSparkWallet::getIsSet() {
return isSet;
}

CAmount CSparkWallet::getFullBalance() {
return getAvailableBalance() + getUnconfirmedBalance();
}
Expand Down Expand Up @@ -169,11 +177,17 @@ CAmount CSparkWallet::getAddressUnconfirmedBalance(const spark::Address& address
}

spark::Address CSparkWallet::generateNextAddress() {
if (!isSet)
throw std::runtime_error("No Spark wallet.");

lastDiversifier++;
return spark::Address(viewKey, lastDiversifier);
}

spark::Address CSparkWallet::generateNewAddress() {
if (!isSet)
throw std::runtime_error("No Spark wallet.");

lastDiversifier++;
spark::Address address(viewKey, lastDiversifier);

Expand All @@ -184,13 +198,19 @@ spark::Address CSparkWallet::generateNewAddress() {
}

spark::Address CSparkWallet::getDefaultAddress() {
if (!isSet)
throw std::runtime_error("No Spark wallet.");

if (addresses.count(0))
return addresses[0];
lastDiversifier = 0;
return spark::Address(viewKey, lastDiversifier);
}

spark::Address CSparkWallet::getChangeAddress() {
if (!isSet)
throw std::runtime_error("No Spark wallet.");

return spark::Address(viewKey, SPARK_CHANGE_D);
}

Expand Down Expand Up @@ -224,10 +244,16 @@ spark::SpendKey CSparkWallet::generateSpendKey(const spark::Params* params) {
}

spark::FullViewKey CSparkWallet::generateFullViewKey(const spark::SpendKey& spend_key) {
if (!isSet)
throw std::runtime_error("No Spark wallet.");

return spark::FullViewKey(spend_key);
}

spark::IncomingViewKey CSparkWallet::generateIncomingViewKey(const spark::FullViewKey& full_view_key) {
if (!isSet)
throw std::runtime_error("No Spark wallet.");

viewKey = spark::IncomingViewKey(full_view_key);
return viewKey;
}
Expand All @@ -237,13 +263,19 @@ std::unordered_map<int32_t, spark::Address> CSparkWallet::getAllAddresses() {
}

spark::Address CSparkWallet::getAddress(const int32_t& i) {
if (!isSet)
throw std::runtime_error("No Spark wallet.");

if (lastDiversifier < i || addresses.count(i) == 0)
return spark::Address(viewKey, lastDiversifier);

return addresses[i];
}

bool CSparkWallet::isAddressMine(const std::string& encodedAddr) {
if (!isSet)
false;

const spark::Params* params = spark::Params::get_default();
spark::Address address(params);
try {
Expand Down Expand Up @@ -308,6 +340,9 @@ std::unordered_map<uint256, CSparkMintMeta> CSparkWallet::getMintMap() const {


spark::Coin CSparkWallet::getCoinFromMeta(const CSparkMintMeta& meta) const {
if (!isSet)
return spark::Coin();

const spark::Params* params = spark::Params::get_default();
if (meta.coin != spark::Coin())
return meta.coin;
Expand Down Expand Up @@ -409,6 +444,9 @@ CSparkMintMeta CSparkWallet::getMintMeta(const secp_primitives::Scalar& nonce) {
}

bool CSparkWallet::getMintAmount(spark::Coin coin, CAmount& amount) {
if (!isSet)
return false;

spark::IdentifiedCoinData identifiedCoinData;
try {
identifiedCoinData = coin.identify(this->viewKey);
Expand Down Expand Up @@ -484,6 +522,9 @@ void CSparkWallet::UpdateSpendStateFromBlock(const CBlock& block) {
}

bool CSparkWallet::isMine(spark::Coin coin) const {
if (!isSet)
return false;

try {
spark::IdentifiedCoinData identifiedCoinData = coin.identify(this->viewKey);
} catch (...) {
Expand All @@ -506,6 +547,8 @@ bool CSparkWallet::isMine(const std::vector<GroupElement>& lTags) const {
}

CAmount CSparkWallet::getMyCoinV(spark::Coin coin) const {
if (!isSet)
return 0;
CAmount v(0);
try {
spark::IdentifiedCoinData identifiedCoinData = coin.identify(this->viewKey);
Expand All @@ -517,6 +560,9 @@ CAmount CSparkWallet::getMyCoinV(spark::Coin coin) const {
}

bool CSparkWallet::getMyCoinIsChange(spark::Coin coin) const {
if (!isSet)
return false;

try {
spark::IdentifiedCoinData identifiedCoinData = coin.identify(this->viewKey);
return isChangeAddress(identifiedCoinData.i);
Expand All @@ -539,6 +585,9 @@ CAmount CSparkWallet::getMySpendAmount(const std::vector<GroupElement>& lTags) c
}

void CSparkWallet::UpdateMintState(const std::vector<spark::Coin>& coins, const uint256& txHash, CWalletDB& walletdb) {
if (!isSet)
return;

spark::CSparkState *sparkState = spark::CSparkState::GetState();
for (auto coin : coins) {
try {
Expand Down Expand Up @@ -612,6 +661,9 @@ void CSparkWallet::UpdateMintStateFromBlock(const CBlock& block) {
}

void CSparkWallet::RemoveSparkMints(const std::vector<spark::Coin>& mints) {
if (!isSet)
return;

for (auto coin : mints) {
try {
spark::IdentifiedCoinData identifiedCoinData = coin.identify(this->viewKey);
Expand Down Expand Up @@ -1170,6 +1222,8 @@ CWalletTx CSparkWallet::CreateSparkSpendTransaction(
const std::vector<std::pair<spark::OutputCoinData, bool>>& privateRecipients,
CAmount &fee,
const CCoinControl *coinControl) {
if (!isSet)
throw std::runtime_error(_("No Spark wallet."));

if (recipients.empty() && privateRecipients.empty()) {
throw std::runtime_error(_("Either recipients or newMints has to be nonempty."));
Expand Down
4 changes: 4 additions & 0 deletions src/spark/sparkwallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class CSparkWallet {
// assign diversifier in to to current value
void updatetDiversifierInDB(CWalletDB& walletdb);

bool getIsSet();

// functions for key set generation
spark::SpendKey generateSpendKey(const spark::Params* params);
spark::FullViewKey generateFullViewKey(const spark::SpendKey& spend_key);
Expand Down Expand Up @@ -145,6 +147,8 @@ class CSparkWallet {
mutable CCriticalSection cs_spark_wallet;

private:
bool isSet;

std::string strWalletFile;
// this is latest used diversifier
int32_t lastDiversifier;
Expand Down
3 changes: 3 additions & 0 deletions src/wallet/crypter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn, const bool& fF
pwalletMain->zwallet->SetupWallet(hashSeedMaster, false);
pwalletMain->zwallet->SyncWithChain();
}

if (pwalletMain->sparkWallet && !pwalletMain->sparkWallet->getIsSet())
pwalletMain->sparkWallet = std::make_unique<CSparkWallet>(pwalletMain->strWalletFile);
}
NotifyStatusChanged(this);
return true;
Expand Down
35 changes: 35 additions & 0 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,9 @@ UniValue gettotalbalance(const JSONRPCRequest& request)
EnsureSparkWalletIsAvailable();
LOCK2(cs_main, pwallet->cs_wallet);

if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time.\n");

return ValueFromAmount(pwallet->GetBalance() + pwallet->GetPrivateBalance().first + pwallet->sparkWallet->getAvailableBalance());
}

Expand Down Expand Up @@ -3203,6 +3206,9 @@ UniValue listunspentsparkmints(const JSONRPCRequest& request) {
UniValue results(UniValue::VARR);;
assert(pwallet != NULL);

if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

std::list<CSparkMintMeta> coins = pwallet->sparkWallet->GetAvailableSparkCoins();
LogPrintf("coins.size()=%s\n", coins.size());
BOOST_FOREACH(const auto& coin, coins)
Expand Down Expand Up @@ -3248,6 +3254,9 @@ UniValue listsparkmints(const JSONRPCRequest& request) {

EnsureSparkWalletIsAvailable();

if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

UniValue results(UniValue::VARR);;
assert(pwallet != NULL);

Expand Down Expand Up @@ -3293,6 +3302,8 @@ UniValue getsparkdefaultaddress(const JSONRPCRequest& request) {
EnsureSparkWalletIsAvailable();

assert(pwallet != NULL);
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

spark::Address address = pwallet->sparkWallet->getDefaultAddress();
unsigned char network = spark::GetNetworkType();
Expand All @@ -3317,6 +3328,8 @@ UniValue getnewsparkaddress(const JSONRPCRequest& request) {
EnsureSparkWalletIsAvailable();

assert(pwallet != NULL);
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

spark::Address address = pwallet->sparkWallet->generateNewAddress();
unsigned char network = spark::GetNetworkType();
Expand All @@ -3342,6 +3355,8 @@ UniValue getallsparkaddresses(const JSONRPCRequest& request) {
EnsureSparkWalletIsAvailable();

assert(pwallet != NULL);
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

std::unordered_map<int32_t, spark::Address> addresses = pwallet->sparkWallet->getAllAddresses();
unsigned char network = spark::GetNetworkType();
Expand Down Expand Up @@ -3370,6 +3385,8 @@ UniValue listsparkspends(const JSONRPCRequest& request) {

EnsureSparkWalletIsAvailable();
assert(pwallet != NULL);
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

std::list<CSparkSpendEntry> spends = pwallet->sparkWallet->ListSparkSpends();

Expand Down Expand Up @@ -3401,6 +3418,9 @@ UniValue getsparkbalance(const JSONRPCRequest& request) {

EnsureSparkWalletIsAvailable();
assert(pwallet != NULL);
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

UniValue results(UniValue::VOBJ);
results.push_back(Pair("availableBalance",pwallet->sparkWallet->getAvailableBalance()));
results.push_back(Pair("unconfirmedBalance",pwallet->sparkWallet->getUnconfirmedBalance()));
Expand All @@ -3425,6 +3445,9 @@ UniValue getsparkaddressbalance(const JSONRPCRequest& request) {

EnsureSparkWalletIsAvailable();
assert(pwallet != NULL);
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

std::string strAddress = request.params[0].get_str();
const spark::Params* params = spark::Params::get_default();
unsigned char network = spark::GetNetworkType();
Expand Down Expand Up @@ -3461,6 +3484,8 @@ UniValue resetsparkmints(const JSONRPCRequest& request) {
"WARNING: Run this only for testing and if you fully understand what it does.\n");

EnsureSparkWalletIsAvailable();
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

std::vector<CSparkMintMeta> listMints;
CWalletDB walletdb(pwallet->strWalletFile);
Expand All @@ -3487,6 +3512,8 @@ UniValue setsparkmintstatus(const JSONRPCRequest& request) {
"Set mintIsUsed status to True or False");

EnsureSparkWalletIsAvailable();
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

uint256 lTagHash;
lTagHash.SetHex(request.params[0].get_str());
Expand Down Expand Up @@ -3534,6 +3561,8 @@ UniValue mintspark(const JSONRPCRequest& request)
);
EnsureWalletIsUnlocked(pwallet);
EnsureSparkWalletIsAvailable();
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

// Ensure spark mints is already accepted by network so users will not lost their coins
// due to other nodes will treat it as garbage data.
Expand Down Expand Up @@ -3611,6 +3640,8 @@ UniValue automintspark(const JSONRPCRequest& request) {

EnsureWalletIsUnlocked(pwallet);
EnsureSparkWalletIsAvailable();
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

// Ensure spark mints is already accepted by network so users will not lost their coins
// due to other nodes will treat it as garbage data.
Expand Down Expand Up @@ -3663,6 +3694,8 @@ UniValue spendspark(const JSONRPCRequest& request)

EnsureWalletIsUnlocked(pwallet);
EnsureSparkWalletIsAvailable();
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

// Ensure spark mints is already accepted by network so users will not lost their coins
// due to other nodes will treat it as garbage data.
Expand Down Expand Up @@ -3789,6 +3822,8 @@ UniValue lelantustospark(const JSONRPCRequest& request) {

EnsureWalletIsUnlocked(pwallet);
EnsureSparkWalletIsAvailable();
if(!pwallet->sparkWallet->getIsSet())
throw std::runtime_error("No Spark Wallet, most probably your wallet is locked with password, please unclock it one time\n");

assert(pwallet != NULL);
std::string strFailReason = "";
Expand Down

0 comments on commit ddb04d8

Please sign in to comment.