Coins: don't allow selecting unspendable coins

This commit is contained in:
tobtoht 2024-01-07 16:56:04 +01:00
parent 1a597fd2d9
commit a5ff6a3b67
No known key found for this signature in database
GPG key ID: E45B10DD027D2472
2 changed files with 36 additions and 29 deletions

View file

@ -186,17 +186,16 @@ void CoinsWidget::thawAllSelected() {
} }
void CoinsWidget::spendSelected() { void CoinsWidget::spendSelected() {
QModelIndexList list = ui->coins->selectionModel()->selectedRows(); QVector<CoinsInfo*> selectedCoins = this->currentEntries();
QStringList keyimages; QStringList keyimages;
for (QModelIndex index: list) {
QString keyImage = m_model->entryFromIndex(m_proxyModel->mapToSource(index))->keyImage();
if (keyImage == "0100000000000000000000000000000000000000000000000000000000000000") { for (const auto coin : selectedCoins) {
Utils::showError(this, "Unable to select output to spend", "Selected output has unknown key image"); if (!coin) return;
return;
} bool spendable = this->isCoinSpendable(coin);
if (!spendable) return;
QString keyImage = coin->keyImage();
keyimages << keyImage; keyimages << keyImage;
} }
@ -220,27 +219,10 @@ void CoinsWidget::onSweepOutputs() {
for (const auto coin : selectedCoins) { for (const auto coin : selectedCoins) {
if (!coin) return; if (!coin) return;
bool spendable = this->isCoinSpendable(coin);
if (!spendable) return;
QString keyImage = coin->keyImage(); 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); keyImages.push_back(keyImage);
totalAmount += coin->amount(); totalAmount += coin->amount();
} }
@ -347,4 +329,28 @@ void CoinsWidget::editLabel() {
ui->coins->edit(index); 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; CoinsWidget::~CoinsWidget() = default;

View file

@ -86,6 +86,7 @@ private:
CoinsInfo* currentEntry(); CoinsInfo* currentEntry();
QVector<CoinsInfo*> currentEntries(); QVector<CoinsInfo*> currentEntries();
QStringList selectedPubkeys(); QStringList selectedPubkeys();
bool isCoinSpendable(CoinsInfo* coin);
}; };