From b8da08ca42657427d856483c8cca1c42bf877fee Mon Sep 17 00:00:00 2001 From: howard-stearns Date: Wed, 30 Aug 2017 11:29:43 -0700 Subject: [PATCH 1/2] reset wallet --- .../resources/qml/hifi/commerce/wallet/Help.qml | 13 +++++++++++++ interface/src/commerce/Ledger.cpp | 7 +++++++ interface/src/commerce/Ledger.h | 3 +++ interface/src/commerce/QmlCommerce.cpp | 7 +++++++ interface/src/commerce/QmlCommerce.h | 1 + interface/src/commerce/Wallet.cpp | 14 +++++++++++++- interface/src/commerce/Wallet.h | 2 ++ 7 files changed, 46 insertions(+), 1 deletion(-) diff --git a/interface/resources/qml/hifi/commerce/wallet/Help.qml b/interface/resources/qml/hifi/commerce/wallet/Help.qml index 2252cbfb59..589143ca84 100644 --- a/interface/resources/qml/hifi/commerce/wallet/Help.qml +++ b/interface/resources/qml/hifi/commerce/wallet/Help.qml @@ -31,6 +31,7 @@ Item { // "Unavailable" RalewayRegular { + id: helpText; text: "Help me!"; // Anchors anchors.fill: parent; @@ -43,6 +44,18 @@ Item { horizontalAlignment: Text.AlignHCenter; verticalAlignment: Text.AlignVCenter; } + HifiControlsUit.Button { + color: hifi.buttons.black; + colorScheme: hifi.colorSchemes.dark; + anchors.bottom: helpText.bottom; + anchors.horizontalCenter: parent.horizontalCenter; + height: 50; + width: 250; + text: "Testing: Reset Wallet!"; + onClicked: { + commerce.reset(); + } + } // // FUNCTION DEFINITIONS START diff --git a/interface/src/commerce/Ledger.cpp b/interface/src/commerce/Ledger.cpp index dddfae6455..08eb79017f 100644 --- a/interface/src/commerce/Ledger.cpp +++ b/interface/src/commerce/Ledger.cpp @@ -111,3 +111,10 @@ void Ledger::inventory(const QStringList& keys) { void Ledger::history(const QStringList& keys) { keysQuery("history", "historySuccess", "historyFailure"); } + +// The api/failResponse is called just for the side effect of logging. +void Ledger::resetSuccess(QNetworkReply& reply) { apiResponse("reset", reply); } +void Ledger::resetFailure(QNetworkReply& reply) { failResponse("reset", reply); } +void Ledger::reset() { + send("reset_user_hfc_account", "resetSuccess", "resetFailure", QNetworkAccessManager::PutOperation, QJsonObject()); +} \ No newline at end of file diff --git a/interface/src/commerce/Ledger.h b/interface/src/commerce/Ledger.h index 7d3fdef0c0..55b648aa4c 100644 --- a/interface/src/commerce/Ledger.h +++ b/interface/src/commerce/Ledger.h @@ -29,6 +29,7 @@ public: void balance(const QStringList& keys); void inventory(const QStringList& keys); void history(const QStringList& keys); + void reset(); signals: void buyResult(QJsonObject result); @@ -48,6 +49,8 @@ public slots: void inventoryFailure(QNetworkReply& reply); void historySuccess(QNetworkReply& reply); void historyFailure(QNetworkReply& reply); + void resetSuccess(QNetworkReply& reply); + void resetFailure(QNetworkReply& reply); private: QJsonObject apiResponse(const QString& label, QNetworkReply& reply); diff --git a/interface/src/commerce/QmlCommerce.cpp b/interface/src/commerce/QmlCommerce.cpp index bf6bcc221c..655f228672 100644 --- a/interface/src/commerce/QmlCommerce.cpp +++ b/interface/src/commerce/QmlCommerce.cpp @@ -85,3 +85,10 @@ void QmlCommerce::getKeyFilePathIfExists() { auto wallet = DependencyManager::get(); wallet->sendKeyFilePathIfExists(); } + +void QmlCommerce::reset() { + auto ledger = DependencyManager::get(); + auto wallet = DependencyManager::get(); + ledger->reset(); + wallet->reset(); +} \ No newline at end of file diff --git a/interface/src/commerce/QmlCommerce.h b/interface/src/commerce/QmlCommerce.h index fd913ae4b7..deb11b7714 100644 --- a/interface/src/commerce/QmlCommerce.h +++ b/interface/src/commerce/QmlCommerce.h @@ -50,6 +50,7 @@ protected: Q_INVOKABLE void setPassphrase(const QString& passphrase); Q_INVOKABLE void getPassphraseSetupStatus(); Q_INVOKABLE void getKeyFilePathIfExists(); + Q_INVOKABLE void reset(); }; #endif // hifi_QmlCommerce_h diff --git a/interface/src/commerce/Wallet.cpp b/interface/src/commerce/Wallet.cpp index 4cbcb5f1d3..363269e9fc 100644 --- a/interface/src/commerce/Wallet.cpp +++ b/interface/src/commerce/Wallet.cpp @@ -464,7 +464,9 @@ void Wallet::getSecurityImage() { } // decrypt and return - if (decryptFile(imageFilePath(), &data, &dataLen)) { + QString filePath(imageFilePath()); + QFileInfo fileInfo(filePath); + if (fileInfo.exists() && decryptFile(filePath, &data, &dataLen)) { // create the pixmap _securityImage = new QPixmap(); _securityImage->loadFromData(data, dataLen, "jpg"); @@ -488,3 +490,13 @@ void Wallet::sendKeyFilePathIfExists() { emit keyFilePathIfExistsResult(""); } } + +void Wallet::reset() { + _publicKeys.clear(); + delete _securityImage; + delete _passphrase; + QFile keyFile(keyFilePath()); + QFile imageFile(imageFilePath()); + keyFile.remove(); + imageFile.remove(); +} \ No newline at end of file diff --git a/interface/src/commerce/Wallet.h b/interface/src/commerce/Wallet.h index 478a35625d..4acd913181 100644 --- a/interface/src/commerce/Wallet.h +++ b/interface/src/commerce/Wallet.h @@ -40,6 +40,8 @@ public: void setPassphrase(const QString& passphrase); QString* getPassphrase() { return _passphrase; } + void reset(); + signals: void securityImageResult(bool exists) ; void keyFilePathIfExistsResult(const QString& path); From 988ee8c8d1b99ab6ae2394c2790b01a56c25cbd9 Mon Sep 17 00:00:00 2001 From: David Kelly Date: Wed, 30 Aug 2017 15:23:54 -0700 Subject: [PATCH 2/2] fix image provider, couple other things --- .../qml/hifi/commerce/wallet/Help.qml | 1 + .../qml/hifi/commerce/wallet/Wallet.qml | 18 +++++++++++++----- interface/src/commerce/Wallet.cpp | 14 +++++++++++++- libraries/ui/src/ui/ImageProvider.cpp | 19 +++++++++++++++++-- libraries/ui/src/ui/ImageProvider.h | 4 ++-- scripts/system/commerce/wallet.js | 4 ++++ 6 files changed, 50 insertions(+), 10 deletions(-) diff --git a/interface/resources/qml/hifi/commerce/wallet/Help.qml b/interface/resources/qml/hifi/commerce/wallet/Help.qml index 589143ca84..47b3f6daf6 100644 --- a/interface/resources/qml/hifi/commerce/wallet/Help.qml +++ b/interface/resources/qml/hifi/commerce/wallet/Help.qml @@ -54,6 +54,7 @@ Item { text: "Testing: Reset Wallet!"; onClicked: { commerce.reset(); + sendSignalToWallet({method: 'walletReset'}); } } diff --git a/interface/resources/qml/hifi/commerce/wallet/Wallet.qml b/interface/resources/qml/hifi/commerce/wallet/Wallet.qml index 1cb6b45d5e..3b1caab5f6 100644 --- a/interface/resources/qml/hifi/commerce/wallet/Wallet.qml +++ b/interface/resources/qml/hifi/commerce/wallet/Wallet.qml @@ -107,7 +107,7 @@ Rectangle { anchors.centerIn: walletSetupLightboxContainer; width: walletSetupLightboxContainer.width - 50; height: walletSetupLightboxContainer.height - 50; - + Connections { onSendSignalToWallet: { if (msg.method === 'walletSetup_raiseKeyboard') { @@ -127,7 +127,7 @@ Rectangle { anchors.centerIn: walletSetupLightboxContainer; width: walletSetupLightboxContainer.width - 50; height: walletSetupLightboxContainer.height - 50; - + Connections { onSendSignalToWallet: { sendToScript(msg); @@ -196,7 +196,7 @@ Rectangle { commerce.getLoginStatus(); } } - + NeedsLogIn { id: needsLogIn; visible: root.activeView === "needsLogIn"; @@ -225,7 +225,7 @@ Rectangle { anchors.bottom: tabButtonsContainer.top; anchors.left: parent.left; anchors.right: parent.right; - + Connections { onSendSignalToWallet: { if (msg.method === 'setUpClicked') { @@ -295,6 +295,14 @@ Rectangle { anchors.leftMargin: 16; anchors.right: parent.right; anchors.rightMargin: 16; + + Connections { + onSendSignalToWallet: { + if (msg.method === 'walletReset') { + sendToScript(msg); + } + } + } } @@ -509,7 +517,7 @@ Rectangle { } // // TAB BUTTONS END - // + // Item { id: keyboardContainer; diff --git a/interface/src/commerce/Wallet.cpp b/interface/src/commerce/Wallet.cpp index 363269e9fc..32852602d7 100644 --- a/interface/src/commerce/Wallet.cpp +++ b/interface/src/commerce/Wallet.cpp @@ -14,6 +14,7 @@ #include "Wallet.h" #include "Application.h" #include "ui/ImageProvider.h" +#include "scripting/HMDScriptingInterface.h" #include #include @@ -493,10 +494,21 @@ void Wallet::sendKeyFilePathIfExists() { void Wallet::reset() { _publicKeys.clear(); + delete _securityImage; + _securityImage = nullptr; + + // tell the provider we got nothing + updateImageProvider(); delete _passphrase; + + // for now we need to maintain the hard-coded passphrase. + // FIXME: remove this line as part of wiring up the passphrase + // and probably set it to nullptr + _passphrase = new QString("pwd"); + QFile keyFile(keyFilePath()); QFile imageFile(imageFilePath()); keyFile.remove(); imageFile.remove(); -} \ No newline at end of file +} diff --git a/libraries/ui/src/ui/ImageProvider.cpp b/libraries/ui/src/ui/ImageProvider.cpp index 6398e6fad2..c74ed0cb44 100644 --- a/libraries/ui/src/ui/ImageProvider.cpp +++ b/libraries/ui/src/ui/ImageProvider.cpp @@ -18,10 +18,25 @@ const QString ImageProvider::PROVIDER_NAME = "security"; QReadWriteLock ImageProvider::_rwLock; QPixmap* ImageProvider::_securityImage = nullptr; -void ImageProvider::setSecurityImage(QPixmap* pixmap) { +ImageProvider::~ImageProvider() { + QWriteLocker lock(&_rwLock); + if (_securityImage) { + delete _securityImage; + _securityImage = nullptr; + } +} + +void ImageProvider::setSecurityImage(const QPixmap* pixmap) { // no need to delete old one, that is managed by the wallet QWriteLocker lock(&_rwLock); - _securityImage = pixmap; + if (_securityImage) { + delete _securityImage; + } + if (pixmap) { + _securityImage = new QPixmap(*pixmap); + } else { + _securityImage = nullptr; + } } QPixmap ImageProvider::requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) { diff --git a/libraries/ui/src/ui/ImageProvider.h b/libraries/ui/src/ui/ImageProvider.h index 5bd2e254e7..0093b60655 100644 --- a/libraries/ui/src/ui/ImageProvider.h +++ b/libraries/ui/src/ui/ImageProvider.h @@ -20,10 +20,10 @@ public: static const QString PROVIDER_NAME; ImageProvider() : QQuickImageProvider(QQuickImageProvider::Pixmap) {} - + virtual ~ImageProvider(); QPixmap requestPixmap(const QString& id, QSize* size, const QSize& requestedSize) override; - void setSecurityImage(QPixmap* pixmap); + void setSecurityImage(const QPixmap* pixmap); protected: static QReadWriteLock _rwLock; static QPixmap* _securityImage; diff --git a/scripts/system/commerce/wallet.js b/scripts/system/commerce/wallet.js index 34c602513e..a7b7b50379 100644 --- a/scripts/system/commerce/wallet.js +++ b/scripts/system/commerce/wallet.js @@ -70,6 +70,10 @@ case 'maybeEnableHmdPreview': Menu.setIsOptionChecked("Disable Preview", isHmdPreviewDisabled); break; + case 'walletReset': + onButtonClicked(); + onButtonClicked(); + break; default: print('Unrecognized message from QML:', JSON.stringify(message)); }