Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
adityapk00 committed Nov 19, 2019
2 parents 440704f + 363d02c commit 31d80fd
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 125 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Zecwallet-Lite is z-Addr first, Sapling compatible wallet lightwallet for Zcash. It has full support for all Zcash features:
## ZecWallet Lite
Zecwallet-Lite is z-Addr first, Sapling compatible lightwallet client for Zcash. It has full support for all Zcash features:
- Send + Receive fully shielded transactions
- Supports transparent addresses and transactions
- Full support for incoming and outgoing memos
Expand Down
6 changes: 3 additions & 3 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ crate-type = ["staticlib"]
[dependencies]
libc = "0.2.58"
lazy_static = "1.4.0"
zecwalletlitelib = { git = "https://github.com/adityapk00/zecwallet-light-cli", rev = "af0e0b9b2bcc131804d5e940f94d45c6fef74481" }
zecwalletlitelib = { git = "https://github.com/adityapk00/zecwallet-light-cli", rev = "4f946da7745ff6372c2475eeb2bbcd06a729eef1" }
9 changes: 9 additions & 0 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub extern fn litelib_initialize_new(dangerous: bool, server: *const c_char) ->
}
};

// Initialize logging
let _ = lightclient.init_logging();

let seed = match lightclient.do_seed_phrase() {
Ok(s) => s.dump(),
Err(e) => {
Expand Down Expand Up @@ -105,6 +108,9 @@ pub extern fn litelib_initialize_new_from_phrase(dangerous: bool, server: *const
}
};

// Initialize logging
let _ = lightclient.init_logging();

LIGHTCLIENT.lock().unwrap().replace(Some(Arc::new(lightclient)));

let c_str = CString::new("OK").unwrap();
Expand Down Expand Up @@ -137,6 +143,9 @@ pub extern fn litelib_initialize_existing(dangerous: bool, server: *const c_char
}
};

// Initialize logging
let _ = lightclient.init_logging();

LIGHTCLIENT.lock().unwrap().replace(Some(Arc::new(lightclient)));

