2020-05-24 13:20:03 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2021-01-05 00:25:23 +01:00
|
|
|
* 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>
|
|
|
|
|
|
2021-10-18 22:14:12 +02:00
|
|
|
#include <Mnemonic.h>
|
2020-05-24 13:20:03 +02:00
|
|
|
#include <QList>
|
|
|
|
|
#include <QString>
|
2021-10-31 15:19:35 +01:00
|
|
|
#include <QDateTime>
|
2020-05-24 13:20:03 +02:00
|
|
|
|
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;
|
2021-10-25 19:42:13 +02:00
|
|
|
class NewWalletConfig;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
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)
|
2021-08-09 18:46:38 +02:00
|
|
|
Q_PROPERTY(QString version READ version CONSTANT)
|
|
|
|
|
Q_PROPERTY(QString libsVersion READ libsVersion CONSTANT)
|
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)
|
2020-12-26 14:40:01 +01:00
|
|
|
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,
|
2021-10-18 22:14:12 +02:00
|
|
|
LegacySH,
|
|
|
|
|
PartialMnemonic,
|
2021-10-31 13:46:47 +01:00
|
|
|
PartialMnemonicWithTypo,
|
|
|
|
|
CorrectMnemonic,
|
|
|
|
|
MissingLexicon
|
2020-05-24 13:20:03 +02:00
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
|
|
2021-01-16 14:28:09 +01:00
|
|
|
/// return a pool for the current thread;
|
2020-12-17 23:12:39 +01:00
|
|
|
static Streaming::BufferPool &pool(int reserveSize);
|
|
|
|
|
|
2021-01-16 14:28:09 +01:00
|
|
|
/// 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);
|
2021-01-16 14:28:09 +01:00
|
|
|
|
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 {
|
2021-01-06 15:26:46 +01:00
|
|
|
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().
|
2021-01-06 15:26:46 +01:00
|
|
|
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
|
|
|
|
2021-10-31 15:19:35 +01:00
|
|
|
Q_INVOKABLE QString formatDate(QDateTime date) const;
|
|
|
|
|
Q_INVOKABLE QString formatDateTime(QDateTime datetime) const;
|
|
|
|
|
|
2021-10-25 19:42:13 +02:00
|
|
|
/// create a new HD wallet with an optional name.
|
|
|
|
|
Q_INVOKABLE NewWalletConfig* createNewWallet(const QString &derivationPath, const QString &password = QString(), const QString &walletName = QString());
|
|
|
|
|
/// create a new seed-less wallet with an optional name.
|
|
|
|
|
Q_INVOKABLE NewWalletConfig* createNewBasicWallet(const QString &walletName = QString());
|
2020-06-08 14:24:27 +02:00
|
|
|
/// swipe a paper wallet with an optional name
|
2021-11-01 17:58:11 +01:00
|
|
|
Q_INVOKABLE NewWalletConfig* createImportedWallet(const QString &privateKey, const QString &walletName, int startHeight);
|
2020-05-24 13:20:03 +02:00
|
|
|
|
2021-10-18 22:14:12 +02:00
|
|
|
/**
|
|
|
|
|
* Import a mnemonics based (BIP39) wallet.
|
|
|
|
|
* Warning; will throw if the mnemonic is invalid
|
|
|
|
|
*
|
|
|
|
|
* @param mnemonic, the list of words (validated!) in the BIP39 format.
|
|
|
|
|
* @param password, the optional password that goes with a BIP39 wallet.
|
|
|
|
|
* @param derivationPath the path that turns this mnemonic into a hierarchically deterministic wallet.
|
|
|
|
|
* @param walletName the name the user knows this wallet as.
|
2021-10-21 20:54:34 +02:00
|
|
|
* @param startHeight the first block we should check for transactions, or zero for "future blocks"
|
|
|
|
|
* If you set this to 1 then we set it to a more sane value of when Bitcoin became more well known.
|
2021-10-18 22:14:12 +02:00
|
|
|
*/
|
2021-10-25 19:42:13 +02:00
|
|
|
Q_INVOKABLE NewWalletConfig *createImportedHDWallet(const QString &mnemonic, const QString &password, const QString &derivationPath, const QString &walletName, int startHeight = 0);
|
2021-10-18 22:14:12 +02:00
|
|
|
|
2021-10-18 23:19:57 +02:00
|
|
|
Q_INVOKABLE bool checkDerivation(const QString &path) const;
|
|
|
|
|
|
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;
|
2021-05-08 00:03:07 +02:00
|
|
|
/**
|
|
|
|
|
* Returns the amount of digits allowed behind the decimal-separator.
|
|
|
|
|
* The 'unit' the user pics (BCH/milli-bch/micro-bch) just moves the decimal separator
|
|
|
|
|
* and we measure that by the amount of digits we still allow behind the separator.
|
|
|
|
|
* For the "BCH" unit this is 8, for sats this is zero.
|
|
|
|
|
*/
|
2020-06-12 20:53:01 +02:00
|
|
|
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);
|
|
|
|
|
|
2020-12-26 14:40:01 +01:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
|
2020-12-26 14:40:01 +01:00
|
|
|
/**
|
|
|
|
|
* 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);
|
|
|
|
|
|
2021-01-05 00:25:23 +01:00
|
|
|
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);
|
|
|
|
|
|
2021-08-09 18:46:38 +02:00
|
|
|
QString version() const;
|
|
|
|
|
QString libsVersion() const;
|
|
|
|
|
|
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();
|
2020-12-26 14:40:01 +01:00
|
|
|
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
|
|
|
|
2021-11-02 14:24:47 +01:00
|
|
|
private slots:
|
|
|
|
|
void loadingCompleted();
|
|
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
private:
|
|
|
|
|
void init();
|
|
|
|
|
void saveData();
|
|
|
|
|
void saveAll();
|
2021-10-21 17:04:20 +02:00
|
|
|
// create wallet and add to list. Please consider calling saveData after
|
2020-05-24 13:20:03 +02:00
|
|
|
Wallet *createWallet(const QString &name);
|
2021-05-28 18:13:50 +02:00
|
|
|
uint32_t walletStartHeightHint() const;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
2021-10-18 22:14:12 +02:00
|
|
|
mutable Mnemonic m_hdSeedValidator;
|
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;
|
2021-10-25 19:42:13 +02:00
|
|
|
QString m_defaultDerivationPath;
|
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;
|
2020-12-26 14:40:01 +01:00
|
|
|
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
|