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