Skip to content

Commit

Permalink
Coins: don't allow selecting unspendable coins
Browse files Browse the repository at this point in the history
  • Loading branch information
tobtoht committed Jan 7, 2024
1 parent 1a597fd commit a5ff6a3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
64 changes: 35 additions & 29 deletions src/CoinsWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,16 @@ void CoinsWidget::thawAllSelected() {
}

void CoinsWidget::spendSelected() {
QModelIndexList list = ui->coins->selectionModel()->selectedRows();

QVector<CoinsInfo*> selectedCoins = this->currentEntries();
QStringList keyimages;
for (QModelIndex index: list) {
QString keyImage = m_model->entryFromIndex(m_proxyModel->mapToSource(index))->keyImage();

if (keyImage == "0100000000000000000000000000000000000000000000000000000000000000") {
Utils::showError(this, "Unable to select output to spend", "Selected output has unknown key image");
return;
}

for (const auto coin : selectedCoins) {
if (!coin) return;

bool spendable = this->isCoinSpendable(coin);
if (!spendable) return;

QString keyImage = coin->keyImage();
keyimages << keyImage;
}

Expand All @@ -220,27 +219,10 @@ void CoinsWidget::onSweepOutputs() {
for (const auto coin : selectedCoins) {
if (!coin) return;

QString keyImage = coin->keyImage();
if (!coin->keyImageKnown()) {
Utils::showError(this, "Unable to sweep outputs", "Selected output has unknown key image");
return;
}

if (coin->spent()) {
Utils::showError(this, "Unable to sweep outputs", "Selected output was already spent");
return;
}

if (coin->frozen()) {
Utils::showError(this, "Unable to sweep outputs", "Selected output is frozen", {"Thaw the selected output(s) before spending"}, "freeze_thaw_outputs");
return;
}

if (!coin->unlocked()) {
Utils::showError(this, "Unable to sweep outputs", "Selected output is locked", {"Wait until the output has reached the required number of confirmation before spending."});
return;
}
bool spendable = this->isCoinSpendable(coin);
if (!spendable) return;

QString keyImage = coin->keyImage();
keyImages.push_back(keyImage);
totalAmount += coin->amount();
}
Expand Down Expand Up @@ -347,4 +329,28 @@ void CoinsWidget::editLabel() {
ui->coins->edit(index);
}

bool CoinsWidget::isCoinSpendable(CoinsInfo *coin) {
if (!coin->keyImageKnown()) {
Utils::showError(this, "Unable to spend outputs", "Selected output has unknown key image");
return false;
}

if (coin->spent()) {
Utils::showError(this, "Unable to spend outputs", "Selected output was already spent");
return false;
}

if (coin->frozen()) {
Utils::showError(this, "Unable to spend outputs", "Selected output is frozen", {"Thaw the selected output(s) before spending"}, "freeze_thaw_outputs");
return false;
}

if (!coin->unlocked()) {
Utils::showError(this, "Unable to spend outputs", "Selected output is locked", {"Wait until the output has reached the required number of confirmation before spending."});
return false;
}

return true;
}

CoinsWidget::~CoinsWidget() = default;
1 change: 1 addition & 0 deletions src/CoinsWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ private slots:
CoinsInfo* currentEntry();
QVector<CoinsInfo*> currentEntries();
QStringList selectedPubkeys();
bool isCoinSpendable(CoinsInfo* coin);
};


Expand Down

0 comments on commit a5ff6a3

Please sign in to comment.