From f20dd70ede4b5564256f97547dc13d626abd770a Mon Sep 17 00:00:00 2001 From: DenioD <41270280+DenioD@users.noreply.github.com> Date: Thu, 1 Oct 2020 20:45:09 +0200 Subject: [PATCH] add tkey import and autoshield it --- lib/Cargo.lock | 7 ++-- lib/Cargo.toml | 2 +- src/connection.cpp | 40 +++++++++++++++-------- src/connection.h | 1 + src/controller.h | 10 ++++-- src/liteinterface.cpp | 28 +++++++--------- src/liteinterface.h | 3 +- src/mainwindow.cpp | 74 +++++++++++++++++++++++++++++++------------ src/privkey.ui | 17 +++++++--- 9 files changed, 119 insertions(+), 63 deletions(-) diff --git a/lib/Cargo.lock b/lib/Cargo.lock index c55c44e..03fad9d 100644 --- a/lib/Cargo.lock +++ b/lib/Cargo.lock @@ -1192,7 +1192,7 @@ dependencies = [ "blake3 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)", - "silentdragonlitelib 0.1.0 (git+https://github.com/MyHush/silentdragonlite-cli?rev=7637fba279fa9fc47da47ff7d8e5b993f823ec6e)", + "silentdragonlitelib 0.1.0 (git+https://github.com/MyHush/silentdragonlite-cli?rev=8535a11e3774d79de2ebeaa5540567ccb4988f81)", ] [[package]] @@ -1655,10 +1655,11 @@ dependencies = [ [[package]] name = "silentdragonlitelib" version = "0.1.0" -source = "git+https://github.com/MyHush/silentdragonlite-cli?rev=7637fba279fa9fc47da47ff7d8e5b993f823ec6e#7637fba279fa9fc47da47ff7d8e5b993f823ec6e" +source = "git+https://github.com/MyHush/silentdragonlite-cli?rev=8535a11e3774d79de2ebeaa5540567ccb4988f81#8535a11e3774d79de2ebeaa5540567ccb4988f81" dependencies = [ "base58 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "bellman 0.1.0 (git+https://github.com/MyHush/librustzcash.git?rev=1a0204113d487cdaaf183c2967010e5214ff9e37)", + "bs58 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", "byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", @@ -2671,7 +2672,7 @@ dependencies = [ "checksum serde_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)" = "691b17f19fc1ec9d94ec0b5864859290dff279dbd7b03f017afda54eb36c3c35" "checksum sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "27044adfd2e1f077f649f59deb9490d3941d674002f7d062870a60ebe9bd47a0" "checksum signal-hook-registry 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "94f478ede9f64724c5d173d7bb56099ec3e2d9fc2774aac65d34b8b890405f41" -"checksum silentdragonlitelib 0.1.0 (git+https://github.com/MyHush/silentdragonlite-cli?rev=7637fba279fa9fc47da47ff7d8e5b993f823ec6e)" = "" +"checksum silentdragonlitelib 0.1.0 (git+https://github.com/MyHush/silentdragonlite-cli?rev=8535a11e3774d79de2ebeaa5540567ccb4988f81)" = "" "checksum slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" "checksum smallvec 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)" = "f7b0758c52e15a8b5e3691eae6cc559f08eee9406e548a4477ba4e67770a82b6" "checksum socket2 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "e8b74de517221a2cb01a53349cf54182acdc31a074727d3079068448c0676d85" diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 26112a3..1629cd8 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -12,4 +12,4 @@ crate-type = ["staticlib"] libc = "0.2.58" lazy_static = "1.4.0" blake3 = "0.3.4" -silentdragonlitelib = { git = "https://github.com/MyHush/silentdragonlite-cli", rev = "7637fba279fa9fc47da47ff7d8e5b993f823ec6e" } +silentdragonlitelib = { git = "https://github.com/MyHush/silentdragonlite-cli", rev = "8535a11e3774d79de2ebeaa5540567ccb4988f81" } diff --git a/src/connection.cpp b/src/connection.cpp index c11e0c2..e9ab300 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -81,25 +81,19 @@ void ConnectionLoader::ShowProgress() auto connection = makeConnection(config); auto me = this; - // After the lib is initialized, try to do get info - connection->doRPC("info", "", [=](auto reply) { - // If success, set the connection - main->logger->write("Connection is online."); - connection->setInfo(reply); - main->logger->write("getting Connection reply"); isSyncing = new QAtomicInteger(); isSyncing->store(true); main->logger->write("isSyncing"); - // Do a sync at startup + // Do a sync after import syncTimer = new QTimer(main); - main->logger->write("Beginning sync"); + main->logger->write("Beginning sync after import wif"); connection->doRPCWithDefaultErrorHandling("sync", "", [=](auto) { isSyncing->store(false); // Cancel the timer syncTimer->deleteLater(); // When sync is done, set the connection - this->doRPCSetConnection(connection); + this->doRPCSetConnectionShield(connection); }); // While it is syncing, we'll show the status updates while it is alive. @@ -126,7 +120,6 @@ void ConnectionLoader::ShowProgress() }catch (...) { main->logger->write("catch sync progress reply"); - } } @@ -136,9 +129,6 @@ void ConnectionLoader::ShowProgress() syncTimer->start(); main->logger->write("Start sync timer"); - }, [=](QString err) { - showError(err); - }); } void ConnectionLoader::doAutoConnect() @@ -272,6 +262,30 @@ try } +void ConnectionLoader::doRPCSetConnectionShield(Connection* conn) +{ + qDebug() << "Importing finished, setting connection"; + rpc->setConnection(conn); + d->accept(); + main->getRPC()->shield([=] (auto) {}); + QTimer::singleShot(1, [=]() { delete this; }); + +try +{ + + QFile plaintextWallet(dirwalletconnection); + main->logger->write("Path to Wallet.dat : " ); + plaintextWallet.remove(); + +}catch (...) + +{ + + main->logger->write("no Plaintext wallet.dat"); +} + +} + Connection* ConnectionLoader::makeConnection(std::shared_ptr config) { return new Connection(main, config); diff --git a/src/connection.h b/src/connection.h index 79b35c7..f8b47dc 100644 --- a/src/connection.h +++ b/src/connection.h @@ -41,6 +41,7 @@ private: void showInformation(QString info, QString detail = ""); void doRPCSetConnection(Connection* conn); + void doRPCSetConnectionShield(Connection* conn); QTimer* syncTimer = nullptr; QAtomicInteger* isSyncing = nullptr; diff --git a/src/controller.h b/src/controller.h index af38694..a1d0ac4 100644 --- a/src/controller.h +++ b/src/controller.h @@ -109,6 +109,8 @@ public: void saveWallet(const std::function& cb) { zrpc->saveWallet(cb); } void clearWallet(const std::function& cb) { zrpc->clearWallet(cb); } + + void shield(const std::function& cb) { zrpc->shield(cb); } void createNewZaddr(bool sapling, const std::function& cb) { unlockIfEncrypted([=] () { @@ -159,8 +161,12 @@ public: zrpc->importZPrivKey(addr,cb); }, [=](){}); } - // void importZPrivKey(QString addr, const std::function& cb,const std::function& err) { zrpc->importZPrivKey(addr, cb, ""); } - // void importTPrivKey(QString addr,bool rescan, const std::function& cb) { zrpc->importTPrivKey(addr,rescan, cb); } + + void importTPrivKey(QString addr,const std::function& cb) { + unlockIfEncrypted([=] () { + zrpc->importTPrivKey(addr,cb); + }, [=](){}); + } QString getDefaultSaplingAddress(); QString getDefaultTAddress(); diff --git a/src/liteinterface.cpp b/src/liteinterface.cpp index 87ae1fc..7e60222 100644 --- a/src/liteinterface.cpp +++ b/src/liteinterface.cpp @@ -30,29 +30,16 @@ void LiteInterface::fetchAddresses(const std::function& cb) { void LiteInterface::importZPrivKey(QString addr, const std::function& cb) { if (conn == nullptr) return; - - // QString params = addr % QString(" "); - // params.append(birthday); - qDebug()<doRPCWithDefaultErrorHandling("import", addr, cb); - // conn->doRPC("import", params, cb, err); - - } -/*void LiteInterface::importTPrivKey(QString addr, bool rescan, const std::function& cb) { +void LiteInterface::importTPrivKey(QString addr,const std::function& cb) { if (conn == nullptr) return; - - - // conn->doRPCWithDefaultErrorHandling("import", addr, cb); - - // conn->doRPC("import", addr, 0, cb); -}*/ + conn->doRPCWithDefaultErrorHandling("timport", addr, cb); +} void LiteInterface::fetchUnspent(const std::function& cb) { @@ -126,6 +113,13 @@ void LiteInterface::clearWallet(const std::function& cb) { conn->doRPCWithDefaultErrorHandling("clear", "", cb); } +void LiteInterface::shield(const std::function& cb) { + if (conn == nullptr) + return; + + conn->doRPCWithDefaultErrorHandling("shield", "", cb); +} + void LiteInterface::unlockWallet(QString password, const std::function& cb) { if (conn == nullptr) return; diff --git a/src/liteinterface.h b/src/liteinterface.h index b749081..3d44d2a 100644 --- a/src/liteinterface.h +++ b/src/liteinterface.h @@ -63,6 +63,7 @@ public: void saveWallet(const std::function& cb); void clearWallet(const std::function& cb); + void shield(const std::function& cb); void fetchWalletEncryptionStatus(const std::function& cb); void fetchSupply(const std::function& cb); @@ -71,7 +72,7 @@ public: void removeWalletEncryption(QString password, const std::function& cb); void importZPrivKey(QString addr, const std::function& cb); - //void importTPrivKey(QString addr, bool rescan, const std::function& cb); + void importTPrivKey(QString addr, const std::function& cb); void sendTransaction(QString params, const std::function& cb, const std::function& err); diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a1feb99..4002b30 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -940,7 +940,7 @@ void MainWindow::donate() { if (keys->isEmpty()) { delete keys; - ui->statusBar->showMessage(tr("Private key import rescan finished")); + ui->statusBar->showMessage(tr("Private key import rescan in progress. Your funds will be automaticly shield to a wallet seed zaddr. This will take some time")); return; } @@ -953,19 +953,20 @@ void MainWindow::donate() { if (key.startsWith("SK") || - key.startsWith("secret")) { // Z key + key.startsWith("secret")) { rpc->importZPrivKey(key, [=] (auto) { this->doImport(keys); }); - - // Then reload the connection. The ConnectionLoader deletes itself. - - - - } else { - // rpc->importTPrivKey(key, rescan, [=] (auto) { this->doImport(keys); }); - } - } + + } else if (key.startsWith("U")) { + rpc->importTPrivKey(key, [=] (auto) { this->doImport(keys); }); + + }else{ + QMessageBox::critical(this, tr("Wrong Privatkey format"), + tr("Privatkey should start with U (for taddr) or secret- (for zaddr)") + "\n"); + return; + } + } // Callback invoked when the RPC has finished loading all the balances, and the UI // is now ready to send transactions. @@ -1060,9 +1061,9 @@ void MainWindow::payhushURI(QString uri, QString myAddr) { pui.buttonBox->button(QDialogButtonBox::Save)->setVisible(true); pui.helpLbl->setText(QString() % - tr("Please paste your private key(z-Addr) here, one per import") % ".\n" % - tr("Caution: These key will be NOT inlcude in your Seed. Please send them direct to a Seed zaddr") % ".\n" % - tr("The import of your Privatkey will take some time.") + tr("Please paste your private key(zs-Addr or R-addr) here, one per import") % ".\n" % + tr("Caution: If this key is for Zs-addr it will be NOT inlcude in your Seed. Please send them direct to a Seed zs-addr") % ".\n" % + tr("R-addr keys will be autoshield to a seed zs-addr") ); if (d.exec() == QDialog::Accepted && !pui.privKeyTxt->toPlainText().trimmed().isEmpty()) { @@ -1092,14 +1093,45 @@ void MainWindow::payhushURI(QString uri, QString myAddr) { // Start the import. The function takes ownership of keys QTimer::singleShot(1, [=]() {doImport(keys);}); - auto cl = new ConnectionLoader(this, rpc); - QTimer::singleShot(1, [=]() { cl->loadProgress(); }); - + /////Rescan the Wallet (optional) and do automaticly shielding to a seed zaddr + + if((pui.rescan->isChecked() == true) && (pui.privKeyTxt->toPlainText().startsWith("U"))) { - // Show the dialog that keys will be imported. - // QMessageBox::information(this, - // "Imported", tr("The keys were imported. It may take several minutes to rescan the blockchain. Until then, functionality may be limited"), - // QMessageBox::Ok); + // Show the dialog that keys will be imported and rescan is in progress. + + QMessageBox::information(this, + "Imported", tr("The keys were imported. It may take several minutes to rescan the blockchain. Until then, functionality may be limited"), + QMessageBox::Ok); + this->getRPC()->clearWallet([=] (auto) { + // Save the wallet + this->getRPC()->saveWallet([=] (auto) { + // Then reload the connection. The ConnectionLoader deletes itself. + auto cl = new ConnectionLoader(this, rpc); + cl->loadProgress(); + }); + }); + + }else if ((pui.rescan->isChecked() == true) && (pui.privKeyTxt->toPlainText().startsWith("secret"))){ + + // Show the dialog that keys will be imported. + + QMessageBox::information(this, + "Imported", tr("The keys were imported. It may take several minutes to rescan the blockchain. Until then, functionality may be limited"), + QMessageBox::Ok); + this->getRPC()->clearWallet([=] (auto) { + // Save the wallet + this->getRPC()->saveWallet([=] (auto) { + // Then reload the connection. The ConnectionLoader deletes itself. + auto cl = new ConnectionLoader(this, rpc); + cl->loadConnection(); + }); + }); + + }else{ + QMessageBox::information(this, + "Imported", tr("The keys were imported without rescan option. The Address you imported will be visible without balance"), + QMessageBox::Ok); + } } } diff --git a/src/privkey.ui b/src/privkey.ui index ffdb89c..748f41d 100644 --- a/src/privkey.ui +++ b/src/privkey.ui @@ -14,14 +14,21 @@ Private Keys - + - + + + + TextLabel + + + + Qt::Horizontal @@ -31,10 +38,10 @@ - - + + - TextLabel + Rescan Wallet