2020-04-16 13:14:08 -04:00
|
|
|
// Copyright (c) 2011-2020 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 12:20:43 -03:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2014-11-03 12:16:40 -03:00
|
|
|
#ifndef BITCOIN_QT_WALLETMODEL_H
|
|
|
|
#define BITCOIN_QT_WALLETMODEL_H
|
2011-06-30 12:05:29 -04:00
|
|
|
|
2019-10-12 13:49:29 -03:00
|
|
|
#if defined(HAVE_CONFIG_H)
|
|
|
|
#include <config/bitcoin-config.h>
|
|
|
|
#endif
|
|
|
|
|
2017-09-19 22:12:25 -03:00
|
|
|
#include <key.h>
|
|
|
|
#include <script/standard.h>
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <qt/walletmodeltransaction.h>
|
2011-11-26 03:02:04 -03:00
|
|
|
|
2018-04-07 04:42:02 -03:00
|
|
|
#include <interfaces/wallet.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <support/allocators/secure.h>
|
2011-06-30 12:05:29 -04:00
|
|
|
|
2013-11-16 13:37:31 -03:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <QObject>
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2018-02-10 23:06:35 -03:00
|
|
|
enum class OutputType;
|
2018-01-16 17:11:40 -03:00
|
|
|
|
2011-06-30 12:05:29 -04:00
|
|
|
class AddressTableModel;
|
2018-08-01 13:38:45 -04:00
|
|
|
class ClientModel;
|
2013-04-13 02:13:08 -03:00
|
|
|
class OptionsModel;
|
2015-07-28 10:20:14 -03:00
|
|
|
class PlatformStyle;
|
2013-11-05 14:03:05 -03:00
|
|
|
class RecentRequestsTableModel;
|
2019-11-19 06:10:40 -03:00
|
|
|
class SendCoinsRecipient;
|
2013-12-16 18:54:02 -03:00
|
|
|
class TransactionTableModel;
|
2013-08-30 14:04:48 -04:00
|
|
|
class WalletModelTransaction;
|
2013-11-16 13:37:31 -03:00
|
|
|
|
|
|
|
class CCoinControl;
|
2013-08-12 11:03:03 -04:00
|
|
|
class CKeyID;
|
|
|
|
class COutPoint;
|
2013-11-16 13:37:31 -03:00
|
|
|
class COutput;
|
|
|
|
class CPubKey;
|
|
|
|
class uint256;
|
2013-04-13 02:13:08 -03:00
|
|
|
|
2018-04-07 04:42:02 -03:00
|
|
|
namespace interfaces {
|
2017-04-17 19:56:44 -03:00
|
|
|
class Node;
|
2018-04-07 04:42:02 -03:00
|
|
|
} // namespace interfaces
|
2017-04-17 19:56:44 -03:00
|
|
|
|
2012-07-05 11:43:28 -04:00
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
class QTimer;
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
2011-11-13 09:19:52 -03:00
|
|
|
/** Interface to Bitcoin wallet from Qt view code. */
|
2011-06-30 12:05:29 -04:00
|
|
|
class WalletModel : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2013-01-23 17:51:02 -03:00
|
|
|
|
2011-06-30 12:05:29 -04:00
|
|
|
public:
|
2018-08-01 13:38:45 -04:00
|
|
|
explicit WalletModel(std::unique_ptr<interfaces::Wallet> wallet, ClientModel& client_model, const PlatformStyle *platformStyle, QObject *parent = nullptr);
|
2012-05-06 13:40:58 -04:00
|
|
|
~WalletModel();
|
2011-06-30 12:05:29 -04:00
|
|
|
|
2011-08-31 11:08:31 -03:00
|
|
|
enum StatusCode // Returned by sendCoins
|
2011-06-30 12:05:29 -04:00
|
|
|
{
|
|
|
|
OK,
|
|
|
|
InvalidAmount,
|
|
|
|
InvalidAddress,
|
|
|
|
AmountExceedsBalance,
|
|
|
|
AmountWithFeeExceedsBalance,
|
2011-07-16 13:01:05 -04:00
|
|
|
DuplicateAddress,
|
2011-08-31 11:08:31 -03:00
|
|
|
TransactionCreationFailed, // Error returned when wallet is still locked
|
2015-01-30 23:54:55 -03:00
|
|
|
AbsurdFee,
|
2015-01-08 10:42:04 -03:00
|
|
|
PaymentRequestExpired
|
2011-06-30 12:05:29 -04:00
|
|
|
};
|
|
|
|
|
2011-08-23 15:08:42 -03:00
|
|
|
enum EncryptionStatus
|
|
|
|
{
|
|
|
|
Unencrypted, // !wallet->IsCrypted()
|
|
|
|
Locked, // wallet->IsCrypted() && wallet->IsLocked()
|
|
|
|
Unlocked // wallet->IsCrypted() && !wallet->IsLocked()
|
|
|
|
};
|
|
|
|
|
2011-06-30 12:05:29 -04:00
|
|
|
OptionsModel *getOptionsModel();
|
|
|
|
AddressTableModel *getAddressTableModel();
|
|
|
|
TransactionTableModel *getTransactionTableModel();
|
2013-11-05 14:03:05 -03:00
|
|
|
RecentRequestsTableModel *getRecentRequestsTableModel();
|
2011-06-30 12:05:29 -04:00
|
|
|
|
2011-08-23 15:08:42 -03:00
|
|
|
EncryptionStatus getEncryptionStatus() const;
|
|
|
|
|
2011-07-16 13:01:05 -04:00
|
|
|
// Check address for validity
|
|
|
|
bool validateAddress(const QString &address);
|
|
|
|
|
2011-08-08 11:38:17 -04:00
|
|
|
// Return status record for SendCoins, contains error id + information
|
2011-07-16 13:01:05 -04:00
|
|
|
struct SendCoinsReturn
|
|
|
|
{
|
2016-10-30 14:22:22 -03:00
|
|
|
SendCoinsReturn(StatusCode _status = OK, QString _reasonCommitFailed = "")
|
|
|
|
: status(_status),
|
|
|
|
reasonCommitFailed(_reasonCommitFailed)
|
|
|
|
{
|
|
|
|
}
|
2011-07-16 13:01:05 -04:00
|
|
|
StatusCode status;
|
2016-10-30 14:22:22 -03:00
|
|
|
QString reasonCommitFailed;
|
2011-07-16 13:01:05 -04:00
|
|
|
};
|
|
|
|
|
2013-08-30 14:04:48 -04:00
|
|
|
// prepare transaction for getting txfee before sending coins
|
2017-06-28 16:41:55 -04:00
|
|
|
SendCoinsReturn prepareTransaction(WalletModelTransaction &transaction, const CCoinControl& coinControl);
|
2013-08-30 14:04:48 -04:00
|
|
|
|
2011-08-08 11:38:17 -04:00
|
|
|
// Send coins to a list of recipients
|
2013-08-30 14:04:48 -04:00
|
|
|
SendCoinsReturn sendCoins(WalletModelTransaction &transaction);
|
2011-08-24 17:07:26 -03:00
|
|
|
|
|
|
|
// Wallet encryption
|
2020-10-28 13:43:42 -03:00
|
|
|
bool setWalletEncrypted(const SecureString& passphrase);
|
2011-08-24 17:07:26 -03:00
|
|
|
// Passphrase only needed when unlocking
|
2011-11-26 03:02:04 -03:00
|
|
|
bool setWalletLocked(bool locked, const SecureString &passPhrase=SecureString());
|
|
|
|
bool changePassphrase(const SecureString &oldPass, const SecureString &newPass);
|
2011-08-24 17:07:26 -03:00
|
|
|
|
|
|
|
// RAI object for unlocking wallet, returned by requestUnlock()
|
|
|
|
class UnlockContext
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UnlockContext(WalletModel *wallet, bool valid, bool relock);
|
|
|
|
~UnlockContext();
|
|
|
|
|
|
|
|
bool isValid() const { return valid; }
|
|
|
|
|
2019-05-09 21:07:33 -04:00
|
|
|
// Copy constructor is disabled.
|
|
|
|
UnlockContext(const UnlockContext&) = delete;
|
|
|
|
// Move operator and constructor transfer the context
|
|
|
|
UnlockContext(UnlockContext&& obj) { CopyFrom(std::move(obj)); }
|
|
|
|
UnlockContext& operator=(UnlockContext&& rhs) { CopyFrom(std::move(rhs)); return *this; }
|
2011-08-24 17:07:26 -03:00
|
|
|
private:
|
|
|
|
WalletModel *wallet;
|
|
|
|
bool valid;
|
|
|
|
mutable bool relock; // mutable, as it can be set to false by copying
|
|
|
|
|
2019-05-09 21:07:33 -04:00
|
|
|
UnlockContext& operator=(const UnlockContext&) = default;
|
|
|
|
void CopyFrom(UnlockContext&& rhs);
|
2011-08-24 17:07:26 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
UnlockContext requestUnlock();
|
|
|
|
|
2014-01-14 01:05:43 -03:00
|
|
|
void loadReceiveRequests(std::vector<std::string>& vReceiveRequests);
|
|
|
|
bool saveReceiveRequest(const std::string &sAddress, const int64_t nId, const std::string &sRequest);
|
|
|
|
|
2018-08-01 12:43:46 -04:00
|
|
|
bool bumpFee(uint256 hash, uint256& new_hash);
|
2017-02-03 18:04:39 -03:00
|
|
|
|
2016-09-21 07:37:00 -03:00
|
|
|
static bool isWalletEnabled();
|
|
|
|
|
2018-04-07 04:42:02 -03:00
|
|
|
interfaces::Node& node() const { return m_node; }
|
|
|
|
interfaces::Wallet& wallet() const { return *m_wallet; }
|
2020-01-23 19:31:16 -03:00
|
|
|
ClientModel& clientModel() const { return *m_client_model; }
|
2020-04-24 18:06:26 -04:00
|
|
|
void setClientModel(ClientModel* client_model);
|
2016-10-21 05:24:03 -03:00
|
|
|
|
2018-03-06 01:26:40 -03:00
|
|
|
QString getWalletName() const;
|
2018-06-25 17:22:01 -04:00
|
|
|
QString getDisplayName() const;
|
2018-03-06 01:26:40 -03:00
|
|
|
|
2017-04-17 19:56:44 -03:00
|
|
|
bool isMultiwallet();
|
2018-03-29 11:59:57 -03:00
|
|
|
|
|
|
|
AddressTableModel* getAddressTableModel() const { return addressTableModel; }
|
2020-01-13 15:50:44 -03:00
|
|
|
|
|
|
|
void refresh(bool pk_hash_only = false);
|
2020-06-22 16:35:48 -04:00
|
|
|
|
|
|
|
uint256 getLastBlockProcessed() const;
|
|
|
|
|
2011-06-30 12:05:29 -04:00
|
|
|
private:
|
2018-04-07 04:42:02 -03:00
|
|
|
std::unique_ptr<interfaces::Wallet> m_wallet;
|
2018-06-05 06:17:28 -04:00
|
|
|
std::unique_ptr<interfaces::Handler> m_handler_unload;
|
2018-04-07 04:42:02 -03:00
|
|
|
std::unique_ptr<interfaces::Handler> m_handler_status_changed;
|
|
|
|
std::unique_ptr<interfaces::Handler> m_handler_address_book_changed;
|
|
|
|
std::unique_ptr<interfaces::Handler> m_handler_transaction_changed;
|
|
|
|
std::unique_ptr<interfaces::Handler> m_handler_show_progress;
|
|
|
|
std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
|
2019-01-18 19:05:32 -03:00
|
|
|
std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
|
2020-04-24 18:06:26 -04:00
|
|
|
ClientModel* m_client_model;
|
2018-04-07 04:42:02 -03:00
|
|
|
interfaces::Node& m_node;
|
2017-04-17 19:56:44 -03:00
|
|
|
|
2014-07-26 15:05:11 -04:00
|
|
|
bool fHaveWatchOnly;
|
2019-01-05 08:02:12 -03:00
|
|
|
bool fForceCheckBalanceChanged{false};
|
2011-06-30 12:05:29 -04:00
|
|
|
|
|
|
|
// Wallet has an options model for wallet-specific options
|
|
|
|
// (transaction fee, for example)
|
|
|
|
OptionsModel *optionsModel;
|
|
|
|
|
|
|
|
AddressTableModel *addressTableModel;
|
|
|
|
TransactionTableModel *transactionTableModel;
|
2013-11-05 14:03:05 -03:00
|
|
|
RecentRequestsTableModel *recentRequestsTableModel;
|
2011-06-30 12:05:29 -04:00
|
|
|
|
2011-08-31 11:08:31 -03:00
|
|
|
// Cache some values to be able to detect changes
|
2018-04-07 04:42:02 -03:00
|
|
|
interfaces::WalletBalances m_cached_balances;
|
2011-08-23 15:08:42 -03:00
|
|
|
EncryptionStatus cachedEncryptionStatus;
|
2020-04-24 18:06:26 -04:00
|
|
|
QTimer* timer;
|
2012-07-05 11:43:28 -04:00
|
|
|
|
2020-01-23 19:31:16 -03:00
|
|
|
// Block hash denoting when the last balance update was done.
|
|
|
|
uint256 m_cached_last_update_tip{};
|
|
|
|
|
2012-05-06 13:40:58 -04:00
|
|
|
void subscribeToCoreSignals();
|
|
|
|
void unsubscribeFromCoreSignals();
|
2018-04-07 04:42:02 -03:00
|
|
|
void checkBalanceChanged(const interfaces::WalletBalances& new_balances);
|
2012-07-05 11:43:28 -04:00
|
|
|
|
2015-07-14 08:59:05 -03:00
|
|
|
Q_SIGNALS:
|
2011-08-31 11:08:31 -03:00
|
|
|
// Signal that balance in wallet changed
|
2018-04-07 04:42:02 -03:00
|
|
|
void balanceChanged(const interfaces::WalletBalances& balances);
|
2011-08-31 11:08:31 -03:00
|
|
|
|
|
|
|
// Encryption status of wallet changed
|
2016-10-24 04:21:51 -03:00
|
|
|
void encryptionStatusChanged();
|
2011-08-31 11:08:31 -03:00
|
|
|
|
|
|
|
// Signal emitted when wallet needs to be unlocked
|
|
|
|
// It is valid behaviour for listeners to keep the wallet locked after this signal;
|
|
|
|
// this means that the unlocking failed or was cancelled.
|
2011-08-24 17:07:26 -03:00
|
|
|
void requireUnlock();
|
2011-06-30 12:05:29 -04:00
|
|
|
|
2013-10-24 11:02:39 -03:00
|
|
|
// Fired when a message should be reported to the user
|
2012-12-03 09:24:42 -03:00
|
|
|
void message(const QString &title, const QString &message, unsigned int style);
|
2011-06-30 12:05:29 -04:00
|
|
|
|
2013-07-22 02:50:39 -04:00
|
|
|
// Coins sent: from wallet, to recipient, in (serialized) transaction:
|
2018-03-23 18:14:39 -03:00
|
|
|
void coinsSent(WalletModel* wallet, SendCoinsRecipient recipient, QByteArray transaction);
|
2013-07-22 02:50:39 -04:00
|
|
|
|
2014-03-18 20:26:14 -03:00
|
|
|
// Show progress dialog e.g. for rescan
|
|
|
|
void showProgress(const QString &title, int nProgress);
|
|
|
|
|
2014-07-26 15:05:11 -04:00
|
|
|
// Watch-only address added
|
|
|
|
void notifyWatchonlyChanged(bool fHaveWatchonly);
|
|
|
|
|
2018-06-05 06:17:28 -04:00
|
|
|
// Signal that wallet is about to be removed
|
|
|
|
void unload();
|
|
|
|
|
2019-01-18 19:05:32 -03:00
|
|
|
// Notify that there are now keys in the keypool
|
|
|
|
void canGetAddressesChanged();
|
|
|
|
|
2015-07-14 08:59:05 -03:00
|
|
|
public Q_SLOTS:
|
2019-10-12 18:26:47 -03:00
|
|
|
/* Starts a timer to periodically update the balance */
|
|
|
|
void startPollBalance();
|
|
|
|
|
2012-05-05 10:07:14 -04:00
|
|
|
/* Wallet status might have changed */
|
|
|
|
void updateStatus();
|
|
|
|
/* New transaction, or transaction changed status */
|
2014-10-28 15:52:21 -03:00
|
|
|
void updateTransaction();
|
2012-05-05 10:07:14 -04:00
|
|
|
/* New, updated or removed address book entry */
|
2013-08-29 10:19:43 -04:00
|
|
|
void updateAddressBook(const QString &address, const QString &label, bool isMine, const QString &purpose, int status);
|
2014-08-28 17:20:46 -04:00
|
|
|
/* Watch-only added */
|
2014-07-26 15:05:11 -04:00
|
|
|
void updateWatchOnlyFlag(bool fHaveWatchonly);
|
2012-07-05 11:43:28 -04:00
|
|
|
/* Current, immature or unconfirmed balance might have changed - emit 'balanceChanged' if so */
|
|
|
|
void pollBalanceChanged();
|
2011-06-30 12:05:29 -04:00
|
|
|
};
|
|
|
|
|
2014-11-03 12:16:40 -03:00
|
|
|
#endif // BITCOIN_QT_WALLETMODEL_H
|