2021-01-05 00:25:23 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2020-2021 Tom Zander <tom@flowee.org>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
#ifndef PAYMENTREQUEST_H
|
|
|
|
|
#define PAYMENTREQUEST_H
|
|
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
2021-01-06 15:26:46 +01:00
|
|
|
#include <primitives/pubkey.h>
|
|
|
|
|
|
2021-01-05 00:25:23 +01:00
|
|
|
class Wallet;
|
2021-04-21 17:17:47 +02:00
|
|
|
class AccountInfo;
|
2021-01-05 00:25:23 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This is a user-created request for payment to a specific wallet/address.
|
|
|
|
|
* The request created by the user is similar to an invoice, but when the payment
|
|
|
|
|
* is fulfilled the data is transferred to the wallet and this request is deleted.
|
|
|
|
|
*/
|
|
|
|
|
class PaymentRequest : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
Q_PROPERTY(QString message READ message WRITE setMessage NOTIFY messageChanged)
|
|
|
|
|
Q_PROPERTY(QString qr READ qrCodeString NOTIFY qrCodeStringChanged)
|
|
|
|
|
Q_PROPERTY(double amount READ amountFP WRITE setAmountFP NOTIFY amountChanged)
|
2021-01-13 17:43:10 +01:00
|
|
|
Q_PROPERTY(double amountSeen READ amountSeenFP NOTIFY amountSeenChanged)
|
2021-01-06 15:26:46 +01:00
|
|
|
Q_PROPERTY(bool legacy READ useLegacyAddress WRITE setUseLegacyAddress NOTIFY legacyChanged)
|
2021-01-07 20:10:09 +01:00
|
|
|
Q_PROPERTY(SaveState saveState READ saveState WRITE setSaveState NOTIFY saveStateChanged)
|
2021-01-14 12:41:47 +01:00
|
|
|
Q_PROPERTY(PaymentState state READ paymentState NOTIFY paymentStateChanged)
|
2021-05-05 12:42:26 +02:00
|
|
|
Q_PROPERTY(bool stored READ stored WRITE setStored NOTIFY storedChanged)
|
2021-01-05 00:25:23 +01:00
|
|
|
public:
|
2021-01-07 20:10:09 +01:00
|
|
|
/// The state of this payment
|
|
|
|
|
enum PaymentState {
|
|
|
|
|
Unpaid, //< we have not seen any payment yet.
|
2021-01-13 17:43:10 +01:00
|
|
|
DoubleSpentSeen,//< We have seen a double-spend-proof (DSP). This is bad.
|
2021-01-07 20:10:09 +01:00
|
|
|
PaymentSeen, //< A payment has been seen, there is still risk.
|
|
|
|
|
PaymentSeenOk, //< A payment has been seen, we waited and no DSP came.
|
|
|
|
|
Confirmed //< We got paid.
|
2021-01-05 00:25:23 +01:00
|
|
|
};
|
2021-01-07 20:10:09 +01:00
|
|
|
enum SaveState {
|
2021-01-15 14:01:01 +01:00
|
|
|
Temporary,
|
2021-05-05 12:42:26 +02:00
|
|
|
Stored
|
2021-01-07 20:10:09 +01:00
|
|
|
};
|
|
|
|
|
Q_ENUM(SaveState PaymentState)
|
|
|
|
|
|
|
|
|
|
/// Dummy constructor.
|
|
|
|
|
PaymentRequest(QObject *parent = nullptr);
|
2021-01-05 00:25:23 +01:00
|
|
|
explicit PaymentRequest(Wallet *wallet, QObject *parent = nullptr);
|
|
|
|
|
~PaymentRequest();
|
|
|
|
|
|
|
|
|
|
QString message() const;
|
|
|
|
|
void setMessage(const QString &message);
|
|
|
|
|
|
2021-01-13 17:43:10 +01:00
|
|
|
/// Set the amount requested (as floating point) in sats.
|
2021-01-05 00:25:23 +01:00
|
|
|
void setAmountFP(double amount);
|
2021-01-13 17:43:10 +01:00
|
|
|
/// return the amount requested (in sats)
|
2021-01-05 00:25:23 +01:00
|
|
|
double amountFP() const;
|
2021-01-13 17:43:10 +01:00
|
|
|
/// return the amount received (in sats)
|
|
|
|
|
double amountSeenFP() const;
|
|
|
|
|
/// return the amount requested (in sats)
|
2021-01-05 00:25:23 +01:00
|
|
|
qint64 amount() const;
|
2021-01-13 17:43:10 +01:00
|
|
|
/// return the amount received (in sats)
|
|
|
|
|
qint64 amountSeen() const;
|
2021-01-05 00:25:23 +01:00
|
|
|
|
|
|
|
|
QString qrCodeString() const;
|
|
|
|
|
|
2021-01-06 15:26:46 +01:00
|
|
|
bool useLegacyAddress();
|
|
|
|
|
void setUseLegacyAddress(bool on);
|
|
|
|
|
|
2021-01-07 20:10:09 +01:00
|
|
|
SaveState saveState() const;
|
2021-05-05 12:42:26 +02:00
|
|
|
void setSaveState(PaymentRequest::SaveState saveState);
|
2021-01-07 20:10:09 +01:00
|
|
|
|
|
|
|
|
PaymentState paymentState() const;
|
|
|
|
|
|
2021-01-13 17:43:10 +01:00
|
|
|
/**
|
|
|
|
|
* Add a payment made towards fulfilling the request.
|
|
|
|
|
* @param ref the Wallet reference to the payment.
|
|
|
|
|
* @param value the amount paid, in satoshis.
|
|
|
|
|
* @param blockHeight or -1 when not mined yet
|
|
|
|
|
*/
|
|
|
|
|
void addPayment(uint64_t ref, int64_t value, int blockHeight = -1);
|
|
|
|
|
/**
|
|
|
|
|
* Mark a payment as rejected (typically double-spent).
|
|
|
|
|
* @param ref the Wallet reference to the payment.
|
|
|
|
|
* @param value the amount paid, in satoshis.
|
|
|
|
|
*/
|
|
|
|
|
void paymentRejected(uint64_t ref, int64_t value);
|
|
|
|
|
|
2021-05-05 12:42:26 +02:00
|
|
|
bool stored() const;
|
|
|
|
|
/// if /a on, mark payment request as one to store and keep around until fulfilled or deleted
|
|
|
|
|
void setStored(bool on);
|
|
|
|
|
|
2021-12-01 10:57:21 +01:00
|
|
|
/**
|
|
|
|
|
* This ties the request to a different wallet.
|
|
|
|
|
* \sa setWallet()
|
|
|
|
|
*/
|
2021-04-21 17:17:47 +02:00
|
|
|
Q_INVOKABLE void switchAccount(AccountInfo *ai);
|
|
|
|
|
|
|
|
|
|
void setWallet(Wallet *wallet);
|
2021-01-13 17:43:10 +01:00
|
|
|
|
2021-01-05 00:25:23 +01:00
|
|
|
signals:
|
|
|
|
|
void messageChanged();
|
|
|
|
|
void qrCodeStringChanged();
|
|
|
|
|
void amountChanged();
|
2021-01-13 17:43:10 +01:00
|
|
|
void amountSeenChanged();
|
2021-01-06 15:26:46 +01:00
|
|
|
void legacyChanged();
|
2021-01-07 20:10:09 +01:00
|
|
|
void saveStateChanged();
|
|
|
|
|
void paymentStateChanged();
|
2021-04-21 17:17:47 +02:00
|
|
|
void walletChanged();
|
2021-05-05 12:42:26 +02:00
|
|
|
void storedChanged();
|
2021-01-05 00:25:23 +01:00
|
|
|
|
2021-01-06 23:15:54 +01:00
|
|
|
protected:
|
|
|
|
|
friend class Wallet;
|
|
|
|
|
explicit PaymentRequest(Wallet *wallet, int paymentType);
|
|
|
|
|
|
2021-01-05 00:25:23 +01:00
|
|
|
private:
|
2021-01-16 14:28:09 +01:00
|
|
|
void setPaymentState(PaymentState newState);
|
|
|
|
|
|
|
|
|
|
|
2021-04-21 17:17:47 +02:00
|
|
|
Wallet *m_wallet;
|
2021-01-05 00:25:23 +01:00
|
|
|
QString m_message;
|
2021-01-06 15:26:46 +01:00
|
|
|
CKeyID m_address;
|
2021-01-05 00:25:23 +01:00
|
|
|
int m_privKeyId = -1; // refers to the Wallets list of private keys
|
2021-01-06 15:26:46 +01:00
|
|
|
bool m_unusedRequest = true; ///< true as long as the user did not decide to save the request
|
|
|
|
|
bool m_useLegacyAddressFormat = false;
|
2021-01-15 14:01:01 +01:00
|
|
|
bool m_dirty = true; // true is state changed and we need saving.
|
2021-01-05 00:25:23 +01:00
|
|
|
qint64 m_amountRequested = 0;
|
2021-01-13 17:43:10 +01:00
|
|
|
qint64 m_amountSeen = 0;
|
2021-01-15 14:01:01 +01:00
|
|
|
SaveState m_saveState = Temporary;
|
2021-01-07 20:10:09 +01:00
|
|
|
PaymentState m_paymentState = Unpaid;
|
2021-01-13 17:43:10 +01:00
|
|
|
|
|
|
|
|
QList<uint64_t> m_incomingOutputRefs; // see Wallet::OutputRef
|
2021-01-05 00:25:23 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|