From 2bba5c4787536c6ff59007b3a9f33b8fe94f75d5 Mon Sep 17 00:00:00 2001 From: tobtoht Date: Thu, 8 Jul 2021 02:34:27 +0200 Subject: [PATCH] Trezor: add passphrase support --- src/MainWindow.cpp | 20 +++++++++++++++++ src/MainWindow.h | 1 + src/WindowManager.cpp | 21 ++++++++++++++++++ src/WindowManager.h | 1 + src/libwalletqt/WalletManager.cpp | 36 +++++++++++++++---------------- 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp index 2cc7816..c36717c 100644 --- a/src/MainWindow.cpp +++ b/src/MainWindow.cpp @@ -377,6 +377,7 @@ void MainWindow::initWalletContext() { // Wallet connect(m_ctx->wallet, &Wallet::connectionStatusChanged, this, &MainWindow::onConnectionStatusChanged); connect(m_ctx->wallet, &Wallet::currentSubaddressAccountChanged, this, &MainWindow::updateTitle); + connect(m_ctx->wallet, &Wallet::walletPassphraseNeeded, this, &MainWindow::onWalletPassphraseNeeded); } void MainWindow::menuToggleTabVisible(const QString &key){ @@ -1190,6 +1191,25 @@ void MainWindow::onDeviceButtonPressed() { m_splashDialog->hide(); } +void MainWindow::onWalletPassphraseNeeded(bool on_device) { + auto button = QMessageBox::question(nullptr, "Wallet Passphrase Needed", "Enter passphrase on hardware wallet?\n\n" + "It is recommended to enter passphrase on " + "the hardware wallet for better security.", + QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); + if (button == QMessageBox::Yes) { + m_ctx->wallet->onPassphraseEntered("", true, false); + return; + } + + bool ok; + QString passphrase = QInputDialog::getText(nullptr, "Wallet Passphrase Needed", "Enter passphrase:", QLineEdit::EchoMode::Password, "", &ok); + if (ok) { + m_ctx->wallet->onPassphraseEntered(passphrase, false, false); + } else { + m_ctx->wallet->onPassphraseEntered(passphrase, false, true); + } +} + void MainWindow::updateNetStats() { if (!m_ctx->wallet || m_ctx->wallet->connectionStatus() == Wallet::ConnectionStatus_Disconnected || m_ctx->wallet->connectionStatus() == Wallet::ConnectionStatus_Synchronized) diff --git a/src/MainWindow.h b/src/MainWindow.h index 41272f8..aa90fa4 100644 --- a/src/MainWindow.h +++ b/src/MainWindow.h @@ -174,6 +174,7 @@ private slots: void onDeviceError(const QString &error); void onDeviceButtonRequest(quint64 code); void onDeviceButtonPressed(); + void onWalletPassphraseNeeded(bool on_device); void menuHwDeviceClicked(); void onUpdatesAvailable(const QJsonObject &updates); void toggleSearchbar(bool enabled); diff --git a/src/WindowManager.cpp b/src/WindowManager.cpp index b67b5c6..33dd4cc 100644 --- a/src/WindowManager.cpp +++ b/src/WindowManager.cpp @@ -3,6 +3,7 @@ #include "WindowManager.h" +#include #include #include "constants.h" @@ -24,6 +25,7 @@ WindowManager::WindowManager() { connect(m_walletManager, &WalletManager::deviceButtonRequest, this, &WindowManager::onDeviceButtonRequest); connect(m_walletManager, &WalletManager::deviceButtonPressed, this, &WindowManager::onDeviceButtonPressed); connect(m_walletManager, &WalletManager::deviceError, this, &WindowManager::onDeviceError); + connect(m_walletManager, &WalletManager::walletPassphraseNeeded, this, &WindowManager::onWalletPassphraseNeeded); connect(qApp, &QGuiApplication::lastWindowClosed, this, &WindowManager::quitAfterLastWindow); @@ -393,6 +395,25 @@ void WindowManager::onDeviceError(const QString &errorMessage) { qCritical() << Q_FUNC_INFO << errorMessage; } +void WindowManager::onWalletPassphraseNeeded(bool on_device) { + auto button = QMessageBox::question(nullptr, "Wallet Passphrase Needed", "Enter passphrase on hardware wallet?\n\n" + "It is recommended to enter passphrase on " + "the hardware wallet for better security.", + QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes); + if (button == QMessageBox::Yes) { + m_walletManager->onPassphraseEntered("", true, false); + return; + } + + bool ok; + QString passphrase = QInputDialog::getText(nullptr, "Wallet Passphrase Needed", "Enter passphrase:", QLineEdit::EchoMode::Password, "", &ok); + if (ok) { + m_walletManager->onPassphraseEntered(passphrase, false, false); + } else { + m_walletManager->onPassphraseEntered(passphrase, false, true); + } +} + // ######################## TRAY ######################## void WindowManager::buildTrayMenu() { diff --git a/src/WindowManager.h b/src/WindowManager.h index 1bf852f..801d537 100644 --- a/src/WindowManager.h +++ b/src/WindowManager.h @@ -41,6 +41,7 @@ private slots: void onDeviceButtonRequest(quint64 code); void onDeviceButtonPressed(); void onDeviceError(const QString &errorMessage); + void onWalletPassphraseNeeded(bool on_device); private: void tryCreateWallet(FeatherSeed seed, const QString &path, const QString &password, const QString &seedOffset); diff --git a/src/libwalletqt/WalletManager.cpp b/src/libwalletqt/WalletManager.cpp index 0c1d655..9c47dd8 100644 --- a/src/libwalletqt/WalletManager.cpp +++ b/src/libwalletqt/WalletManager.cpp @@ -9,40 +9,40 @@ class WalletPassphraseListenerImpl : public Monero::WalletListener, public PassphraseReceiver { public: - WalletPassphraseListenerImpl(WalletManager * mgr): m_mgr(mgr), m_phelper(mgr) {} + explicit WalletPassphraseListenerImpl(WalletManager * mgr): m_mgr(mgr), m_phelper(mgr) {} - virtual void moneySpent(const std::string &txId, uint64_t amount) override { (void)txId; (void)amount; }; - virtual void moneyReceived(const std::string &txId, uint64_t amount) override { (void)txId; (void)amount; }; - virtual void unconfirmedMoneyReceived(const std::string &txId, uint64_t amount) override { (void)txId; (void)amount; }; - virtual void newBlock(uint64_t height) override { (void) height; }; - virtual void updated() override {}; - virtual void refreshed(bool success) override {}; + void moneySpent(const std::string &txId, uint64_t amount) override { (void)txId; (void)amount; }; + void moneyReceived(const std::string &txId, uint64_t amount) override { (void)txId; (void)amount; }; + void unconfirmedMoneyReceived(const std::string &txId, uint64_t amount) override { (void)txId; (void)amount; }; + void newBlock(uint64_t height) override { (void) height; }; + void updated() override {}; + void refreshed(bool success) override {}; - virtual void onPassphraseEntered(const QString &passphrase, bool enter_on_device, bool entry_abort) override + void onPassphraseEntered(const QString &passphrase, bool enter_on_device, bool entry_abort) override { qDebug() << __FUNCTION__; m_phelper.onPassphraseEntered(passphrase, enter_on_device, entry_abort); } -// virtual Monero::optional onDevicePassphraseRequest(bool & on_device) override -// { -// qDebug() << __FUNCTION__; -// return m_phelper.onDevicePassphraseRequest(on_device); -// } -// - virtual void onDeviceButtonRequest(uint64_t code) override + Monero::optional onDevicePassphraseRequest(bool & on_device) override + { + qDebug() << __FUNCTION__; + return m_phelper.onDevicePassphraseRequest(on_device); + } + + void onDeviceButtonRequest(uint64_t code) override { qDebug() << __FUNCTION__; emit m_mgr->deviceButtonRequest(code); } -// - virtual void onDeviceButtonPressed() override + + void onDeviceButtonPressed() override { qDebug() << __FUNCTION__; emit m_mgr->deviceButtonPressed(); } - virtual void onDeviceError(const std::string &message) override + void onDeviceError(const std::string &message) override { qDebug() << __FUNCTION__; emit m_mgr->deviceError(QString::fromStdString(message));