From 402b80d289ab937db05672e90bace360b3e155bd Mon Sep 17 00:00:00 2001 From: Aditya Kulkarni Date: Tue, 16 Oct 2018 22:59:22 -0700 Subject: [PATCH] Auto detect testnet and mainnet ports --- src/main.cpp | 2 + src/mainwindow.cpp | 50 +++++++++++++------- src/mainwindow.h | 3 +- src/mainwindow.ui | 29 ++++++++++++ src/precompiled.h | 1 + src/rpc.cpp | 27 +++++++++-- src/scripts/mkrelease.sh | 27 +++++++---- src/scripts/mkwinrelease.ps1 | 2 +- src/sendtab.cpp | 1 - src/settings.cpp | 92 +++++++++++++++++++++++------------- src/settings.h | 10 +++- src/settings.ui | 34 +++++++++---- src/ui_mainwindow.h | 20 ++++++-- src/ui_settings.h | 34 +++++++++---- 14 files changed, 241 insertions(+), 91 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index d009fce..f8c0000 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include "mainwindow.h" +#include "settings.h" #include "precompiled.h" int main(int argc, char *argv[]) @@ -19,6 +20,7 @@ int main(int argc, char *argv[]) QCoreApplication::setOrganizationDomain("adityapk.com"); QCoreApplication::setApplicationName("zcash-qt-wallet"); + Settings::init(); MainWindow w; w.setWindowTitle("zcash-qt-wallet v" + QString(APP_VERSION)); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 110fa55..4e5841c 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -17,9 +17,6 @@ MainWindow::MainWindow(QWidget *parent) : { ui->setupUi(this); - // Load settings - settings = new Settings(); - // Status Bar loadingLabel = new QLabel(); loadingMovie = new QMovie(":/icons/res/loading.gif"); @@ -47,23 +44,22 @@ MainWindow::MainWindow(QWidget *parent) : settings.port->setValidator(&validator); // Load previous values into the dialog - settings.hostname ->setText(this->getSettings()->getHost()); - settings.port ->setText(this->getSettings()->getPort()); - settings.rpcuser ->setText(this->getSettings()->getUsernamePassword().split(":")[0]); - settings.rpcpassword->setText(this->getSettings()->getUsernamePassword().split(":")[1]); + settings.hostname ->setText(Settings::getInstance()->getHost()); + settings.port ->setText(Settings::getInstance()->getPort()); + settings.rpcuser ->setText(Settings::getInstance()->getUsernamePassword().split(":")[0]); + settings.rpcpassword->setText(Settings::getInstance()->getUsernamePassword().split(":")[1]); if (settingsDialog.exec() == QDialog::Accepted) { // Save settings QSettings s; - s.setValue("connection/host", settings.hostname->text()); - s.setValue("connection/port", settings.port->text()); - s.setValue("connection/rpcuser", settings.rpcuser->text()); - s.setValue("connection/rpcpassword", settings.rpcpassword->text()); + s.setValue("connection/host", settings.hostname->text()); + s.setValue("connection/port", settings.port->text()); + s.setValue("connection/rpcuser", settings.rpcuser->text()); + s.setValue("connection/rpcpassword", settings.rpcpassword->text()); s.sync(); - // Then refresh everything - this->getSettings()->refresh(); + // Then refresh everything. this->rpc->reloadConnectionInfo(); this->rpc->refresh(); }; @@ -75,6 +71,8 @@ MainWindow::MainWindow(QWidget *parent) : // Set up donate action QObject::connect(ui->actionDonate, &QAction::triggered, this, &MainWindow::donate); + QObject::connect(ui->actionImport_Private_Keys, &QAction::triggered, this, &MainWindow::importPrivKeys); + // Set up about action QObject::connect(ui->actionAbout, &QAction::triggered, [=] () { QDialog aboutDialog(this); @@ -111,6 +109,28 @@ void MainWindow::donate() { ui->tabWidget->setCurrentIndex(1); } +void MainWindow::importPrivKeys() { + bool ok; + QString text = QInputDialog::getMultiLineText( + this, "Import Private Keys", + QString() + + "Please paste your private keys (zAddr or tAddr) here, one per line.\n" + + "The keys will be imported into your connected zcashd node", + "", &ok); + if (ok && !text.isEmpty()) { + auto keys = text.split("\n"); + for (int i=0; i < keys.length(); i++) { + auto key = keys[i].trimmed(); + if (key.startsWith("S") || + key.startsWith("secret")) { // Z key + + } else { // T Key + + } + } + } +} + void MainWindow::setupBalancesTab() { ui->unconfirmedWarning->setVisible(false); @@ -292,10 +312,6 @@ void MainWindow::setupRecieveTab() { } -Settings* MainWindow::getSettings() { - return settings; -} - MainWindow::~MainWindow() { delete ui; diff --git a/src/mainwindow.h b/src/mainwindow.h index 81ea8a9..c64460f 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -19,8 +19,6 @@ public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); - Settings* getSettings(); - Ui::MainWindow* ui; QLabel* statusLabel; @@ -45,6 +43,7 @@ private: QString doSendTxValidations(QString fromAddr, QList> toAddrs); void donate(); + void importPrivKeys(); RPC* rpc; Settings* settings; diff --git a/src/mainwindow.ui b/src/mainwindow.ui index 0c6007f..03ee812 100644 --- a/src/mainwindow.ui +++ b/src/mainwindow.ui @@ -222,11 +222,33 @@ + + + 0 + 0 + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + true + + + + Qt::Horizontal + + + + 40 + 20 + + + + @@ -622,6 +644,8 @@ File + + @@ -657,6 +681,11 @@ Donate + + + Import Private Keys + + diff --git a/src/precompiled.h b/src/precompiled.h index 1eacdee..c9d8b13 100644 --- a/src/precompiled.h +++ b/src/precompiled.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include #include diff --git a/src/rpc.cpp b/src/rpc.cpp index 271bacb..b4d58df 100644 --- a/src/rpc.cpp +++ b/src/rpc.cpp @@ -60,13 +60,13 @@ void RPC::reloadConnectionInfo() { QUrl myurl; myurl.setScheme("http"); //https also applicable - myurl.setHost(main->getSettings()->getHost()); - myurl.setPort(main->getSettings()->getPort().toInt()); + myurl.setHost(Settings::getInstance()->getHost()); + myurl.setPort(Settings::getInstance()->getPort().toInt()); request.setUrl(myurl); request.setHeader(QNetworkRequest::ContentTypeHeader, "text/plain"); - QString headerData = "Basic " + main->getSettings()->getUsernamePassword().toLocal8Bit().toBase64(); + QString headerData = "Basic " + Settings::getInstance()->getUsernamePassword().toLocal8Bit().toBase64(); request.setRawHeader("Authorization", headerData.toLocal8Bit()); } @@ -385,6 +385,22 @@ void RPC::refreshBalances() { void RPC::refreshTransactions() { auto txdata = new QList(); + /* + auto getZReceivedTransactions = ([=] (const json& reply) { + for (auto& it : reply.get()) { + TransactionItem tx( + QString("receive"), + QDateTime::fromSecsSinceEpoch(it["time"].get()).toLocalTime().toString(), + (it["address"].is_null() ? "" : QString::fromStdString(it["address"])), + QString::fromStdString(it["txid"]), + it["amount"].get(), + it["confirmations"].get() + ); + + txdata->push_front(tx); + } + }); + */ getTransactions([=] (json reply) { for (auto& it : reply.get()) { @@ -426,7 +442,10 @@ void RPC::refreshTxStatus(const QString& newOpid) { // And if it ended up successful QString status = QString::fromStdString(it["status"]); if (status == "success") { - main->ui->statusBar->showMessage(" Tx " % id % " computed successfully and submitted"); + auto txid = QString::fromStdString(it["result"]["txid"]); + qDebug() << "Tx complete. txid " << txid; + QGuiApplication::clipboard()->setText(txid); + main->ui->statusBar->showMessage("Tx completed! Txid copied to clipboard (" + txid + ")"); main->loadingLabel->setVisible(false); watchingOps.remove(id); diff --git a/src/scripts/mkrelease.sh b/src/scripts/mkrelease.sh index cae8c2e..ea44f52 100755 --- a/src/scripts/mkrelease.sh +++ b/src/scripts/mkrelease.sh @@ -4,36 +4,43 @@ if [ -z $QT_STATIC ]; then echo "QT_STATIC is not set"; exit 1; fi if [ -z $APP_VERSION ]; then echo "APP_VERSION is not set"; exit 1; fi if [ -z $PREV_VERSION ]; then echo "PREV_VERSION is not set"; exit 1; fi -echo "Updating version numbers" +echo -n "Version files." # Replace the version number in the .pro file so it gets picked up everywhere sed -i "s/${PREV_VERSION}/${APP_VERSION}/g" zcash-qt-wallet.pro > /dev/null # Also update it in the README.md sed -i "s/${PREV_VERSION}/${APP_VERSION}/g" README.md > /dev/null +echo "[OK]" -echo "Configuring" + +echo -n "Configuring..." make distclean > /dev/null $QT_STATIC/bin/qmake zcash-qt-wallet.pro -spec linux-clang CONFIG+=release > /dev/null +echo "[OK]" + -echo "Building" +echo -n "Building......" rm -rf bin/zcash-qt-wallet* > /dev/null make -j$(nproc) > /dev/null +echo "[OK]" -# Test for Qt -echo "Testing for no Qt" +# Test for Qt +echo -n "Static link..." if [[ $(ldd zcash-qt-wallet | grep -i "Qt") ]]; then echo "FOUND QT; ABORT"; exit 1 -else - echo "No Qt found" fi +echo "[OK]" + -echo "Packaging" +echo -n "Packaging....." mkdir bin/zcash-qt-wallet-v$APP_VERSION > /dev/null cp zcash-qt-wallet bin/zcash-qt-wallet-v$APP_VERSION > /dev/null cp README.md bin/zcash-qt-wallet-v$APP_VERSION > /dev/null cp LICENSE bin/zcash-qt-wallet-v$APP_VERSION > /dev/null -cd bin && tar cvf zcash-qt-wallet-v$APP_VERSION.tar.gz zcash-qt-wallet-v$APP_VERSION/ > /dev/null +cd bin && tar cvf linux-zcash-qt-wallet-v$APP_VERSION.tar.gz zcash-qt-wallet-v$APP_VERSION/ > /dev/null +echo "[OK]" + -echo "Done" \ No newline at end of file +echo "Done. Build is bin/linux-zcash-qt-wallet-v$APP_VERSION.tar.gz" \ No newline at end of file diff --git a/src/scripts/mkwinrelease.ps1 b/src/scripts/mkwinrelease.ps1 index 6bca465..c045c11 100644 --- a/src/scripts/mkwinrelease.ps1 +++ b/src/scripts/mkwinrelease.ps1 @@ -24,6 +24,6 @@ Copy-Item LICENSE release/$target | Out-Null Copy-Item README.md release/$target | Out-Null echo "Zipping" -Compress-Archive -LiteralPath release/$target -DestinationPath "release/$target.zip" +Compress-Archive -LiteralPath release/$target -DestinationPath "release/Windows-$target.zip" echo "Done" \ No newline at end of file diff --git a/src/sendtab.cpp b/src/sendtab.cpp index 8c7826d..c3981ed 100644 --- a/src/sendtab.cpp +++ b/src/sendtab.cpp @@ -252,7 +252,6 @@ void MainWindow::sendButton() { } } - // Add sender json params = json::array(); params.push_back(fromAddr.toStdString()); diff --git a/src/settings.cpp b/src/settings.cpp index 97ee2ae..2de4ee4 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -2,9 +2,24 @@ #include "settings.h" -Settings::Settings() -{ - refresh(); +Settings* Settings::instance = nullptr; + +Settings* Settings::init() { + if (instance != nullptr) return instance; + + instance = new Settings(); + + // Load from settings first, because if they are redefined in the zcash.conf file, + // we'll overwrite them. + instance->loadFromSettings(); + // Overwrite if any are defined in the zcash.conf + instance->loadFromFile(); + + return instance; +} + +Settings* Settings::getInstance() { + return instance; } QString Settings::getHost() { @@ -25,43 +40,56 @@ QString Settings::getUsernamePassword() { return username % ":" % password; } -void Settings::refresh() { - // First step is to try and load from the QT Settings +void Settings::loadFromSettings() { + // First step is to try and load from the QT Settings. These are loaded first, because + // they could be overridden by whats in the zcash.conf, which will take precedence. QSettings s; host = s.value("connection/host", "127.0.0.1" ).toString(); port = s.value("connection/port", "8232" ).toString(); username = s.value("connection/rpcuser", "" ).toString(); - password = s.value("connection/rpcpassword", "" ).toString(); - - if (username == "") { - // Nothing in QT Settings, so try to read from file. - QString zcashdconf = QStandardPaths::locate(QStandardPaths::HomeLocation, ".zcash/zcash.conf"); - if (zcashdconf.isNull()) { - // No zcash file, just return with nothing - return; - } + password = s.value("connection/rpcpassword", "" ).toString(); +} - QFile file(zcashdconf); - if (!file.open(QIODevice::ReadOnly)) { - qDebug() << file.errorString(); - return; - } +void Settings::loadFromFile() { + // Nothing in QT Settings, so try to read from file. + QString zcashdconf = QStandardPaths::locate(QStandardPaths::HomeLocation, ".zcash/zcash.conf"); + if (zcashdconf.isNull()) { + // No zcash file, just return with nothing + return; + } - QTextStream in(&file); + QFile file(zcashdconf); + if (!file.open(QIODevice::ReadOnly)) { + qDebug() << file.errorString(); + return; + } - while (!in.atEnd()) { - QString line = in.readLine(); - QStringList fields = line.split("="); + // If file was found, then setup some defaults + overridePort = "8232"; - if (fields[0].trimmed().toLower() == "rpcuser") { - username = fields[1].trimmed(); - } - if (fields[0].trimmed().toLower() == "rpcpassword") { - password = fields[1].trimmed(); - } - } + QTextStream in(&file); + + while (!in.atEnd()) { + QString line = in.readLine(); + QStringList fields = line.split("="); - file.close(); + if (fields[0].trimmed().toLower() == "rpcuser") { + username = fields[1].trimmed(); + } + if (fields[0].trimmed().toLower() == "rpcpassword") { + password = fields[1].trimmed(); + } + if (fields[0].trimmed().toLower() == "rpcport") { + overridePort = fields[1].trimmed(); + } + if (fields[0].trimmed().toLower() == "testnet" && + fields[1].trimmed() == "1" && + overridePort.isEmpty()) { + overridePort = "18232"; + } } -} \ No newline at end of file + + file.close(); + +} diff --git a/src/settings.h b/src/settings.h index 6581fed..a811d3d 100644 --- a/src/settings.h +++ b/src/settings.h @@ -6,7 +6,8 @@ class Settings { public: - Settings(); + static Settings* init(); + static Settings* getInstance(); QString getUsernamePassword(); QString getHost(); @@ -15,8 +16,13 @@ public: void setDefaultPort(int port) {overridePort = QString::number(port);} double fees() { return 0.0001; } - void refresh(); + void loadFromSettings(); + void loadFromFile(); private: + // This class can only be accessed through Settings::getInstance() + Settings() = default; + + static Settings* instance; QString host; QString port; diff --git a/src/settings.ui b/src/settings.ui index 732ef72..38890c1 100644 --- a/src/settings.ui +++ b/src/settings.ui @@ -6,8 +6,8 @@ 0 0 - 400 - 362 + 441 + 430 @@ -38,7 +38,7 @@ 10 - + @@ -51,10 +51,10 @@ - + - + @@ -67,21 +67,21 @@ - + 127.0.0.1 - + 8232 - + @@ -94,10 +94,10 @@ - + - + @@ -110,6 +110,20 @@ + + + + <html><head/><body><p>Values configured in ~/.zcash/zcash.conf <br/>will overwrite these values</p></body></html> + + + + + + + Qt::Horizontal + + + diff --git a/src/ui_mainwindow.h b/src/ui_mainwindow.h index d80de91..d1066f7 100644 --- a/src/ui_mainwindow.h +++ b/src/ui_mainwindow.h @@ -45,6 +45,7 @@ public: QAction *actionAbout; QAction *actionSettings; QAction *actionDonate; + QAction *actionImport_Private_Keys; QWidget *centralWidget; QGridLayout *gridLayout_3; QTabWidget *tabWidget; @@ -78,6 +79,7 @@ public: QHBoxLayout *horizontalLayout_15; QLabel *label_5; QLineEdit *sendAddressBalance; + QSpacerItem *horizontalSpacer_6; QGroupBox *groupBox_3; QVBoxLayout *verticalLayout_3; QScrollArea *sendToScrollArea; @@ -148,6 +150,8 @@ public: actionSettings->setObjectName(QStringLiteral("actionSettings")); actionDonate = new QAction(MainWindow); actionDonate->setObjectName(QStringLiteral("actionDonate")); + actionImport_Private_Keys = new QAction(MainWindow); + actionImport_Private_Keys->setObjectName(QStringLiteral("actionImport_Private_Keys")); centralWidget = new QWidget(MainWindow); centralWidget->setObjectName(QStringLiteral("centralWidget")); gridLayout_3 = new QGridLayout(centralWidget); @@ -312,10 +316,20 @@ public: sendAddressBalance = new QLineEdit(groupBox_4); sendAddressBalance->setObjectName(QStringLiteral("sendAddressBalance")); + QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + sizePolicy.setHorizontalStretch(0); + sizePolicy.setVerticalStretch(0); + sizePolicy.setHeightForWidth(sendAddressBalance->sizePolicy().hasHeightForWidth()); + sendAddressBalance->setSizePolicy(sizePolicy); + sendAddressBalance->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter); sendAddressBalance->setReadOnly(true); horizontalLayout_15->addWidget(sendAddressBalance); + horizontalSpacer_6 = new QSpacerItem(40, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); + + horizontalLayout_15->addItem(horizontalSpacer_6); + verticalLayout_6->addLayout(horizontalLayout_15); @@ -508,9 +522,6 @@ public: rdioTAddr = new QRadioButton(groupBox_6); rdioTAddr->setObjectName(QStringLiteral("rdioTAddr")); - QSizePolicy sizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); - sizePolicy.setHorizontalStretch(0); - sizePolicy.setVerticalStretch(0); sizePolicy.setHeightForWidth(rdioTAddr->sizePolicy().hasHeightForWidth()); rdioTAddr->setSizePolicy(sizePolicy); @@ -622,6 +633,8 @@ public: menuBar->addAction(menuBalance->menuAction()); menuBar->addAction(menuHelp->menuAction()); + menuBalance->addAction(actionImport_Private_Keys); + menuBalance->addSeparator(); menuBalance->addAction(actionSettings); menuBalance->addSeparator(); menuBalance->addAction(actionExit); @@ -643,6 +656,7 @@ public: actionAbout->setText(QApplication::translate("MainWindow", "About", nullptr)); actionSettings->setText(QApplication::translate("MainWindow", "Settings", nullptr)); actionDonate->setText(QApplication::translate("MainWindow", "Donate", nullptr)); + actionImport_Private_Keys->setText(QApplication::translate("MainWindow", "Import Private Keys", nullptr)); groupBox->setTitle(QApplication::translate("MainWindow", "Summary", nullptr)); label->setText(QApplication::translate("MainWindow", "Shielded", nullptr)); balSheilded->setText(QString()); diff --git a/src/ui_settings.h b/src/ui_settings.h index 98a2a53..867869b 100644 --- a/src/ui_settings.h +++ b/src/ui_settings.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -42,6 +43,8 @@ public: QLabel *label_4; QLineEdit *rpcpassword; QLabel *label_2; + QLabel *label_5; + QFrame *line; QSpacerItem *verticalSpacer; QDialogButtonBox *buttonBox; @@ -49,7 +52,7 @@ public: { if (Settings->objectName().isEmpty()) Settings->setObjectName(QStringLiteral("Settings")); - Settings->resize(400, 362); + Settings->resize(441, 430); Settings->setModal(true); verticalLayout = new QVBoxLayout(Settings); verticalLayout->setObjectName(QStringLiteral("verticalLayout")); @@ -70,45 +73,57 @@ public: label_3->setObjectName(QStringLiteral("label_3")); label_3->setMinimumSize(QSize(60, 0)); - gridLayout->addWidget(label_3, 4, 0, 1, 1); + gridLayout->addWidget(label_3, 6, 0, 1, 1); rpcuser = new QLineEdit(groupBox); rpcuser->setObjectName(QStringLiteral("rpcuser")); - gridLayout->addWidget(rpcuser, 5, 0, 1, 1); + gridLayout->addWidget(rpcuser, 7, 0, 1, 1); label = new QLabel(groupBox); label->setObjectName(QStringLiteral("label")); label->setMinimumSize(QSize(60, 0)); - gridLayout->addWidget(label, 0, 0, 1, 1); + gridLayout->addWidget(label, 2, 0, 1, 1); hostname = new QLineEdit(groupBox); hostname->setObjectName(QStringLiteral("hostname")); - gridLayout->addWidget(hostname, 1, 0, 1, 1); + gridLayout->addWidget(hostname, 3, 0, 1, 1); port = new QLineEdit(groupBox); port->setObjectName(QStringLiteral("port")); - gridLayout->addWidget(port, 3, 0, 1, 1); + gridLayout->addWidget(port, 5, 0, 1, 1); label_4 = new QLabel(groupBox); label_4->setObjectName(QStringLiteral("label_4")); label_4->setMinimumSize(QSize(60, 0)); - gridLayout->addWidget(label_4, 6, 0, 1, 1); + gridLayout->addWidget(label_4, 8, 0, 1, 1); rpcpassword = new QLineEdit(groupBox); rpcpassword->setObjectName(QStringLiteral("rpcpassword")); - gridLayout->addWidget(rpcpassword, 7, 0, 1, 1); + gridLayout->addWidget(rpcpassword, 9, 0, 1, 1); label_2 = new QLabel(groupBox); label_2->setObjectName(QStringLiteral("label_2")); label_2->setMinimumSize(QSize(60, 0)); - gridLayout->addWidget(label_2, 2, 0, 1, 1); + gridLayout->addWidget(label_2, 4, 0, 1, 1); + + label_5 = new QLabel(groupBox); + label_5->setObjectName(QStringLiteral("label_5")); + + gridLayout->addWidget(label_5, 0, 0, 1, 1); + + line = new QFrame(groupBox); + line->setObjectName(QStringLiteral("line")); + line->setFrameShape(QFrame::HLine); + line->setFrameShadow(QFrame::Sunken); + + gridLayout->addWidget(line, 1, 0, 1, 1); verticalLayout_2->addWidget(groupBox); @@ -156,6 +171,7 @@ public: port->setPlaceholderText(QApplication::translate("Settings", "8232", nullptr)); label_4->setText(QApplication::translate("Settings", "RPC Password", nullptr)); label_2->setText(QApplication::translate("Settings", "Port", nullptr)); + label_5->setText(QApplication::translate("Settings", "

Values configured in ~/.zcash/zcash.conf
will overwrite these values

", nullptr)); tabWidget->setTabText(tabWidget->indexOf(tab), QApplication::translate("Settings", "Connection", nullptr)); } // retranslateUi