-
Notifications
You must be signed in to change notification settings - Fork 50
Add validation to the Send form (address and amount) #462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
0640751
7a0d186
aa927c5
e65e7bf
001f4de
5845f68
204fa38
cc15afd
29e568c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,14 @@ | |
#include <qml/models/sendrecipient.h> | ||
|
||
#include <qml/bitcoinamount.h> | ||
#include <qml/models/walletqmlmodel.h> | ||
|
||
SendRecipient::SendRecipient(QObject* parent) | ||
: QObject(parent), m_amount(new BitcoinAmount(this)) | ||
#include <key_io.h> | ||
|
||
SendRecipient::SendRecipient(WalletQmlModel* wallet, QObject* parent) | ||
: QObject(parent), m_wallet(wallet), m_amount(new BitcoinAmount(this)) | ||
{ | ||
connect(m_amount, &BitcoinAmount::amountChanged, this, &SendRecipient::validateAmount); | ||
} | ||
|
||
QString SendRecipient::address() const | ||
|
@@ -21,6 +25,20 @@ void SendRecipient::setAddress(const QString& address) | |
if (m_address != address) { | ||
m_address = address; | ||
Q_EMIT addressChanged(); | ||
validateAddress(); | ||
} | ||
} | ||
|
||
QString SendRecipient::addressError() const | ||
{ | ||
return m_addressError; | ||
} | ||
|
||
void SendRecipient::setAddressError(const QString& error) | ||
{ | ||
if (m_addressError != error) { | ||
m_addressError = error; | ||
Q_EMIT addressErrorChanged(); | ||
} | ||
} | ||
|
||
|
@@ -42,6 +60,19 @@ BitcoinAmount* SendRecipient::amount() const | |
return m_amount; | ||
} | ||
|
||
QString SendRecipient::amountError() const | ||
{ | ||
return m_amountError; | ||
} | ||
|
||
void SendRecipient::setAmountError(const QString& error) | ||
{ | ||
if (m_amountError != error) { | ||
m_amountError = error; | ||
Q_EMIT amountErrorChanged(); | ||
} | ||
} | ||
|
||
QString SendRecipient::message() const | ||
{ | ||
return m_message; | ||
|
@@ -77,3 +108,42 @@ void SendRecipient::clear() | |
Q_EMIT messageChanged(); | ||
Q_EMIT amount()->amountChanged(); | ||
} | ||
|
||
void SendRecipient::validateAddress() | ||
{ | ||
if (!m_address.isEmpty() && !IsValidDestinationString(m_address.toStdString())) { | ||
if (IsValidDestinationString(m_address.toStdString(), *CChainParams::Main())) { | ||
setAddressError(tr("Address is valid for mainnet, not the current network")); | ||
} else if (IsValidDestinationString(m_address.toStdString(), *CChainParams::TestNet())) { | ||
setAddressError(tr("Address is valid for testnet, not the current network")); | ||
} else { | ||
setAddressError(tr("Invalid address format")); | ||
} | ||
} else { | ||
setAddressError(""); | ||
} | ||
|
||
Q_EMIT isValidChanged(); | ||
} | ||
|
||
void SendRecipient::validateAmount() | ||
{ | ||
if (m_amount->isSet()) { | ||
if (m_amount->satoshi() <= 0) { | ||
setAmountError(tr("Amount must be greater than zero")); | ||
} else if (m_amount->satoshi() > MAX_MONEY) { | ||
setAmountError(tr("Amount exceeds maximum limit of 21,000,000 BTC")); | ||
} else if (m_amount->satoshi() > m_wallet->balanceSatoshi()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could remove this case for now as it allows removing the circular dependency? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this might be a good idea and I can just do this validation when creating the transaction in the wallet model There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think im going to leave the circular dependency in for now.and take another look when doing validation with multiple recipients |
||
setAmountError(tr("Amount exceeds available balance")); | ||
} else { | ||
setAmountError(""); | ||
} | ||
} | ||
|
||
Q_EMIT isValidChanged(); | ||
} | ||
|
||
bool SendRecipient::isValid() const | ||
{ | ||
return m_addressError.isEmpty() && m_amountError.isEmpty() && m_amount->satoshi() > 0 && !m_address.isEmpty(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm able to enter zero without the error.
The error was displayed the first time I entered 0, after that the error wasn't there anymore