Skip to content

Commit 6115d09

Browse files
committed
gui: notify the user early if any of the recipients is invalid.
As we are already validating the entire recipient data before adding it to the temporal recipients vector (address and amount), the 'SendCoinsEntry' validation function can just return the proper error code, so it can be processed and the user can be notified right away. Without requiring to unlock the wallet, nor walk through the entire recipients vector later again on the flow just to validate the same things twice.
1 parent c6832ce commit 6115d09

File tree

3 files changed

+35
-43
lines changed

3 files changed

+35
-43
lines changed

src/qt/sendcoinsdialog.cpp

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -252,34 +252,28 @@ SendCoinsDialog::~SendCoinsDialog()
252252
bool SendCoinsDialog::PrepareSendText(QString& question_string, QString& informative_text, QString& detailed_text)
253253
{
254254
QList<SendCoinsRecipient> recipients;
255-
bool valid = true;
256-
257-
for(int i = 0; i < ui->entries->count(); ++i)
258-
{
259-
SendCoinsEntry *entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
260-
if(entry)
261-
{
262-
if(entry->validate(model->node()))
263-
{
264-
recipients.append(entry->getValue());
265-
}
266-
else if (valid)
267-
{
255+
for (int i = 0; i < ui->entries->count(); ++i) {
256+
SendCoinsEntry* entry = qobject_cast<SendCoinsEntry*>(ui->entries->itemAt(i)->widget());
257+
if (entry) {
258+
// Validate recipient and notify user if needed
259+
auto res_validate = entry->validate(model->node());
260+
if (res_validate.status != WalletModel::OK) {
268261
ui->scrollArea->ensureWidgetVisible(entry);
269-
valid = false;
262+
processSendCoinsReturn(res_validate); // notify user
263+
return false; // invalid entry
270264
}
265+
266+
recipients.append(entry->getValue());
271267
}
272268
}
273269

274-
if(!valid || recipients.isEmpty())
275-
{
270+
if (recipients.isEmpty()) {
276271
return false;
277272
}
278273

279274
fNewRecipientAllowed = false;
280275
WalletModel::UnlockContext ctx(model->requestUnlock());
281-
if(!ctx.isValid())
282-
{
276+
if (!ctx.isValid()) {
283277
// Unlock wallet was cancelled
284278
fNewRecipientAllowed = true;
285279
return false;

src/qt/sendcoinsentry.cpp

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -131,39 +131,30 @@ void SendCoinsEntry::useAvailableBalanceClicked()
131131
Q_EMIT useAvailableBalance(this);
132132
}
133133

134-
bool SendCoinsEntry::validate(interfaces::Node& node)
134+
WalletModel::SendCoinsReturn SendCoinsEntry::validate(interfaces::Node& node)
135135
{
136-
if (!model)
137-
return false;
138-
139-
// Check input validity
140-
bool retval = true;
136+
assert(model);
141137

142-
if (!model->validateAddress(ui->payTo->text()))
143-
{
138+
// Validate address
139+
if (!model->validateAddress(ui->payTo->text())) {
144140
ui->payTo->setValid(false);
145-
retval = false;
146-
}
147-
148-
if (!ui->payAmount->validate())
149-
{
150-
retval = false;
141+
return WalletModel::InvalidAddress;
151142
}
152143

153-
// Sending a zero amount is invalid
154-
if (ui->payAmount->value(nullptr) <= 0)
155-
{
156-
ui->payAmount->setValid(false);
157-
retval = false;
144+
// Validate amount, must be valid and positive
145+
if (!ui->payAmount->validate() ||
146+
ui->payAmount->value(nullptr) <= 0) {
147+
return WalletModel::InvalidAmount;
158148
}
159149

160-
// Reject dust outputs:
161-
if (retval && GUIUtil::isDust(node, ui->payTo->text(), ui->payAmount->value())) {
150+
// Reject dust outputs
151+
if (GUIUtil::isDust(node, ui->payTo->text(), ui->payAmount->value())) {
162152
ui->payAmount->setValid(false);
163-
retval = false;
153+
return WalletModel::InvalidAmount;
164154
}
165155

166-
return retval;
156+
// all good
157+
return WalletModel::OK;
167158
}
168159

169160
SendCoinsRecipient SendCoinsEntry::getValue()

src/qt/sendcoinsentry.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
#define BITCOIN_QT_SENDCOINSENTRY_H
77

88
#include <qt/sendcoinsrecipient.h>
9+
#include <qt/walletmodel.h> // for Walletmodel::StatusCode
910

1011
#include <QStackedWidget>
1112

12-
class WalletModel;
1313
class PlatformStyle;
1414

1515
namespace interfaces {
@@ -34,7 +34,14 @@ class SendCoinsEntry : public QStackedWidget
3434
~SendCoinsEntry();
3535

3636
void setModel(WalletModel *model);
37-
bool validate(interfaces::Node& node);
37+
38+
/**
39+
* Validates that:
40+
* 1) The address is valid (network supported).
41+
* 2) The amount is valid and greater than 0.
42+
* 3) The amount is not dust.
43+
*/
44+
WalletModel::SendCoinsReturn validate(interfaces::Node& node);
3845
SendCoinsRecipient getValue();
3946

4047
/** Return whether the entry is still empty and unedited */

0 commit comments

Comments
 (0)