Skip to content

Commit 3cab2ce

Browse files
ryanofskyjnewbery
authored andcommitted
Remove direct bitcoin calls from qt/paymentserver.cpp
1 parent 3ec2ebc commit 3cab2ce

File tree

8 files changed

+29
-26
lines changed

8 files changed

+29
-26
lines changed

src/interface/node.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class NodeImpl : public Node
5656
bool softSetArg(const std::string& arg, const std::string& value) override { return gArgs.SoftSetArg(arg, value); }
5757
bool softSetBoolArg(const std::string& arg, bool value) override { return gArgs.SoftSetBoolArg(arg, value); }
5858
void selectParams(const std::string& network) override { SelectParams(network); }
59+
std::string getNetwork() override { return Params().NetworkIDString(); }
5960
void initLogging() override { InitLogging(); }
6061
void initParameterInteraction() override { InitParameterInteraction(); }
6162
std::string getWarnings(const std::string& type) override { return GetWarnings(type); }

src/interface/node.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class Node
5454
//! Choose network parameters.
5555
virtual void selectParams(const std::string& network) = 0;
5656

57+
//! Get network name.
58+
virtual std::string getNetwork() = 0;
59+
5760
//! Init logging.
5861
virtual void initLogging() = 0;
5962

src/qt/bitcoin.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,8 +476,8 @@ void BitcoinApplication::initializeResult(bool success)
476476
fFirstWallet = false;
477477
}
478478

479-
connect(walletModel, SIGNAL(coinsSent(CWallet*,SendCoinsRecipient,QByteArray)),
480-
paymentServer, SLOT(fetchPaymentACK(CWallet*,const SendCoinsRecipient&,QByteArray)));
479+
connect(walletModel, SIGNAL(coinsSent(WalletModel*,SendCoinsRecipient,QByteArray)),
480+
paymentServer, SLOT(fetchPaymentACK(WalletModel*,const SendCoinsRecipient&,QByteArray)));
481481

482482
m_wallet_models.push_back(walletModel);
483483
}
@@ -638,7 +638,7 @@ int main(int argc, char *argv[])
638638
}
639639
#ifdef ENABLE_WALLET
640640
// Parse URIs on command line -- this can affect Params()
641-
PaymentServer::ipcParseCommandLine(argc, argv);
641+
PaymentServer::ipcParseCommandLine(*node, argc, argv);
642642
#endif
643643

644644
QScopedPointer<const NetworkStyle> networkStyle(NetworkStyle::instantiate(QString::fromStdString(Params().NetworkIDString())));

src/qt/paymentserver.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <qt/optionsmodel.h>
1010

