Skip to content

Commit 03042fb

Browse files
committed
qml: Add PaymentRequest to WalletQmlModel
1 parent 1705458 commit 03042fb

File tree

5 files changed

+207
-1
lines changed

5 files changed

+207
-1
lines changed

src/Makefile.qt.include

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ QT_MOC_CPP = \
4343
qml/models/moc_networktraffictower.cpp \
4444
qml/models/moc_nodemodel.cpp \
4545
qml/models/moc_options_model.cpp \
46+
qml/models/moc_paymentrequest.cpp \
4647
qml/models/moc_peerdetailsmodel.cpp \
4748
qml/models/moc_peerlistsortproxy.cpp \\
4849
qml/models/moc_sendrecipient.cpp \
@@ -136,6 +137,7 @@ BITCOIN_QT_H = \
136137
qml/models/networktraffictower.h \
137138
qml/models/nodemodel.h \
138139
qml/models/options_model.h \
140+
qml/models/paymentrequest.h \
139141
qml/models/peerdetailsmodel.h \
140142
qml/models/peerlistsortproxy.h \
141143
qml/models/transaction.h \
@@ -338,6 +340,7 @@ BITCOIN_QML_BASE_CPP = \
338340
qml/models/networktraffictower.cpp \
339341
qml/models/nodemodel.cpp \
340342
qml/models/options_model.cpp \
343+
qml/models/paymentrequest.cpp \
341344
qml/models/peerdetailsmodel.cpp \
342345
qml/models/peerlistsortproxy.cpp \
343346
qml/models/transaction.cpp \

src/qml/models/paymentrequest.cpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Copyright (c) 2024-2025 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <qml/bitcoinamount.h>
6+
#include <qml/models/paymentrequest.h>
7+
8+
#include <addresstype.h>
9+
#include <key_io.h>
10+
11+
PaymentRequest::PaymentRequest(QObject* parent)
12+
: QObject(parent)
13+
{
14+
m_amount = new BitcoinAmount(this);
15+
m_label = "";
16+
m_message = "";
17+
m_id = "";
18+
}
19+
20+
QString PaymentRequest::address() const
21+
{
22+
return QString::fromStdString(EncodeDestination(m_destination));
23+
}
24+
25+
QString PaymentRequest::label() const
26+
{
27+
return m_label;
28+
}
29+
30+
void PaymentRequest::setLabel(const QString& label)
31+
{
32+
if (m_label == label)
33+
return;
34+
35+
m_label = label;
36+
Q_EMIT labelChanged();
37+
}
38+
39+
QString PaymentRequest::message() const
40+
{
41+
return m_message;
42+
}
43+
44+
void PaymentRequest::setMessage(const QString& message)
45+
{
46+
if (m_message == message)
47+
return;
48+
49+
m_message = message;
50+
Q_EMIT messageChanged();
51+
}
52+
53+
BitcoinAmount* PaymentRequest::amount() const
54+
{
55+
return m_amount;
56+
}
57+
58+
QString PaymentRequest::id() const
59+
{
60+
return m_id;
61+
}
62+
63+
void PaymentRequest::setId(const unsigned int id)
64+
{
65+
m_id = QString::number(id);
66+
Q_EMIT idChanged();
67+
}
68+
69+
void PaymentRequest::setDestination(const CTxDestination& destination)
70+
{
71+
m_destination = destination;
72+
Q_EMIT addressChanged();
73+
}
74+
75+
CTxDestination PaymentRequest::destination() const
76+
{
77+
return m_destination;
78+
}
79+
80+
void PaymentRequest::setAmountError(const QString& error)
81+
{
82+
if (m_amountError == error)
83+
return;
84+
85+
m_amountError = error;
86+
Q_EMIT amountErrorChanged();
87+
}
88+
89+
QString PaymentRequest::amountError() const
90+
{
91+
return m_amountError;
92+
}
93+
94+
void PaymentRequest::clear()
95+
{
96+
m_destination = CNoDestination();
97+
m_label.clear();
98+
m_message.clear();
99+
m_amount->clear();
100+
m_amountError.clear();
101+
m_id.clear();
102+
Q_EMIT addressChanged();
103+
Q_EMIT labelChanged();
104+
Q_EMIT messageChanged();
105+
Q_EMIT amountErrorChanged();
106+
Q_EMIT idChanged();
107+
}

