Files
pay/FloweePay.h
T

234 lines
7.4 KiB
C++
Raw Permalink Normal View History

2020-05-24 13:20:03 +02:00
/*
* This file is part of the Flowee project
* Copyright (C) 2020-2021 Tom Zander <tom@flowee.org>
2020-05-24 13:20:03 +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 FLOWEEPAY_H
#define FLOWEEPAY_H
2021-02-05 16:56:02 +01:00
#include "NotificationManager.h"
2020-05-24 13:20:03 +02:00
#include <QObject>
#include <DownloadManager.h>
2020-10-17 17:34:40 +02:00
#include <P2PNetInterface.h>
2020-05-24 13:20:03 +02:00
#include <WorkerThreads.h>
#include <QList>
#include <QString>
2020-12-17 23:12:39 +01:00
const std::string &chainPrefix();
namespace Streaming {
class BufferPool;
}
2020-05-24 13:20:03 +02:00
class Wallet;
2020-10-17 17:34:40 +02:00
class FloweePay : public QObject, WorkerThreads, P2PNetInterface
2020-05-24 13:20:03 +02:00
{
Q_OBJECT
2020-06-12 20:53:01 +02:00
Q_PROPERTY(QString unitName READ unitName NOTIFY unitChanged)
2020-06-11 17:56:06 +02:00
Q_PROPERTY(int windowWidth READ windowWidth WRITE setWindowWidth NOTIFY windowWidthChanged)
Q_PROPERTY(int windowHeight READ windowHeight WRITE setWindowHeight NOTIFY windowHeightChanged)
2020-06-12 20:53:01 +02:00
Q_PROPERTY(int unitAllowedDecimals READ unitAllowedDecimals NOTIFY unitChanged)
2020-10-17 17:34:40 +02:00
Q_PROPERTY(int headerChainHeight READ headerChainHeight NOTIFY headerChainHeightChanged)
Q_PROPERTY(int expectedChainHeight READ expectedChainHeight NOTIFY expectedChainHeightChanged)
2021-02-06 16:30:51 +01:00
Q_PROPERTY(int chainHeight READ chainHeight NOTIFY headerChainHeightChanged)
2020-10-14 15:12:33 +02:00
Q_PROPERTY(bool useDarkSkin READ darkSkin WRITE setDarkSkin NOTIFY darkSkinChanged)
2020-11-17 22:11:11 +01:00
Q_PROPERTY(bool isMainChain READ isMainChain CONSTANT)
2021-05-01 16:21:58 +02:00
Q_PROPERTY(bool hideBalance READ hideBalance WRITE setHideBalance NOTIFY hideBalanceChanged)
2021-05-04 17:41:49 +02:00
Q_PROPERTY(bool preferSchnorr READ preferSchnorr WRITE setPreferSchnorr NOTIFY preferSchnorrChanged);
2020-10-15 20:04:10 +02:00
Q_PROPERTY(UnitOfBitcoin unit READ unit WRITE setUnit NOTIFY unitChanged)
2021-01-16 17:01:18 +01:00
Q_PROPERTY(int dspTimeout READ dspTimeout WRITE setDspTimeout NOTIFY dspTimeoutChanged)
2020-05-24 13:20:03 +02:00
public:
enum StringType {
Unknown = 0,
PrivateKey,
CashPKH,
CashSH,
LegacyPKH,
LegacySH
};
enum UnitOfBitcoin {
BCH,
MilliBCH,
MicroBCH,
2020-06-12 20:53:01 +02:00
Bits,
Satoshis
2020-05-24 13:20:03 +02:00
};
2020-10-15 20:04:10 +02:00
2020-05-24 13:20:03 +02:00
FloweePay();
~FloweePay();
2020-10-29 21:51:52 +01:00
/**
* Select the chain that the FloweePay singleton will be created for.
* Only has effect if you call this before the first call to instance();
*/
static void selectChain(P2PNet::Chain chain);
2020-05-24 13:20:03 +02:00
static FloweePay *instance();
QList<Wallet *> wallets() const;
DownloadManager* p2pNet();
/// return a pool for the current thread;
2020-12-17 23:12:39 +01:00
static Streaming::BufferPool &pool(int reserveSize);
/// return the amount of milli-seconds we wait for a double-spent-proof
2021-01-16 17:01:18 +01:00
int dspTimeout() const;
void setDspTimeout(int milliseconds);
2020-06-08 14:24:27 +02:00
/// return the app data location
2020-05-24 13:20:03 +02:00
QString basedir() const;
2020-06-08 14:24:27 +02:00
/// for a price, in satoshis, return a formatted string in unitName().
2020-06-12 20:53:01 +02:00
Q_INVOKABLE inline QString priceToString(double price) const {
return FloweePay::priceToString(static_cast<qint64>(price), m_unit);
2020-06-12 20:53:01 +02:00
}
/// for a price, in satoshis, return a formatted string in unitName().
QString priceToString(qint64 price) const {
return FloweePay::priceToString(price, m_unit);
}
/// for a price, in satoshis, return a formatted string
static QString priceToString(qint64 price, UnitOfBitcoin unit);
2020-05-24 13:20:03 +02:00
2020-06-08 14:24:27 +02:00
/// create a new wallet with an optional name.
2020-05-24 13:20:03 +02:00
Q_INVOKABLE void createNewWallet(const QString &walletName = QString());
2020-06-08 14:24:27 +02:00
/// swipe a paper wallet with an optional name
2020-05-24 13:20:03 +02:00
Q_INVOKABLE void createImportedWallet(const QString &privateKey, const QString &walletName);
2020-06-08 14:24:27 +02:00
/// take a bitcoin-address and identify the type.
2021-02-05 17:47:17 +01:00
Q_INVOKABLE FloweePay::StringType identifyString(const QString &string) const;
2020-05-24 13:20:03 +02:00
2021-04-22 16:13:40 +02:00
Q_INVOKABLE QString nameOfUnit(FloweePay::UnitOfBitcoin unit) const;
2020-05-24 13:20:03 +02:00
/// returns the unit of our prices. BCH, for instance.
QString unitName() const;
2020-06-12 20:53:01 +02:00
/// returns the amount of digits allowed behind the decimal-separator.
int unitAllowedDecimals() const;
2020-05-24 13:20:03 +02:00
2020-06-11 17:56:06 +02:00
int windowWidth() const;
void setWindowWidth(int windowWidth);
int windowHeight() const;
void setWindowHeight(int windowHeight);
2020-10-14 15:12:33 +02:00
bool darkSkin() const;
void setDarkSkin(bool darkSkin);
2020-10-15 20:04:10 +02:00
UnitOfBitcoin unit() const;
void setUnit(const UnitOfBitcoin &unit);
/**
* Return the chain-height of validated headers.
* When we connect to some peers we get updated headers and thus the first
* indication of the world status is the header chain height.
*/
2020-10-17 17:34:40 +02:00
int headerChainHeight() const;
/**
* Return the chain-height that based on the date/time we expect
* to be at.
*/
int expectedChainHeight() const;
/**
* The best known chainHeight.
* On startup the headerChainHeight is likely going to be outdated
* as much as our accountBlockHeight is. Which would result in bad info.
* For the time we have not yet gotten the headers therefore we will
* use the mathematically determined expectedChainHeight. We stop using
* that as soon as we get some movement in the headers one as the expected
* one may be off by several blocks.
*/
int chainHeight();
2020-10-17 17:34:40 +02:00
// P2PNetInterface interface
void blockchainHeightChanged(int newHeight);
2021-03-31 12:10:25 +02:00
/**
* Returns true if this is the mainchain.
* \see selectChain()
*/
2020-11-17 22:11:11 +01:00
bool isMainChain() const {
return m_chain == P2PNet::MainChain;
}
2021-03-31 12:10:25 +02:00
/**
* Returns the chain this Pay was created with.
* \see selectChain()
*/
2020-10-29 21:51:52 +01:00
P2PNet::Chain chain() const;
2020-12-17 23:12:39 +01:00
const std::string &chainPrefix() const { return m_chainPrefix; }
2020-10-29 21:51:52 +01:00
2021-05-01 16:21:58 +02:00
/**
* return if the user engaged the hide-balance feature to avoid people seeing the balance on-screen.
*/
bool hideBalance() const;
/**
* Set the hide-balance on or off.
*/
void setHideBalance(bool hideBalance);
Q_INVOKABLE void copyToClipboard(const QString &text);
2020-10-15 20:04:10 +02:00
Q_ENUM(StringType UnitOfBitcoin)
2021-05-01 16:21:58 +02:00
2021-05-04 17:41:49 +02:00
bool preferSchnorr() const;
void setPreferSchnorr(bool preferSchnorr);
2020-05-24 13:20:03 +02:00
signals:
void loadComplete();
2020-06-08 14:24:27 +02:00
/// \internal
2020-05-24 13:20:03 +02:00
void loadComplete_priv();
2020-06-12 20:53:01 +02:00
void unitChanged();
2020-05-24 13:20:03 +02:00
void walletsChanged();
2020-10-14 15:12:33 +02:00
void darkSkinChanged();
2020-05-24 13:20:03 +02:00
2020-06-11 17:56:06 +02:00
void windowWidthChanged();
void windowHeightChanged();
2020-10-17 17:34:40 +02:00
void headerChainHeightChanged();
void expectedChainHeightChanged();
2021-01-16 17:01:18 +01:00
void dspTimeoutChanged();
2021-05-01 16:21:58 +02:00
void hideBalanceChanged();
2021-05-04 17:41:49 +02:00
void preferSchnorrChanged();
2020-06-11 17:56:06 +02:00
2020-05-24 13:20:03 +02:00
private:
void init();
void saveData();
void saveAll();
Wallet *createWallet(const QString &name);
2020-10-30 18:06:58 +01:00
int walletStartHeightHint() const;
2020-05-24 13:20:03 +02:00
2021-02-05 16:56:02 +01:00
2020-05-24 13:20:03 +02:00
UnitOfBitcoin m_unit = BCH;
QString m_basedir;
2020-10-29 21:51:52 +01:00
P2PNet::Chain m_chain = P2PNet::MainChain;
2020-12-17 23:12:39 +01:00
std::string m_chainPrefix;
2020-05-24 13:20:03 +02:00
std::unique_ptr<DownloadManager> m_downloadManager;
2021-02-05 16:56:02 +01:00
NotificationManager m_notifications;
2020-05-24 13:20:03 +02:00
QList<Wallet*> m_wallets;
2021-05-01 16:21:58 +02:00
int m_dspTimeout = 5000;
int m_windowWidth = 500;
int m_windowHeight = 500;
int m_initialHeaderChainHeight = 0;
2021-05-01 16:21:58 +02:00
bool m_darkSkin = true;
2021-04-21 15:17:08 +02:00
bool m_createStartWallet = false;
2021-05-01 16:21:58 +02:00
bool m_hideBalance = false;
2021-05-04 17:41:49 +02:00
bool m_preferSchnorr = true;
2020-05-24 13:20:03 +02:00
};
#endif