Files
pay/PaymentRequest.h
T

152 lines
5.1 KiB
C++
Raw Permalink Normal View History

/*
* 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>
#include <primitives/pubkey.h>
class Wallet;
2021-04-21 17:17:47 +02:00
class AccountInfo;
/**
* 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)
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)
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)
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-07 20:10:09 +01:00
enum SaveState {
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);
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.
void setAmountFP(double amount);
2021-01-13 17:43:10 +01:00
/// return the amount requested (in sats)
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)
qint64 amount() const;
2021-01-13 17:43:10 +01:00
/// return the amount received (in sats)
qint64 amountSeen() const;
QString qrCodeString() const;
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
signals:
void messageChanged();
void qrCodeStringChanged();
void amountChanged();
2021-01-13 17:43:10 +01:00
void amountSeenChanged();
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();
private slots:
void walletEncryptionChanged();
2021-01-06 23:15:54 +01:00
protected:
friend class Wallet;
explicit PaymentRequest(Wallet *wallet, int paymentType);
private:
void setPaymentState(PaymentState newState);
2021-04-21 17:17:47 +02:00
Wallet *m_wallet;
QString m_message;
2022-07-06 22:06:58 +02:00
KeyId m_address;
int m_privKeyId = -1; // refers to the Wallets list of private keys
bool m_unusedRequest = true; ///< true as long as the user did not decide to save the request
bool m_useLegacyAddressFormat = false;
bool m_dirty = true; // true is state changed and we need saving.
qint64 m_amountRequested = 0;
2021-01-13 17:43:10 +01:00
qint64 m_amountSeen = 0;
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
};
#endif