diff --git a/src/dummywallet.cpp b/src/dummywallet.cpp index ffae41aad..9f70f09b7 100644 --- a/src/dummywallet.cpp +++ b/src/dummywallet.cpp @@ -1,67 +1,77 @@ // Copyright (c) 2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include +#include #include #include class CChainParams; class CWallet; +enum class WalletCreationStatus; namespace interfaces { class Chain; } class DummyWalletInit : public WalletInitInterface { public: bool HasWalletSupport() const override { return false; } void AddWalletOptions() const override; bool ParameterInteraction() const override { return true; } void Construct(NodeContext &node) const override { LogPrintf("No wallet support compiled in!\n"); } }; void DummyWalletInit::AddWalletOptions() const { std::vector opts = { "-avoidpartialspends", "-disablewallet", "-fallbackfee=", "-keypool=", "-maxtxfee=", "-mintxfee=", "-paytxfee=", "-rescan", "-salvagewallet", "-spendzeroconfchange", "-upgradewallet", "-wallet=", "-walletbroadcast", "-walletdir=", "-walletnotify=", "-zapwallettxes=", // Wallet debug options "-dblogsize=", "-flushwallet", "-privdb", "-walletrejectlongchains"}; gArgs.AddHiddenArgs(opts); } const WalletInitInterface &g_wallet_init_interface = DummyWalletInit(); fs::path GetWalletDir() { throw std::logic_error("Wallet function called in non-wallet build."); } std::vector ListWalletDir() { throw std::logic_error("Wallet function called in non-wallet build."); } std::vector> GetWallets() { throw std::logic_error("Wallet function called in non-wallet build."); } std::shared_ptr LoadWallet(const CChainParams &chainParams, interfaces::Chain &chain, const std::string &name, std::string &error, std::string &warning) { throw std::logic_error("Wallet function called in non-wallet build."); } +WalletCreationStatus +CreateWallet(const CChainParams &chainParams, interfaces::Chain &chain, + const SecureString &passphrase, uint64_t wallet_creation_flags, + const std::string &name, std::string &error, std::string &warning, + std::shared_ptr &result) { + throw std::logic_error("Wallet function called in non-wallet build."); +} + namespace interfaces { class Wallet; std::unique_ptr MakeWallet(const std::shared_ptr &wallet) { throw std::logic_error("Wallet function called in non-wallet build."); } } // namespace interfaces diff --git a/src/interfaces/node.cpp b/src/interfaces/node.cpp index 9bb1b15a5..398f4ab6c 100644 --- a/src/interfaces/node.cpp +++ b/src/interfaces/node.cpp @@ -1,343 +1,361 @@ // Copyright (c) 2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include +#include #include #include #include #include #include #include #if defined(HAVE_CONFIG_H) #include #endif #include class HTTPRPCRequestProcessor; class CWallet; fs::path GetWalletDir(); std::vector ListWalletDir(); std::vector> GetWallets(); std::shared_ptr LoadWallet(const CChainParams &chainParams, interfaces::Chain &chain, const std::string &name, std::string &error, std::string &warning); +WalletCreationStatus +CreateWallet(const CChainParams ¶ms, interfaces::Chain &chain, + const SecureString &passphrase, uint64_t wallet_creation_flags, + const std::string &name, std::string &error, std::string &warning, + std::shared_ptr &result); namespace interfaces { class Wallet; namespace { class NodeImpl : public Node { public: void initError(const std::string &message) override { InitError(message); } bool parseParameters(int argc, const char *const argv[], std::string &error) override { return gArgs.ParseParameters(argc, argv, error); } bool readConfigFiles(std::string &error) override { return gArgs.ReadConfigFiles(error, true); } bool softSetArg(const std::string &arg, const std::string &value) override { return gArgs.SoftSetArg(arg, value); } bool softSetBoolArg(const std::string &arg, bool value) override { return gArgs.SoftSetBoolArg(arg, value); } void selectParams(const std::string &network) override { SelectParams(network); } uint64_t getAssumedBlockchainSize() override { return Params().AssumedBlockchainSize(); } uint64_t getAssumedChainStateSize() override { return Params().AssumedChainStateSize(); } std::string getNetwork() override { return Params().NetworkIDString(); } void initLogging() override { InitLogging(); } void initParameterInteraction() override { InitParameterInteraction(); } std::string getWarnings(const std::string &type) override { return GetWarnings(type); } bool baseInitialize(Config &config) override { return AppInitBasicSetup() && AppInitParameterInteraction(config) && AppInitSanityChecks() && AppInitLockDataDirectory(); } bool appInitMain(Config &config, RPCServer &rpcServer, HTTPRPCRequestProcessor &httpRPCRequestProcessor) override { m_context.chain = MakeChain(m_context, config.GetChainParams()); return AppInitMain(config, rpcServer, httpRPCRequestProcessor, m_context); } void appShutdown() override { Interrupt(m_context); Shutdown(m_context); } void startShutdown() override { StartShutdown(); } bool shutdownRequested() override { return ShutdownRequested(); } void mapPort(bool use_upnp) override { if (use_upnp) { StartMapPort(); } else { InterruptMapPort(); StopMapPort(); } } void setupServerArgs() override { return SetupServerArgs(); } bool getProxy(Network net, proxyType &proxy_info) override { return GetProxy(net, proxy_info); } size_t getNodeCount(CConnman::NumConnections flags) override { return m_context.connman ? m_context.connman->GetNodeCount(flags) : 0; } bool getNodesStats(NodesStats &stats) override { stats.clear(); if (m_context.connman) { std::vector stats_temp; m_context.connman->GetNodeStats(stats_temp); stats.reserve(stats_temp.size()); for (auto &node_stats_temp : stats_temp) { stats.emplace_back(std::move(node_stats_temp), false, CNodeStateStats()); } // Try to retrieve the CNodeStateStats for each node. TRY_LOCK(::cs_main, lockMain); if (lockMain) { for (auto &node_stats : stats) { std::get<1>(node_stats) = GetNodeStateStats(std::get<0>(node_stats).nodeid, std::get<2>(node_stats)); } } return true; } return false; } bool getBanned(banmap_t &banmap) override { if (m_context.banman) { m_context.banman->GetBanned(banmap); return true; } return false; } bool ban(const CNetAddr &net_addr, BanReason reason, int64_t ban_time_offset) override { if (m_context.banman) { m_context.banman->Ban(net_addr, reason, ban_time_offset); return true; } return false; } bool unban(const CSubNet &ip) override { if (m_context.banman) { m_context.banman->Unban(ip); return true; } return false; } bool disconnect(const CNetAddr &net_addr) override { if (m_context.connman) { return m_context.connman->DisconnectNode(net_addr); } return false; } bool disconnect(NodeId id) override { if (m_context.connman) { return m_context.connman->DisconnectNode(id); } return false; } int64_t getTotalBytesRecv() override { return m_context.connman ? m_context.connman->GetTotalBytesRecv() : 0; } int64_t getTotalBytesSent() override { return m_context.connman ? m_context.connman->GetTotalBytesSent() : 0; } size_t getMempoolSize() override { return g_mempool.size(); } size_t getMempoolDynamicUsage() override { return g_mempool.DynamicMemoryUsage(); } bool getHeaderTip(int &height, int64_t &block_time) override { LOCK(::cs_main); if (::pindexBestHeader) { height = ::pindexBestHeader->nHeight; block_time = ::pindexBestHeader->GetBlockTime(); return true; } return false; } int getNumBlocks() override { LOCK(::cs_main); return ::ChainActive().Height(); } int64_t getLastBlockTime() override { LOCK(::cs_main); if (::ChainActive().Tip()) { return ::ChainActive().Tip()->GetBlockTime(); } // Genesis block's time of current network return Params().GenesisBlock().GetBlockTime(); } double getVerificationProgress() override { const CBlockIndex *tip; { LOCK(::cs_main); tip = ::ChainActive().Tip(); } return GuessVerificationProgress(Params().TxData(), tip); } bool isInitialBlockDownload() override { return ::ChainstateActive().IsInitialBlockDownload(); } bool getReindex() override { return ::fReindex; } bool getImporting() override { return ::fImporting; } void setNetworkActive(bool active) override { if (m_context.connman) { m_context.connman->SetNetworkActive(active); } } bool getNetworkActive() override { return m_context.connman && m_context.connman->GetNetworkActive(); } CFeeRate estimateSmartFee() override { return g_mempool.estimateFee(); } CFeeRate getDustRelayFee() override { return ::dustRelayFee; } UniValue executeRpc(Config &config, const std::string &command, const UniValue ¶ms, const std::string &uri) override { JSONRPCRequest req; req.params = params; req.strMethod = command; req.URI = uri; return ::tableRPC.execute(config, req); } std::vector listRpcCommands() override { return ::tableRPC.listCommands(); } void rpcSetTimerInterfaceIfUnset(RPCTimerInterface *iface) override { RPCSetTimerInterfaceIfUnset(iface); } void rpcUnsetTimerInterface(RPCTimerInterface *iface) override { RPCUnsetTimerInterface(iface); } bool getUnspentOutput(const COutPoint &output, Coin &coin) override { LOCK(::cs_main); return ::pcoinsTip->GetCoin(output, coin); } std::string getWalletDir() override { return GetWalletDir().string(); } std::vector listWalletDir() override { std::vector paths; for (auto &path : ListWalletDir()) { paths.push_back(path.string()); } return paths; } std::vector> getWallets() override { std::vector> wallets; for (const std::shared_ptr &wallet : GetWallets()) { wallets.emplace_back(MakeWallet(wallet)); } return wallets; } std::unique_ptr loadWallet(const CChainParams ¶ms, const std::string &name, std::string &error, std::string &warning) const override { return MakeWallet( LoadWallet(params, *m_context.chain, name, error, warning)); } + WalletCreationStatus + createWallet(const CChainParams ¶ms, const SecureString &passphrase, + uint64_t wallet_creation_flags, const std::string &name, + std::string &error, std::string &warning, + std::unique_ptr &result) override { + std::shared_ptr wallet; + WalletCreationStatus status = CreateWallet( + params, *m_context.chain, passphrase, wallet_creation_flags, + name, error, warning, wallet); + result = MakeWallet(wallet); + return status; + } std::unique_ptr handleInitMessage(InitMessageFn fn) override { return MakeHandler(::uiInterface.InitMessage_connect(fn)); } std::unique_ptr handleMessageBox(MessageBoxFn fn) override { return MakeHandler(::uiInterface.ThreadSafeMessageBox_connect(fn)); } std::unique_ptr handleQuestion(QuestionFn fn) override { return MakeHandler(::uiInterface.ThreadSafeQuestion_connect(fn)); } std::unique_ptr handleShowProgress(ShowProgressFn fn) override { return MakeHandler(::uiInterface.ShowProgress_connect(fn)); } std::unique_ptr handleLoadWallet(LoadWalletFn fn) override { return MakeHandler(::uiInterface.LoadWallet_connect( [fn](std::unique_ptr &wallet) { fn(std::move(wallet)); })); } std::unique_ptr handleNotifyNumConnectionsChanged( NotifyNumConnectionsChangedFn fn) override { return MakeHandler( ::uiInterface.NotifyNumConnectionsChanged_connect(fn)); } std::unique_ptr handleNotifyNetworkActiveChanged( NotifyNetworkActiveChangedFn fn) override { return MakeHandler( ::uiInterface.NotifyNetworkActiveChanged_connect(fn)); } std::unique_ptr handleNotifyAlertChanged(NotifyAlertChangedFn fn) override { return MakeHandler(::uiInterface.NotifyAlertChanged_connect(fn)); } std::unique_ptr handleBannedListChanged(BannedListChangedFn fn) override { return MakeHandler(::uiInterface.BannedListChanged_connect(fn)); } std::unique_ptr handleNotifyBlockTip(NotifyBlockTipFn fn) override { return MakeHandler(::uiInterface.NotifyBlockTip_connect( [fn](bool initial_download, const CBlockIndex *block) { fn(initial_download, block->nHeight, block->GetBlockTime(), GuessVerificationProgress(Params().TxData(), block)); })); } std::unique_ptr handleNotifyHeaderTip(NotifyHeaderTipFn fn) override { return MakeHandler(::uiInterface.NotifyHeaderTip_connect( [fn](bool initial_download, const CBlockIndex *block) { fn(initial_download, block->nHeight, block->GetBlockTime(), GuessVerificationProgress(Params().TxData(), block)); })); } NodeContext *context() override { return &m_context; } NodeContext m_context; }; } // namespace std::unique_ptr MakeNode() { return std::make_unique(); } } // namespace interfaces diff --git a/src/interfaces/node.h b/src/interfaces/node.h index 9f3654e65..6d8b98502 100644 --- a/src/interfaces/node.h +++ b/src/interfaces/node.h @@ -1,281 +1,290 @@ // Copyright (c) 2018 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_INTERFACES_NODE_H #define BITCOIN_INTERFACES_NODE_H -#include // For banmap_t -#include // For Amount -#include // For CConnman::NumConnections -#include // For Network +#include // For banmap_t +#include // For Amount +#include // For CConnman::NumConnections +#include // For Network +#include // For SecureString #include #include #include #include #include #include #include class BanMan; class CCoinControl; class CFeeRate; struct CNodeStateStats; struct CNodeStats; class Coin; class Config; class HTTPRPCRequestProcessor; struct NodeContext; class proxyType; class RPCServer; class RPCTimerInterface; class UniValue; +enum class WalletCreationStatus; namespace interfaces { class Handler; class Wallet; //! Top-level interface for a bitcoin node (bitcoind process). class Node { public: virtual ~Node() {} //! Send init error. virtual void initError(const std::string &message) = 0; //! Set command line arguments. virtual bool parseParameters(int argc, const char *const argv[], std::string &error) = 0; //! Set a command line argument if it doesn't already have a value virtual bool softSetArg(const std::string &arg, const std::string &value) = 0; //! Set a command line boolean argument if it doesn't already have a value virtual bool softSetBoolArg(const std::string &arg, bool value) = 0; //! Load settings from configuration file. virtual bool readConfigFiles(std::string &error) = 0; //! Choose network parameters. virtual void selectParams(const std::string &network) = 0; //! Get the (assumed) blockchain size. virtual uint64_t getAssumedBlockchainSize() = 0; //! Get the (assumed) chain state size. virtual uint64_t getAssumedChainStateSize() = 0; //! Get network name. virtual std::string getNetwork() = 0; //! Init logging. virtual void initLogging() = 0; //! Init parameter interaction. virtual void initParameterInteraction() = 0; //! Get warnings. virtual std::string getWarnings(const std::string &type) = 0; //! Initialize app dependencies. virtual bool baseInitialize(Config &config) = 0; //! Start node. virtual bool appInitMain(Config &config, RPCServer &rpcServer, HTTPRPCRequestProcessor &httpRPCRequestProcessor) = 0; //! Stop node. virtual void appShutdown() = 0; //! Start shutdown. virtual void startShutdown() = 0; //! Return whether shutdown was requested. virtual bool shutdownRequested() = 0; //! Setup arguments virtual void setupServerArgs() = 0; //! Map port. virtual void mapPort(bool use_upnp) = 0; //! Get proxy. virtual bool getProxy(Network net, proxyType &proxy_info) = 0; //! Get number of connections. virtual size_t getNodeCount(CConnman::NumConnections flags) = 0; //! Get stats for connected nodes. using NodesStats = std::vector>; virtual bool getNodesStats(NodesStats &stats) = 0; //! Get ban map entries. virtual bool getBanned(banmap_t &banmap) = 0; //! Ban node. virtual bool ban(const CNetAddr &net_addr, BanReason reason, int64_t ban_time_offset) = 0; //! Unban node. virtual bool unban(const CSubNet &ip) = 0; //! Disconnect node by address. virtual bool disconnect(const CNetAddr &net_addr) = 0; //! Disconnect node by id. virtual bool disconnect(NodeId id) = 0; //! Get total bytes recv. virtual int64_t getTotalBytesRecv() = 0; //! Get total bytes sent. virtual int64_t getTotalBytesSent() = 0; //! Get mempool size. virtual size_t getMempoolSize() = 0; //! Get mempool dynamic usage. virtual size_t getMempoolDynamicUsage() = 0; //! Get header tip height and time. virtual bool getHeaderTip(int &height, int64_t &block_time) = 0; //! Get num blocks. virtual int getNumBlocks() = 0; //! Get last block time. virtual int64_t getLastBlockTime() = 0; //! Get verification progress. virtual double getVerificationProgress() = 0; //! Is initial block download. virtual bool isInitialBlockDownload() = 0; //! Get reindex. virtual bool getReindex() = 0; //! Get importing. virtual bool getImporting() = 0; //! Set network active. virtual void setNetworkActive(bool active) = 0; //! Get network active. virtual bool getNetworkActive() = 0; //! Estimate smart fee. virtual CFeeRate estimateSmartFee() = 0; //! Get dust relay fee. virtual CFeeRate getDustRelayFee() = 0; //! Execute rpc command. virtual UniValue executeRpc(Config &config, const std::string &command, const UniValue ¶ms, const std::string &uri) = 0; //! List rpc commands. virtual std::vector listRpcCommands() = 0; //! Set RPC timer interface if unset. virtual void rpcSetTimerInterfaceIfUnset(RPCTimerInterface *iface) = 0; //! Unset RPC timer interface. virtual void rpcUnsetTimerInterface(RPCTimerInterface *iface) = 0; //! Get unspent outputs associated with a transaction. virtual bool getUnspentOutput(const COutPoint &output, Coin &coin) = 0; //! Return default wallet directory. virtual std::string getWalletDir() = 0; //! Return available wallets in wallet directory. virtual std::vector listWalletDir() = 0; //! Return interfaces for accessing wallets (if any). virtual std::vector> getWallets() = 0; //! Attempts to load a wallet from file or directory. //! The loaded wallet is also notified to handlers previously registered //! with handleLoadWallet. virtual std::unique_ptr loadWallet(const CChainParams ¶ms, const std::string &name, std::string &error, std::string &warning) const = 0; + //! Create a wallet from file + virtual WalletCreationStatus + createWallet(const CChainParams ¶ms, const SecureString &passphrase, + uint64_t wallet_creation_flags, const std::string &name, + std::string &error, std::string &warning, + std::unique_ptr &result) = 0; + //! Register handler for init messages. using InitMessageFn = std::function; virtual std::unique_ptr handleInitMessage(InitMessageFn fn) = 0; //! Register handler for message box messages. using MessageBoxFn = std::function; virtual std::unique_ptr handleMessageBox(MessageBoxFn fn) = 0; //! Register handler for question messages. using QuestionFn = std::function; virtual std::unique_ptr handleQuestion(QuestionFn fn) = 0; //! Register handler for progress messages. using ShowProgressFn = std::function; virtual std::unique_ptr handleShowProgress(ShowProgressFn fn) = 0; //! Register handler for load wallet messages. using LoadWalletFn = std::function wallet)>; virtual std::unique_ptr handleLoadWallet(LoadWalletFn fn) = 0; //! Register handler for number of connections changed messages. using NotifyNumConnectionsChangedFn = std::function; virtual std::unique_ptr handleNotifyNumConnectionsChanged(NotifyNumConnectionsChangedFn fn) = 0; //! Register handler for network active messages. using NotifyNetworkActiveChangedFn = std::function; virtual std::unique_ptr handleNotifyNetworkActiveChanged(NotifyNetworkActiveChangedFn fn) = 0; //! Register handler for notify alert messages. using NotifyAlertChangedFn = std::function; virtual std::unique_ptr handleNotifyAlertChanged(NotifyAlertChangedFn fn) = 0; //! Register handler for ban list messages. using BannedListChangedFn = std::function; virtual std::unique_ptr handleBannedListChanged(BannedListChangedFn fn) = 0; //! Register handler for block tip messages. using NotifyBlockTipFn = std::function; virtual std::unique_ptr handleNotifyBlockTip(NotifyBlockTipFn fn) = 0; //! Register handler for header tip messages. using NotifyHeaderTipFn = std::function; virtual std::unique_ptr handleNotifyHeaderTip(NotifyHeaderTipFn fn) = 0; //! Return pointer to internal chain interface, useful for testing. virtual NodeContext *context() { return nullptr; } }; //! Return implementation of Node interface. std::unique_ptr MakeNode(); } // namespace interfaces #endif // BITCOIN_INTERFACES_NODE_H diff --git a/src/qt/createwalletdialog.cpp b/src/qt/createwalletdialog.cpp index 81244cfa1..3cc3766dc 100644 --- a/src/qt/createwalletdialog.cpp +++ b/src/qt/createwalletdialog.cpp @@ -1,58 +1,58 @@ // Copyright (c) 2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #if defined(HAVE_CONFIG_H) #include #endif #include #include #include CreateWalletDialog::CreateWalletDialog(QWidget *parent) : QDialog(parent), ui(new Ui::CreateWalletDialog) { ui->setupUi(this); - ui->buttonBox->button(QDialogButtonBox::Ok)->setText("Create"); + ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Create")); ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(false); ui->wallet_name_line_edit->setFocus(Qt::ActiveWindowFocusReason); connect(ui->wallet_name_line_edit, &QLineEdit::textEdited, [this](const QString &text) { ui->buttonBox->button(QDialogButtonBox::Ok) ->setEnabled(!text.isEmpty()); }); connect(ui->encrypt_wallet_checkbox, &QCheckBox::toggled, [this](bool checked) { // Disable disable_privkeys_checkbox when encrypt is set to // true, enable it when encrypt is false ui->disable_privkeys_checkbox->setEnabled(!checked); // When the disable_privkeys_checkbox is disabled, uncheck it. if (!ui->disable_privkeys_checkbox->isEnabled()) { ui->disable_privkeys_checkbox->setChecked(false); } }); } CreateWalletDialog::~CreateWalletDialog() { delete ui; } QString CreateWalletDialog::walletName() const { return ui->wallet_name_line_edit->text(); } bool CreateWalletDialog::encrypt() const { return ui->encrypt_wallet_checkbox->isChecked(); } bool CreateWalletDialog::disablePrivateKeys() const { return ui->disable_privkeys_checkbox->isChecked(); } bool CreateWalletDialog::blank() const { return ui->blank_wallet_checkbox->isChecked(); } diff --git a/src/qt/guiconstants.h b/src/qt/guiconstants.h index 1f4e682e8..36a53092a 100644 --- a/src/qt/guiconstants.h +++ b/src/qt/guiconstants.h @@ -1,55 +1,57 @@ // Copyright (c) 2011-2016 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_QT_GUICONSTANTS_H #define BITCOIN_QT_GUICONSTANTS_H +#include + /* Milliseconds between model updates */ static const int MODEL_UPDATE_DELAY = 250; /* AskPassphraseDialog -- Maximum passphrase length */ static const int MAX_PASSPHRASE_SIZE = 1024; /* BitcoinGUI -- Size of icons in status bar */ static const int STATUSBAR_ICONSIZE = 16; static const bool DEFAULT_SPLASHSCREEN = true; /* Invalid field background style */ #define STYLE_INVALID "background:#FF8080" /* Transaction list -- unconfirmed transaction */ #define COLOR_UNCONFIRMED QColor(128, 128, 128) /* Transaction list -- negative amount */ #define COLOR_NEGATIVE QColor(255, 0, 0) /* Transaction list -- bare address (without label) */ #define COLOR_BAREADDRESS QColor(140, 140, 140) /* Transaction list -- TX status decoration - open until date */ #define COLOR_TX_STATUS_OPENUNTILDATE QColor(64, 64, 255) /* Transaction list -- TX status decoration - danger, tx needs attention */ #define COLOR_TX_STATUS_DANGER QColor(200, 100, 100) /* Transaction list -- TX status decoration - default color */ #define COLOR_BLACK QColor(0, 0, 0) /* Tooltips longer than this (in characters) are converted into rich text, so that they can be word-wrapped. */ static const int TOOLTIP_WRAP_THRESHOLD = 80; /* Maximum allowed URI length */ static const int MAX_URI_LENGTH = 255; /* QRCodeDialog -- size of exported QR Code image */ #define QR_IMAGE_SIZE 350 /* Number of frames in spinner animation */ #define SPINNER_FRAMES 36 #define QAPP_ORG_NAME "BitcoinABC" #define QAPP_ORG_DOMAIN "bitcoinabc.org" #define QAPP_APP_NAME_DEFAULT "BitcoinABC-Qt" #define QAPP_APP_NAME_TESTNET "BitcoinABC-Qt-testnet" #define QAPP_APP_NAME_REGTEST "BitcoinABC-Qt-regtest" #endif // BITCOIN_QT_GUICONSTANTS_H diff --git a/src/qt/walletcontroller.cpp b/src/qt/walletcontroller.cpp index 765b2f111..844efa0c3 100644 --- a/src/qt/walletcontroller.cpp +++ b/src/qt/walletcontroller.cpp @@ -1,212 +1,307 @@ // Copyright (c) 2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include +#include +#include #include #include +#include + #include #include #include #include #include #include #include WalletController::WalletController(interfaces::Node &node, const PlatformStyle *platform_style, OptionsModel *options_model, QObject *parent) : QObject(parent), m_activity_thread(new QThread(this)), m_activity_worker(new QObject), m_node(node), m_platform_style(platform_style), m_options_model(options_model) { m_handler_load_wallet = m_node.handleLoadWallet( [this](std::unique_ptr wallet) { getOrCreateWallet(std::move(wallet)); }); for (std::unique_ptr &wallet : m_node.getWallets()) { getOrCreateWallet(std::move(wallet)); } m_activity_worker->moveToThread(m_activity_thread); m_activity_thread->start(); } // Not using the default destructor because not all member types definitions are // available in the header, just forward declared. WalletController::~WalletController() { m_activity_thread->quit(); m_activity_thread->wait(); delete m_activity_worker; } std::vector WalletController::getOpenWallets() const { QMutexLocker locker(&m_mutex); return m_wallets; } std::map WalletController::listWalletDir() const { QMutexLocker locker(&m_mutex); std::map wallets; for (const std::string &name : m_node.listWalletDir()) { wallets[name] = false; } for (WalletModel *wallet_model : m_wallets) { auto it = wallets.find(wallet_model->wallet().getWalletName()); if (it != wallets.end()) { it->second = true; } } return wallets; } void WalletController::closeWallet(WalletModel *wallet_model, QWidget *parent) { QMessageBox box(parent); box.setWindowTitle(tr("Close wallet")); box.setText(tr("Are you sure you wish to close wallet %1?") .arg(wallet_model->getDisplayName())); box.setInformativeText( tr("Closing the wallet for too long can result in having to resync the " "entire chain if pruning is enabled.")); box.setStandardButtons(QMessageBox::Yes | QMessageBox::Cancel); box.setDefaultButton(QMessageBox::Yes); if (box.exec() != QMessageBox::Yes) { return; } // First remove wallet from node. wallet_model->wallet().remove(); // Now release the model. removeAndDeleteWallet(wallet_model); } WalletModel *WalletController::getOrCreateWallet( std::unique_ptr wallet) { QMutexLocker locker(&m_mutex); // Return model instance if exists. if (!m_wallets.empty()) { std::string name = wallet->getWalletName(); for (WalletModel *wallet_model : m_wallets) { if (wallet_model->wallet().getWalletName() == name) { return wallet_model; } } } // Instantiate model and register it. WalletModel *wallet_model = new WalletModel( std::move(wallet), m_node, m_platform_style, m_options_model, nullptr); // Handler callback runs in a different thread so fix wallet model thread // affinity. wallet_model->moveToThread(thread()); wallet_model->setParent(this); m_wallets.push_back(wallet_model); connect( wallet_model, &WalletModel::unload, this, [this, wallet_model] { // Defer removeAndDeleteWallet when no modal widget is active. // TODO: remove this workaround by removing usage of QDiallog::exec. if (QApplication::activeModalWidget()) { connect( qApp, &QApplication::focusWindowChanged, wallet_model, [this, wallet_model]() { if (!QApplication::activeModalWidget()) { removeAndDeleteWallet(wallet_model); } }, Qt::QueuedConnection); } else { removeAndDeleteWallet(wallet_model); } }, Qt::QueuedConnection); // Re-emit coinsSent signal from wallet model. connect(wallet_model, &WalletModel::coinsSent, this, &WalletController::coinsSent); // Notify walletAdded signal on the GUI thread. Q_EMIT walletAdded(wallet_model); return wallet_model; } void WalletController::removeAndDeleteWallet(WalletModel *wallet_model) { // Unregister wallet model. { QMutexLocker locker(&m_mutex); m_wallets.erase( std::remove(m_wallets.begin(), m_wallets.end(), wallet_model)); } Q_EMIT walletRemoved(wallet_model); // Currently this can trigger the unload since the model can hold the last // CWallet shared pointer. delete wallet_model; } WalletControllerActivity::WalletControllerActivity( WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams) : QObject(wallet_controller), m_wallet_controller(wallet_controller), m_parent_widget(parent_widget), m_chainparams(chainparams) {} WalletControllerActivity::~WalletControllerActivity() { delete m_progress_dialog; } void WalletControllerActivity::showProgressDialog(const QString &label_text) { m_progress_dialog = new QProgressDialog(m_parent_widget); m_progress_dialog->setLabelText(label_text); m_progress_dialog->setRange(0, 0); m_progress_dialog->setCancelButton(nullptr); m_progress_dialog->setWindowModality(Qt::ApplicationModal); GUIUtil::PolishProgressDialog(m_progress_dialog); } +CreateWalletActivity::CreateWalletActivity(WalletController *wallet_controller, + QWidget *parent_widget, + const CChainParams &chainparams) + : WalletControllerActivity(wallet_controller, parent_widget, chainparams) { + m_passphrase.reserve(MAX_PASSPHRASE_SIZE); +} + +CreateWalletActivity::~CreateWalletActivity() { + delete m_create_wallet_dialog; + delete m_passphrase_dialog; +} + +void CreateWalletActivity::askPassphrase() { + m_passphrase_dialog = new AskPassphraseDialog( + AskPassphraseDialog::Encrypt, m_parent_widget, &m_passphrase); + m_passphrase_dialog->show(); + + connect(m_passphrase_dialog, &QObject::destroyed, + [this] { m_passphrase_dialog = nullptr; }); + connect(m_passphrase_dialog, &QDialog::accepted, + [this] { createWallet(); }); + connect(m_passphrase_dialog, &QDialog::rejected, + [this] { Q_EMIT finished(); }); +} + +void CreateWalletActivity::createWallet() { + showProgressDialog( + tr("Creating Wallet %1...") + .arg(m_create_wallet_dialog->walletName().toHtmlEscaped())); + + std::string name = m_create_wallet_dialog->walletName().toStdString(); + uint64_t flags = 0; + if (m_create_wallet_dialog->disablePrivateKeys()) { + flags |= WALLET_FLAG_DISABLE_PRIVATE_KEYS; + } + if (m_create_wallet_dialog->blank()) { + flags |= WALLET_FLAG_BLANK_WALLET; + } + + QTimer::singleShot(500, worker(), [this, name, flags] { + std::unique_ptr wallet; + WalletCreationStatus status = + node().createWallet(this->m_chainparams, m_passphrase, flags, name, + m_error_message, m_warning_message, wallet); + + if (status == WalletCreationStatus::SUCCESS) { + m_wallet_model = + m_wallet_controller->getOrCreateWallet(std::move(wallet)); + } + + QTimer::singleShot(500, this, &CreateWalletActivity::finish); + }); +} + +void CreateWalletActivity::finish() { + m_progress_dialog->hide(); + + if (!m_error_message.empty()) { + QMessageBox::critical(m_parent_widget, tr("Create wallet failed"), + QString::fromStdString(m_error_message)); + } else if (!m_warning_message.empty()) { + QMessageBox::warning(m_parent_widget, tr("Create wallet warning"), + QString::fromStdString(m_warning_message)); + } + + if (m_wallet_model) { + Q_EMIT created(m_wallet_model); + } + + Q_EMIT finished(); +} + +void CreateWalletActivity::create() { + m_create_wallet_dialog = new CreateWalletDialog(m_parent_widget); + m_create_wallet_dialog->setWindowModality(Qt::ApplicationModal); + m_create_wallet_dialog->show(); + + connect(m_create_wallet_dialog, &QObject::destroyed, + [this] { m_create_wallet_dialog = nullptr; }); + connect(m_create_wallet_dialog, &QDialog::rejected, + [this] { Q_EMIT finished(); }); + connect(m_create_wallet_dialog, &QDialog::accepted, [this] { + if (m_create_wallet_dialog->encrypt()) { + askPassphrase(); + } else { + createWallet(); + } + }); +} + OpenWalletActivity::OpenWalletActivity(WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams) : WalletControllerActivity(wallet_controller, parent_widget, chainparams) {} void OpenWalletActivity::finish() { m_progress_dialog->hide(); if (!m_error_message.empty()) { QMessageBox::critical(m_parent_widget, tr("Open wallet failed"), QString::fromStdString(m_error_message)); } else if (!m_warning_message.empty()) { QMessageBox::warning(m_parent_widget, tr("Open wallet warning"), QString::fromStdString(m_warning_message)); } if (m_wallet_model) { Q_EMIT opened(m_wallet_model); } Q_EMIT finished(); } void OpenWalletActivity::open(const std::string &path) { QString name = path.empty() ? QString("[" + tr("default wallet") + "]") : QString::fromStdString(path); showProgressDialog( tr("Opening Wallet %1...").arg(name.toHtmlEscaped())); QTimer::singleShot(0, worker(), [this, path] { std::unique_ptr wallet = node().loadWallet( this->m_chainparams, path, m_error_message, m_warning_message); if (wallet) { m_wallet_model = m_wallet_controller->getOrCreateWallet(std::move(wallet)); } QTimer::singleShot(0, this, &OpenWalletActivity::finish); }); } diff --git a/src/qt/walletcontroller.h b/src/qt/walletcontroller.h index 852afb0d8..da48811bf 100644 --- a/src/qt/walletcontroller.h +++ b/src/qt/walletcontroller.h @@ -1,123 +1,152 @@ // Copyright (c) 2019 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. #ifndef BITCOIN_QT_WALLETCONTROLLER_H #define BITCOIN_QT_WALLETCONTROLLER_H #include +#include #include #include #include #include #include #include #include #include #include #include +#include class OptionsModel; class PlatformStyle; namespace interfaces { class Handler; class Node; } // namespace interfaces +class AskPassphraseDialog; +class CreateWalletActivity; +class CreateWalletDialog; class OpenWalletActivity; class WalletControllerActivity; /** * Controller between interfaces::Node, WalletModel instances and the GUI. */ class WalletController : public QObject { Q_OBJECT void removeAndDeleteWallet(WalletModel *wallet_model); public: WalletController(interfaces::Node &node, const PlatformStyle *platform_style, OptionsModel *options_model, QObject *parent); ~WalletController(); //! Returns wallet models currently open. std::vector getOpenWallets() const; WalletModel *getOrCreateWallet(std::unique_ptr wallet); //! Returns all wallet names in the wallet dir mapped to whether the wallet //! is loaded. std::map listWalletDir() const; void closeWallet(WalletModel *wallet_model, QWidget *parent = nullptr); Q_SIGNALS: void walletAdded(WalletModel *wallet_model); void walletRemoved(WalletModel *wallet_model); void coinsSent(WalletModel *wallet_model, SendCoinsRecipient recipient, QByteArray transaction); private: QThread *const m_activity_thread; QObject *const m_activity_worker; interfaces::Node &m_node; const PlatformStyle *const m_platform_style; OptionsModel *const m_options_model; mutable QMutex m_mutex; std::vector m_wallets; std::unique_ptr m_handler_load_wallet; friend class WalletControllerActivity; }; class WalletControllerActivity : public QObject { Q_OBJECT public: WalletControllerActivity(WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams); virtual ~WalletControllerActivity(); Q_SIGNALS: void finished(); protected: interfaces::Node &node() const { return m_wallet_controller->m_node; } QObject *worker() const { return m_wallet_controller->m_activity_worker; } void showProgressDialog(const QString &label_text); WalletController *const m_wallet_controller; QWidget *const m_parent_widget; QProgressDialog *m_progress_dialog{nullptr}; WalletModel *m_wallet_model{nullptr}; std::string m_error_message; std::string m_warning_message; const CChainParams &m_chainparams; }; +class CreateWalletActivity : public WalletControllerActivity { + Q_OBJECT + +public: + CreateWalletActivity(WalletController *wallet_controller, + QWidget *parent_widget, + const CChainParams &chainparams); + virtual ~CreateWalletActivity(); + + void create(); + +Q_SIGNALS: + void created(WalletModel *wallet_model); + +private: + void askPassphrase(); + void createWallet(); + void finish(); + + SecureString m_passphrase; + CreateWalletDialog *m_create_wallet_dialog{nullptr}; + AskPassphraseDialog *m_passphrase_dialog{nullptr}; +}; + class OpenWalletActivity : public WalletControllerActivity { Q_OBJECT public: OpenWalletActivity(WalletController *wallet_controller, QWidget *parent_widget, const CChainParams &chainparams); void open(const std::string &path); Q_SIGNALS: void opened(WalletModel *wallet_model); private: void finish(); }; #endif // BITCOIN_QT_WALLETCONTROLLER_H