Skip to content

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

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
e79588b
qml: Introduce SendRecipientsListModel
johnny9 Mar 28, 2025
7f16391
qml: Add Multiple Recipients toggle to Send menu
johnny9 Mar 30, 2025
a4efd63
qml: Add Multiple Recipients bar to Send form
johnny9 Apr 2, 2025
c2bb783
qml: Reduce size of recipient selectors
johnny9 May 3, 2025
6ee0284
qml: Add remove button to multiple recipients
johnny9 May 8, 2025
a9acdf0
qml: Prepare transaction with recipients list
johnny9 May 10, 2025
53c7bbc
qml: Add MultipleSendReview page
johnny9 May 29, 2025
eea58e4
qml: Add plus big filled icon
johnny9 May 31, 2025
6613d40
qml: Replace NavButton with IconButton in Send
johnny9 May 31, 2025
6ef1148
qml: Cleanup BitcoinAmount
johnny9 May 31, 2025
72c3a9b
qml: Add total calculation to SendRecipientsListModel
johnny9 May 31, 2025
a5fd44e
qml: Commit Recipient amount when active focus is lost
johnny9 May 31, 2025
eec0763
qml: Clear Send form after sending transaciton
johnny9 May 31, 2025
7c4252d
qml: Add alert icon
johnny9 Jun 1, 2025
a5aba54
qml: Add validation to Send form address and amount
johnny9 Jun 1, 2025
2c8ce2c
qml: Split amount and display updating for Bitcoin amount input
johnny9 Jun 4, 2025
ccb0622
qml: Add validators to address and amount inputs to limit characters
johnny9 Jun 4, 2025
289cbcc
qml: Add maximum length for address input
johnny9 Jun 4, 2025
7e94eb8
qml: Specify maximum BTC amount in error string
johnny9 Jun 4, 2025
6a2e6d0
qml: Don't set/clear error string unless needed
johnny9 Jun 4, 2025
f1ad73a
qml: Add const to sendrecipient's m_wallet
johnny9 Jun 4, 2025
3630bc2
qml: Provide information if the supplied addres is valid for another …
johnny9 Jun 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Makefile.qt.include
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ QT_MOC_CPP = \
qml/models/moc_nodemodel.cpp \
qml/models/moc_options_model.cpp \
qml/models/moc_peerdetailsmodel.cpp \
qml/models/moc_peerlistsortproxy.cpp \
qml/models/moc_peerlistsortproxy.cpp \\
qml/models/moc_sendrecipient.cpp \
qml/models/moc_sendrecipientslistmodel.cpp \
qml/models/moc_transaction.cpp \
qml/models/moc_sendrecipient.cpp \
qml/models/moc_walletlistmodel.cpp \
Expand Down Expand Up @@ -138,6 +140,7 @@ BITCOIN_QT_H = \
qml/models/peerlistsortproxy.h \
qml/models/transaction.h \
qml/models/sendrecipient.h \
qml/models/sendrecipientslistmodel.h \
qml/models/walletlistmodel.h \
qml/models/walletqmlmodel.h \
qml/models/walletqmlmodeltransaction.h \
Expand Down Expand Up @@ -339,6 +342,7 @@ BITCOIN_QML_BASE_CPP = \
qml/models/peerlistsortproxy.cpp \
qml/models/transaction.cpp \
qml/models/sendrecipient.cpp \
qml/models/sendrecipientslistmodel.cpp \
qml/models/walletlistmodel.cpp \
qml/models/walletqmlmodel.cpp \
qml/models/walletqmlmodeltransaction.cpp \
Expand All @@ -353,6 +357,7 @@ QML_RES_FONTS = \

