From 187ed71a8ae1fc480bbbba7609619b052b14dc8b Mon Sep 17 00:00:00 2001 From: David Kelly Date: Tue, 12 Sep 2017 10:55:27 -0700 Subject: [PATCH] more cleanup, plus now keyfile associated with user directly --- interface/src/commerce/Wallet.cpp | 29 ++++++++++++++++++--------- interface/src/commerce/Wallet.h | 2 +- libraries/ui/src/ui/ImageProvider.cpp | 6 +++++- 3 files changed, 26 insertions(+), 11 deletions(-) diff --git a/interface/src/commerce/Wallet.cpp b/interface/src/commerce/Wallet.cpp index 227d2de96b..9b850cdb49 100644 --- a/interface/src/commerce/Wallet.cpp +++ b/interface/src/commerce/Wallet.cpp @@ -18,6 +18,7 @@ #include #include +#include #include #include @@ -54,7 +55,8 @@ void initialize() { } QString keyFilePath() { - return PathUtils::getAppDataFilePath(KEY_FILE); + auto accountManager = DependencyManager::get(); + return PathUtils::getAppDataFilePath(QString("%1.%2").arg(accountManager->getAccountInfo().getUsername(), KEY_FILE)); } // use the cached _passphrase if it exists, otherwise we need to prompt @@ -262,6 +264,15 @@ RSA* readPrivateKey(const char* filename) { return key; } +// QT's QByteArray will convert to Base64 without any embedded newlines. This just +// writes it with embedded newlines, which is more readable. +void outputBase64WithNewlines(QFile& file, const QByteArray& b64Array) { + for (int i = 0; i < b64Array.size(); i += 64) { + file.write(b64Array.mid(i, 64)); + file.write("\n"); + } +} + void initializeAESKeys(unsigned char* ivec, unsigned char* ckey, const QByteArray& salt) { // use the ones in the wallet auto wallet = DependencyManager::get(); @@ -331,11 +342,11 @@ bool Wallet::writeSecurityImage(const QPixmap* pixmap, const QString& outputFile QByteArray output((const char*)outputFileBuffer, outSize); // now APPEND to the file, + QByteArray b64output = output.toBase64(); QFile outputFile(outputFilePath); outputFile.open(QIODevice::Append); outputFile.write(IMAGE_HEADER); - outputFile.write(output.toBase64()); - outputFile.write("\n"); + outputBase64WithNewlines(outputFile, b64output); outputFile.write(IMAGE_FOOTER); outputFile.close(); @@ -551,9 +562,11 @@ void Wallet::getSecurityImage() { return; } - // decrypt and return + bool success = false; + // decrypt and return. Don't bother if we have no file to decrypt, or + // no salt set yet. QFileInfo fileInfo(keyFilePath()); - if (fileInfo.exists() && readSecurityImage(keyFilePath(), &data, &dataLen)) { + if (fileInfo.exists() && _salt.size() > 0 && readSecurityImage(keyFilePath(), &data, &dataLen)) { // create the pixmap _securityImage = new QPixmap(); _securityImage->loadFromData(data, dataLen, "jpg"); @@ -562,11 +575,9 @@ void Wallet::getSecurityImage() { updateImageProvider(); delete[] data; - emit securityImageResult(true); - } else { - qCDebug(commerce) << "failed to decrypt security image (maybe none saved yet?)"; - emit securityImageResult(false); + success = true; } + emit securityImageResult(success); } void Wallet::sendKeyFilePathIfExists() { QString filePath(keyFilePath()); diff --git a/interface/src/commerce/Wallet.h b/interface/src/commerce/Wallet.h index 013f038fb2..b8913e9a5e 100644 --- a/interface/src/commerce/Wallet.h +++ b/interface/src/commerce/Wallet.h @@ -55,7 +55,7 @@ signals: private: QStringList _publicKeys{}; QPixmap* _securityImage { nullptr }; - QByteArray _salt {"iamsalt!"}; + QByteArray _salt; QByteArray _iv; QByteArray _ckey; QString* _passphrase { new QString("") }; diff --git a/libraries/ui/src/ui/ImageProvider.cpp b/libraries/ui/src/ui/ImageProvider.cpp index c74ed0cb44..7bbad43f2e 100644 --- a/libraries/ui/src/ui/ImageProvider.cpp +++ b/libraries/ui/src/ui/ImageProvider.cpp @@ -51,5 +51,9 @@ QPixmap ImageProvider::requestPixmap(const QString& id, QSize* size, const QSize return _securityImage->copy(); } } - return QPixmap(); + // otherwise just return a grey pixmap. This avoids annoying error messages in qml we would get + // when sending a 'null' pixmap (QPixmap()) + QPixmap greyPixmap(200, 200); + greyPixmap.fill(QColor("darkGrey")); + return greyPixmap; }