2020-05-24 13:20:03 +02:00
|
|
|
/*
|
|
|
|
|
* This file is part of the Flowee project
|
2022-04-07 18:16:03 +02:00
|
|
|
* Copyright (C) 2020-2022 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 FLOWEE_WALLET_H
|
|
|
|
|
#define FLOWEE_WALLET_H
|
|
|
|
|
|
2022-05-17 00:44:51 +02:00
|
|
|
#include <QObject>
|
|
|
|
|
#include <QMutex>
|
|
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
#include <DataListenerInterface.h>
|
|
|
|
|
#include <PrivacySegment.h>
|
|
|
|
|
|
2022-05-17 00:44:51 +02:00
|
|
|
#include <HDMasterKey.h>
|
2022-07-11 18:03:21 +02:00
|
|
|
#include <HDMasterPubkey.h>
|
2020-05-24 13:20:03 +02:00
|
|
|
#include <primitives/key.h>
|
|
|
|
|
#include <primitives/pubkey.h>
|
2021-11-02 11:09:56 +01:00
|
|
|
#include <primitives/Tx.h>
|
2020-05-24 13:20:03 +02:00
|
|
|
|
|
|
|
|
#include <boost/unordered_map.hpp>
|
|
|
|
|
#include <boost/filesystem.hpp>
|
|
|
|
|
|
2020-11-04 21:58:17 +01:00
|
|
|
class WalletInfoObject;
|
2020-12-17 23:12:39 +01:00
|
|
|
class TransactionInfo;
|
2021-01-06 23:15:54 +01:00
|
|
|
class PaymentRequest;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
2021-02-05 16:56:02 +01:00
|
|
|
namespace P2PNet {
|
|
|
|
|
struct Notification;
|
|
|
|
|
}
|
|
|
|
|
|
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.
|
|
|
|
|
*/
|
2022-05-17 22:26:48 +02:00
|
|
|
Wallet(const boost::filesystem::path &basedir, uint16_t segmentId, uint32_t encryptionSeed = 0);
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-31 16:56:03 +01:00
|
|
|
inline bool isValid() const {
|
|
|
|
|
return m_txid != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-11 19:26:59 +02:00
|
|
|
Wallet::OutputRef& operator=(const OutputRef&) = default;
|
|
|
|
|
|
2020-06-08 14:31:45 +02:00
|
|
|
private:
|
|
|
|
|
uint32_t m_txid = 0; // index in m_walletTransactions
|
|
|
|
|
uint16_t m_outputIndex = 0;
|
|
|
|
|
};
|
|
|
|
|
|
2021-05-04 17:41:49 +02:00
|
|
|
enum SignatureType {
|
|
|
|
|
SignedAsEcdsa = 0,
|
|
|
|
|
SignedAsSchnorr,
|
|
|
|
|
NotUsedYet,
|
|
|
|
|
};
|
|
|
|
|
|
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;
|
2021-10-13 18:11:49 +02:00
|
|
|
void headerSyncComplete() 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.
|
2021-05-28 18:13:50 +02:00
|
|
|
void createNewPrivateKey(uint32_t currentBlockheight);
|
2020-11-06 20:05:42 +01:00
|
|
|
/// import an existing private key.
|
2021-05-28 18:13:50 +02:00
|
|
|
bool addPrivateKey(const QString &privKey, uint32_t 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-12-17 23:12:39 +01:00
|
|
|
inline const uint256 &txid(OutputRef ref) const {
|
|
|
|
|
return txid(ref.txIndex());
|
|
|
|
|
}
|
|
|
|
|
/// Fetch UTXO txid
|
|
|
|
|
const uint256 &txid(int txIndex) const;
|
2020-11-06 20:05:42 +01:00
|
|
|
/// Fetch UTXO output
|
2021-11-08 15:21:53 +01:00
|
|
|
Tx::Output txOutput(OutputRef ref) const;
|
2021-11-23 16:46:37 +01:00
|
|
|
/// Fetch UTXO value (in sats)
|
|
|
|
|
qint64 utxoOutputValue(OutputRef ref) const;
|
2021-05-04 17:41:49 +02:00
|
|
|
|
|
|
|
|
struct PrivKeyData {
|
|
|
|
|
int privKeyId = 0;
|
2022-07-06 22:15:08 +02:00
|
|
|
PrivateKey key;
|
2021-05-04 17:41:49 +02:00
|
|
|
SignatureType sigType;
|
|
|
|
|
};
|
2020-11-06 20:05:42 +01:00
|
|
|
/// Fetch UTXO key
|
2021-05-04 17:41:49 +02:00
|
|
|
PrivKeyData unlockKey(OutputRef ref) const;
|
|
|
|
|
|
2021-11-08 15:23:29 +01:00
|
|
|
/**
|
|
|
|
|
* Register the signature type for the given private key.
|
|
|
|
|
* The unlockKey() method will return the sigType stored for
|
|
|
|
|
* a specific private key. By calling this method we
|
|
|
|
|
* store the signature type for this key, for future requests.
|
|
|
|
|
*/
|
2021-05-04 17:41:49 +02:00
|
|
|
void updateSignatureType(const PrivKeyData &data);
|
2020-06-08 14:37:29 +02:00
|
|
|
|
2021-01-06 15:26:46 +01:00
|
|
|
/// Return a bitcoin address (160 bits ripe key) for deposit.
|
2022-07-06 22:06:58 +02:00
|
|
|
KeyId nextUnusedAddress();
|
2021-01-06 15:26:46 +01:00
|
|
|
|
2021-11-09 10:37:31 +01:00
|
|
|
/// Return a bitcoin address (160 bits ripe key) for change.
|
2022-07-06 22:06:58 +02:00
|
|
|
KeyId nextUnusedChangeAddress();
|
2021-11-09 10:37:31 +01:00
|
|
|
|
|
|
|
|
enum PrivKeyType {
|
|
|
|
|
ChangePath, ///< The private keys created from a HD wallets change derivation
|
|
|
|
|
ReceivePath ///< All the other types of private keys
|
|
|
|
|
};
|
|
|
|
|
|
2021-01-06 15:26:46 +01:00
|
|
|
/// Return a private-key-index for deposits and reserve it from re-use.
|
2022-07-06 22:06:58 +02:00
|
|
|
int reserveUnusedAddress(KeyId &keyId, PrivKeyType pkt = ReceivePath);
|
2021-01-06 15:26:46 +01:00
|
|
|
|
|
|
|
|
/// The opposite of reserveUnusedAddress, free for usage an address.
|
|
|
|
|
void unreserveAddress(int index);
|
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
|
|
|
|
2021-11-08 15:23:29 +01:00
|
|
|
/// a simple boolean that indicates only address zero will ever be used.
|
2020-06-11 10:24:05 +02:00
|
|
|
bool isSingleAddressWallet() const;
|
2021-11-08 15:23:29 +01:00
|
|
|
void setSingleAddressWallet(bool on);
|
2020-06-11 10:24:05 +02:00
|
|
|
|
2021-11-08 15:23:29 +01:00
|
|
|
// Fill the transactionInfo object from walletTransaction \a txIndex
|
2020-12-17 23:12:39 +01:00
|
|
|
void fetchTransactionInfo(TransactionInfo *info, int txIndex);
|
|
|
|
|
|
2021-01-06 23:15:54 +01:00
|
|
|
void addPaymentRequest(PaymentRequest *pr);
|
|
|
|
|
void removePaymentRequest(PaymentRequest *pr);
|
|
|
|
|
|
2021-01-07 20:10:09 +01:00
|
|
|
QList<PaymentRequest *> paymentRequests() const;
|
|
|
|
|
|
2022-05-13 19:29:25 +02:00
|
|
|
/**
|
|
|
|
|
* Returns a 'seed' to add to the user password on a (partially) encrypted
|
|
|
|
|
* wallet.
|
|
|
|
|
* \see encryptWallet()
|
|
|
|
|
*/
|
|
|
|
|
uint32_t encryptionSeed() const;
|
|
|
|
|
|
2022-05-14 14:27:51 +02:00
|
|
|
/**
|
|
|
|
|
* For an already encrypted wallet, set the seed.
|
|
|
|
|
* The usage is, in order;
|
|
|
|
|
* @code
|
|
|
|
|
* setEncryptionSeed(walletSeed);
|
2022-06-19 14:29:27 +02:00
|
|
|
* setEncryptionLevel(walletLevel, userPassword);
|
2022-05-14 14:27:51 +02:00
|
|
|
* @code
|
|
|
|
|
*
|
|
|
|
|
* After this methods like decrypt() become available as well as the ability
|
|
|
|
|
* to sync a wallet that auto-generates its own keys.
|
|
|
|
|
*/
|
2022-05-13 19:29:25 +02:00
|
|
|
void setEncryptionSeed(uint32_t newEncryptionSeed);
|
|
|
|
|
|
|
|
|
|
enum EncryptionLevel {
|
|
|
|
|
NotEncrypted,
|
|
|
|
|
/**
|
|
|
|
|
* This will encrypt the private keys of the wallet, which effectively
|
|
|
|
|
* removes only one functio of the wallet, to sign transactions spending funds.
|
|
|
|
|
*/
|
|
|
|
|
SecretsEncrypted,
|
|
|
|
|
/**
|
|
|
|
|
* This level of encryption aims to encrypt all functionality of
|
|
|
|
|
* the wallet to the level that even reading the on-disk files
|
|
|
|
|
* an attacker will not be able to figure out which addresses are ours.
|
|
|
|
|
*/
|
|
|
|
|
FullyEncrypted
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the level of encryption applied to this wallet's saved state.
|
|
|
|
|
*
|
|
|
|
|
* When the wallet changes from NotEncrypted to a higher level, we
|
|
|
|
|
* calculate an encryptionSeed() and (re)write an encrypted version
|
|
|
|
|
* of the wallet to disk.
|
|
|
|
|
*
|
|
|
|
|
* Setting a level higher than NotEncrypted will remove the relevant
|
|
|
|
|
* data from the in-memory representation of this wallet, requiring
|
2022-06-19 14:29:27 +02:00
|
|
|
* a call to decrypt() before usage.
|
2022-05-13 19:29:25 +02:00
|
|
|
*/
|
2022-06-19 14:29:27 +02:00
|
|
|
void setEncryption(EncryptionLevel level, const QString &password);
|
2022-05-13 19:29:25 +02:00
|
|
|
EncryptionLevel encryption() const;
|
|
|
|
|
/// Read encrypted secrets from disk into memory
|
2022-06-19 14:29:27 +02:00
|
|
|
bool decrypt(const QString &password);
|
2022-06-23 13:58:36 +02:00
|
|
|
bool isDecrypted() const;
|
2022-06-19 14:29:27 +02:00
|
|
|
|
|
|
|
|
// forget all encrypted secrets.
|
|
|
|
|
void forgetEncryptedSecrets();
|
2022-05-13 19:29:25 +02:00
|
|
|
|
2021-01-31 22:51:55 +01:00
|
|
|
#ifdef IN_TESTS
|
|
|
|
|
/**
|
|
|
|
|
* Unit tests can call this to populate a wallet with some dummy transactions.
|
|
|
|
|
*/
|
|
|
|
|
void addTestTransactions();
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-04-21 15:17:08 +02:00
|
|
|
/**
|
2021-11-08 15:23:29 +01:00
|
|
|
* When true, then this wallet is the result of a user-initiated action.
|
2021-04-21 15:17:08 +02:00
|
|
|
* Otherwise it was created by the system for some reason and is empty.
|
|
|
|
|
*/
|
|
|
|
|
bool userOwnedWallet() const;
|
|
|
|
|
/**
|
|
|
|
|
* Changes a wallet to be user-owned or not.
|
|
|
|
|
*/
|
|
|
|
|
void setUserOwnedWallet(bool userOwnedWallet);
|
|
|
|
|
|
2021-11-29 23:16:45 +01:00
|
|
|
/**
|
|
|
|
|
* Set comment field on this transaction, assuming it has been accepted by this wallet.
|
|
|
|
|
*/
|
|
|
|
|
void setTransactionComment(const Tx &transaction, const QString &comment);
|
|
|
|
|
|
2021-07-31 17:19:34 +02:00
|
|
|
struct WalletSecret {
|
2022-07-06 22:15:08 +02:00
|
|
|
PrivateKey privKey;
|
2022-07-12 15:12:50 +02:00
|
|
|
std::vector<char> encryptedPrivKey;
|
2022-07-06 22:06:58 +02:00
|
|
|
KeyId address;
|
2021-10-21 17:04:20 +02:00
|
|
|
/*
|
|
|
|
|
* initial height for a secret key is relevant for the getmerkleblock call.
|
|
|
|
|
* The special value 0 implies that the key is created but not yet shared and thus
|
|
|
|
|
* initial height is still in the future.
|
|
|
|
|
*/
|
2021-07-31 17:19:34 +02:00
|
|
|
uint32_t initialHeight = 0;
|
|
|
|
|
SignatureType signatureType = NotUsedYet;
|
2021-10-21 14:57:20 +02:00
|
|
|
bool fromHdWallet = false;
|
|
|
|
|
bool fromChangeChain = false;
|
|
|
|
|
int hdDerivationIndex = -1;
|
2021-07-31 17:19:34 +02:00
|
|
|
/// if true, this address has been reseved to receive funds on
|
|
|
|
|
bool reserved = false; // in-mem-only
|
2022-05-18 12:59:01 +02:00
|
|
|
|
|
|
|
|
void setPrivKey(const Streaming::ConstBuffer &data);
|
2021-07-31 17:19:34 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// Return the private keys and details owned by this wallet.
|
|
|
|
|
std::map<int, WalletSecret> walletSecrets() const;
|
|
|
|
|
|
2021-10-30 15:23:43 +02:00
|
|
|
struct KeyDetails {
|
|
|
|
|
qint64 saldo; ///< The amount of Satoshi currently on this key.
|
|
|
|
|
int coins; ///< The amount of coins that make up the current saldo
|
|
|
|
|
int historicalCoins; ///< The amount of coins ever seen on this key.
|
|
|
|
|
};
|
|
|
|
|
KeyDetails fetchKeyDetails(int privKeyId) const;
|
2022-07-06 22:06:58 +02:00
|
|
|
int findPrivKeyId(const KeyId &address) const;
|
2021-07-31 17:19:34 +02:00
|
|
|
|
2021-10-14 14:42:27 +02:00
|
|
|
/// Returns true if this wallet is backed by a Hierarchically Deterministic seed.
|
|
|
|
|
bool isHDWallet() const;
|
|
|
|
|
/// Provided that this is a HD wallet, return the seed-words (aka mnemonic)
|
|
|
|
|
QString hdWalletMnemonic() const;
|
|
|
|
|
/// Provided that this is a HD wallet, return the seed-words (aka mnemonic) passphrase
|
|
|
|
|
QString hdWalletMnemonicPwd() const;
|
|
|
|
|
/// Provided that this is a HD wallet, return the derivation path used for this wallet.
|
|
|
|
|
QString derivationPath() const;
|
|
|
|
|
/**
|
|
|
|
|
* Create a HD masterkey that will be used for future creation of new private keys
|
|
|
|
|
*
|
|
|
|
|
* @param mnemonic The seed-string. Please validate the mnemonic before you pass it in here! This method assumes it is valid.
|
|
|
|
|
* @param pwd the password that was created with the seed-phrase. Can be empty.
|
|
|
|
|
* @param derivationPath the derivation steps. We will add 2 ints to this internally, so typically this vector has 3 fields.
|
2021-10-21 17:04:20 +02:00
|
|
|
* @param initialBlockHeight either a timestamp or blockheight, or zero if unsure.
|
2021-10-14 14:42:27 +02:00
|
|
|
*/
|
2021-10-21 20:54:34 +02:00
|
|
|
void createHDMasterKey(const QString &mnemonic, const QString &pwd, const std::vector<uint32_t> &derivationPath, uint32_t startHeight = 0);
|
2021-10-14 14:42:27 +02:00
|
|
|
|
2021-10-31 15:19:35 +01:00
|
|
|
/// return the height of the last seen transaction that is mined
|
|
|
|
|
int lastTransactionTimestamp() const;
|
|
|
|
|
|
2021-11-08 15:24:48 +01:00
|
|
|
/**
|
|
|
|
|
* Lock a the output from being spent in auto-generated transactions.
|
|
|
|
|
* @returns true if successful, false if the UTXO did not exist.
|
|
|
|
|
*/
|
|
|
|
|
bool lockUTXO(OutputRef outputRef);
|
|
|
|
|
bool unlockUTXO(OutputRef outputRef);
|
2021-11-23 17:46:06 +01:00
|
|
|
bool isLocked(OutputRef outputRef) const;
|
2021-11-08 15:24:48 +01:00
|
|
|
|
2021-11-23 16:46:37 +01:00
|
|
|
/// Check the loaded wallet version Id and make internal changes to upgrade it to current.
|
2021-11-02 14:24:47 +01:00
|
|
|
void performUpgrades();
|
|
|
|
|
|
2021-10-25 19:42:13 +02:00
|
|
|
public slots:
|
|
|
|
|
void delayedSave();
|
2021-10-14 14:42:27 +02:00
|
|
|
|
2021-11-08 19:35:48 +01:00
|
|
|
protected slots:
|
2020-11-04 21:58:17 +01:00
|
|
|
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();
|
2021-07-30 15:56:52 +02:00
|
|
|
void delayedSaveTimeout();
|
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();
|
2021-01-07 20:10:09 +01:00
|
|
|
void paymentRequestsChanged();
|
2021-04-21 15:17:08 +02:00
|
|
|
void userOwnedChanged();
|
2021-12-04 19:13:26 +01:00
|
|
|
void transactionChanged(int txIndex);
|
2021-01-27 20:03:02 +01:00
|
|
|
void transactionConfirmed(int txIndex);
|
2022-05-18 12:59:01 +02:00
|
|
|
void encryptionChanged();
|
2021-01-27 20:03:02 +01:00
|
|
|
|
2021-07-30 15:56:52 +02:00
|
|
|
// \internal
|
|
|
|
|
void startDelayedSave();
|
|
|
|
|
|
2020-10-15 19:18:54 +02:00
|
|
|
protected:
|
|
|
|
|
Wallet();
|
|
|
|
|
|
2022-05-17 16:21:59 +02:00
|
|
|
#ifndef IN_TESTS
|
2020-05-24 13:20:03 +02:00
|
|
|
private:
|
2022-05-17 16:21:59 +02:00
|
|
|
#endif
|
2021-01-28 22:57:27 +01:00
|
|
|
/// see also saveWallet()
|
2020-05-24 13:20:03 +02:00
|
|
|
void loadWallet();
|
2022-05-14 14:27:51 +02:00
|
|
|
// called by loadSecrets and decrypt()
|
|
|
|
|
Streaming::ConstBuffer readSecrets() const;
|
2020-05-24 13:20:03 +02:00
|
|
|
void loadSecrets();
|
|
|
|
|
void saveSecrets();
|
|
|
|
|
|
2021-01-28 22:57:27 +01:00
|
|
|
/**
|
|
|
|
|
* Pass in an outputscript (scriptPubKey) and return the
|
|
|
|
|
* wallet-secret ID that can unlock it.
|
|
|
|
|
*
|
|
|
|
|
* @return the wallet-secret-id or -1 if none match.
|
|
|
|
|
*/
|
2020-11-06 20:05:42 +01:00
|
|
|
int findSecretFor(const Streaming::ConstBuffer &outputScript) const;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
2021-01-28 22:57:27 +01:00
|
|
|
/// Fill the bloom filter with all the unspent transactions and addresses we handle.
|
2020-05-24 13:20:03 +02:00
|
|
|
void rebuildBloom();
|
|
|
|
|
|
2021-10-31 16:56:03 +01:00
|
|
|
// helper method called from both newTransaction and newTransactions
|
2022-05-11 21:00:48 +02:00
|
|
|
void updateSignatureTypes(const std::map<uint64_t, SignatureType> &txData);
|
2021-10-31 16:56:03 +01:00
|
|
|
|
2021-11-26 16:09:42 +01:00
|
|
|
/// Helper method for findInputsFor
|
|
|
|
|
int scoreForSolution(const OutputSet &set, int64_t change,
|
|
|
|
|
size_t unspentOutputCount) const;
|
|
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
std::unique_ptr<PrivacySegment> m_segment;
|
2021-02-01 21:58:06 +01:00
|
|
|
mutable QMutex m_lock;
|
2021-01-28 22:57:27 +01:00
|
|
|
/// used to determine if we need to persist the wallet
|
2020-05-24 13:20:03 +02:00
|
|
|
bool m_walletChanged = false;
|
2022-06-24 15:02:52 +02:00
|
|
|
/// Used to determine if we need to save the wallet-name file
|
|
|
|
|
bool m_walletNameChanged = false;
|
2021-01-28 22:57:27 +01:00
|
|
|
/// used to determine if we need to persist the private keys
|
2020-05-24 13:20:03 +02:00
|
|
|
bool m_secretsChanged = false;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
2021-10-14 14:42:27 +02:00
|
|
|
struct HierarchicallyDeterministicWalletData {
|
|
|
|
|
/// the strings should have utf8 encoded text.
|
2022-07-14 17:03:00 +02:00
|
|
|
HierarchicallyDeterministicWalletData(const Streaming::ConstBuffer &seedWords, const std::vector<uint32_t> &derivationPath, const Streaming::ConstBuffer &pwd);
|
2022-07-11 18:03:21 +02:00
|
|
|
HierarchicallyDeterministicWalletData(const std::string &xpub, const std::vector<uint32_t> &derivationPath);
|
2021-10-14 14:42:27 +02:00
|
|
|
HDMasterKey masterKey;
|
2022-07-11 18:03:21 +02:00
|
|
|
HDMasterPubkey masterPubkey;
|
2022-07-14 15:05:02 +02:00
|
|
|
std::vector<char, secure_allocator<char> > walletMnemonic; // utf8-encoding
|
|
|
|
|
std::vector<char, secure_allocator<char> > walletMnemonicPwd;// utf8-encoding
|
2022-07-12 15:12:50 +02:00
|
|
|
|
|
|
|
|
std::vector<char> encryptedWalletMnemonic;
|
|
|
|
|
std::vector<char> encryptedWalletMnemonicPwd;
|
|
|
|
|
|
2021-10-21 14:57:20 +02:00
|
|
|
std::vector<uint32_t> derivationPath; // contains the last created privkey. (full derivation path)
|
2021-11-01 13:33:33 +01:00
|
|
|
int lastMainKey = -1; // for derivation {BASE} + 0 / [num]
|
|
|
|
|
int lastChangeKey = -1; // for derivation {BASE} + 1 / [num]
|
|
|
|
|
// two mem-only limits indicating the last key included in the bloom filter.
|
|
|
|
|
int lastIncludedMainKey = -1;
|
|
|
|
|
int lastIncludedChangeKey = -1;
|
2021-10-14 14:42:27 +02:00
|
|
|
};
|
|
|
|
|
std::unique_ptr<HierarchicallyDeterministicWalletData> m_hdData;
|
|
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
QString m_name;
|
2021-11-02 14:24:47 +01:00
|
|
|
int m_walletVersion;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
|
|
|
|
struct Output {
|
|
|
|
|
int walletSecretId = -1;
|
2020-12-25 23:27:14 +01:00
|
|
|
uint64_t value = 0;
|
2020-05-24 13:20:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct WalletTransaction {
|
|
|
|
|
uint256 txid;
|
|
|
|
|
uint256 minedBlock;
|
|
|
|
|
int minedBlockHeight = 0;
|
2020-11-06 20:05:42 +01:00
|
|
|
bool isCoinbase = false;
|
2021-11-16 15:01:02 +01:00
|
|
|
bool isCashFusionTx = false;
|
2021-01-15 14:01:01 +01:00
|
|
|
QString userComment;
|
2020-05-24 13:20:03 +02:00
|
|
|
|
2021-11-09 20:49:37 +01:00
|
|
|
bool isUnconfirmed() const;
|
|
|
|
|
bool isRejected() const;
|
|
|
|
|
|
2021-05-03 12:40:48 +02:00
|
|
|
/*
|
2021-11-02 14:24:47 +01:00
|
|
|
* To keep track of chained transactions we store for an input a entry here,
|
|
|
|
|
* should it come from an output stored in the walletTransactins map.
|
|
|
|
|
*
|
|
|
|
|
* Notice that only outputs matching one of our keys are eligable, so these
|
|
|
|
|
* are inputs that spent them.
|
|
|
|
|
*
|
|
|
|
|
* Key: index of input.
|
|
|
|
|
* Value: encoded OutputRef
|
2021-05-03 12:40:48 +02:00
|
|
|
*/
|
2020-05-24 13:20:03 +02:00
|
|
|
std::map<int, uint64_t> inputToWTX;
|
|
|
|
|
std::map<int, Output> outputs; // output-index to Output (only ones we can/could spend)
|
|
|
|
|
};
|
|
|
|
|
|
2021-10-31 16:56:03 +01:00
|
|
|
/**
|
|
|
|
|
* Parse a transaction and match the inputs / outputs to our UTXO and known addresses.
|
|
|
|
|
* This creates a fully populated WalletTransaction instance.
|
|
|
|
|
*
|
|
|
|
|
* @param tx the original, input transaction.
|
|
|
|
|
* @param txid the transaction-id for tx
|
|
|
|
|
* @param types[out] this map is filled with inputs and the signature types used.
|
|
|
|
|
* @param change an optional notification instance to highlight the changes that this transaction causes.
|
|
|
|
|
*/
|
|
|
|
|
WalletTransaction createWalletTransactionFromTx(const Tx &tx, const uint256 &txid, std::map<uint64_t, SignatureType> &types, P2PNet::Notification *change = nullptr) const;
|
2020-11-02 21:49:06 +01:00
|
|
|
void saveTransaction(const Tx &tx);
|
2020-12-17 23:12:39 +01:00
|
|
|
/**
|
|
|
|
|
* returns a Tx if this txid was saved in this wallet.
|
|
|
|
|
*
|
|
|
|
|
* pool.reserve() is called by this method with the actual tx size.
|
|
|
|
|
*/
|
|
|
|
|
Tx loadTransaction(const uint256 &txid, Streaming::BufferPool &pool) const;
|
2020-11-02 21:49:06 +01:00
|
|
|
|
2021-11-02 14:24:47 +01:00
|
|
|
/// helper method to upgrade older wallets and find the sigType from transactions to populate the field in walletSecrets.
|
|
|
|
|
/// Returns true if any changes were made.
|
|
|
|
|
void populateSigType();
|
|
|
|
|
|
2021-10-21 14:57:20 +02:00
|
|
|
// check if we need to create more private keys based on if this transaction
|
|
|
|
|
// used private keys close to the index we have created and keep track off.
|
2021-10-21 20:54:34 +02:00
|
|
|
// returns true if new private keys have been derived from the HD masterkey
|
2021-11-01 13:33:33 +01:00
|
|
|
bool updateHDSignatures(const WalletTransaction &wtx, bool &updateBloom);
|
2021-10-21 14:57:20 +02:00
|
|
|
|
|
|
|
|
/// use the hdData master key to create a number of private keys (WalletSecrets).
|
2021-10-21 20:54:34 +02:00
|
|
|
void deriveHDKeys(int mainChain, int changeChain, uint32_t startHeight = 0);
|
2021-10-21 14:57:20 +02:00
|
|
|
|
2020-05-24 13:20:03 +02:00
|
|
|
std::map<int, WalletTransaction> m_walletTransactions;
|
|
|
|
|
|
|
|
|
|
typedef boost::unordered_map<uint256, int, HashShortener> TxIdCash;
|
2021-11-29 23:16:45 +01:00
|
|
|
TxIdCash m_txidCache; // txid -> m_walletTransactions-id
|
2020-05-24 13:20:03 +02:00
|
|
|
|
2021-05-03 12:40:48 +02:00
|
|
|
/*
|
|
|
|
|
* Our little UTXO.
|
|
|
|
|
* OutputRef -> value (in sat)
|
|
|
|
|
*/
|
|
|
|
|
std::map<uint64_t, uint64_t> m_unspentOutputs;
|
|
|
|
|
/*
|
|
|
|
|
* Unspent outputs can be 'locked' from spending for several reasons. User decided or
|
|
|
|
|
* simply because an unconfirmed tx spent it.
|
|
|
|
|
* We remember those here, where the 'key-value' pair follows the same concept for the
|
|
|
|
|
* key as the m_unspentoUutputs and
|
|
|
|
|
* the 'value' is the WalletTransaction index that actually was responsible for locking it.
|
|
|
|
|
* or it is 0 if the locking was user-decided.
|
|
|
|
|
*/
|
|
|
|
|
std::map<uint64_t, int> m_lockedOutputs;
|
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;
|
2021-10-29 18:20:42 +02:00
|
|
|
friend class WalletSecretsModel;
|
2021-11-21 17:46:17 +01:00
|
|
|
friend class WalletCoinsModel;
|
2020-06-11 10:24:05 +02:00
|
|
|
|
2021-07-30 15:56:52 +02:00
|
|
|
// user settings
|
2020-06-11 10:24:05 +02:00
|
|
|
bool m_singleAddressWallet = false;
|
2021-04-21 15:17:08 +02:00
|
|
|
bool m_userOwnedWallet = true;
|
2020-11-04 21:58:17 +01:00
|
|
|
|
2021-07-30 15:56:52 +02:00
|
|
|
// operational
|
2022-06-19 14:29:27 +02:00
|
|
|
/// Remove the encryption key (created from password), making the wallet unable to save private keys.
|
|
|
|
|
void clearEncryptionKey();
|
|
|
|
|
/// turn the password into a key. Return false if it is not the correct one.
|
|
|
|
|
bool parsePassword(const QString &password);
|
2021-07-30 15:56:52 +02:00
|
|
|
bool m_saveStarted = false;
|
2022-05-14 14:27:51 +02:00
|
|
|
bool m_haveEncryptionKey = false;
|
2022-06-19 14:29:27 +02:00
|
|
|
uint32_t m_encryptionSeed = 0;
|
2022-05-18 20:16:34 +02:00
|
|
|
uint16_t m_encryptionChecksum = 0;
|
|
|
|
|
EncryptionLevel m_encryptionLevel = NotEncrypted;
|
2022-05-14 14:27:51 +02:00
|
|
|
std::vector<char, secure_allocator<char>> m_encryptionKey;
|
2022-05-17 00:44:51 +02:00
|
|
|
std::vector<char, secure_allocator<char>> m_encryptionIR;
|
2021-07-30 15:56:52 +02:00
|
|
|
|
2020-11-04 21:58:17 +01:00
|
|
|
QList<std::shared_ptr<WalletInfoObject>> m_broadcastingTransactions;
|
2021-01-06 23:15:54 +01:00
|
|
|
|
2022-07-15 14:14:33 +02:00
|
|
|
struct PRData {
|
|
|
|
|
PaymentRequest *pr = nullptr;
|
|
|
|
|
bool saved = false; // true if this PR has been persisted to disk
|
|
|
|
|
};
|
|
|
|
|
QList<PRData> m_paymentRequests;
|
2020-05-24 13:20:03 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|