From a5ff6a3b676870ff70ebd1771e14eed106fad6a1 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Sun, 7 Jan 2024 16:56:04 +0100 Subject: [PATCH] Coins: don't allow selecting unspendable coins --- src/CoinsWidget.cpp | 64 +++++++++++++++++++++++++-------------------- src/CoinsWidget.h | 1 + 2 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/CoinsWidget.cpp b/src/CoinsWidget.cpp index 3b6394e..84715d2 100644 --- a/src/CoinsWidget.cpp +++ b/src/CoinsWidget.cpp @@ -186,17 +186,16 @@ void CoinsWidget::thawAllSelected() { } void CoinsWidget::spendSelected() { - QModelIndexList list = ui->coins->selectionModel()->selectedRows(); - + QVector 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; } @@ -220,27 +219,10 @@ void CoinsWidget::onSweepOutputs() { for (const auto coin : selectedCoins) { if (!coin) return; + bool spendable = this->isCoinSpendable(coin); + if (!spendable) 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; - } - keyImages.push_back(keyImage); totalAmount += coin->amount(); } @@ -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; \ No newline at end of file diff --git a/src/CoinsWidget.h b/src/CoinsWidget.h index a6156f6..6088972 100644 --- a/src/CoinsWidget.h +++ b/src/CoinsWidget.h @@ -86,6 +86,7 @@ private: CoinsInfo* currentEntry(); QVector currentEntries(); QStringList selectedPubkeys(); + bool isCoinSpendable(CoinsInfo* coin); };