Example #1
0
void Ledger::alreadyOwned(const QString& marketplaceId) {
    auto wallet = DependencyManager::get<Wallet>();
    QString endpoint = "already_owned";
    QJsonObject request;
    QStringList cachedPublicKeys = wallet->listPublicKeys();
    if (!cachedPublicKeys.isEmpty()) {
        request["public_keys"] = QJsonArray::fromStringList(wallet->listPublicKeys());
        request["marketplace_item_id"] = marketplaceId;
        send(endpoint, "alreadyOwnedSuccess", "alreadyOwnedFailure", QNetworkAccessManager::PutOperation, AccountManagerAuth::Required, request);
    } else {
        qDebug(commerce) << "User attempted to use the alreadyOwned endpoint, but cachedPublicKeys was empty!";
    }
}
Example #2
0
void Ledger::updateLocation(const QString& asset_id, const QString& location, const bool& alsoUpdateSiblings, const bool controlledFailure) {
    auto wallet = DependencyManager::get<Wallet>();
    auto walletScriptingInterface = DependencyManager::get<WalletScriptingInterface>();
    uint walletStatus = walletScriptingInterface->getWalletStatus();

    if (walletStatus != (uint)wallet->WALLET_STATUS_READY) {
        emit walletScriptingInterface->walletNotSetup();
        qDebug(commerce) << "User attempted to update the location of a certificate, but their wallet wasn't ready. Status:" << walletStatus;
    } else {
        QStringList cachedPublicKeys = wallet->listPublicKeys();
        if (!cachedPublicKeys.isEmpty()) {
            QString key = cachedPublicKeys[0];
            QJsonObject transaction;
            transaction["certificate_id"] = asset_id;
            transaction["place_name"] = location;
            if (alsoUpdateSiblings) {
                transaction["also_update_siblings"] = true;
            }
            QJsonDocument transactionDoc{ transaction };
            auto transactionString = transactionDoc.toJson(QJsonDocument::Compact);
            signedSend("transaction", transactionString, key, "location", "updateLocationSuccess", "updateLocationFailure", controlledFailure);
        } else {
            qDebug(commerce) << "User attempted to update the location of a certificate, but cachedPublicKeys was empty!";
        }
    }
}
Example #3
0
void QmlCommerce::balance() {
    auto ledger = DependencyManager::get<Ledger>();
    auto wallet = DependencyManager::get<Wallet>();
    QStringList cachedPublicKeys = wallet->listPublicKeys();
    if (!cachedPublicKeys.isEmpty()) {
        ledger->balance(cachedPublicKeys);
    }
}
Example #4
0
void QmlCommerce::history(const int& pageNumber) {
    auto ledger = DependencyManager::get<Ledger>();
    auto wallet = DependencyManager::get<Wallet>();
    QStringList cachedPublicKeys = wallet->listPublicKeys();
    if (!cachedPublicKeys.isEmpty()) {
        ledger->history(cachedPublicKeys, pageNumber);
    }
}
Example #5
0
void Ledger::keysQuery(const QString& endpoint, const QString& success, const QString& fail, QJsonObject& requestParams) {
    auto wallet = DependencyManager::get<Wallet>();
    QStringList cachedPublicKeys = wallet->listPublicKeys();
    if (!cachedPublicKeys.isEmpty()) {
        requestParams["public_keys"] = QJsonArray::fromStringList(cachedPublicKeys);
        send(endpoint, success, fail, QNetworkAccessManager::PostOperation, AccountManagerAuth::Required, requestParams);
    } else {
        qDebug(commerce) << "User attempted to call keysQuery, but cachedPublicKeys was empty!";
    }
}
Example #6
0
void Ledger::getAvailableUpdates(const QString& itemId) {
    auto wallet = DependencyManager::get<Wallet>();
    QString endpoint = "available_updates";
    QJsonObject request;
    request["public_keys"] = QJsonArray::fromStringList(wallet->listPublicKeys());
    if (!itemId.isEmpty()) {
        request["marketplace_item_id"] = itemId;
    }
    send(endpoint, "availableUpdatesSuccess", "availableUpdatesFailure", QNetworkAccessManager::PutOperation, AccountManagerAuth::Required, request);
}
Example #7
0
void QmlCommerce::transferHfcToUsername(const QString& username, const int& amount, const QString& optionalMessage) {
    auto ledger = DependencyManager::get<Ledger>();
    auto wallet = DependencyManager::get<Wallet>();
    QStringList keys = wallet->listPublicKeys();
    if (keys.count() == 0) {
        QJsonObject result{ { "status", "fail" },{ "message", "Uninitialized Wallet." } };
        return emit buyResult(result);
    }
    QString key = keys[0];
    ledger->transferHfcToUsername(key, username, amount, optionalMessage);
}
Example #8
0
bool QmlCommerce::buy(const QString& assetId, int cost, const QString& buyerUsername) {
    auto ledger = DependencyManager::get<Ledger>();
    auto wallet = DependencyManager::get<Wallet>();
    QStringList keys = wallet->listPublicKeys();
    if (keys.count() == 0) {
        return false;
    }
    QString key = keys[0];
    // For now, we receive at the same key that pays for it.
    return ledger->buy(key, cost, assetId, key, buyerUsername);
}
Example #9
0
void QmlCommerce::updateItem(const QString& certificateId) {
    auto ledger = DependencyManager::get<Ledger>();
    auto wallet = DependencyManager::get<Wallet>();
    QStringList keys = wallet->listPublicKeys();
    if (keys.count() == 0) {
        QJsonObject result{ { "status", "fail" }, { "message", "Uninitialized Wallet." } };
        return emit updateItemResult(result);
    }
    QString key = keys[0];
    ledger->updateItem(key, certificateId);
}
Example #10
0
void QmlCommerce::buy(const QString& assetId, int cost, const bool controlledFailure) {
    auto ledger = DependencyManager::get<Ledger>();
    auto wallet = DependencyManager::get<Wallet>();
    QStringList keys = wallet->listPublicKeys();
    if (keys.count() == 0) {
        QJsonObject result{ { "status", "fail" }, { "message", "Uninitialized Wallet." } };
        return emit buyResult(result);
    }
    QString key = keys[0];
    // For now, we receive at the same key that pays for it.
    ledger->buy(key, cost, assetId, key, controlledFailure);
}
Example #11
0
void QmlCommerce::inventory(const QString& editionFilter,
                            const QString& typeFilter,
                            const QString& titleFilter,
                            const int& page,
                            const int& perPage) {
    auto ledger = DependencyManager::get<Ledger>();
    auto wallet = DependencyManager::get<Wallet>();
    QStringList cachedPublicKeys = wallet->listPublicKeys();
    if (!cachedPublicKeys.isEmpty()) {
        ledger->inventory(editionFilter, typeFilter, titleFilter, page, perPage);
    }
}
Example #12
0
void QmlCommerce::authorizeAssetTransfer(const QString& couponID,
    const QString& certificateID,
    const int& amount,
    const QString& optionalMessage) {
    auto ledger = DependencyManager::get<Ledger>();
    auto wallet = DependencyManager::get<Wallet>();
    QStringList keys = wallet->listPublicKeys();
    if (keys.count() == 0) {
        QJsonObject result{ { "status", "fail" }, { "message", "Uninitialized Wallet." } };
        return emit authorizeAssetTransferResult(result);
    }
    QString key = keys[0];
    ledger->authorizeAssetTransfer(key, couponID, certificateID, amount, optionalMessage);
}
Example #13
0
void Ledger::certificateInfoSuccess(QNetworkReply& reply) {
    auto wallet = DependencyManager::get<Wallet>();
    auto accountManager = DependencyManager::get<AccountManager>();

    QByteArray response = reply.readAll();
    QJsonObject replyObject = QJsonDocument::fromJson(response).object();

    QStringList keys = wallet->listPublicKeys();
    if (keys.count() != 0) {
        QJsonObject data = replyObject["data"].toObject();
        if (data["transfer_recipient_key"].toString() == keys[0]) {
            replyObject.insert("isMyCert", true);
        }
    }
    qInfo(commerce) << "certificateInfo" << "response" << QJsonDocument(replyObject).toJson(QJsonDocument::Compact);
    emit certificateInfoResult(replyObject);
}
Example #14
0
void Ledger::accountSuccess(QNetworkReply& reply) {
    // lets set the appropriate stuff in the wallet now
    auto wallet = DependencyManager::get<Wallet>();
    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());
    QString remotePublicKey = data["public_key"].toString();
    bool isOverride = wallet->wasSoftReset();

    wallet->setSalt(salt);
    wallet->setIv(iv);
    wallet->setCKey(ckey);

    QString keyStatus = "ok";
    QStringList localPublicKeys = wallet->listPublicKeys();
    if (remotePublicKey.isEmpty() || isOverride) {
        if (!localPublicKeys.isEmpty()) {
            QString key = localPublicKeys.first();
            receiveAt(key, key);
        }
    } else {
        if (localPublicKeys.isEmpty()) {
            keyStatus = "preexisting";
        } else if (localPublicKeys.first() != remotePublicKey) {
            keyStatus = "conflicting";
        }
    }

    // none of the hfc account info should be emitted
    QJsonObject json;
    QJsonObject responseData{ { "status", "success"} };
    json["keyStatus"] = keyStatus;
    responseData["data"] = json;
    emit accountResult(responseData);
}
Example #15
0
void Ledger::historySuccess(QNetworkReply& reply) {
    // here we send a historyResult with some extra stuff in it
    // Namely, the styled text we'd like to show.  The issue is the
    // QML cannot do that easily since it doesn't know what the wallet
    // public key(s) are.  Let's keep it that way
    QByteArray response = reply.readAll();
    QJsonObject data = QJsonDocument::fromJson(response).object();
    qInfo(commerce) << "history" << "response" << QJsonDocument(data).toJson(QJsonDocument::Compact);

    // we will need the array of public keys from the wallet
    auto wallet = DependencyManager::get<Wallet>();
    auto keys = wallet->listPublicKeys();

    // now we need to loop through the transactions and add fancy text...
    auto historyArray = data.find("data").value().toObject().find("history").value().toArray();
    QJsonArray newHistoryArray;

    // TODO: do this with 0 copies if possible
    for (auto it = historyArray.begin(); it != historyArray.end(); it++) {
        // We have 2 text fields to synthesize, the one on the left is a listing
        // of the HFC in/out of your wallet.  The one on the right contains an explaination
        // of the transaction.  That could be just the memo (if it is a regular purchase), or
        // more text (plus the optional memo) if an hfc transfer
        auto valueObject = (*it).toObject();
        valueObject["hfc_text"] = hfcString(valueObject["sent_money"], valueObject["received_money"]);
        valueObject["transaction_text"] = transactionString(valueObject);
        newHistoryArray.push_back(valueObject);
    }
    // now copy the rest of the json -- this is inefficient
    // TODO: try to do this without making copies
    QJsonObject newData;
    newData["status"] = "success";
    QJsonObject newDataData;
    newDataData["history"] = newHistoryArray;
    newData["data"] = newDataData;
    newData["current_page"] = data["current_page"].toInt();
    emit historyResult(newData);
}