From efdc340c5c0452e9116f662888a74515852fa922 Mon Sep 17 00:00:00 2001 From: David Kelly Date: Thu, 7 Sep 2017 18:15:04 -0700 Subject: [PATCH] Use new hfc account endpoint --- .../qml/hifi/commerce/wallet/Wallet.qml | 9 ++++++- interface/src/commerce/Ledger.cpp | 25 +++++++++++++++++++ interface/src/commerce/Ledger.h | 4 +++ interface/src/commerce/QmlCommerce.cpp | 6 +++++ interface/src/commerce/QmlCommerce.h | 2 ++ interface/src/commerce/Wallet.cpp | 8 +++--- interface/src/commerce/Wallet.h | 6 +++++ 7 files changed, 55 insertions(+), 5 deletions(-) diff --git a/interface/resources/qml/hifi/commerce/wallet/Wallet.qml b/interface/resources/qml/hifi/commerce/wallet/Wallet.qml index 401ef89374..1efed8b941 100644 --- a/interface/resources/qml/hifi/commerce/wallet/Wallet.qml +++ b/interface/resources/qml/hifi/commerce/wallet/Wallet.qml @@ -33,12 +33,19 @@ Rectangle { Hifi.QmlCommerce { id: commerce; + onAccountResult: { + if(result.status === "success") { + commerce.getKeyFilePathIfExists(); + } else { + // unsure how to handle a failure here. We definitely cannot proceed. + } + } onLoginStatusResult: { if (!isLoggedIn && root.activeView !== "needsLogIn") { root.activeView = "needsLogIn"; } else if (isLoggedIn) { root.activeView = "initialize"; - commerce.getKeyFilePathIfExists(); + commerce.account(); } } diff --git a/interface/src/commerce/Ledger.cpp b/interface/src/commerce/Ledger.cpp index 2055229059..a1488af1fe 100644 --- a/interface/src/commerce/Ledger.cpp +++ b/interface/src/commerce/Ledger.cpp @@ -176,3 +176,28 @@ void Ledger::resetFailure(QNetworkReply& reply) { failResponse("reset", reply); void Ledger::reset() { send("reset_user_hfc_account", "resetSuccess", "resetFailure", QNetworkAccessManager::PutOperation, QJsonObject()); } + +void Ledger::accountSuccess(QNetworkReply& reply) { + // lets set the appropriate stuff in the wallet now + auto wallet = DependencyManager::get(); + QByteArray response = reply.readAll(); + QJsonObject data = QJsonDocument::fromJson(response).object()["data"].toObject(); + + auto salt = QByteArray::fromBase64(data["salt"].toString().toUtf8()); + auto iv = QByteArray::fromBase64(data["iv"].toString().toUtf8()); + auto ckey = QByteArray::fromBase64(data["ckey"].toString().toUtf8()); + + wallet->setSalt(salt); + wallet->setIv(iv); + wallet->setCKey(ckey); + + // none of the hfc account info should be emitted + emit accountResult(QJsonObject{ {"status", "success"} }); +} + +void Ledger::accountFailure(QNetworkReply& reply) { + failResponse("account", reply); +} +void Ledger::account() { + send("hfc_account", "accountSuccess", "accountFailure", QNetworkAccessManager::PutOperation, QJsonObject()); +} diff --git a/interface/src/commerce/Ledger.h b/interface/src/commerce/Ledger.h index 55b648aa4c..6aadf5afb0 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 account(); void reset(); signals: @@ -37,6 +38,7 @@ signals: void balanceResult(QJsonObject result); void inventoryResult(QJsonObject result); void historyResult(QJsonObject result); + void accountResult(QJsonObject result); public slots: void buySuccess(QNetworkReply& reply); @@ -51,6 +53,8 @@ public slots: void historyFailure(QNetworkReply& reply); void resetSuccess(QNetworkReply& reply); void resetFailure(QNetworkReply& reply); + void accountSuccess(QNetworkReply& reply); + void accountFailure(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 8681fcba50..9315a2e9c5 100644 --- a/interface/src/commerce/QmlCommerce.cpp +++ b/interface/src/commerce/QmlCommerce.cpp @@ -27,6 +27,7 @@ QmlCommerce::QmlCommerce(QQuickItem* parent) : OffscreenQmlDialog(parent) { connect(wallet.data(), &Wallet::securityImageResult, this, &QmlCommerce::securityImageResult); connect(ledger.data(), &Ledger::historyResult, this, &QmlCommerce::historyResult); connect(wallet.data(), &Wallet::keyFilePathIfExistsResult, this, &QmlCommerce::keyFilePathIfExistsResult); + connect(ledger.data(), &Ledger::accountResult, this, &QmlCommerce::accountResult); } void QmlCommerce::getLoginStatus() { @@ -106,3 +107,8 @@ void QmlCommerce::reset() { ledger->reset(); wallet->reset(); } + +void QmlCommerce::account() { + auto ledger = DependencyManager::get(); + ledger->account(); +} diff --git a/interface/src/commerce/QmlCommerce.h b/interface/src/commerce/QmlCommerce.h index 7d9317efae..7b0bab95a6 100644 --- a/interface/src/commerce/QmlCommerce.h +++ b/interface/src/commerce/QmlCommerce.h @@ -39,6 +39,7 @@ signals: void balanceResult(QJsonObject result); void inventoryResult(QJsonObject result); void historyResult(QJsonObject result); + void accountResult(QJsonObject result); protected: Q_INVOKABLE void getLoginStatus(); @@ -55,6 +56,7 @@ protected: Q_INVOKABLE void history(); Q_INVOKABLE void generateKeyPair(); Q_INVOKABLE void reset(); + Q_INVOKABLE void account(); }; #endif // hifi_QmlCommerce_h diff --git a/interface/src/commerce/Wallet.cpp b/interface/src/commerce/Wallet.cpp index 8bdb8305d5..6bd010730f 100644 --- a/interface/src/commerce/Wallet.cpp +++ b/interface/src/commerce/Wallet.cpp @@ -257,10 +257,10 @@ RSA* readPrivateKey(const char* filename) { static const unsigned char IVEC[16] = "IAmAnIVecYay123"; void initializeAESKeys(unsigned char* ivec, unsigned char* ckey, const QByteArray& salt) { - // first ivec - memcpy(ivec, IVEC, 16); - auto hash = QCryptographicHash::hash(salt, QCryptographicHash::Sha256); - memcpy(ckey, hash.data(), 32); + // use the ones in the wallet + auto wallet = DependencyManager::get(); + memcpy(ivec, wallet->getIv(), 16); + memcpy(ckey, wallet->getCKey(), 32); } Wallet::~Wallet() { diff --git a/interface/src/commerce/Wallet.h b/interface/src/commerce/Wallet.h index f72b7adc41..f3c2ac399b 100644 --- a/interface/src/commerce/Wallet.h +++ b/interface/src/commerce/Wallet.h @@ -35,6 +35,10 @@ public: void setSalt(const QByteArray& salt) { _salt = salt; } QByteArray getSalt() { return _salt; } + void setIv(const QByteArray& iv) { _iv = iv; } + QByteArray getIv() { return _iv; } + void setCKey(const QByteArray& ckey) { _ckey = ckey; } + QByteArray getCKey() { return _ckey; } void setPassphrase(const QString& passphrase); QString* getPassphrase() { return _passphrase; } @@ -52,6 +56,8 @@ private: QStringList _publicKeys{}; QPixmap* _securityImage { nullptr }; QByteArray _salt {"iamsalt!"}; + QByteArray _iv; + QByteArray _ckey; QString* _passphrase { new QString("") }; void updateImageProvider();