You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

paymentserver.h 4.9KB

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