103 lines
2.7 KiB
C++
103 lines
2.7 KiB
C++
|
|
/*
|
||
|
|
* This file is part of the Flowee project
|
||
|
|
* Copyright (C) 2025 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 WALLETDATA_H
|
||
|
|
#define WALLETDATA_H
|
||
|
|
|
||
|
|
#include <QObject>
|
||
|
|
|
||
|
|
#include <Wallet.h>
|
||
|
|
|
||
|
|
#include <utils/streaming/ConstBuffer.h>
|
||
|
|
#include <utils/primitives/PrivateKey.h>
|
||
|
|
#include <utils/uint256.h>
|
||
|
|
#include <apputils/SimpleHttpClient.h>
|
||
|
|
|
||
|
|
class BackupSyncModuleInfo;
|
||
|
|
|
||
|
|
/*
|
||
|
|
* This object monitors the Wallet for us.
|
||
|
|
*
|
||
|
|
* We like to upload data based on actual events, events that are
|
||
|
|
* expected to change the save file, requiring a sync.
|
||
|
|
*/
|
||
|
|
class WalletData : public QObject
|
||
|
|
{
|
||
|
|
Q_OBJECT
|
||
|
|
public:
|
||
|
|
explicit WalletData(const std::shared_ptr<Wallet> &wallet, BackupSyncModuleInfo *parent);
|
||
|
|
|
||
|
|
std::shared_ptr<Wallet> wallet() const;
|
||
|
|
|
||
|
|
void setPrivKeyData(const Streaming::ConstBuffer &data);
|
||
|
|
const PrivateKey &privKey() const;
|
||
|
|
|
||
|
|
QString fancyAddess() const;
|
||
|
|
|
||
|
|
void markDirty(); // needs save, but it immediately schedules an upload.
|
||
|
|
bool needsSave() const;
|
||
|
|
|
||
|
|
uint32_t lastSavedTime() const;
|
||
|
|
void setLastSavedTime(uint32_t newLastSavedTime);
|
||
|
|
|
||
|
|
enum RestoreReason {
|
||
|
|
BeforeSave,
|
||
|
|
JustRestore
|
||
|
|
};
|
||
|
|
|
||
|
|
// call this to ensure that we can call fancyAddess() later.
|
||
|
|
bool deriveAuthKey();
|
||
|
|
|
||
|
|
signals:
|
||
|
|
void lastSavedChanged();
|
||
|
|
void restoreDone(bool success);
|
||
|
|
|
||
|
|
public slots:
|
||
|
|
void upload();
|
||
|
|
void restore(RestoreReason reason);
|
||
|
|
|
||
|
|
private slots:
|
||
|
|
void onTxUpdated(int txIndex);
|
||
|
|
void onTxConfirmed(int txIndex);
|
||
|
|
void onTxAdded(int firstNew, int count);
|
||
|
|
|
||
|
|
private:
|
||
|
|
struct SaveData {
|
||
|
|
Streaming::ConstBuffer data;
|
||
|
|
uint256 dataHash; // the unencrypted blob without the envelope
|
||
|
|
uint32_t timestamp = 0;
|
||
|
|
};
|
||
|
|
|
||
|
|
private slots:
|
||
|
|
SaveData save();
|
||
|
|
|
||
|
|
private:
|
||
|
|
void restoreFrom(const QString &savename);
|
||
|
|
void restoreDownload(const Streaming::ConstBuffer &blob);
|
||
|
|
|
||
|
|
|
||
|
|
BackupSyncModuleInfo *m_parent;
|
||
|
|
uint256 m_lastSaveHash;
|
||
|
|
std::shared_ptr<Wallet> m_wallet;
|
||
|
|
PrivateKey m_privateKey;
|
||
|
|
bool m_needsSave = false;
|
||
|
|
uint32_t m_lastSavedTime = 0; // epoch style timestamp (UTC)
|
||
|
|
std::shared_ptr<SimpleHttpClient> m_httpClient;
|
||
|
|
};
|
||
|
|
|
||
|
|
#endif
|