let c_str = CString::new("OK").unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/about.ui
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string notr="true">Zecwallet Lightclient</string>
<string notr="true">Zecwallet Lite</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
Expand Down
8 changes: 4 additions & 4 deletions src/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ void ConnectionLoader::doAutoConnect() {
qDebug() << "Doing autoconnect";

auto config = std::shared_ptr<ConnectionConfig>(new ConnectionConfig());
config->dangerous = true;
config->dangerous = false;
config->server = Settings::getInstance()->getSettings().server;

// Initialize the library
main->logger->write(QObject::tr("Attempting to initialize library with ") + config->server);

// Check to see if there's an existing wallet
if (litelib_wallet_exists(Settings::getChainName().toStdString().c_str())) {
if (litelib_wallet_exists(Settings::getDefaultChainName().toStdString().c_str())) {
main->logger->write(QObject::tr("Using existing wallet."));
char* resp = litelib_initialize_existing(config->dangerous, config->server.toStdString().c_str());
QString response = litelib_process_response(resp);
Expand All @@ -68,9 +68,10 @@ void ConnectionLoader::doAutoConnect() {
auto me = this;

// After the lib is initialized, try to do get info
connection->doRPC("info", "", [=](auto) {
connection->doRPC("info", "", [=](auto reply) {
// If success, set the connection
main->logger->write("Connection is online.");
connection->setInfo(reply);

isSyncing = new QAtomicInteger<bool>();
isSyncing->store(true);
Expand Down Expand Up @@ -138,7 +139,6 @@ Connection* ConnectionLoader::makeConnection(std::shared_ptr<ConnectionConfig> c

// Update the UI with the status
void ConnectionLoader::showInformation(QString info, QString detail) {
qDebug() << "Showing info " << info << ":" << detail;
connD->status->setText(info);
connD->statusDetail->setText(detail);
}
Expand Down
6 changes: 5 additions & 1 deletion src/connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,12 @@ Q_OBJECT

void showTxError(const QString& error);

json getInfo() { return serverInfo; }
void setInfo(const json& info) { serverInfo = info; }

private:
bool shutdownInProgress = false;
bool shutdownInProgress = false;
json serverInfo;
};

#endif
46 changes: 27 additions & 19 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ void Controller::setConnection(Connection* c) {

ui->statusBar->showMessage("Ready!");

processInfo(c->getInfo());

// If we're allowed to get the Zec Price, get the prices
if (Settings::getInstance()->getAllowFetchPrices())
refreshZECPrice();
Expand Down Expand Up @@ -137,42 +139,48 @@ void Controller::refresh(bool force) {
getInfoThenRefresh(force);
}

void Controller::processInfo(const json& info) {
// Testnet?
QString chainName;
if (!info["chain_name"].is_null()) {
chainName = QString::fromStdString(info["chain_name"].get<json::string_t>());
Settings::getInstance()->setTestnet(chainName == "test");
};


QString version = QString::fromStdString(info["version"].get<json::string_t>());
Settings::getInstance()->setZcashdVersion(version);

// Recurring pamynets are testnet only
if (!Settings::getInstance()->isTestnet())
main->disableRecurring();
}

void Controller::getInfoThenRefresh(bool force) {
if (!zrpc->haveConnection())
return noConnection();

static bool prevCallSucceeded = false;

zrpc->fetchInfo([=] (const json& reply) {
prevCallSucceeded = true;

// Testnet?
QString chainName;
if (!reply["chain_name"].is_null()) {
chainName = QString::fromStdString(reply["chain_name"].get<json::string_t>());
Settings::getInstance()->setTestnet(chainName == "test");
};

// Recurring pamynets are testnet only
if (!Settings::getInstance()->isTestnet())
main->disableRecurring();
zrpc->fetchLatestBlock([=] (const json& reply) {
prevCallSucceeded = true;

int curBlock = reply["latest_block_height"].get<json::number_integer_t>();
int curBlock = reply["height"].get<json::number_integer_t>();
bool doUpdate = force || (model->getLatestBlock() != curBlock);
model->setLatestBlock(curBlock);

main->logger->write(QString("Refresh. curblock ") % QString::number(curBlock) % ", update=" % (doUpdate ? "true" : "false") );

// Connected, so display checkmark.
auto tooltip = Settings::getInstance()->getSettings().server + "\n" + QString::fromStdString(reply.dump());
auto tooltip = Settings::getInstance()->getSettings().server + "\n" +
QString::fromStdString(zrpc->getConnection()->getInfo().dump());
QIcon i(":/icons/res/connected.gif");
QString chainName = Settings::getInstance()->isTestnet() ? "test" : "main";
main->statusLabel->setText(chainName + "(" + QString::number(curBlock) + ")");
main->statusLabel->setToolTip(tooltip);
main->statusIcon->setPixmap(i.pixmap(16, 16));
main->statusIcon->setToolTip(tooltip);

//int version = reply["version"].get<json::string_t>();
int version = 1;
Settings::getInstance()->setZcashdVersion(version);

// See if recurring payments needs anything
Recurring::getInstance()->processPending(main);

Expand Down
3 changes: 3 additions & 0 deletions src/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ class Controller

void saveWallet(const std::function<void(json)>& cb) { zrpc->saveWallet(cb); }

void clearWallet(const std::function<void(json)>& cb) { zrpc->clearWallet(cb); }

void createNewZaddr(bool sapling, const std::function<void(json)>& cb) {
unlockIfEncrypted([=] () {
zrpc->createNewZaddr(sapling, cb);
Expand Down Expand Up @@ -107,6 +109,7 @@ class Controller
QString getDefaultTAddress();

private:
void processInfo(const json&);
void refreshBalances();

void refreshTransactions();
Expand Down
16 changes: 16 additions & 0 deletions src/liteinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ void LiteInterface::saveWallet(const std::function<void(json)>& cb) {
conn->doRPCWithDefaultErrorHandling("save", "", cb);
}

void LiteInterface::clearWallet(const std::function<void(json)>& cb) {
if (conn == nullptr)
return;

conn->doRPCWithDefaultErrorHandling("clear", "", cb);
}

void LiteInterface::unlockWallet(QString password, const std::function<void(json)>& cb) {
if (conn == nullptr)
return;
Expand Down Expand Up @@ -130,6 +137,15 @@ void LiteInterface::fetchInfo(const std::function<void(json)>& cb,
conn->doRPC("info", "", cb, err);
}


void LiteInterface::fetchLatestBlock(const std::function<void(json)>& cb,
const std::function<void(QString)>& err) {
if (conn == nullptr)
return;

conn->doRPC("height", "", cb, err);
}

/**
* Method to get all the private keys for both z and t addresses. It will make 2 batch calls,
* combine the result, and call the callback with a single list containing both the t-addr and z-addr
Expand Down
4 changes: 4 additions & 0 deletions src/liteinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class LiteInterface {

void fetchInfo(const std::function<void(json)>& cb,
const std::function<void(QString)>& err);

void fetchLatestBlock(const std::function<void(json)>& cb,
const std::function<void(QString)>& err);

void fetchBalance(const std::function<void(json)>& cb);

Expand All @@ -55,6 +58,7 @@ class LiteInterface {
void fetchSeed(const std::function<void(json)>&);

void saveWallet(const std::function<void(json)>& cb);
void clearWallet(const std::function<void(json)>& cb);

void fetchWalletEncryptionStatus(const std::function<void(json)>& cb);
void encryptWallet(QString password, const std::function<void(json)>& cb);
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ class Application : public SignalHandler


w = new MainWindow();
w->setWindowTitle("Zecwallet Lightclient v" + QString(APP_VERSION));
w->setWindowTitle("Zecwallet Lite v" + QString(APP_VERSION));

// If there was a payment URI on the command line, pay it
if (parser.positionalArguments().length() > 0) {
Expand Down
49 changes: 35 additions & 14 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "connection.h"
#include "requestdialog.h"
#include "websockets.h"
#include <QRegularExpression>

using json = nlohmann::json;

Expand Down Expand Up @@ -104,6 +105,20 @@ MainWindow::MainWindow(QWidget *parent) :
AppDataServer::getInstance()->connectAppDialog(this);
});

// Rescan
QObject::connect(ui->actionRescan, &QAction::triggered, [=]() {
// To rescan, we clear the wallet state, and then reload the connection
// This will start a sync, and show the scanning status.
getRPC()->clearWallet([=] (auto) {
// Save the wallet
getRPC()->saveWallet([=] (auto) {
// Then reload the connection. The ConnectionLoader deletes itself.
auto cl = new ConnectionLoader(this, rpc);
cl->loadConnection();
});
});
});

// Address Book
QObject::connect(ui->action_Address_Book, &QAction::triggered, this, &MainWindow::addressBook);

Expand Down Expand Up @@ -407,19 +422,17 @@ void MainWindow::setupSettingsModal() {
// Fetch prices
settings.chkFetchPrices->setChecked(Settings::getInstance()->getAllowFetchPrices());

// List of default servers
settings.cmbServer->addItem("https://lightd-main.zecwallet.co:443");
settings.cmbServer->addItem("https://lightd-main.zcashfr.io:443");

// Load current values into the dialog
auto conf = Settings::getInstance()->getSettings();
settings.txtServer->setText(conf.server);
settings.cmbServer->setCurrentText(conf.server);

// Connection tab by default
settings.tabWidget->setCurrentIndex(0);

// Enable the troubleshooting options only if using embedded zcashd
if (!rpc->isEmbedded()) {
settings.chkRescan->setEnabled(false);
settings.chkRescan->setToolTip(tr("You're using an external zcashd. Please restart zcashd with -rescan"));
}

if (settingsDialog.exec() == QDialog::Accepted) {
// Check for updates
Settings::getInstance()->setCheckForUpdates(settings.chkCheckUpdates->isChecked());
Expand All @@ -428,22 +441,30 @@ void MainWindow::setupSettingsModal() {
Settings::getInstance()->setAllowFetchPrices(settings.chkFetchPrices->isChecked());

// Save the server
Settings::getInstance()->saveSettings(settings.txtServer->text().trimmed());
bool reloadConnection = false;
if (conf.server != settings.cmbServer->currentText().trimmed()) {
reloadConnection = true;
}
Settings::getInstance()->saveSettings(settings.cmbServer->currentText().trimmed());

if (false /* connection needs reloading?*/) {
if (reloadConnection) {
// Save settings
Settings::getInstance()->saveSettings(settings.txtServer->text());

auto cl = new ConnectionLoader(this, rpc);
cl->loadConnection();
Settings::getInstance()->saveSettings(settings.cmbServer->currentText());

// Save the wallet
getRPC()->saveWallet([=] (auto) {
// Then reload the connection. The ConnectionLoader deletes itself.
auto cl = new ConnectionLoader(this, rpc);
cl->loadConnection();
});
}
}
});
}

void MainWindow::addressBook() {
// Check to see if there is a target.
QRegExp re("Address[0-9]+", Qt::CaseInsensitive);
QRegularExpression re("Address[0-9]+", QRegularExpression::CaseInsensitiveOption);
for (auto target: ui->sendToWidgets->findChildren<QLineEdit *>(re)) {
if (target->hasFocus()) {
AddressBook::open(this, target);
Expand Down
Loading

0 comments on commit 31d80fd

Please sign in to comment.