From a135753cef63c1192170f8dd9805f418568953a9 Mon Sep 17 00:00:00 2001 From: Duke Leto Date: Sun, 30 May 2021 15:14:05 -0400 Subject: [PATCH] logging and error handling --- src/Chat/Chat.cpp | 35 ++++++++++++++------------------- src/DataStore/ChatDataStore.cpp | 9 +-------- src/connection.cpp | 2 ++ src/controller.cpp | 13 +++++++----- src/mainwindow.cpp | 27 +++++++++++-------------- 5 files changed, 38 insertions(+), 48 deletions(-) diff --git a/src/Chat/Chat.cpp b/src/Chat/Chat.cpp index 7cc06c9..0f6fb44 100644 --- a/src/Chat/Chat.cpp +++ b/src/Chat/Chat.cpp @@ -93,21 +93,17 @@ void Chat::renderChatBox(Ui::MainWindow *ui, QListView *view, QLabel *label) DataStore::getChatDataStore()->dump(); // test to see if the chat items in datastore are correctly dumped to json qDebug() << __func__ << ": looking at memos..."; - for (auto &p : AddressBook::getInstance()->getAllAddressLabels()) + for (auto &contact : AddressBook::getInstance()->getAllAddressLabels()) { - for (auto &c : DataStore::getChatDataStore()->getAllMemos()) - { + for (auto &memo : DataStore::getChatDataStore()->getAllMemos()) { + if ( (contact.getName() == ui->contactNameMemo->text().trimmed()) && + (contact.getPartnerAddress() == memo.second.getAddress()) && + (memo.second.isOutgoing() == true)) { - if ( - (p.getName() == ui->contactNameMemo->text().trimmed()) && - (p.getPartnerAddress() == c.second.getAddress()) && - (c.second.isOutgoing() == true)) - { - - QStandardItem *Items = new QStandardItem(c.second.toChatLine()); + QStandardItem *Items = new QStandardItem(memo.second.toChatLine()); Items->setData(OUTGOING, Qt::UserRole + 1); - qDebug() << __func__ << ": appending row to OUTGOING chatitems to contact " << p.getName() << " with item " << Items; + qDebug() << __func__ << ": appending row to OUTGOING chatitems to contact " << contact.getName() << " with item " << Items; chat->appendRow(Items); ui->listChat->setModel(chat); @@ -115,16 +111,15 @@ void Chat::renderChatBox(Ui::MainWindow *ui, QListView *view, QLabel *label) ui->listChat->setModel(chat); } - if ( - (p.getName() == ui->contactNameMemo->text().trimmed()) && - (p.getMyAddress() == c.second.getAddress()) && - (c.second.isOutgoing() == false) && - (c.second.getCid() == p.getCid()) - ) - { - QStandardItem *Items1 = new QStandardItem(c.second.toChatLine()); + qDebug() << __func__ << ": memo.first=" << memo.first; + if ( (contact.getName() == ui->contactNameMemo->text().trimmed()) && + (contact.getMyAddress() == memo.second.getAddress()) && + (memo.second.isOutgoing() == false) && + (memo.second.getCid() == contact.getCid()) + ) { + QStandardItem *Items1 = new QStandardItem(memo.second.toChatLine()); Items1->setData(INCOMING, Qt::UserRole + 1); - qDebug() << __func__ << ": appending row to INCOMING chatitems to contact " << p.getName() << "with cid=" << p.getCid() << " and item " << Items1; + qDebug() << __func__ << ": appending row to INCOMING chatitems to contact " << contact.getName() << "with txid=" << memo.second.getTxid() << " cid=" << contact.getCid() << " item " << Items1 << " memo=" << memo.second.getMemo(); chat->appendRow(Items1); ui->listChat->setModel(chat); ui->memoTxtChat->setEnabled(true); diff --git a/src/DataStore/ChatDataStore.cpp b/src/DataStore/ChatDataStore.cpp index dbea024..0230012 100644 --- a/src/DataStore/ChatDataStore.cpp +++ b/src/DataStore/ChatDataStore.cpp @@ -160,14 +160,7 @@ std::map ChatDataStore::getAllMemos() std::map filteredItems; for(auto &c: this->data) { - if ( - - (c.second.getMemo().startsWith("{") == false) && - (c.second.getMemo().isEmpty() == false) - - - ) - { + if ((c.second.getMemo().startsWith("{") == false) && (c.second.getMemo().isEmpty() == false) ) { filteredItems[c.first] = c.second; } } diff --git a/src/connection.cpp b/src/connection.cpp index c244ee8..ad6dcd9 100644 --- a/src/connection.cpp +++ b/src/connection.cpp @@ -267,6 +267,7 @@ void ConnectionLoader::doRPCSetConnection(Connection* conn) try { QFile plaintextWallet(dirwalletconnection); main->logger->write("Path to Wallet.dat : " ); + qDebug() << __func__ << ": wallet path =" << plaintextWallet; plaintextWallet.remove(); } catch (...) { @@ -286,6 +287,7 @@ void ConnectionLoader::doRPCSetConnectionShield(Connection* conn) try { QFile plaintextWallet(dirwalletconnection); main->logger->write("Path to Wallet.dat : " ); + qDebug() << __func__ << ": wallet path =" << plaintextWallet; plaintextWallet.remove(); } catch (...) { main->logger->write("no Plaintext wallet.dat"); diff --git a/src/controller.cpp b/src/controller.cpp index 2ff4dd3..4d9eefd 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -1236,13 +1236,13 @@ void Controller::refreshTransactions() { } int position = it["position"].get(); - int ciphercheck = memo.length() - crypto_secretstream_xchacha20poly1305_ABYTES; + qDebug() << __func__ << ": position=" << position << " headerbytes=" << headerbytes + << " ciphercheck=" << ciphercheck << " for memo=" << memo; if ((memo.startsWith("{") == false) && (headerbytes > 0) && (ciphercheck > 0)) { - if (chatModel->getMemoByTx(txid) == QString("0xdeadbeef")) - { + if (chatModel->getMemoByTx(txid) == QString("0xdeadbeef")) { if (position == 1) { chatModel->addMemo(txid, headerbytes); @@ -1352,10 +1352,13 @@ void Controller::refreshTransactions() { DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); } else { - // + qDebug() << __func__ << ": ignoring txid="<< txid; } + //} else if (memo.startsWith("{")) { + //qDebug() << __func__ << ": ignoring a header memo"; } else { + // Add a chatitem for the initial CR ChatItem item = ChatItem( datetime, address, @@ -1371,7 +1374,7 @@ void Controller::refreshTransactions() { isContact ); - qDebug() << "refreshTransactions: adding chatItem with memo=" << memo; + qDebug() << "refreshTransactions: adding chatItem for initial CR with memo=" << memo; DataStore::getChatDataStore()->setData(ChatIDGenerator::getInstance()->generateID(item), item); } } diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index a34c4c7..1ffadb7 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -86,15 +86,17 @@ MainWindow::MainWindow(QWidget *parent) : }else{} logger = new Logger(this, QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)).filePath("silentdragonlite-wallet.log")); + // Check for encryption - if(fileExists(dirwalletenc)) { + qDebug() << __func__ << ": decrypting wallet=" << dirwalletenc; this->removeWalletEncryptionStartUp(); } ui->memoTxtChat->setAutoFillBackground(false); - ui->memoTxtChat->setPlaceholderText("Send Message (you can only write messages after the initial message from your contact)"); + // TODO: make this HTML with some emoji + ui->memoTxtChat->setPlaceholderText("Send Memo (you can only write memo after the initial message from your contact)"); ui->memoTxtChat->setTextColor(Qt::white); // Status Bar @@ -543,10 +545,8 @@ void MainWindow::removeWalletEncryption() { QObject::connect(ed.txtConfirmPassword, &QLineEdit::textChanged, fnPasswordEdited); QObject::connect(ed.txtPassword, &QLineEdit::textChanged, fnPasswordEdited); - if (d.exec() == QDialog::Accepted) - { + if (d.exec() == QDialog::Accepted) { QString passphraseBlank = ed.txtPassword->text(); // data comes from user inputs - QString passphrase = QString("HUSH3") + passphraseBlank + QString("SDL"); int length = passphrase.length(); @@ -563,18 +563,17 @@ void MainWindow::removeWalletEncryption() { #define hash ((const unsigned char *) sequence1) - #define PASSWORD sequence #define KEY_LEN crypto_box_SEEDBYTES unsigned char key[KEY_LEN]; - if (crypto_pwhash - (key, sizeof key, PASSWORD, strlen(PASSWORD), hash, - crypto_pwhash_OPSLIMIT_SENSITIVE, crypto_pwhash_MEMLIMIT_SENSITIVE, - crypto_pwhash_ALG_DEFAULT) != 0) { + if (crypto_pwhash(key, sizeof key, PASSWORD, strlen(PASSWORD), hash, + crypto_pwhash_OPSLIMIT_SENSITIVE, crypto_pwhash_MEMLIMIT_SENSITIVE, crypto_pwhash_ALG_DEFAULT) != 0) { /* out of memory */ -} + qDebug() << "crypto_pwhash failed!"; + return; + } auto dir = QDir(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation)); auto dirHome = QDir(QStandardPaths::writableLocation(QStandardPaths::HomeLocation)); @@ -587,9 +586,7 @@ void MainWindow::removeWalletEncryption() { QFile filencrypted(dirwalletenc); QFile wallet(dirwallet); - if (wallet.size() > 0) - { - + if (wallet.size() > 0) { QMessageBox::information(this, tr("Wallet decryption Success"), QString("Successfully delete the encryption"), QMessageBox::Ok @@ -597,7 +594,7 @@ void MainWindow::removeWalletEncryption() { filencrypted.remove(); - }else{ + } else { QMessageBox::critical(this, tr("Wallet Encryption Failed"), QString("False password, please try again"),