Skip to content

Commit adc9258

Browse files
committed
gui: Menu action for exporting a watchonly wallet
1 parent a2aab6d commit adc9258

4 files changed

Lines changed: 26 additions & 0 deletions

File tree

src/interfaces/wallet.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,9 @@ class Wallet
302302

303303
//! Return pointer to internal wallet class, useful for testing.
304304
virtual wallet::CWallet* wallet() { return nullptr; }
305+
306+
//! Export a watchonly wallet file. See CWallet::ExportWatchOnlyWallet
307+
virtual util::Result<std::string> exportWatchOnlyWallet(const fs::path& destination) = 0;
305308
};
306309

307310
//! Wallet chain client that in addition to having chain client methods for

src/qt/bitcoingui.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ void BitcoinGUI::createActions()
377377
m_mask_values_action->setStatusTip(tr("Mask the values in the Overview tab"));
378378
m_mask_values_action->setCheckable(true);
379379

380+
m_export_watchonly_action = new QAction(tr("Export watch-only wallet"), this);
381+
m_export_watchonly_action->setEnabled(false);
382+
m_export_watchonly_action->setStatusTip(tr("Export a watch-only version of the current wallet that can be restored onto another node."));
383+
380384
connect(quitAction, &QAction::triggered, this, &BitcoinGUI::quitRequested);
381385
connect(aboutAction, &QAction::triggered, this, &BitcoinGUI::aboutClicked);
382386
connect(aboutQtAction, &QAction::triggered, qApp, QApplication::aboutQt);
@@ -524,6 +528,16 @@ void BitcoinGUI::createActions()
524528
});
525529
connect(m_mask_values_action, &QAction::toggled, this, &BitcoinGUI::setPrivacy);
526530
connect(m_mask_values_action, &QAction::toggled, this, &BitcoinGUI::enableHistoryAction);
531+
connect(m_export_watchonly_action, &QAction::triggered, [this] {
532+
QString destination = GUIUtil::getSaveFileName(this, tr("Save Watch-only Wallet Export"), QString(), QString(), nullptr);
533+
if (destination.isEmpty()) return;
534+
util::Result<std::string> export_res = walletFrame->currentWalletModel()->wallet().exportWatchOnlyWallet(GUIUtil::QStringToPath(destination));
535+
if (export_res) {
536+
QMessageBox::information(nullptr, tr("Export Successful"), tr("The wallet has been exported to ") + QString::fromStdString(*export_res));
537+
} else {
538+
QMessageBox::critical(nullptr, tr("Export Error"), QString::fromStdString(util::ErrorString(export_res).translated));
539+
}
540+
});
527541
}
528542
#endif // ENABLE_WALLET
529543

@@ -547,6 +561,7 @@ void BitcoinGUI::createMenuBar()
547561
file->addSeparator();
548562
file->addAction(backupWalletAction);
549563
file->addAction(m_restore_wallet_action);
564+
file->addAction(m_export_watchonly_action);
550565
file->addSeparator();
551566
file->addAction(openAction);
552567
file->addAction(signMessageAction);
@@ -755,6 +770,7 @@ void BitcoinGUI::setWalletController(WalletController* wallet_controller, bool s
755770
m_restore_wallet_action->setEnabled(true);
756771
m_migrate_wallet_action->setEnabled(true);
757772
m_migrate_wallet_action->setMenu(m_migrate_wallet_menu);
773+
m_export_watchonly_action->setEnabled(true);
758774

759775
GUIUtil::ExceptionSafeConnect(wallet_controller, &WalletController::walletAdded, this, &BitcoinGUI::addWallet);
760776
connect(wallet_controller, &WalletController::walletRemoved, this, &BitcoinGUI::removeWallet);

src/qt/bitcoingui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ class BitcoinGUI : public QMainWindow
170170
#ifdef ENABLE_WALLET
171171
QLabel *m_wallet_selector_label = nullptr;
172172
QComboBox* m_wallet_selector = nullptr;
173+
QAction* m_export_watchonly_action{nullptr};
173174
#endif // ENABLE_WALLET
174175
QSystemTrayIcon* trayIcon = nullptr;
175176
const std::unique_ptr<QMenu> trayIconMenu;

src/wallet/interfaces.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <util/ui_change_type.h>
2222
#include <wallet/coincontrol.h>
2323
#include <wallet/context.h>
24+
#include <wallet/export.h>
2425
#include <wallet/feebumper.h>
2526
#include <wallet/fees.h>
2627
#include <wallet/load.h>
@@ -522,6 +523,11 @@ class WalletImpl : public Wallet
522523
}
523524
CWallet* wallet() override { return m_wallet.get(); }
524525

526+
util::Result<std::string> exportWatchOnlyWallet(const fs::path& destination) override {
527+
LOCK(m_wallet->cs_wallet);
528+
return ExportWatchOnlyWallet(*m_wallet, destination, m_context);
529+
}
530+
525531
WalletContext& m_context;
526532
std::shared_ptr<CWallet> m_wallet;
527533
};

0 commit comments

Comments
 (0)