From 38fde43009e4221030b89285791f9e397d6f2548 Mon Sep 17 00:00:00 2001 From: Zach Fox Date: Wed, 8 Nov 2017 13:19:47 -0800 Subject: [PATCH] ...simplify your life --- .../qml/hifi/commerce/wallet/WalletHome.qml | 4 -- interface/src/commerce/Ledger.cpp | 13 +--- interface/src/commerce/QmlCommerce.cpp | 5 -- interface/src/commerce/QmlCommerce.h | 1 - interface/src/commerce/Wallet.cpp | 61 +++++++------------ interface/src/commerce/Wallet.h | 5 -- 6 files changed, 22 insertions(+), 67 deletions(-) diff --git a/interface/resources/qml/hifi/commerce/wallet/WalletHome.qml b/interface/resources/qml/hifi/commerce/wallet/WalletHome.qml index 44d3d54f58..fd7ce0fdfd 100644 --- a/interface/resources/qml/hifi/commerce/wallet/WalletHome.qml +++ b/interface/resources/qml/hifi/commerce/wallet/WalletHome.qml @@ -43,10 +43,6 @@ Item { calculatePendingAndInvalidated(); } - - if (transactionHistoryModel.count === 0) { - commerce.setMustRegenerateKeypair(true); - } refreshTimer.start(); } } diff --git a/interface/src/commerce/Ledger.cpp b/interface/src/commerce/Ledger.cpp index b344e00430..49ae1bc18c 100644 --- a/interface/src/commerce/Ledger.cpp +++ b/interface/src/commerce/Ledger.cpp @@ -43,6 +43,7 @@ QJsonObject Ledger::failResponse(const QString& label, QNetworkReply& reply) { #define FailHandler(NAME) void Ledger::NAME##Failure(QNetworkReply& reply) { emit NAME##Result(failResponse(#NAME, reply)); } #define Handler(NAME) ApiHandler(NAME) FailHandler(NAME) Handler(buy) +Handler(receiveAt) Handler(balance) Handler(inventory) @@ -89,18 +90,6 @@ void Ledger::buy(const QString& hfc_key, int cost, const QString& asset_id, cons signedSend("transaction", transactionString, hfc_key, "buy", "buySuccess", "buyFailure", controlled_failure); } -void Ledger::receiveAtSuccess(QNetworkReply& reply) { - auto wallet = DependencyManager::get(); - QByteArray response = reply.readAll(); - QJsonObject data = QJsonDocument::fromJson(response).object(); - - // ZRF FIXME! Change to something like `data["status"] == fail` - // Not on "The List" for receiving HFC - if (true) { - wallet->setMustRegenerateKeypair(true); - } -} -void Ledger::receiveAtFailure(QNetworkReply& reply) { failResponse("receiveAt", reply); } bool Ledger::receiveAt(const QString& hfc_key, const QString& old_key, const QString& machine_fingerprint) { auto accountManager = DependencyManager::get(); if (!accountManager->isLoggedIn()) { diff --git a/interface/src/commerce/QmlCommerce.cpp b/interface/src/commerce/QmlCommerce.cpp index ebd2d9ae7c..f29e46d843 100644 --- a/interface/src/commerce/QmlCommerce.cpp +++ b/interface/src/commerce/QmlCommerce.cpp @@ -57,11 +57,6 @@ void QmlCommerce::getWalletAuthenticatedStatus() { emit walletAuthenticatedStatusResult(wallet->walletIsAuthenticatedWithPassphrase()); } -void QmlCommerce::setMustRegenerateKeypair(const bool& val) { - auto wallet = DependencyManager::get(); - wallet->setMustRegenerateKeypair(val); -} - void QmlCommerce::getSecurityImage() { auto wallet = DependencyManager::get(); wallet->getSecurityImage(); diff --git a/interface/src/commerce/QmlCommerce.h b/interface/src/commerce/QmlCommerce.h index a4e2ab8f42..d4f4aa35d2 100644 --- a/interface/src/commerce/QmlCommerce.h +++ b/interface/src/commerce/QmlCommerce.h @@ -54,7 +54,6 @@ protected: Q_INVOKABLE void getKeyFilePathIfExists(); Q_INVOKABLE void getSecurityImage(); Q_INVOKABLE void getWalletAuthenticatedStatus(); - Q_INVOKABLE void setMustRegenerateKeypair(const bool& val); Q_INVOKABLE void chooseSecurityImage(const QString& imageFile); Q_INVOKABLE void setPassphrase(const QString& passphrase); diff --git a/interface/src/commerce/Wallet.cpp b/interface/src/commerce/Wallet.cpp index 05c356cea9..9bc8dc9e43 100644 --- a/interface/src/commerce/Wallet.cpp +++ b/interface/src/commerce/Wallet.cpp @@ -324,19 +324,9 @@ Wallet::Wallet() { connect(ledger.data(), &Ledger::accountResult, this, [&]() { auto wallet = DependencyManager::get(); - auto ledger = DependencyManager::get(); auto walletScriptingInterface = DependencyManager::get(); uint status; - if (_mustRegenerateKeypair || getKeyFilePath() == "") { - qCDebug(commerce) << "Regenerating keys and resetting user_hfc_account. _mustRegenerateKeypair:" - << _mustRegenerateKeypair << "keyFilePath:" << getKeyFilePath(); - _mustRegenerateKeypair = false; - resetKeysOnly(); - ledger->reset(); // Hits `reset_user_hfc_account` endpoint - generateKeyPair(); // Hits `receive_at` endpoint - } - if (wallet->getKeyFilePath() == "" || !wallet->getSecurityImage()) { status = (uint)WalletStatus::WALLET_STATUS_NOT_SET_UP; } else if (!wallet->walletIsAuthenticatedWithPassphrase()) { @@ -535,32 +525,25 @@ bool Wallet::generateKeyPair() { // FIXME: initialize OpenSSL elsewhere soon initialize(); - qCInfo(commerce) << "Generating keypair..."; - QPair keyPair = generateRSAKeypair(); + qCInfo(commerce) << "Generating keypair."; + auto keyPair = generateRSAKeypair(); + + writeBackupInstructions(); + + // TODO: redo this soon -- need error checking and so on + writeSecurityImage(_securityImage, keyFilePath()); QString oldKey = _publicKeys.count() == 0 ? "" : _publicKeys.last(); - if (keyPair.first) { - writeBackupInstructions(); + QString key = keyPair.first->toBase64(); + _publicKeys.push_back(key); + qCDebug(commerce) << "public key:" << key; - // TODO: redo this soon -- need error checking and so on - if (_securityImage) { - writeSecurityImage(_securityImage, keyFilePath()); - } - - QString key = keyPair.first->toBase64(); - _publicKeys.push_back(key); - qCDebug(commerce) << "public key:" << key; - - // It's arguable whether we want to change the receiveAt every time, but: - // 1. It's certainly needed the first time, when createIfNeeded answers true. - // 2. It is maximally private, and we can step back from that later if desired. - // 3. It maximally exercises all the machinery, so we are most likely to surface issues now. - auto ledger = DependencyManager::get(); - QString machineFingerprint = uuidStringWithoutCurlyBraces(FingerprintUtils::getMachineFingerprint()); - return ledger->receiveAt(key, oldKey, machineFingerprint); - } else { - qCDebug(commerce) << "Failure generating keys!"; - return false; - } + // It's arguable whether we want to change the receiveAt every time, but: + // 1. It's certainly needed the first time, when createIfNeeded answers true. + // 2. It is maximally private, and we can step back from that later if desired. + // 3. It maximally exercises all the machinery, so we are most likely to surface issues now. + auto ledger = DependencyManager::get(); + QString machineFingerprint = uuidStringWithoutCurlyBraces(FingerprintUtils::getMachineFingerprint()); + return ledger->receiveAt(key, oldKey, machineFingerprint); } QStringList Wallet::listPublicKeys() { @@ -678,13 +661,9 @@ QString Wallet::getKeyFilePath() { } } -void Wallet::resetKeysOnly() { +void Wallet::reset() { _publicKeys.clear(); - QFile keyFile(keyFilePath()); - keyFile.remove(); -} -void Wallet::reset() { delete _securityImage; _securityImage = nullptr; @@ -692,7 +671,9 @@ void Wallet::reset() { updateImageProvider(); _passphrase->clear(); - resetKeysOnly(); + + QFile keyFile(keyFilePath()); + keyFile.remove(); } bool Wallet::writeWallet(const QString& newPassphrase) { RSA* keys = readKeys(keyFilePath().toStdString().c_str()); diff --git a/interface/src/commerce/Wallet.h b/interface/src/commerce/Wallet.h index f0f13aaf6e..ed145df451 100644 --- a/interface/src/commerce/Wallet.h +++ b/interface/src/commerce/Wallet.h @@ -49,7 +49,6 @@ public: bool walletIsAuthenticatedWithPassphrase(); bool changePassphrase(const QString& newPassphrase); - void resetKeysOnly(); void reset(); void getWalletStatus(); @@ -60,8 +59,6 @@ public: WALLET_STATUS_READY }; - void setMustRegenerateKeypair(const bool& val) { _mustRegenerateKeypair = val; } - signals: void securityImageResult(bool exists); void keyFilePathIfExistsResult(const QString& path); @@ -86,8 +83,6 @@ private: bool writeBackupInstructions(); void account(); - - bool _mustRegenerateKeypair { false }; }; #endif // hifi_Wallet_h