Skip to content

Add Multiple Recipients option to the Send form #450

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 13 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
10 changes: 8 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 Down Expand Up @@ -379,6 +383,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 +425,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 +489,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
4 changes: 3 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 Down Expand Up @@ -126,6 +127,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
155 changes: 77 additions & 78 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 @@ -58,97 +72,82 @@ 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 amountChanged();
}

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);

QStringList parts = amount.remove(' ').split(".");
if (btcSanitized.isEmpty() || btcSanitized == ".") return 0;

QString whole = parts[0];
QString decimals;
QString cleaned = btcSanitized;
if (cleaned.startsWith('.')) cleaned.prepend('0');

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();
}

return result;
setSatoshi(newSat);
}
32 changes: 17 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 amountChanged)
Q_PROPERTY(qint64 satoshi READ satoshi WRITE setSatoshi NOTIFY amountChanged)

public:
enum class Unit {
Expand All @@ -30,27 +30,29 @@ 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);

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();

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