2020-10-14 15:12:33 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2022-04-05 16:37:09 +02:00
|
|
|
* Copyright (C) 2020-2022 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/>.
|
|
|
|
|
*/
|
|
|
|
|
#include "AccountInfo.h"
|
|
|
|
|
#include "WalletHistoryModel.h"
|
2020-12-17 23:12:39 +01:00
|
|
|
#include "FloweePay.h"
|
2020-10-14 15:12:33 +02:00
|
|
|
|
|
|
|
|
#include <p2p/PrivacySegment.h>
|
2021-10-31 15:19:35 +01:00
|
|
|
#include <utils/base58.h>
|
|
|
|
|
#include <utils/primitives/key.h>
|
|
|
|
|
#include <utils/cashaddr.h>
|
2022-04-05 21:18:39 +02:00
|
|
|
#include <SyncSPVAction.h>
|
2021-07-31 17:19:34 +02:00
|
|
|
|
|
|
|
|
|
2020-10-14 15:12:33 +02:00
|
|
|
AccountInfo::AccountInfo(Wallet *wallet, QObject *parent)
|
|
|
|
|
: QObject(parent),
|
|
|
|
|
m_wallet(wallet)
|
|
|
|
|
{
|
|
|
|
|
connect(wallet, SIGNAL(utxosChanged()), this, SIGNAL(utxosChanged()), Qt::QueuedConnection);
|
2022-04-06 20:51:00 +02:00
|
|
|
connect(wallet, SIGNAL(balanceChanged()), this, SLOT(balanceHasChanged()), Qt::QueuedConnection);
|
2020-10-17 17:34:40 +02:00
|
|
|
connect(wallet, SIGNAL(lastBlockSynchedChanged()), this, SIGNAL(lastBlockSynchedChanged()), Qt::QueuedConnection);
|
2022-04-06 18:18:49 +02:00
|
|
|
connect(wallet, SIGNAL(lastBlockSynchedChanged()), this, SIGNAL(timeBehindChanged()), Qt::QueuedConnection);
|
2021-01-07 20:10:09 +01:00
|
|
|
connect(wallet, SIGNAL(paymentRequestsChanged()), this, SIGNAL(paymentRequestsChanged()), Qt::QueuedConnection);
|
2022-06-20 22:52:57 +02:00
|
|
|
connect(wallet, SIGNAL(encryptionChanged()), this, SLOT(walletEncryptionChanged()), Qt::QueuedConnection);
|
2022-04-06 18:18:49 +02:00
|
|
|
connect(FloweePay::instance(), SIGNAL(headerChainHeightChanged()), this, SIGNAL(timeBehindChanged()));
|
2020-10-14 15:12:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AccountInfo::id() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->segment()->segmentId();
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-06 22:15:03 +01:00
|
|
|
double AccountInfo::balanceConfirmed() const
|
2020-10-14 15:12:33 +02:00
|
|
|
{
|
2020-11-06 22:15:03 +01:00
|
|
|
return static_cast<double>(m_wallet->balanceConfirmed());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double AccountInfo::balanceUnconfirmed() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<double>(m_wallet->balanceUnconfirmed());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double AccountInfo::balanceImmature() const
|
|
|
|
|
{
|
|
|
|
|
return static_cast<double>(m_wallet->balanceImmature());
|
2020-10-14 15:12:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AccountInfo::unspentOutputCount() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->unspentOutputCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int AccountInfo::historicalOutputCount() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->historicalOutputCount();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AccountInfo::setName(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
if (m_wallet->name() == name)
|
|
|
|
|
return;
|
|
|
|
|
m_wallet->setName(name);
|
|
|
|
|
emit nameChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString AccountInfo::name() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->name();
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-17 17:34:40 +02:00
|
|
|
int AccountInfo::lastBlockSynched() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_wallet->segment())
|
|
|
|
|
return 0;
|
|
|
|
|
return m_wallet->segment()->lastBlockSynched();
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-03 13:58:31 +01:00
|
|
|
QDateTime AccountInfo::lastBlockSynchedTime() const
|
|
|
|
|
{
|
|
|
|
|
if (!m_wallet->segment() || m_wallet->segment()->lastBlockSynched() < 1)
|
|
|
|
|
return QDateTime();
|
|
|
|
|
auto timestamp = FloweePay::instance()->p2pNet()->blockchain().block(m_wallet->segment()->lastBlockSynched()).nTime;
|
|
|
|
|
if (timestamp == 0)
|
|
|
|
|
return QDateTime();
|
2022-04-06 18:18:49 +02:00
|
|
|
return QDateTime::fromTime_t(timestamp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString AccountInfo::timeBehind() const
|
|
|
|
|
{
|
|
|
|
|
const int accountHeight = lastBlockSynched();
|
|
|
|
|
if (accountHeight <= 0) // For accounts that only expect tx in the future.
|
|
|
|
|
return tr("Up to date");
|
|
|
|
|
const int chainHeight = FloweePay::instance()->chainHeight();
|
|
|
|
|
|
|
|
|
|
const int diff = chainHeight - accountHeight;
|
|
|
|
|
if (diff < 0) // we are ahead???
|
|
|
|
|
return "--";
|
|
|
|
|
auto days = diff / 144.;
|
|
|
|
|
auto weeks = diff / 1008.;
|
|
|
|
|
if (days > 10)
|
|
|
|
|
return tr("%1 weeks behind", "", std::ceil(weeks)).arg(std::ceil(weeks));
|
|
|
|
|
auto hours = diff / 6.;
|
|
|
|
|
if (hours > 48)
|
|
|
|
|
return tr("%1 days behind", "", std::ceil(days)).arg(std::ceil(days));
|
|
|
|
|
if (diff == 0)
|
|
|
|
|
return tr("Up to date");
|
|
|
|
|
if (diff < 3 && !isArchived())
|
|
|
|
|
return tr("Updating");
|
|
|
|
|
return tr("%1 hours behind", "", std::ceil(hours)).arg(std::ceil(hours));
|
2021-11-03 13:58:31 +01:00
|
|
|
}
|
|
|
|
|
|
2020-10-14 15:12:33 +02:00
|
|
|
WalletHistoryModel *AccountInfo::historyModel()
|
|
|
|
|
{
|
2022-06-20 22:52:57 +02:00
|
|
|
if (m_model == nullptr) {
|
2020-10-14 15:12:33 +02:00
|
|
|
m_model.reset(new WalletHistoryModel(m_wallet, this));
|
2022-06-20 22:52:57 +02:00
|
|
|
}
|
2020-10-14 15:12:33 +02:00
|
|
|
return m_model.get();
|
|
|
|
|
}
|
2020-10-19 14:05:38 +02:00
|
|
|
|
2021-10-29 18:20:42 +02:00
|
|
|
WalletSecretsModel *AccountInfo::secretsModel()
|
|
|
|
|
{
|
2022-06-20 22:52:57 +02:00
|
|
|
if (m_secretsModel == nullptr) {
|
2021-10-29 18:20:42 +02:00
|
|
|
m_secretsModel.reset(new WalletSecretsModel(m_wallet, this));
|
2022-06-20 22:52:57 +02:00
|
|
|
}
|
2021-10-29 18:20:42 +02:00
|
|
|
return m_secretsModel.get();
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 15:19:35 +01:00
|
|
|
QDateTime AccountInfo::lastMinedTransaction() const
|
|
|
|
|
{
|
|
|
|
|
assert(m_wallet);
|
|
|
|
|
const int blockHeight = m_wallet->lastTransactionTimestamp();
|
|
|
|
|
if (blockHeight <= 0)
|
|
|
|
|
return QDateTime();
|
2022-03-23 00:02:05 +01:00
|
|
|
auto timestamp = FloweePay::instance()->p2pNet()->blockchain().block(blockHeight).nTime;
|
|
|
|
|
if (timestamp == 0)
|
|
|
|
|
return QDateTime();
|
|
|
|
|
return QDateTime::fromTime_t(timestamp);
|
2021-12-04 11:25:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AccountInfo::isArchived() const
|
|
|
|
|
{
|
|
|
|
|
auto segment =m_wallet->segment();
|
|
|
|
|
if (!segment)
|
2022-04-05 16:37:09 +02:00
|
|
|
return true;
|
2021-12-04 11:25:27 +01:00
|
|
|
return segment->priority() == PrivacySegment::OnlyManual;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AccountInfo::setIsArchived(bool archived)
|
|
|
|
|
{
|
|
|
|
|
auto segment = m_wallet->segment();
|
|
|
|
|
if (!segment)
|
|
|
|
|
return;
|
|
|
|
|
if ((segment->priority() == PrivacySegment::OnlyManual) == archived)
|
|
|
|
|
return;
|
|
|
|
|
segment->setPriority(archived ? PrivacySegment::OnlyManual : PrivacySegment::Normal);
|
|
|
|
|
emit isArchivedChanged();
|
2022-04-05 21:18:39 +02:00
|
|
|
|
|
|
|
|
if (!archived && !FloweePay::instance()->isOffline()) {
|
|
|
|
|
// make sure that we get peers for the no longer archived wallet.
|
|
|
|
|
FloweePay::instance()->p2pNet()->addAction<SyncSPVAction>();
|
|
|
|
|
}
|
2022-07-14 14:43:10 +02:00
|
|
|
if (archived)
|
|
|
|
|
closeWallet();
|
2021-10-31 15:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
2022-04-06 20:51:00 +02:00
|
|
|
void AccountInfo::balanceHasChanged()
|
|
|
|
|
{
|
|
|
|
|
emit balanceChanged();
|
|
|
|
|
|
|
|
|
|
/* us getting called is very likely due to a new transaction that has been made known to the wallet. */
|
2022-07-21 16:37:18 +02:00
|
|
|
if (!m_hasFreshTransactions && m_wallet->isDecrypted()) {
|
2022-04-06 20:51:00 +02:00
|
|
|
// check this
|
|
|
|
|
const int blockHeight = m_wallet->lastTransactionTimestamp();
|
|
|
|
|
if (m_lastTxHeight < blockHeight) {
|
|
|
|
|
setHasFreshTransactions(true);
|
|
|
|
|
m_lastTxHeight = blockHeight;
|
|
|
|
|
}
|
|
|
|
|
if (blockHeight == 0) // an unconfirmed one
|
|
|
|
|
setHasFreshTransactions(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-20 22:52:57 +02:00
|
|
|
void AccountInfo::walletEncryptionChanged()
|
|
|
|
|
{
|
2022-07-20 20:20:23 +02:00
|
|
|
auto m = m_model.release();
|
|
|
|
|
if (m)
|
|
|
|
|
m->deleteLater();
|
|
|
|
|
auto s = m_secretsModel.release();
|
|
|
|
|
if (s)
|
|
|
|
|
s->deleteLater();
|
2022-06-20 22:52:57 +02:00
|
|
|
emit modelsChanged();
|
|
|
|
|
emit encryptionChanged();
|
|
|
|
|
emit nameChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-06 20:51:00 +02:00
|
|
|
bool AccountInfo::hasFreshTransactions() const
|
|
|
|
|
{
|
|
|
|
|
return m_hasFreshTransactions;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AccountInfo::setHasFreshTransactions(bool fresh)
|
|
|
|
|
{
|
|
|
|
|
if (m_hasFreshTransactions == fresh)
|
|
|
|
|
return;
|
|
|
|
|
m_hasFreshTransactions = fresh;
|
|
|
|
|
emit hasFreshTransactionsChanged();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 12:59:01 +02:00
|
|
|
bool AccountInfo::needsPinToPay() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->encryption() == Wallet::SecretsEncrypted;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AccountInfo::needsPinToOpen() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->encryption() == Wallet::FullyEncrypted;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 15:44:28 +02:00
|
|
|
bool AccountInfo::isDecrypted() const
|
|
|
|
|
{
|
|
|
|
|
if (m_wallet->encryption() == Wallet::NotEncrypted)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
const auto &secrets = m_wallet->walletSecrets();
|
|
|
|
|
if (secrets.empty())
|
|
|
|
|
return false;
|
|
|
|
|
for (auto i = secrets.begin(); i != secrets.end(); ++i) {
|
|
|
|
|
if (i->second.privKey.isValid())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-19 14:05:38 +02:00
|
|
|
void AccountInfo::setDefaultWallet(bool isDefault)
|
|
|
|
|
{
|
2021-12-04 11:25:27 +01:00
|
|
|
auto segment = m_wallet->segment();
|
2020-10-19 14:05:38 +02:00
|
|
|
if (!segment)
|
|
|
|
|
return;
|
2021-12-04 11:25:27 +01:00
|
|
|
if ((segment->priority() == PrivacySegment::First) == isDefault)
|
2020-10-19 14:05:38 +02:00
|
|
|
return;
|
|
|
|
|
segment->setPriority(isDefault ? PrivacySegment::First : PrivacySegment::Normal);
|
|
|
|
|
emit isDefaultWalletChanged();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool AccountInfo::isDefaultWallet()
|
|
|
|
|
{
|
|
|
|
|
auto segment =m_wallet->segment();
|
|
|
|
|
if (!segment)
|
|
|
|
|
return false;
|
|
|
|
|
return segment->priority() == PrivacySegment::First;
|
|
|
|
|
}
|
2020-12-17 23:12:39 +01:00
|
|
|
|
2021-04-21 15:17:08 +02:00
|
|
|
bool AccountInfo::userOwnedWallet()
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->userOwnedWallet();
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-07 20:10:09 +01:00
|
|
|
QList<QObject *> AccountInfo::paymentRequests() const
|
|
|
|
|
{
|
|
|
|
|
QList<QObject*> answer;
|
2021-04-21 15:17:08 +02:00
|
|
|
for (auto *pr : m_wallet->paymentRequests()) {
|
2021-01-07 20:10:09 +01:00
|
|
|
answer.append(pr);
|
|
|
|
|
}
|
|
|
|
|
return answer;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-17 23:12:39 +01:00
|
|
|
TransactionInfo* AccountInfo::txInfo(int walletIndex, QObject *parent)
|
|
|
|
|
{
|
|
|
|
|
Q_ASSERT(parent);
|
|
|
|
|
auto info = new TransactionInfo(parent);
|
|
|
|
|
m_wallet->fetchTransactionInfo(info, walletIndex);
|
|
|
|
|
return info;
|
|
|
|
|
}
|
2021-01-05 00:25:23 +01:00
|
|
|
|
|
|
|
|
QObject *AccountInfo::createPaymentRequest(QObject *parent)
|
|
|
|
|
{
|
|
|
|
|
return new PaymentRequest(m_wallet, parent);
|
|
|
|
|
}
|
2021-05-05 14:27:45 +02:00
|
|
|
|
2022-05-18 12:59:01 +02:00
|
|
|
void AccountInfo::encryptPinToPay(const QString &password)
|
|
|
|
|
{
|
2022-06-19 14:29:27 +02:00
|
|
|
m_wallet->setEncryption(Wallet::SecretsEncrypted, password);
|
2022-05-18 12:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AccountInfo::encryptPinToOpen(const QString &password)
|
|
|
|
|
{
|
2022-07-22 11:46:26 +02:00
|
|
|
if (m_closeWalletTimer)
|
|
|
|
|
m_closeWalletTimer->stop();
|
2022-06-19 14:29:27 +02:00
|
|
|
m_wallet->setEncryption(Wallet::FullyEncrypted, password);
|
2022-05-18 12:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
2022-05-18 20:16:34 +02:00
|
|
|
bool AccountInfo::decrypt(const QString &password)
|
2022-05-18 12:59:01 +02:00
|
|
|
{
|
2022-07-14 16:11:20 +02:00
|
|
|
const bool shouldAutoclose = m_wallet->encryption() == Wallet::SecretsEncrypted;
|
|
|
|
|
const bool success = m_wallet->decrypt(password);
|
|
|
|
|
if (shouldAutoclose && success) {
|
|
|
|
|
if (!m_closeWalletTimer) {
|
|
|
|
|
m_closeWalletTimer = new QTimer(this);
|
|
|
|
|
connect (m_closeWalletTimer, &QTimer::timeout, m_wallet, [=]() {
|
|
|
|
|
m_wallet->forgetEncryptedSecrets();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
m_closeWalletTimer->stop();
|
|
|
|
|
m_closeWalletTimer->start(200 * 1000); // 200s
|
|
|
|
|
}
|
|
|
|
|
return success;
|
2022-05-18 12:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AccountInfo::closeWallet()
|
|
|
|
|
{
|
|
|
|
|
// this forgets secrets
|
2022-06-19 14:29:27 +02:00
|
|
|
m_wallet->forgetEncryptedSecrets();
|
2022-07-14 16:11:20 +02:00
|
|
|
if (m_closeWalletTimer)
|
|
|
|
|
m_closeWalletTimer->stop();
|
2022-07-21 16:37:18 +02:00
|
|
|
setHasFreshTransactions(false);
|
2022-05-18 12:59:01 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-05 14:27:45 +02:00
|
|
|
bool AccountInfo::isSingleAddressAccount() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->isSingleAddressWallet();
|
|
|
|
|
}
|
2021-07-31 17:19:34 +02:00
|
|
|
|
2021-10-29 18:20:42 +02:00
|
|
|
bool AccountInfo::isHDWallet() const
|
2021-10-21 17:04:20 +02:00
|
|
|
{
|
|
|
|
|
return m_wallet->isHDWallet();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString AccountInfo::hdWalletMnemonic() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->hdWalletMnemonic();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString AccountInfo::hdDerivationPath() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->derivationPath();
|
|
|
|
|
}
|
2022-08-17 18:45:37 +02:00
|
|
|
|
|
|
|
|
QString AccountInfo::xpub() const
|
|
|
|
|
{
|
|
|
|
|
return m_wallet->xpub();
|
|
|
|
|
}
|