Raven Core  3.0.0
P2P Digital Currency
paymentserver.h
Go to the documentation of this file.
1 // Copyright (c) 2011-2016 The Bitcoin Core developers
2 // Copyright (c) 2017-2019 The Raven Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef RAVEN_QT_PAYMENTSERVER_H
7 #define RAVEN_QT_PAYMENTSERVER_H
8 
9 // This class handles payment requests from clicking on
10 // raven: URIs
11 //
12 // This is somewhat tricky, because we have to deal with
13 // the situation where the user clicks on a link during
14 // startup/initialization, when the splash-screen is up
15 // but the main window (and the Send Coins tab) is not.
16 //
17 // So, the strategy is:
18 //
19 // Create the server, and register the event handler,
20 // when the application is created. Save any URIs
21 // received at or during startup in a list.
22 //
23 // When startup is finished and the main window is
24 // shown, a signal is sent to slot uiReady(), which
25 // emits a receivedURI() signal for any payment
26 // requests that happened during startup.
27 //
28 // After startup, receivedURI() happens as usual.
29 //
30 // This class has one more feature: a static
31 // method that finds URIs passed in the command line
32 // and, if a server is running in another process,
33 // sends them to the server.
34 //
35 
36 #include "paymentrequestplus.h"
37 #include "walletmodel.h"
38 
39 #include <QObject>
40 #include <QString>
41 
42 class OptionsModel;
43 
44 class CWallet;
45 
46 QT_BEGIN_NAMESPACE
47 class QApplication;
48 class QByteArray;
49 class QLocalServer;
50 class QNetworkAccessManager;
51 class QNetworkReply;
52 class QSslError;
53 class QUrl;
54 QT_END_NAMESPACE
55 
56 // BIP70 max payment request size in bytes (DoS protection)
57 static const qint64 BIP70_MAX_PAYMENTREQUEST_SIZE = 50000;
58 
59 class PaymentServer : public QObject
60 {
61  Q_OBJECT
62 
63 public:
64  // Parse URIs on command line
65  // Returns false on error
66  static void ipcParseCommandLine(int argc, char *argv[]);
67 
68  // Returns true if there were URIs on the command line
69  // which were successfully sent to an already-running
70  // process.
71  // Note: if a payment request is given, SelectParams(MAIN/TESTNET)
72  // will be called so we startup in the right mode.
73  static bool ipcSendCommandLine();
74 
75  // parent should be QApplication object
76  explicit PaymentServer(QObject* parent, bool startLocalServer = true);
78 
79  // Load root certificate authorities. Pass nullptr (default)
80  // to read from the file specified in the -rootcertificates setting,
81  // or, if that's not set, to use the system default root certificates.
82  // If you pass in a store, you should not X509_STORE_free it: it will be
83  // freed either at exit or when another set of CAs are loaded.
84  static void LoadRootCAs(X509_STORE* store = nullptr);
85 
86  // Return certificate store
87  static X509_STORE* getCertStore();
88 
89  // OptionsModel is used for getting proxy settings and display unit
91 
92  // Verify that the payment request network matches the client network
93  static bool verifyNetwork(const payments::PaymentDetails& requestDetails);
94  // Verify if the payment request is expired
95  static bool verifyExpired(const payments::PaymentDetails& requestDetails);
96  // Verify the payment request size is valid as per BIP70
97  static bool verifySize(qint64 requestSize);
98  // Verify the payment request amount is valid
99  static bool verifyAmount(const CAmount& requestAmount);
100 
101 Q_SIGNALS:
102  // Fired when a valid payment request is received
104 
105  // Fired when a valid PaymentACK is received
106  void receivedPaymentACK(const QString &paymentACKMsg);
107 
108  // Fired when a message should be reported to the user
109  void message(const QString &title, const QString &message, unsigned int style);
110 
111 public Q_SLOTS:
112  // Signal this when the main window's UI is ready
113  // to display payment requests to the user
114  void uiReady();
115 
116  // Submit Payment message to a merchant, get back PaymentACK:
117  void fetchPaymentACK(CWallet* wallet, const SendCoinsRecipient& recipient, QByteArray transaction);
118 
119  // Handle an incoming URI, URI with local file scheme or file
120  void handleURIOrFile(const QString& s);
121 
122 private Q_SLOTS:
123  void handleURIConnection();
124  void netRequestFinished(QNetworkReply*);
125  void reportSslErrors(QNetworkReply*, const QList<QSslError> &);
126  void handlePaymentACK(const QString& paymentACKMsg);
127 
128 protected:
129  // Constructor registers this on the parent QApplication to
130  // receive QEvent::FileOpen and QEvent:Drop events
131  bool eventFilter(QObject *object, QEvent *event);
132 
133 private:
134  static bool readPaymentRequestFromFile(const QString& filename, PaymentRequestPlus& request);
135  bool processPaymentRequest(const PaymentRequestPlus& request, SendCoinsRecipient& recipient);
136  void fetchRequest(const QUrl& url);
137 
138  // Setup networking
139  void initNetManager();
140 
141  bool saveURIs; // true during startup
142  QLocalServer* uriServer;
143 
144  QNetworkAccessManager* netManager; // Used to fetch payment requests
145 
147 };
148 
149 #endif // RAVEN_QT_PAYMENTSERVER_H
static void LoadRootCAs(X509_STORE *store=nullptr)
static bool verifyAmount(const CAmount &requestAmount)
void message(const QString &title, const QString &message, unsigned int style)
static bool verifyNetwork(const payments::PaymentDetails &requestDetails)
void setOptionsModel(OptionsModel *optionsModel)
bool processPaymentRequest(const PaymentRequestPlus &request, SendCoinsRecipient &recipient)
bool eventFilter(QObject *object, QEvent *event)
void handlePaymentACK(const QString &paymentACKMsg)
void receivedPaymentACK(const QString &paymentACKMsg)
void receivedPaymentRequest(SendCoinsRecipient)
static void ipcParseCommandLine(int argc, char *argv[])
QLocalServer * uriServer
void handleURIOrFile(const QString &s)
static bool ipcSendCommandLine()
int64_t CAmount
Amount in corbies (Can be negative)
Definition: amount.h:13
const char * url
Definition: rpcconsole.cpp:65
void netRequestFinished(QNetworkReply *)
static bool readPaymentRequestFromFile(const QString &filename, PaymentRequestPlus &request)
void fetchRequest(const QUrl &url)
static bool verifyExpired(const payments::PaymentDetails &requestDetails)
PaymentServer(QObject *parent, bool startLocalServer=true)
static X509_STORE * getCertStore()
QNetworkAccessManager * netManager
void fetchPaymentACK(CWallet *wallet, const SendCoinsRecipient &recipient, QByteArray transaction)
void reportSslErrors(QNetworkReply *, const QList< QSslError > &)
Interface from Qt to configuration data structure for Raven client.
Definition: optionsmodel.h:23
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
Definition: wallet.h:673
void handleURIConnection()
static bool verifySize(qint64 requestSize)
OptionsModel * optionsModel