1111
#include <chainparams.h>
12+
#include <interface/node.h>
1213
#include <policy/policy.h>
1314
#include <key_io.h>
1415
#include <ui_interface.h>
@@ -199,7 +200,7 @@ void PaymentServer::LoadRootCAs(X509_STORE* _store)
199200
// Warning: ipcSendCommandLine() is called early in init,
200201
// so don't use "Q_EMIT message()", but "QMessageBox::"!
201202
//
202-
void PaymentServer::ipcParseCommandLine(int argc, char* argv[])
203+
void PaymentServer::ipcParseCommandLine(interface::Node& node, int argc, char* argv[])
203204
{
204205
for (int i = 1; i < argc; i++)
205206
{
@@ -221,11 +222,11 @@ void PaymentServer::ipcParseCommandLine(int argc, char* argv[])
221222
auto tempChainParams = CreateChainParams(CBaseChainParams::MAIN);
222223

223224
if (IsValidDestinationString(r.address.toStdString(), *tempChainParams)) {
224-
SelectParams(CBaseChainParams::MAIN);
225+
node.selectParams(CBaseChainParams::MAIN);
225226
} else {
226227
tempChainParams = CreateChainParams(CBaseChainParams::TESTNET);
227228
if (IsValidDestinationString(r.address.toStdString(), *tempChainParams)) {
228-
SelectParams(CBaseChainParams::TESTNET);
229+
node.selectParams(CBaseChainParams::TESTNET);
229230
}
230231
}
231232
}
@@ -239,11 +240,11 @@ void PaymentServer::ipcParseCommandLine(int argc, char* argv[])
239240
{
240241
if (request.getDetails().network() == "main")
241242
{
242-
SelectParams(CBaseChainParams::MAIN);
243+
node.selectParams(CBaseChainParams::MAIN);
243244
}
244245
else if (request.getDetails().network() == "test")
245246
{
246-
SelectParams(CBaseChainParams::TESTNET);
247+
node.selectParams(CBaseChainParams::TESTNET);
247248
}
248249
}
249250
}
@@ -526,7 +527,7 @@ bool PaymentServer::processPaymentRequest(const PaymentRequestPlus& request, Sen
526527

527528
if (request.IsInitialized()) {
528529
// Payment request network matches client network?
529-
if (!verifyNetwork(request.getDetails())) {
530+
if (!verifyNetwork(optionsModel->node(), request.getDetails())) {
530531
Q_EMIT message(tr("Payment request rejected"), tr("Payment request network doesn't match client network."),
531532
CClientUIInterface::MSG_ERROR);
532533

@@ -583,7 +584,7 @@ bool PaymentServer::processPaymentRequest(const PaymentRequestPlus& request, Sen
583584

584585
// Extract and check amounts
585586
CTxOut txOut(sendingTo.second, sendingTo.first);
586-
if (IsDust(txOut, ::dustRelayFee)) {
587+
if (IsDust(txOut, optionsModel->node().getDustRelayFee())) {
587588
Q_EMIT message(tr("Payment request error"), tr("Requested payment amount of %1 is too small (considered dust).")
588589
.arg(BitcoinUnits::formatWithUnit(optionsModel->getDisplayUnit(), sendingTo.second)),
589590
CClientUIInterface::MSG_ERROR);
@@ -621,7 +622,7 @@ void PaymentServer::fetchRequest(const QUrl& url)
621622
netManager->get(netRequest);
622623
}
623624

624-
void PaymentServer::fetchPaymentACK(CWallet* wallet, const SendCoinsRecipient& recipient, QByteArray transaction)
625+
void PaymentServer::fetchPaymentACK(WalletModel* walletModel, const SendCoinsRecipient& recipient, QByteArray transaction)
625626
{
626627
const payments::PaymentDetails& details = recipient.paymentRequest.getDetails();
627628
if (!details.has_payment_url())
@@ -640,17 +641,17 @@ void PaymentServer::fetchPaymentACK(CWallet* wallet, const SendCoinsRecipient& r
640641

641642
// Create a new refund address, or re-use:
642643
CPubKey newKey;
643-
if (wallet->GetKeyFromPool(newKey)) {
644+
if (walletModel->wallet().getKeyFromPool(false /* internal */, newKey)) {
644645
// BIP70 requests encode the scriptPubKey directly, so we are not restricted to address
645646
// types supported by the receiver. As a result, we choose the address format we also
646647
// use for change. Despite an actual payment and not change, this is a close match:
647648
// it's the output type we use subject to privacy issues, but not restricted by what
648649
// other software supports.
649-
const OutputType change_type = wallet->m_default_change_type != OutputType::NONE ? wallet->m_default_change_type : wallet->m_default_address_type;
650-
wallet->LearnRelatedScripts(newKey, change_type);
650+
const OutputType change_type = walletModel->wallet().getDefaultChangeType() != OutputType::NONE ? walletModel->wallet().getDefaultChangeType() : walletModel->wallet().getDefaultAddressType();
651+
walletModel->wallet().learnRelatedScripts(newKey, change_type);
651652
CTxDestination dest = GetDestinationForKey(newKey, change_type);
652653
std::string label = tr("Refund from %1").arg(recipient.authenticatedMerchant).toStdString();
653-
wallet->SetAddressBook(dest, label, "refund");
654+
walletModel->wallet().setAddressBook(dest, label, "refund");
654655

655656
CScript s = GetScriptForDestination(dest);
656657
payments::Output* refund_to = payment.add_refund_to();
@@ -758,14 +759,14 @@ void PaymentServer::handlePaymentACK(const QString& paymentACKMsg)
758759
Q_EMIT message(tr("Payment acknowledged"), paymentACKMsg, CClientUIInterface::ICON_INFORMATION | CClientUIInterface::MODAL);
759760
}
760761

761-
bool PaymentServer::verifyNetwork(const payments::PaymentDetails& requestDetails)
762+
bool PaymentServer::verifyNetwork(interface::Node& node, const payments::PaymentDetails& requestDetails)
762763
{
763-
bool fVerified = requestDetails.network() == Params().NetworkIDString();
764+
bool fVerified = requestDetails.network() == node.getNetwork();
764765
if (!fVerified) {
765766
qWarning() << QString("PaymentServer::%1: Payment request network \"%2\" doesn't match client network \"%3\".")
766767
.arg(__func__)
767768
.arg(QString::fromStdString(requestDetails.network()))
768-
.arg(QString::fromStdString(Params().NetworkIDString()));
769+
.arg(QString::fromStdString(node.getNetwork()));
769770
}
770771
return fVerified;
771772
}

src/qt/paymentserver.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040

4141
class OptionsModel;
4242

43-
class CWallet;
44-
4543
QT_BEGIN_NAMESPACE
4644
class QApplication;
4745
class QByteArray;
@@ -62,7 +60,7 @@ class PaymentServer : public QObject
6260
public:
6361
// Parse URIs on command line
6462
// Returns false on error
65-
static void ipcParseCommandLine(int argc, char *argv[]);
63+
static void ipcParseCommandLine(interface::Node& node, int argc, char *argv[]);
6664

6765
// Returns true if there were URIs on the command line
6866
// which were successfully sent to an already-running
@@ -89,7 +87,7 @@ class PaymentServer : public QObject
8987
void setOptionsModel(OptionsModel *optionsModel);
9088

9189
// Verify that the payment request network matches the client network
92-
static bool verifyNetwork(const payments::PaymentDetails& requestDetails);
90+
static bool verifyNetwork(interface::Node& node, const payments::PaymentDetails& requestDetails);
9391
// Verify if the payment request is expired
9492
static bool verifyExpired(const payments::PaymentDetails& requestDetails);
9593
// Verify the payment request size is valid as per BIP70
@@ -113,7 +111,7 @@ public Q_SLOTS:
113111
void uiReady();
114112

115113
// Submit Payment message to a merchant, get back PaymentACK:
116-
void fetchPaymentACK(CWallet* wallet, const SendCoinsRecipient& recipient, QByteArray transaction);
114+
void fetchPaymentACK(WalletModel* walletModel, const SendCoinsRecipient& recipient, QByteArray transaction);
117115

118116
// Handle an incoming URI, URI with local file scheme or file
119117
void handleURIOrFile(const QString& s);

src/qt/test/paymentservertests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ void PaymentServerTests::paymentServerTests()
147147
// Ensure the request is initialized, because network "main" is default, even for
148148
// uninitialized payment requests and that will fail our test here.
149149
QVERIFY(r.paymentRequest.IsInitialized());
150-
QCOMPARE(PaymentServer::verifyNetwork(r.paymentRequest.getDetails()), false);
150+
QCOMPARE(PaymentServer::verifyNetwork(*node, r.paymentRequest.getDetails()), false);
151151

152152
// Expired payment request (expires is set to 1 = 1970-01-01 00:00:01):
153153
data = DecodeBase64(paymentrequest2_cert2_BASE64);

src/qt/walletmodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(WalletModelTransaction &tran
295295
}
296296
}
297297
}
298-
Q_EMIT coinsSent(cwallet, rcp, transaction_array);
298+
Q_EMIT coinsSent(this, rcp, transaction_array);
299299
}
300300

301301
checkBalanceChanged(m_wallet->getBalances()); // update balance immediately, otherwise there could be a short noticeable delay until pollBalanceChanged hits

src/qt/walletmodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ class WalletModel : public QObject
253253
void message(const QString &title, const QString &message, unsigned int style);
254254

255255
// Coins sent: from wallet, to recipient, in (serialized) transaction:
256-
void coinsSent(CWallet* wallet, SendCoinsRecipient recipient, QByteArray transaction);
256+
void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
257257

258258
// Show progress dialog e.g. for rescan
259259
void showProgress(const QString &title, int nProgress);

0 commit comments

Comments
 (0)