QML_RES_ICONS = \
qml/res/icons/add-wallet-dark.png \
qml/res/icons/alert-filled.png \
qml/res/icons/arrow-down.png \
qml/res/icons/arrow-up.png \
qml/res/icons/bitcoin-circle.png \
Expand All @@ -379,6 +384,7 @@ QML_RES_ICONS = \
qml/res/icons/network-dark.png \
qml/res/icons/network-light.png \
qml/res/icons/plus.png \
qml/res/icons/plus-big-filled.png \
qml/res/icons/pending.png \
qml/res/icons/shutdown.png \
qml/res/icons/singlesig-wallet.png \
Expand Down Expand Up @@ -420,12 +426,12 @@ QML_RES_QML = \
qml/controls/CoreCheckBox.qml \
qml/controls/CoreText.qml \
qml/controls/CoreTextField.qml \
qml/controls/EllipsisMenuButton.qml \
qml/controls/EllipsisMenuToggleItem.qml \
qml/controls/ExternalLink.qml \
qml/controls/FocusBorder.qml \
qml/controls/Header.qml \
qml/controls/Icon.qml \
qml/controls/IconButton.qml \
qml/controls/InformationPage.qml \
qml/controls/IPAddressValueInput.qml \
qml/controls/KeyValueRow.qml \
Expand Down Expand Up @@ -484,6 +490,7 @@ QML_RES_QML = \
qml/pages/wallet/CreatePassword.qml \
qml/pages/wallet/CreateWalletWizard.qml \
qml/pages/wallet/DesktopWallets.qml \
qml/pages/wallet/MultipleSendReview.qml \
qml/pages/wallet/RequestPayment.qml \
qml/pages/wallet/Send.qml \
qml/pages/wallet/SendResult.qml \
Expand Down
5 changes: 4 additions & 1 deletion src/qml/bitcoin_qml.qrc
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
<file>controls/FocusBorder.qml</file>
<file>controls/Header.qml</file>
<file>controls/Icon.qml</file>
<file>controls/IconButton.qml</file>
<file>controls/InformationPage.qml</file>
<file>controls/IPAddressValueInput.qml</file>
<file>controls/KeyValueRow.qml</file>
<file>controls/LabeledTextInput.qml</file>
<file>controls/LabeledCoinControlButton.qml</file>
<file>controls/EllipsisMenuButton.qml</file>
<file>controls/EllipsisMenuToggleItem.qml</file>
<file>controls/NavButton.qml</file>
<file>controls/NavigationBar.qml</file>
Expand Down Expand Up @@ -90,6 +90,7 @@
<file>pages/wallet/CreatePassword.qml</file>
<file>pages/wallet/CreateWalletWizard.qml</file>
<file>pages/wallet/DesktopWallets.qml</file>
<file>pages/wallet/MultipleSendReview.qml</file>
<file>pages/wallet/RequestPayment.qml</file>
<file>pages/wallet/Send.qml</file>
<file>pages/wallet/SendResult.qml</file>
Expand All @@ -99,6 +100,7 @@
</qresource>
<qresource prefix="/icons">
<file alias="add-wallet-dark">res/icons/add-wallet-dark.png</file>
<file alias="alert-filled">res/icons/alert-filled.png</file>
<file alias="arrow-down">res/icons/arrow-down.png</file>
<file alias="arrow-up">res/icons/arrow-up.png</file>
<file alias="bitcoin-circle">res/icons/bitcoin-circle.png</file>
Expand Down Expand Up @@ -126,6 +128,7 @@
<file alias="network-dark">res/icons/network-dark.png</file>
<file alias="network-light">res/icons/network-light.png</file>
<file alias="plus">res/icons/plus.png</file>
<file alias="plus-big-filled">res/icons/plus-big-filled.png</file>
<file alias="pending">res/icons/pending.png</file>
<file alias="shutdown">res/icons/shutdown.png</file>
<file alias="singlesig-wallet">res/icons/singlesig-wallet.png</file>
Expand Down
159 changes: 82 additions & 77 deletions src/qml/bitcoinamount.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 The Bitcoin Core developers
// Copyright (c) 2024-2025 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand All @@ -7,19 +7,9 @@
#include <QRegExp>
#include <QStringList>


