]> Nutra Git (v1) - gamesguru/feather.git/commitdiff
Coins: don't allow selecting unspendable coins
authortobtoht <tob@featherwallet.org>
Sun, 7 Jan 2024 15:56:04 +0000 (16:56 +0100)
committertobtoht <tob@featherwallet.org>
Sun, 7 Jan 2024 15:56:04 +0000 (16:56 +0100)
src/CoinsWidget.cpp
src/CoinsWidget.h

index 3b6394e3fe9f79424781fd75bbc74e0d7a4d413a..84715d27d814f651ae690d6e3442bee515e25ba1 100644 (file)
@@ -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;
     }
 
@@ -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();
     }
@@ -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
index a6156f6420b5272e751196ae47475923143f0577..60889723836d6f4dc76cc13d26c379ea97c0729b 100644 (file)
@@ -86,6 +86,7 @@ private:
     CoinsInfo* currentEntry();
     QVector<CoinsInfo*> currentEntries();
     QStringList selectedPubkeys();
+    bool isCoinSpendable(CoinsInfo* coin);
 };