src/qml/models/paymentrequest.h

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) 2024-2025 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_QML_MODELS_PAYMENTREQUEST_H
6+
#define BITCOIN_QML_MODELS_PAYMENTREQUEST_H
7+
8+
#include <qml/bitcoinamount.h>
9+
10+
#include <addresstype.h>
11+
12+
#include <QObject>
13+
14+
class PaymentRequest : public QObject
15+
{
16+
Q_OBJECT
17+
Q_PROPERTY(QString address READ address NOTIFY addressChanged)
18+
Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
19+
Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
20+
Q_PROPERTY(BitcoinAmount* amount READ amount CONSTANT)
21+
Q_PROPERTY(QString amountError READ amountError NOTIFY amountErrorChanged)
22+
Q_PROPERTY(QString id READ id NOTIFY idChanged)
23+
24+
public:
25+
explicit PaymentRequest(QObject* parent = nullptr);
26+
27+
QString address() const;
28+
29+
QString label() const;
30+
void setLabel(const QString& label);
31+
32+
QString message() const;
33+
void setMessage(const QString& message);
34+
35+
BitcoinAmount* amount() const;
36+
QString amountError() const;
37+
void setAmountError(const QString& error);
38+
39+
QString id() const;
40+
void setId(unsigned int id);
41+
42+
void setDestination(const CTxDestination& destination);
43+
CTxDestination destination() const;
44+
45+
Q_INVOKABLE void clear();
46+
47+
Q_SIGNALS:
48+
void addressChanged();
49+
void labelChanged();
50+
void messageChanged();
51+
void amountErrorChanged();
52+
void idChanged();
53+
54+
private:
55+
CTxDestination m_destination;
56+
QString m_label;
57+
QString m_message;
58+
QString m_amountError;
59+
BitcoinAmount* m_amount;
60+
QString m_id;
61+
};
62+
63+
#endif // BITCOIN_QML_MODELS_PAYMENTREQUEST_H

src/qml/models/walletqmlmodel.cpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11

2-
// Copyright (c) 2024 The Bitcoin Core developers
2+
// Copyright (c) 2024-2025 The Bitcoin Core developers
33
// Distributed under the MIT software license, see the accompanying
44
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
55

66
#include <qml/models/walletqmlmodel.h>
77

88
#include <qml/models/activitylistmodel.h>
9+
#include <qml/models/paymentrequest.h>
910
#include <qml/models/sendrecipient.h>
1011
#include <qml/models/sendrecipientslistmodel.h>
1112
#include <qml/models/walletqmlmodeltransaction.h>
@@ -20,6 +21,8 @@
2021

2122
#include <QTimer>
2223

24+
unsigned int WalletQmlModel::m_next_payment_request_id{1};
25+
2326
WalletQmlModel::WalletQmlModel(std::unique_ptr<interfaces::Wallet> wallet, QObject *parent)
2427
: QObject(parent)
2528
{
@@ -71,6 +74,29 @@ QString WalletQmlModel::name() const
7174
return QString::fromStdString(m_wallet->getWalletName());
7275
}
7376

77+
void WalletQmlModel::commitPaymentRequest()
78+
{
79+
if (!m_current_payment_request) {
80+
return;
81+
}
82+
83+
if (m_current_payment_request->id().isEmpty()) {
84+
m_current_payment_request->setId(m_next_payment_request_id++);
85+
}
86+
87+
if (m_current_payment_request->address().isEmpty()) {
88+
// TODO: handle issues with getting the new address (wallet unlock?)
89+
auto destination = m_wallet->getNewDestination(OutputType::BECH32M,
90+
m_current_payment_request->label().toStdString())
91+
.value();
92+
std::string address = EncodeDestination(destination);
93+
m_current_payment_request->setDestination(destination);
94+
}
95+
96+
m_wallet->setAddressReceiveRequest(
97+
m_current_payment_request->destination(), m_current_payment_request->id().toStdString(), m_current_payment_request->message().toStdString());
98+
}
99+
74100
std::set<interfaces::WalletTx> WalletQmlModel::getWalletTxs() const
75101
{
76102
if (!m_wallet) {

src/qml/models/walletqmlmodel.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <qml/models/activitylistmodel.h>
99
#include <qml/models/coinslistmodel.h>
10+
#include <qml/models/paymentrequest.h>
1011
#include <qml/models/sendrecipient.h>
1112
#include <qml/models/sendrecipientslistmodel.h>
1213
#include <qml/models/walletqmlmodeltransaction.h>
@@ -30,6 +31,7 @@ class WalletQmlModel : public QObject
3031
Q_PROPERTY(CoinsListModel* coinsListModel READ coinsListModel CONSTANT)
3132
Q_PROPERTY(SendRecipientsListModel* recipients READ sendRecipientList CONSTANT)
3233
Q_PROPERTY(WalletQmlModelTransaction* currentTransaction READ currentTransaction NOTIFY currentTransactionChanged)
34+
Q_PROPERTY(PaymentRequest* currentPaymentRequest READ currentPaymentRequest CONSTANT)
3335

3436
public:
3537
WalletQmlModel(std::unique_ptr<interfaces::Wallet> wallet, QObject* parent = nullptr);
@@ -39,6 +41,8 @@ class WalletQmlModel : public QObject
3941
QString name() const;
4042
QString balance() const;
4143
CAmount balanceSatoshi() const;
44+
Q_INVOKABLE void commitPaymentRequest();
45+
PaymentRequest* currentPaymentRequest() const { return m_current_payment_request; }
4246

4347
ActivityListModel* activityListModel() const { return m_activity_list_model; }
4448
CoinsListModel* coinsListModel() const { return m_coins_list_model; }
@@ -73,7 +77,10 @@ class WalletQmlModel : public QObject
7377
void currentTransactionChanged();
7478

7579
private:
80+
static unsigned int m_next_payment_request_id;
81+
7682
std::unique_ptr<interfaces::Wallet> m_wallet;
83+
PaymentRequest* m_current_payment_request{nullptr};
7784
ActivityListModel* m_activity_list_model{nullptr};
7885
CoinsListModel* m_coins_list_model{nullptr};
7986
SendRecipientsListModel* m_send_recipients{nullptr};

0 commit comments

Comments
 (0)