From 73dba4a190df649be37a67b72c4baa3ba839d215 Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Thu, 16 Jan 2020 09:59:38 -0500 Subject: [PATCH] Implement GUI menu option to get the viewing key of a zaddr in balances tab --- silentdragon.pro | 1 + src/mainwindow.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++-- src/mainwindow.h | 1 + src/privkey.ui | 2 +- src/rpc.cpp | 5 ++++ src/rpc.h | 1 + 6 files changed, 78 insertions(+), 3 deletions(-) diff --git a/silentdragon.pro b/silentdragon.pro index 2055e66..9832e5d 100644 --- a/silentdragon.pro +++ b/silentdragon.pro @@ -96,6 +96,7 @@ FORMS += \ src/about.ui \ src/confirm.ui \ src/privkey.ui \ + src/viewkey.ui \ src/memodialog.ui \ src/viewalladdresses.ui \ src/validateaddress.ui \ diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 9cd7e34..f2010d7 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -8,6 +8,7 @@ #include "ui_mobileappconnector.h" #include "ui_addressbook.h" #include "ui_privkey.h" +#include "ui_viewkey.h" #include "ui_about.h" #include "ui_settings.h" #include "ui_viewalladdresses.h" @@ -774,6 +775,69 @@ void MainWindow::exportAllKeys() { exportKeys(""); } +void MainWindow::getViewKey(QString addr) { + QDialog d(this); + Ui_ViewKey vui; + vui.setupUi(&d); + + // Make the window big by default + auto ps = this->geometry(); + QMargins margin = QMargins() + 50; + d.setGeometry(ps.marginsRemoved(margin)); + + Settings::saveRestore(&d); + + vui.viewKeyTxt->setPlainText(tr("Loading...")); + vui.viewKeyTxt->setReadOnly(true); + vui.viewKeyTxt->setLineWrapMode(QPlainTextEdit::LineWrapMode::NoWrap); + + // Disable the save button until it finishes loading + vui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false); + vui.buttonBox->button(QDialogButtonBox::Ok)->setVisible(false); + + bool allKeys = false; //addr.isEmpty() ? true : false; + // Wire up save button + QObject::connect(vui.buttonBox->button(QDialogButtonBox::Save), &QPushButton::clicked, [=] () { + QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), + allKeys ? "hush-all-viewkeys.txt" : "hush-viewkey.txt"); + QFile file(fileName); + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::information(this, tr("Unable to open file"), file.errorString()); + return; + } + QTextStream out(&file); + // TODO: Output in address, viewkey CSV format? + out << vui.viewKeyTxt->toPlainText(); + }); + // TODO: actually get the viewkey of zaddr + + auto isDialogAlive = std::make_shared(true); + + auto fnUpdateUIWithKeys = [=](QList> viewKeys) { + // Check to see if we are still showing. + if (! *(isDialogAlive.get()) ) return; + + QString allKeysTxt; + for (auto keypair : viewKeys) { + allKeysTxt = allKeysTxt % keypair.second % " # addr=" % keypair.first % "\n"; + } + + vui.viewKeyTxt->setPlainText(allKeysTxt); + vui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(true); + }; + + auto fnAddKey = [=](json key) { + QList> singleAddrKey; + singleAddrKey.push_back(QPair(addr, QString::fromStdString(key.get()))); + fnUpdateUIWithKeys(singleAddrKey); + }; + + rpc->getZViewKey(addr, fnAddKey); + + d.exec(); + *isDialogAlive = false; +} + void MainWindow::exportKeys(QString addr) { bool allKeys = addr.isEmpty() ? true : false; @@ -842,8 +906,7 @@ void MainWindow::exportKeys(QString addr) { if (Settings::getInstance()->isZAddress(addr)) { rpc->getZPrivKey(addr, fnAddKey); - } - else { + } else { rpc->getTPrivKey(addr, fnAddKey); } } @@ -912,6 +975,10 @@ void MainWindow::setupBalancesTab() { this->exportKeys(addr); }); + menu.addAction(tr("Get viewing key"), [=] () { + this->getViewKey(addr); + }); + menu.addAction("Send from " % addr.left(40) % (addr.size() > 40 ? "..." : ""), [=]() { fnDoSendFrom(addr); }); diff --git a/src/mainwindow.h b/src/mainwindow.h index 9640d76..9a2f5be 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -124,6 +124,7 @@ private: void importPrivKey(); void exportAllKeys(); void exportKeys(QString addr = ""); + void getViewKey(QString addr = ""); void backupWalletDat(); void exportTransactions(); diff --git a/src/privkey.ui b/src/privkey.ui index 57bdb16..57c3d8f 100644 --- a/src/privkey.ui +++ b/src/privkey.ui @@ -34,7 +34,7 @@ - TextLabel + Private Key(s). Keep Secure! diff --git a/src/rpc.cpp b/src/rpc.cpp index ea77b27..0a80f58 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -171,6 +171,11 @@ void RPC::newTaddr(const std::function& cb) { conn->doRPCWithDefaultErrorHandling(makePayload(method), cb); } +void RPC::getZViewKey(QString addr, const std::function& cb) { + std::string method = "z_exportviewingkey"; + conn->doRPCWithDefaultErrorHandling(makePayload(method, addr.toStdString()), cb); +} + void RPC::getZPrivKey(QString addr, const std::function& cb) { std::string method = "z_exportkey"; conn->doRPCWithDefaultErrorHandling(makePayload(method, addr.toStdString()), cb); diff --git a/src/rpc.h b/src/rpc.h index ef0a292..b599da5 100644 --- a/src/rpc.h +++ b/src/rpc.h @@ -72,6 +72,7 @@ public: void newTaddr(const std::function& cb); void getZPrivKey(QString addr, const std::function& cb); + void getZViewKey(QString addr, const std::function& cb); void getTPrivKey(QString addr, const std::function& cb); void importZPrivKey(QString addr, bool rescan, const std::function& cb); void importTPrivKey(QString addr, bool rescan, const std::function& cb);