From 5ed7cee4808a7f297206c1aac4100394a908835c Mon Sep 17 00:00:00 2001 From: adityapk00 Date: Wed, 21 Nov 2018 23:29:31 -0800 Subject: [PATCH] export priv key in the addresses dialog --- src/mainwindow.cpp | 73 +++++++++++++++++++++++++++++---------------- src/mainwindow.h | 1 + src/mainwindow.ui | 73 ++++++++++++++++++++++++++++++++++++++++----- src/qrcodelabel.cpp | 2 +- src/rpc.cpp | 1 - 5 files changed, 114 insertions(+), 36 deletions(-) diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index c598f05..d59634e 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -677,6 +677,12 @@ void MainWindow::backupWalletDat() { } void MainWindow::exportAllKeys() { + exportKeys(""); +} + +void MainWindow::exportKeys(QString addr) { + bool allKeys = addr.isEmpty() ? true : false; + QDialog d(this); Ui_PrivKey pui; pui.setupUi(&d); @@ -691,7 +697,11 @@ void MainWindow::exportAllKeys() { pui.privKeyTxt->setPlainText("Loading..."); pui.privKeyTxt->setReadOnly(true); pui.privKeyTxt->setLineWrapMode(QPlainTextEdit::LineWrapMode::NoWrap); - pui.helpLbl->setText("These are all the private keys for all the addresses in your wallet"); + + if (allKeys) + pui.helpLbl->setText("These are all the private keys for all the addresses in your wallet"); + else + pui.helpLbl->setText("Private key for " + addr); // Disable the save button until it finishes loading pui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(false); @@ -700,7 +710,7 @@ void MainWindow::exportAllKeys() { // Wire up save button QObject::connect(pui.buttonBox->button(QDialogButtonBox::Save), &QPushButton::clicked, [=] () { QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), - "zcash-all-privatekeys.txt"); + allKeys ? "zcash-all-privatekeys.txt" : "zcash-privatekey.txt"); QFile file(fileName); if (!file.open(QIODevice::WriteOnly)) { QMessageBox::information(this, tr("Unable to open file"), file.errorString()); @@ -712,9 +722,10 @@ void MainWindow::exportAllKeys() { // Call the API auto isDialogAlive = std::make_shared(true); - rpc->getAllPrivKeys([=] (auto privKeys) { + + auto fnUpdateUIWithKeys = [=](QList> privKeys) { // Check to see if we are still showing. - if (! *isDialogAlive.get()) return; + if (!isDialogAlive) return; QString allKeysTxt; for (auto keypair : privKeys) { @@ -723,10 +734,28 @@ void MainWindow::exportAllKeys() { pui.privKeyTxt->setPlainText(allKeysTxt); pui.buttonBox->button(QDialogButtonBox::Save)->setEnabled(true); - }); + }; + if (allKeys) { + rpc->getAllPrivKeys(fnUpdateUIWithKeys); + } + else { + auto fnAddKey = [=](json key) { + QList> singleAddrKey; + singleAddrKey.push_back(QPair(addr, QString::fromStdString(key.get()))); + fnUpdateUIWithKeys(singleAddrKey); + }; + + if (Settings::getInstance()->isZAddress(addr)) { + rpc->getZPrivKey(addr, fnAddKey); + } + else { + rpc->getTPrivKey(addr, fnAddKey); + } + } + d.exec(); - *isDialogAlive.get() = false; + *isDialogAlive = false; } void MainWindow::setupBalancesTab() { @@ -786,26 +815,7 @@ void MainWindow::setupBalancesTab() { }); menu.addAction("Get private key", [=] () { - auto fnCB = [=] (const json& reply) { - auto privKey = QString::fromStdString(reply.get()); - QDialog d(this); - Ui_PrivKey pui; - pui.setupUi(&d); - - pui.helpLbl->setText("Private Key:"); - pui.privKeyTxt->setPlainText(privKey); - pui.privKeyTxt->setReadOnly(true); - pui.privKeyTxt->selectAll(); - pui.buttonBox->button(QDialogButtonBox::Save)->setVisible(false); - pui.buttonBox->button(QDialogButtonBox::Ok)->setVisible(false); - - d.exec(); - }; - - if (Settings::getInstance()->isZAddress(addr)) - rpc->getZPrivKey(addr, fnCB); - else - rpc->getTPrivKey(addr, fnCB); + this->exportKeys(addr); }); menu.addAction("Send from " % addr.left(40) % (addr.size() > 40 ? "..." : ""), [=]() { @@ -1072,6 +1082,9 @@ void MainWindow::setupRecieveTab() { auto curLabel = AddressBook::getInstance()->getLabelForAddress(addr); auto label = ui->rcvLabel->text().trimmed(); + if (curLabel == label) // Nothing to update + return; + QString info; if (!curLabel.isEmpty() && label.isEmpty()) { @@ -1101,6 +1114,14 @@ void MainWindow::setupRecieveTab() { } }); + // Recieve Export Key + QObject::connect(ui->exportKey, &QPushButton::clicked, [=]() { + QString addr = ui->listRecieveAddresses->currentText(); + if (addr.isEmpty()) + return; + + this->exportKeys(addr); + }); } MainWindow::~MainWindow() diff --git a/src/mainwindow.h b/src/mainwindow.h index 265b62c..382b86e 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -92,6 +92,7 @@ private: void postToZBoard(); void importPrivKey(); void exportAllKeys(); + void exportKeys(QString addr = ""); void backupWalletDat(); void doImport(QList* keys); diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 9703b6f..2267199 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -22,7 +22,7 @@ - 2 + 0 @@ -35,7 +35,7 @@ - 300 + 250 0 @@ -52,6 +52,12 @@ + + + 0 + 0 + + Shielded @@ -73,6 +79,12 @@ + + + 0 + 0 + + Transparent @@ -101,6 +113,12 @@ + + + 0 + 0 + + 75 @@ -145,6 +163,12 @@ + + + 0 + 0 + + color: red; @@ -160,6 +184,12 @@ + + + 0 + 0 + + Address Balances @@ -672,6 +702,13 @@ + + + + + + + @@ -684,12 +721,8 @@ 40 - - - - - - TextLabel + + Optional @@ -716,6 +749,30 @@ + + + + + + Export Private Key + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + diff --git a/src/qrcodelabel.cpp b/src/qrcodelabel.cpp index d7541be..1227d69 100644 --- a/src/qrcodelabel.cpp +++ b/src/qrcodelabel.cpp @@ -3,7 +3,7 @@ QRCodeLabel::QRCodeLabel(QWidget *parent) : QLabel(parent) { - this->setMinimumSize(1,1); + this->setMinimumSize(100, 100); setScaledContents(false); } diff --git a/src/rpc.cpp b/src/rpc.cpp index 2cd21f3..2c63d2c 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -285,7 +285,6 @@ void RPC::getAllPrivKeys(const std::function> }); }; - // First get all the t and z addresses. json payloadT = { {"jsonrpc", "1.0"},