Files
pay/src/Payment.h
T

403 lines
14 KiB
C++
Raw Permalink Normal View History

2020-10-14 15:12:33 +02:00
/*
* This file is part of the Flowee project
2024-09-24 21:22:52 +02:00
* Copyright (C) 2020-2024 Tom Zander <tom@flowee.org>
2020-10-14 15:12:33 +02:00
*
* 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 PAYMENT_H
#define PAYMENT_H
2024-10-05 20:34:39 +02:00
#include "FloweePay.h"
2025-06-07 22:23:34 +02:00
#include "RepeatPaymentDetails.h"
2022-12-20 15:17:13 +01:00
2023-09-02 20:23:29 +02:00
#include <QSet>
2021-11-21 17:46:17 +01:00
2021-11-02 11:09:56 +01:00
#include <primitives/Tx.h>
2020-10-14 15:12:33 +02:00
#include <memory>
class Wallet;
2021-11-15 15:49:00 +01:00
class PaymentDetail;
class PaymentDetailOutput;
2021-11-19 11:20:01 +01:00
class PaymentDetailInputs;
2024-10-01 17:55:05 +02:00
class PaymentDetailComment;
2021-11-16 11:47:33 +01:00
class AccountInfo;
2024-01-18 19:54:51 +01:00
class TxInfoObject;
2020-10-14 15:12:33 +02:00
2024-09-24 21:22:52 +02:00
/**
* This represents a single payment, possibly a complex one.
*
* Using the Payment object we allow building a single transaction that can vary in
* complexity from paying one single address. Using 'PaymentDetails' the payment can
* be enriched to excert more control over what funds are sent to what remote parties.
*
* To enable simple and direct usability of this class there is a small subset of features
* available directly on the Payment object. Avoiding the need to create or iterate the child
* details objects.
*
* For this we recognize two modes; either there is exactly one payment-destination (output)
* or there are additional details.
* In the case of the simple, 1-desination setup we provide the setPaymentAmount() and
* setPaymentAmountFiat() methods. Represented in the properties paymentAmount and paymentAmountFiat.
*
* When the UI allows adding more details, or scanning a payment protocol (which may do the same)
* you should avoid writing to those two properties and if you do so anyway we may throw an exception
* which will cause the application to abort.
*/
2020-10-14 15:12:33 +02:00
class Payment : public QObject
{
Q_OBJECT
Q_PROPERTY(int feePerByte READ feePerByte WRITE setFeePerByte NOTIFY feePerByteChanged)
2023-02-22 15:28:06 +01:00
/// The payment-wide amount of funds being sent by this Payment.
2020-10-14 15:12:33 +02:00
Q_PROPERTY(double paymentAmount READ paymentAmount WRITE setPaymentAmount NOTIFY amountChanged)
2023-02-22 15:28:06 +01:00
/// The payment-wide amount of funds being sent by this Payment.
Q_PROPERTY(int paymentAmountFiat READ paymentAmountFiat WRITE setPaymentAmountFiat NOTIFY amountFiatChanged)
2023-02-17 19:22:24 +01:00
/// The single-output address we will send to
2020-10-14 15:12:33 +02:00
Q_PROPERTY(QString targetAddress READ targetAddress WRITE setTargetAddress NOTIFY targetAddressChanged)
// cleaned up and re-formatted
Q_PROPERTY(QString formattedTargetAddress READ formattedTargetAddress NOTIFY targetAddressChanged)
2023-05-31 15:20:11 +02:00
// cleaned but short.
Q_PROPERTY(QString niceAddress READ niceAddress NOTIFY targetAddressChanged)
2023-05-18 21:52:51 +02:00
Q_PROPERTY(bool preferSchnorr READ preferSchnorr WRITE setPreferSchnorr NOTIFY preferSchnorrChanged)
2023-02-17 19:22:24 +01:00
/// If input is valid, tx can be prepared. \see prepare()
2023-05-18 21:52:51 +02:00
Q_PROPERTY(bool isValid READ validate NOTIFY validChanged)
Q_PROPERTY(QList<QObject*> details READ paymentDetails NOTIFY paymentDetailsChanged)
2024-10-05 20:34:39 +02:00
Q_PROPERTY(FloweePay::BroadcastStatus broadcastStatus READ broadcastStatus NOTIFY broadcastStatusChanged)
/// The price of on BCH
Q_PROPERTY(int fiatPrice READ fiatPrice WRITE setFiatPrice NOTIFY fiatPriceChanged)
Q_PROPERTY(AccountInfo *account READ currentAccount WRITE setCurrentAccount NOTIFY currentAccountChanged)
Q_PROPERTY(QString userComment READ userComment WRITE setUserComment NOTIFY userCommentChanged)
2023-05-18 21:52:51 +02:00
Q_PROPERTY(bool walletNeedsPin READ walletNeedsPin NOTIFY walletPinChanged)
2023-02-08 14:14:06 +01:00
Q_PROPERTY(bool autoPrepare READ autoPrepare WRITE setAutoPrepare NOTIFY autoPrepareChanged)
2023-05-16 15:32:33 +02:00
Q_PROPERTY(bool instaPay READ allowInstaPay WRITE setAllowInstaPay NOTIFY allowInstaPayChanged)
2023-12-22 18:52:05 +01:00
Q_PROPERTY(bool simpleAddressTarget READ simpleAddressTarget NOTIFY simpleAddressTargetChanged FINAL)
2025-06-06 20:52:04 +02:00
Q_PROPERTY(RepeatPaymentDetails *repeat READ repeatDetails NOTIFY repeatDetailsChanged FINAL)
// --- Stuff that becomes available / useful after prepare has been called:
/// Tx has been prepared
2023-05-18 21:52:51 +02:00
Q_PROPERTY(bool txPrepared READ txPrepared NOTIFY txPreparedChanged)
Q_PROPERTY(QString txid READ txid NOTIFY txCreated)
Q_PROPERTY(int assignedFee READ assignedFee NOTIFY txCreated)
Q_PROPERTY(int txSize READ txSize NOTIFY txCreated)
/// If prepare() failed, this is set.
2023-05-18 21:52:51 +02:00
Q_PROPERTY(QString error READ error NOTIFY errorChanged)
2021-11-15 15:49:00 +01:00
2023-09-02 20:23:29 +02:00
Q_PROPERTY(QStringList warnings READ warnings NOTIFY warningsChanged)
2020-10-14 15:12:33 +02:00
public:
2021-11-15 15:49:00 +01:00
enum DetailType {
InputSelector,
PayToAddress,
CommentOutput // aka op-return
2021-11-15 15:49:00 +01:00
};
2024-10-05 11:00:30 +02:00
Q_ENUM(DetailType)
2021-11-15 15:49:00 +01:00
2023-09-02 20:23:29 +02:00
enum Warning {
InsecureTransport,
2023-09-05 19:09:46 +02:00
DownloadFailed,
OfflineWarning
2023-09-02 20:23:29 +02:00
};
2024-10-05 11:00:30 +02:00
Q_ENUM(Warning)
2023-09-02 20:23:29 +02:00
2021-11-16 11:47:33 +01:00
Payment(QObject *parent = nullptr);
2025-10-08 15:35:34 +02:00
Payment(const Streaming::ConstBuffer &stored, const std::shared_ptr<Wallet> &assignedWallet);
2020-10-14 15:12:33 +02:00
void setFeePerByte(int sats);
int feePerByte();
2020-10-23 22:34:34 +02:00
/**
2021-11-24 14:56:52 +01:00
* Sats the amount of BCH on our single-detail payment.
*
* This method can no longer be used after addExtraOutput() have been called.
* This method assumes a single output, which is the default for this class.
*
* This sets the amount to pay, in Satoshis, to the target address.
2020-10-23 22:34:34 +02:00
*/
2020-10-14 15:12:33 +02:00
void setPaymentAmount(double amount);
2021-11-24 14:56:52 +01:00
/**
* Returns the total amount of satoshis that are selected by outputs.
*/
2023-02-08 20:55:24 +01:00
double paymentAmount() const;
2023-02-22 15:28:06 +01:00
void setPaymentAmountFiat(int amount);
int paymentAmountFiat() const;
2020-10-14 15:12:33 +02:00
2021-11-24 14:56:52 +01:00
/**
* Sets the address to pay to.
*
* This method can no longer be used after addExtraOutput has been called.
* This method assumes a single output, which is the default for this class.
2023-09-05 19:09:46 +02:00
* @see pasteTargetAddress()
2021-11-24 14:56:52 +01:00
*/
2020-10-14 15:12:33 +02:00
void setTargetAddress(const QString &address);
2023-09-05 19:09:46 +02:00
2021-11-24 14:56:52 +01:00
/**
* Returns the address to pay to, as the user typed it.
*
* This method can no longer be used after addExtraOutput has been called.
* This method assumes a single output, which is the default for this class.
*/
2024-09-24 21:22:52 +02:00
QString targetAddress() const;
2021-11-24 14:56:52 +01:00
/**
* Returns the validated and formatted address to pay to.
*
* This method can no longer be used after addExtraOutput has been called.
* This method assumes a single output, which is the default for this class.
*/
2023-05-31 15:20:11 +02:00
QString formattedTargetAddress() const;
/**
* The nicest to display address version.
* This will always return (if available) the BCH style (cash-address) address, without
* the bitcoincash: prefix.
*/
QString niceAddress() const;
2020-10-14 15:12:33 +02:00
2022-05-18 20:16:34 +02:00
bool walletNeedsPin() const;
/// return true if all fields are correctly populated and we can prepare()
bool validate() const;
/// A user error occured during prepare()
const QString &error() const;
Q_INVOKABLE void prepare();
2023-09-02 20:23:29 +02:00
Q_INVOKABLE void markUserApproved();
Q_INVOKABLE void reset();
2023-03-11 22:05:15 +01:00
Q_INVOKABLE PaymentDetail* addExtraOutput();
Q_INVOKABLE PaymentDetail* addInputSelector();
Q_INVOKABLE PaymentDetail* addCommentOutput();
2021-11-17 14:24:19 +01:00
Q_INVOKABLE void remove(PaymentDetail *detail);
2023-09-05 19:09:46 +02:00
/**
* Set a should-be-correct address and return true if valid.
*
* Calling thie function is very similar to setting the property 'targetAddress'
* with the main difference that should the address pasted not be a valid one,
* this method returns false.
* @see setTargetAddress
*/
Q_INVOKABLE bool pasteTargetAddress(const QString &address);
/**
* Unlock the account in order to allow funding of this payment.
*/
2022-05-18 20:16:34 +02:00
Q_INVOKABLE void decrypt(const QString &password);
2021-11-15 15:49:00 +01:00
2020-10-14 15:12:33 +02:00
/// return the txid, should there be a transaction (otherwise empty string)
QString txid() const;
2021-11-24 14:56:52 +01:00
/// The fee decided to be used in 'prepare()'.
2020-10-14 15:12:33 +02:00
int assignedFee() const;
2021-11-24 14:56:52 +01:00
/// The size of the transaction we prepare()d.
2020-10-14 15:12:33 +02:00
int txSize() const;
2021-11-24 14:56:52 +01:00
/// Return true if prepare() successfully completed.
2021-11-16 11:47:33 +01:00
bool txPrepared() const;
2020-10-14 15:12:33 +02:00
/**
* This returns a total fiat amount input into the prepared transaction.
*/
int effectiveFiatAmount() const;
/**
* This returns a total BCH (in sats) amount that went into the prepared transaction.
*/
double effectiveBchAmount() const;
/// Return the wallet used by the previous prepare()
/// \sa currentAccount
2025-10-08 15:35:34 +02:00
std::shared_ptr<Wallet> wallet() const;
2020-10-14 15:12:33 +02:00
2021-05-04 17:41:49 +02:00
bool preferSchnorr() const;
void setPreferSchnorr(bool preferSchnorr);
2021-11-16 11:47:33 +01:00
QList<QObject *> paymentDetails() const;
2024-10-05 20:34:39 +02:00
FloweePay::BroadcastStatus broadcastStatus() const;
2025-09-07 23:43:29 +02:00
/// returns true if at least one output requires a proper fiat price for payment.
bool usesFiat() const;
/// The exchange rate. The amount of cents for one BCH.
int fiatPrice() const;
/// The exchange rate. The amount of cents for one BCH.
void setFiatPrice(int pricePerCoin);
AccountInfo *currentAccount() const;
void setCurrentAccount(AccountInfo *account);
const QString &userComment() const;
void setUserComment(const QString &comment);
2023-02-08 14:14:06 +01:00
bool autoPrepare() const;
void setAutoPrepare(bool newAutoPrepare);
2023-05-16 15:32:33 +02:00
bool allowInstaPay() const;
void setAllowInstaPay(bool allowIt);
2023-05-16 15:32:33 +02:00
Tx tx() const;
2023-09-02 20:23:29 +02:00
/*
* When an issue occurred with a consumer of this class and the error should be shown to the user.
*/
void forwardError(const QString &error);
/**
* When an warning occurred with a consumer of this class and the warning should be shown to the user.
*/
void addWarning(Warning warning);
QStringList warnings() const;
Q_INVOKABLE void clearWarnings();
/// Bypass the broadcast mechanism and mark the transaction as received.
void confirmReceivedOk();
2023-12-22 18:52:05 +01:00
bool simpleAddressTarget() const;
void setSimpleAddressTarget(bool newSimpleAddressTarget);
2025-06-06 20:52:04 +02:00
Q_INVOKABLE void makeRepeating();
RepeatPaymentDetails *repeatDetails() const;
void setRepeatDetails(RepeatPaymentDetails *newRepeatDetails);
2025-06-07 22:23:34 +02:00
// Store the Payment and its details in a blob.
Streaming::ConstBuffer save(const std::shared_ptr<Streaming::BufferPool> &pool) const;
2025-06-07 22:23:34 +02:00
/**
* Deep copy this Payment object and make it repeating, then return an instance of SavedPayment
*/
Q_INVOKABLE QObject *copyToRepeating(QObject *parent) const;
2026-02-04 14:47:15 +01:00
/**
* When true, a payment object that results in a transaction will save
* the payment itself to the wallet that received the transaction.
* @see Wallet::loadPaymentFor(int index)
*/
bool persistPaidPayment() const;
void setPersistPaidPayment(bool newPersistPaidPayment);
int paymentId() const;
2020-10-14 15:12:33 +02:00
private slots:
2021-11-24 14:56:52 +01:00
void recalcAmounts();
2023-09-02 20:23:29 +02:00
void broadcast();
2020-10-14 15:12:33 +02:00
signals:
void feePerByteChanged();
void amountChanged();
void amountFiatChanged();
2020-10-14 15:12:33 +02:00
void targetAddressChanged();
2021-11-16 11:47:33 +01:00
void txPreparedChanged();
2021-05-04 17:41:49 +02:00
void preferSchnorrChanged();
2021-11-16 11:47:33 +01:00
void paymentDetailsChanged();
void broadcastStatusChanged();
void validChanged();
void errorChanged();
void txCreated();
void fiatPriceChanged();
void currentAccountChanged();
void userCommentChanged();
2022-05-18 20:16:34 +02:00
void walletPinChanged();
2023-02-08 14:14:06 +01:00
void autoPrepareChanged();
2023-05-16 15:32:33 +02:00
void allowInstaPayChanged();
2023-09-02 20:23:29 +02:00
void warningsChanged();
2023-12-22 18:52:05 +01:00
void simpleAddressTargetChanged();
2023-09-02 20:23:29 +02:00
void approvedByUser();
2025-06-06 20:52:04 +02:00
void repeatDetailsChanged();
2020-10-14 15:12:33 +02:00
private:
2023-02-08 14:14:06 +01:00
void doAutoPrepare();
friend class PaymentDetailOutput;
2024-09-24 21:22:52 +02:00
/// Helper methods to get the output, assuming that is the only output.
/// Will throw if the Payment has more than one.
2021-11-15 15:49:00 +01:00
PaymentDetailOutput *soleOut() const;
2024-09-24 21:22:52 +02:00
PaymentDetailOutput *firstOut() const;
2021-11-17 14:24:19 +01:00
void addDetail(PaymentDetail*);
2021-11-15 15:49:00 +01:00
AccountInfo *m_account = nullptr;
// Payment Variable initialization in reset() please
2023-09-02 20:23:29 +02:00
QSet<Warning> m_warnings;
2021-11-15 15:49:00 +01:00
QList<PaymentDetail*> m_paymentDetails;
2023-02-08 14:14:06 +01:00
bool m_autoPrepare = false;
bool m_txPrepared;
bool m_txBroadcastStarted;
bool m_preferSchnorr;
2023-05-16 15:32:33 +02:00
bool m_allowInstaPay = false;
2023-12-22 18:52:05 +01:00
bool m_simpleAddressTarget = true; // only 'advanced' payment protocols set this to false
2026-02-04 14:47:15 +01:00
bool m_persistPaidPayment = false;
2020-10-14 15:12:33 +02:00
Tx m_tx;
int m_fee; // in sats per byte
int m_assignedFee;
int m_fiatPrice = 50000; // price for one whole BCH
int m_paymentId = -1; // how others can refer to this payment without a pointer.
2024-01-18 19:54:51 +01:00
std::shared_ptr<TxInfoObject> m_infoObject;
2025-10-08 15:35:34 +02:00
std::shared_ptr<Wallet> m_wallet;
QString m_error;
QString m_userComment;
2025-06-06 20:52:04 +02:00
2025-06-08 12:46:00 +02:00
RepeatPaymentDetails *m_repeatDetails = nullptr;
2020-10-14 15:12:33 +02:00
};
2021-11-23 17:45:38 +01:00
2021-11-15 15:49:00 +01:00
class PaymentDetail : public QObject
{
Q_OBJECT
Q_PROPERTY(Payment::DetailType type READ type CONSTANT)
Q_PROPERTY(bool collapsable READ collapsable WRITE setCollapsable NOTIFY collapsableChanged)
Q_PROPERTY(bool collapsed READ collapsed WRITE setCollapsed NOTIFY collapsedChanged)
2023-07-07 09:41:04 +02:00
Q_PROPERTY(bool isOutput READ isOutput CONSTANT)
2026-02-04 21:36:29 +01:00
Q_PROPERTY(bool isComment READ isComment CONSTANT)
2023-07-07 09:41:04 +02:00
Q_PROPERTY(bool isInput READ isInputs CONSTANT)
2021-11-15 15:49:00 +01:00
public:
PaymentDetail(Payment *parent, Payment::DetailType type);
2021-11-15 15:49:00 +01:00
Payment::DetailType type() const;
bool collapsable() const;
void setCollapsable(bool newCollapsable);
bool collapsed() const;
void setCollapsed(bool newCollapsed);
2021-11-17 14:24:19 +01:00
inline bool isOutput() const {
return m_type == Payment::PayToAddress;
}
2021-11-19 11:20:01 +01:00
inline bool isInputs() const {
return m_type == Payment::InputSelector;
}
2024-10-01 17:55:05 +02:00
inline bool isComment() const {
return m_type == Payment::CommentOutput;
}
2021-11-15 15:49:00 +01:00
PaymentDetailOutput *toOutput();
2021-11-19 11:20:01 +01:00
PaymentDetailInputs *toInputs();
2024-10-01 17:55:05 +02:00
PaymentDetailComment *toComment();
2021-11-15 15:49:00 +01:00
bool valid() const;
/**
* When the user selects another wallet (via Payment::setCurrentAccount)
* default implementation is empty.
*/
2025-10-08 15:35:34 +02:00
virtual void setWallet(const std::shared_ptr<Wallet> &wallet);
protected:
void setValid(bool valid);
2021-11-15 15:49:00 +01:00
signals:
void collapsableChanged();
void collapsedChanged();
void validChanged();
2021-11-15 15:49:00 +01:00
private:
const Payment::DetailType m_type;
bool m_collapsable = true;
bool m_collapsed = false;
bool m_valid = false; // when all user-input is valid
2021-11-15 15:49:00 +01:00
};
2020-10-14 15:12:33 +02:00
#endif