BitcoinAmount::BitcoinAmount(QObject *parent) : QObject(parent)
BitcoinAmount::BitcoinAmount(QObject* parent)
: QObject(parent)
{
m_unit = Unit::BTC;
}

int BitcoinAmount::decimals(Unit unit)
{
switch (unit) {
case Unit::BTC: return 8;
case Unit::SAT: return 0;
} // no default case, so the compiler can warn about missing cases
assert(false);
}

QString BitcoinAmount::sanitize(const QString &text)
Expand All @@ -43,6 +33,30 @@ QString BitcoinAmount::sanitize(const QString &text)
return result;
}

qint64 BitcoinAmount::satoshi() const
{
return m_satoshi;
}

void BitcoinAmount::setSatoshi(qint64 new_amount)
{
m_isSet = true;
if (m_satoshi != new_amount) {
m_satoshi = new_amount;
Q_EMIT amountChanged();
}
}

void BitcoinAmount::clear()
{
if (!m_isSet && m_satoshi == 0) {
return;
}
m_satoshi = 0;
m_isSet = false;
Q_EMIT amountChanged();
}

BitcoinAmount::Unit BitcoinAmount::unit() const
{
return m_unit;
Expand All @@ -52,103 +66,94 @@ void BitcoinAmount::setUnit(const Unit unit)
{
m_unit = unit;
Q_EMIT unitChanged();
Q_EMIT displayChanged();
}

QString BitcoinAmount::unitLabel() const
{
switch (m_unit) {
case Unit::BTC: return "₿";
case Unit::SAT: return "Sat";
case Unit::SAT: return "sat";
}
assert(false);
}

QString BitcoinAmount::amount() const
void BitcoinAmount::flipUnit()
{
return m_amount;
if (m_unit == Unit::BTC) {
m_unit = Unit::SAT;
} else {
m_unit = Unit::BTC;
}
Q_EMIT unitChanged();
Q_EMIT displayChanged();
}

QString BitcoinAmount::satoshiAmount() const
QString BitcoinAmount::satsToBtcString(qint64 sat)
{
return toSatoshis(m_amount);
}
const bool negative = sat < 0;
qint64 absSat = negative ? -sat : sat;

void BitcoinAmount::setAmount(const QString& new_amount)
{
m_amount = sanitize(new_amount);
Q_EMIT amountChanged();
const qint64 wholePart = absSat / COIN;
const qint64 fracInt = absSat % COIN;
QString fracPart = QString("%1").arg(fracInt, 8, 10, QLatin1Char('0'));

QString result = QString::number(wholePart) + '.' + fracPart;
if (negative) {
result.prepend('-');
}
return result;
}

QString BitcoinAmount::toSatoshis(const QString& text) const
QString BitcoinAmount::toDisplay() const
{
if (!m_isSet) {
return "";
}
if (m_unit == Unit::SAT) {
return text;
return QString::number(m_satoshi);
} else {
return convert(text, m_unit);
return satsToBtcString(m_satoshi);
}
}

long long BitcoinAmount::toSatoshis(QString& amount, const Unit unit)
qint64 BitcoinAmount::btcToSats(const QString& btcSanitized)
{
int num_decimals = decimals(unit);
if (btcSanitized.isEmpty() || btcSanitized == ".") return 0;

QStringList parts = amount.remove(' ').split(".");
QString cleaned = btcSanitized;
if (cleaned.startsWith('.')) cleaned.prepend('0');

QString whole = parts[0];
QString decimals;

if(parts.size() > 1)
{
decimals = parts[1];
QStringList parts = cleaned.split('.');
const qint64 whole = parts[0].isEmpty() ? 0 : parts[0].toLongLong();
qint64 frac = 0;
if (parts.size() == 2) {
frac = parts[1].leftJustified(8, '0').toLongLong();
}
QString str = whole + decimals.leftJustified(num_decimals, '0', true);

return str.toLongLong();
return whole * COIN + frac;
}

QString BitcoinAmount::convert(const QString& amount, Unit unit) const
void BitcoinAmount::fromDisplay(const QString& text)
{
if (amount == "") {
return amount;
}

QString result = amount;
int decimalPosition = result.indexOf(".");

if (decimalPosition == -1) {
decimalPosition = result.length();
result.append(".");
if (text.trimmed().isEmpty()) {
clear();
return;
}

if (unit == Unit::BTC) {
int numDigitsAfterDecimal = result.length() - decimalPosition - 1;
if (numDigitsAfterDecimal < 8) {
result.append(QString(8 - numDigitsAfterDecimal, '0'));
}
result.remove(decimalPosition, 1);

while (result.startsWith('0') && result.length() > 1) {
result.remove(0, 1);
}
} else if (unit == Unit::SAT) {
result.remove(decimalPosition, 1);
int newDecimalPosition = decimalPosition - 8;
if (newDecimalPosition < 1) {
result = QString("0").repeated(-newDecimalPosition) + result;
newDecimalPosition = 0;
}
result.insert(newDecimalPosition, ".");

while (result.endsWith('0') && result.contains('.')) {
result.chop(1);
}
if (result.endsWith('.')) {
result.chop(1);
}
if (result.startsWith('.')) {
result.insert(0, "0");
}
qint64 newSat = 0;
if (m_unit == Unit::BTC) {
QString sanitized = sanitize(text);
newSat = btcToSats(sanitized);
} else {
QString digitsOnly = text;
digitsOnly.remove(QRegExp("[^0-9]"));
newSat = digitsOnly.trimmed().isEmpty() ? 0 : digitsOnly.toLongLong();
}
setSatoshi(newSat);
}

return result;
void BitcoinAmount::format()
{
Q_EMIT displayChanged();
}
37 changes: 22 additions & 15 deletions src/qml/bitcoinamount.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2024 The Bitcoin Core developers
// Copyright (c) 2024-2025 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

Expand All @@ -15,8 +15,8 @@ class BitcoinAmount : public QObject
Q_OBJECT
Q_PROPERTY(Unit unit READ unit WRITE setUnit NOTIFY unitChanged)
Q_PROPERTY(QString unitLabel READ unitLabel NOTIFY unitChanged)
Q_PROPERTY(QString amount READ amount WRITE setAmount NOTIFY amountChanged)
Q_PROPERTY(QString satoshiAmount READ satoshiAmount NOTIFY amountChanged)
Q_PROPERTY(QString display READ toDisplay WRITE fromDisplay NOTIFY displayChanged)
Q_PROPERTY(qint64 satoshi READ satoshi WRITE setSatoshi NOTIFY amountChanged)

public:
enum class Unit {
Expand All @@ -30,27 +30,34 @@ class BitcoinAmount : public QObject
Unit unit() const;
void setUnit(Unit unit);
QString unitLabel() const;
QString amount() const;
void setAmount(const QString& new_amount);
QString satoshiAmount() const;

QString toDisplay() const;
void fromDisplay(const QString& new_amount);
qint64 satoshi() const;
void setSatoshi(qint64 new_amount);

bool isSet() const { return m_isSet; }

Q_INVOKABLE void format();

static QString satsToBtcString(qint64 sat);

public Q_SLOTS:
QString sanitize(const QString& text);
QString convert(const QString& text, Unit unit) const;
QString toSatoshis(const QString& text) const;
void flipUnit();
void clear();

Q_SIGNALS:
void unitChanged();
void unitLabelChanged();
void amountChanged();
void displayChanged();

private:
long long toSatoshis(QString &amount, const Unit unit);
int decimals(Unit unit);
QString sanitize(const QString& text);
static qint64 btcToSats(const QString& btc);

Unit m_unit;
QString m_unitLabel;
QString m_amount;
qint64 m_satoshi{0};
bool m_isSet{false};
Unit m_unit{Unit::BTC};
};

#endif // BITCOIN_QML_BITCOINAMOUNT_H
Loading
Loading