2020-05-24 13:20:03 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
|
|
|
|
* Copyright (C) 2020 Tom Zander <tomz@freedommail.ch>
|
|
|
|
|
*
|
|
|
|
|
* 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 FLOWEE_WALLET_H
|
|
|
|
|
#define FLOWEE_WALLET_H
|
|
|
|
|
|
|
|
|
|
#include <DataListenerInterface.h>
|
|
|
|
|
#include <PrivacySegment.h>
|
|
|
|
|
|
|
|
|
|
#include <primitives/key.h>
|
|
|
|
|
#include <primitives/pubkey.h>
|
2020-06-08 14:37:29 +02:00
|
|
|
#include <primitives/FastTransaction.h>
|
2020-05-24 13:20:03 +02:00
|
|
|
|
|
|
|
|
#include <boost/unordered_map.hpp>
|
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
2020-11-06 22:15:03 +01:00
|
|
|
#include <QRecursiveMutex>
|
2020-05-24 13:20:03 +02:00
|
|
|
#include <QString>
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
2020-11-04 21:58:17 +01:00
|
|
|
class WalletInfoObject;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
|
|
|
|
class Wallet : public QObject, public DataListenerInterface
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2020-10-15 19:18:54 +02:00
|
|
|
/**
|
|
|
|
|
* Create an empty, new, wallet.
|
|
|
|
|
*/
|
|
|
|
|
static Wallet *createWallet(const boost::filesystem::path &basedir, uint16_t segmentId, const QString &name = QString());
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Load existing wallet.
|
|
|
|
|
* This throws should there not be any data found.
|
|
|
|
|
*/
|
2020-05-24 13:20:03 +02:00
|
|
|
Wallet(const boost::filesystem::path &basedir, uint16_t segmentId);
|
2020-11-02 21:49:06 +01:00
|
|
|
~Wallet();
|
2020-05-24 13:20:03 +02:00
|
|
|
|
2020-06-08 14:31:45 +02:00
|
|
|
class OutputRef {
|
|
|
|
|
public:
|
|
|
|
|
explicit OutputRef(int txIndex = 0, int outputIndex = 0); // invalid
|
|
|
|
|
explicit OutputRef(uint64_t encoded);
|
|
|
|
|
OutputRef(const OutputRef &output) = default;
|
|
|
|
|
uint64_t encoded() const;
|
|
|
|
|
|
|
|
|
|
inline int txIndex() const {
|
|
|
|
|
return m_txid;
|
|
|
|
|
}
|
|
|
|
|
inline int outputIndex() const {
|
|
|
|
|
return m_outputIndex;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
inline void setTxIndex(int index) {
|
|
|
|
|
m_txid = index;
|
|
|
|
|
}
|
|
|
|
|
inline void setOutputIndex(int index) {
|
|
|
|
|
m_outputIndex = index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
uint32_t m_txid = 0; // index in m_walletTransactions
|
|
|
|
|
uint16_t m_outputIndex = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
/**
|
|
|
|
|
* @brief newTransactions announces a list of transactions pushed to us from a peer.
|
|
|
|
|
* @param header the block header these transactions appeared in.
|
|
|
|
|
* @param blockHeight the blockheight we know the header under.
|
|
|
|
|
* @param blockTransactions The actual transactions.
|
|
|
|
|
*/
|
|
|
|
|
void newTransactions(const BlockHeader &header, int blockHeight, const std::deque<Tx> &blockTransactions) override;
|
|
|
|
|
// notify about unconfirmed Tx.
|
|
|
|
|
void newTransaction(const Tx &tx) override;
|
2020-11-06 20:05:42 +01:00
|
|
|
/// Let the wallet know that it is up-to-date to \a height
|
2020-10-17 17:34:40 +02:00
|
|
|
void setLastSynchedBlockHeight(int height) override;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
|
|
|
|
PrivacySegment *segment() const;
|
|
|
|
|
|
2020-11-06 20:05:42 +01:00
|
|
|
/// Create a new private key.
|
2020-05-24 13:20:03 +02:00
|
|
|
void createNewPrivateKey(int currentBlockheight);
|
2020-11-06 20:05:42 +01:00
|
|
|
/// import an existing private key.
|
2020-05-24 13:20:03 +02:00
|
|
|
bool addPrivateKey(const QString &privKey, int startBlockHeight);
|
2020-11-06 20:05:42 +01:00
|
|
|
/// Save changed in historical wallet
|
2020-05-24 13:20:03 +02:00
|
|
|
void saveWallet();
|
|
|
|
|
|
2020-11-06 22:15:03 +01:00
|
|
|
qint64 balanceConfirmed() const {
|
|
|
|
|
return m_balanceConfirmed;
|
|
|
|
|
}
|
|
|
|
|
qint64 balanceImmature() const {
|
|
|
|
|
return m_balanceImmature;
|
|
|
|
|
}
|
|
|
|
|
qint64 balanceUnconfirmed() const {
|
|
|
|
|
return m_balanceUnconfirmed;
|
|
|
|
|
}
|
2020-11-02 21:49:06 +01:00
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
/// return the amount of UTXOs that hold money
|
|
|
|
|
int unspentOutputCount() const;
|
|
|
|
|
/// return the amount of UTXOs ever created for this account.
|
|
|
|
|
int historicalOutputCount() const;
|
|
|
|
|
|
2020-11-06 20:05:42 +01:00
|
|
|
/// the user-visible name for the wallet.
|
2020-05-24 13:20:03 +02:00
|
|
|
QString name() const;
|
2020-11-06 20:05:42 +01:00
|
|
|
/// set the user-visible name for the wallet.
|
2020-05-24 13:20:03 +02:00
|
|
|
void setName(const QString &name);
|
|
|
|
|
|
2020-11-06 20:05:42 +01:00
|
|
|
/// Fetch UTXO txid
|
2020-06-08 14:37:29 +02:00
|
|
|
const uint256 &txid(OutputRef ref) const;
|
2020-11-06 20:05:42 +01:00
|
|
|
/// Fetch UTXO output
|
2020-06-08 14:37:29 +02:00
|
|
|
Tx::Output txOutout(OutputRef ref) const;
|
2020-11-06 20:05:42 +01:00
|
|
|
/// Fetch UTXO key
|
2020-06-08 14:37:29 +02:00
|
|
|
const CKey &unlockKey(OutputRef ref) const;
|
|
|
|
|
|
2020-11-06 20:05:42 +01:00
|
|
|
/// Return the bitcoin address (160 bits ripe key) for change.
|
2020-06-11 10:24:05 +02:00
|
|
|
CKeyID nextChangeAddress();
|
2020-06-08 14:37:29 +02:00
|
|
|
|
2020-10-23 19:45:08 +02:00
|
|
|
struct OutputSet {
|
|
|
|
|
std::vector<OutputRef> outputs;
|
|
|
|
|
qint64 totalSats = 0;
|
|
|
|
|
int fee = 0;
|
|
|
|
|
};
|
2020-06-08 14:37:29 +02:00
|
|
|
/**
|
|
|
|
|
* @brief findInputsFor UTXO fulfilment algo finding the inputs for your tx.
|
|
|
|
|
* @param output The amount of satoshis you want to make available (after fee).
|
2020-10-23 22:34:34 +02:00
|
|
|
* A special value of -1 indicates all outputs.
|
2020-06-08 14:37:29 +02:00
|
|
|
* @param feePerByte fee per byte
|
|
|
|
|
* @param txSize the size of the transaction before we add inputs
|
|
|
|
|
* @param[out] change the amount of satoshis we over-provided for the expected \a output.
|
|
|
|
|
* @return The references to the outputs we suggest you fund your tx with.
|
|
|
|
|
*/
|
2020-10-23 19:45:08 +02:00
|
|
|
OutputSet findInputsFor(qint64 output, int feePerByte, int txSize, int64_t &change) const;
|
2020-06-08 14:37:29 +02:00
|
|
|
|
2020-06-11 10:24:05 +02:00
|
|
|
bool isSingleAddressWallet() const;
|
|
|
|
|
void setSingleAddressWallet(bool isSingleAddressWallet);
|
|
|
|
|
|
2020-11-04 21:58:17 +01:00
|
|
|
private slots:
|
|
|
|
|
void broadcastTxFinished(int txIndex, bool success);
|
2020-11-05 22:21:50 +01:00
|
|
|
/// find all not-yet-confirmed transactions and start a broadcast
|
|
|
|
|
void broadcastUnconfirmed();
|
2020-11-06 22:15:03 +01:00
|
|
|
void recalculateBalance();
|
2020-11-04 21:58:17 +01:00
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
signals:
|
2020-11-06 22:15:03 +01:00
|
|
|
void balanceChanged();
|
2020-05-24 13:20:03 +02:00
|
|
|
void utxosChanged();
|
|
|
|
|
void appendedTransactions(int firstNew, int count);
|
2020-10-17 17:34:40 +02:00
|
|
|
void lastBlockSynchedChanged();
|
2020-05-24 13:20:03 +02:00
|
|
|
|
2020-10-15 19:18:54 +02:00
|
|
|
protected:
|
|
|
|
|
Wallet();
|
|
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
private:
|
|
|
|
|
void loadWallet();
|
|
|
|
|
void loadSecrets();
|
|
|
|
|
void saveSecrets();
|
|
|
|
|
|
2020-11-06 20:05:42 +01:00
|
|
|
int findSecretFor(const Streaming::ConstBuffer &outputScript) const;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
|
|
|
|
void rebuildBloom();
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<PrivacySegment> m_segment;
|
2020-11-06 22:15:03 +01:00
|
|
|
mutable QRecursiveMutex m_lock;
|
2020-05-24 13:20:03 +02:00
|
|
|
bool m_walletChanged = false;
|
|
|
|
|
bool m_secretsChanged = false;
|
|
|
|
|
|
|
|
|
|
struct WalletSecret {
|
|
|
|
|
CKey privKey;
|
|
|
|
|
CKeyID address;
|
|
|
|
|
int initialHeight = -1;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int m_nextWalletSecretId = 1;
|
|
|
|
|
int m_nextWalletTransactionId = 1;
|
2020-11-05 22:21:50 +01:00
|
|
|
int m_lastBlockHeightSeen = 0;
|
2020-05-24 13:20:03 +02:00
|
|
|
short m_bloomScore = 0;
|
|
|
|
|
std::map<int, WalletSecret> m_walletSecrets;
|
|
|
|
|
|
|
|
|
|
QString m_name;
|
|
|
|
|
|
|
|
|
|
struct Output {
|
|
|
|
|
int walletSecretId = -1;
|
|
|
|
|
uint64_t value;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct WalletTransaction {
|
|
|
|
|
uint256 txid;
|
|
|
|
|
uint256 minedBlock;
|
|
|
|
|
int minedBlockHeight = 0;
|
2020-11-06 20:05:42 +01:00
|
|
|
bool isCoinbase = false;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
|
|
|
|
// One entry for inputs that spent outputs in this wallet.
|
|
|
|
|
// The key is the input. They value is a composition of the output-index (lower 2 bytes)
|
|
|
|
|
// and the int-key in m_walletTransactions is the middle 4 bytes.
|
|
|
|
|
std::map<int, uint64_t> inputToWTX;
|
|
|
|
|
std::map<int, Output> outputs; // output-index to Output (only ones we can/could spend)
|
|
|
|
|
};
|
|
|
|
|
|
2020-11-06 20:05:42 +01:00
|
|
|
WalletTransaction createWalletTransactionFromTx(const Tx &tx, const uint256 &txid) const;
|
2020-11-02 21:49:06 +01:00
|
|
|
void saveTransaction(const Tx &tx);
|
2020-11-05 22:21:50 +01:00
|
|
|
Tx loadTransaction(const uint256 &txid, Streaming::BufferPool &pool);
|
2020-11-02 21:49:06 +01:00
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
std::map<int, WalletTransaction> m_walletTransactions;
|
|
|
|
|
|
|
|
|
|
typedef boost::unordered_map<uint256, int, HashShortener> TxIdCash;
|
|
|
|
|
TxIdCash m_txidCash; // txid -> m_walletTransactions-id
|
|
|
|
|
|
|
|
|
|
// cache
|
|
|
|
|
std::map<uint64_t, uint64_t> m_unspentOutputs; // composited output -> value (in sat).
|
2020-11-02 21:49:06 +01:00
|
|
|
// composited outputs that have been used in a transaction and should not be spent again.
|
2020-11-03 20:16:36 +01:00
|
|
|
// the 'value' is the WalletTransaction index that actually was responsible for locking it.
|
|
|
|
|
std::map<uint64_t, int> m_autoLockedOutputs;
|
2020-05-24 13:20:03 +02:00
|
|
|
boost::filesystem::path m_basedir;
|
|
|
|
|
|
2020-11-06 22:15:03 +01:00
|
|
|
qint64 m_balanceConfirmed = 0;
|
|
|
|
|
qint64 m_balanceImmature = 0;
|
|
|
|
|
qint64 m_balanceUnconfirmed = 0;
|
|
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
friend class WalletHistoryModel;
|
2020-06-11 10:24:05 +02:00
|
|
|
|
|
|
|
|
bool m_singleAddressWallet = false;
|
2020-11-04 21:58:17 +01:00
|
|
|
|
|
|
|
|
QList<std::shared_ptr<WalletInfoObject>> m_broadcastingTransactions;
|
2020-05-24 13:20:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|