void
StratumConnection::getAddressHistory(
    const bc::client::obelisk_codec::error_handler &onError,
    const bc::client::obelisk_codec::fetch_history_handler &onReply,
    const bc::payment_address &address, size_t fromHeight)
{
    JsonArray params;
    params.append(json_string(address.encoded().c_str()));

    auto errorShim = [onError](Status status)
    {
        onError(std::make_error_code(std::errc::bad_message));
    };

    auto decoder = [onReply](JsonPtr payload) -> Status
    {
        JsonArray arrayJson(payload);

        bc::client::history_list history;
        size_t size = arrayJson.size();
        history.reserve(size);
        for (size_t i = 0; i < size; i++)
        {
            struct HistoryJson:
                public JsonObject
            {
                ABC_JSON_CONSTRUCTORS(HistoryJson, JsonObject)
                ABC_JSON_STRING(txid, "tx_hash", nullptr)
                ABC_JSON_INTEGER(height, "height", 0)
            };
            HistoryJson json(arrayJson[i]);

            bc::hash_digest hash;
            if (!json.txidOk() || !bc::decode_hash(hash, json.txid()))
                return ABC_ERROR(ABC_CC_Error, "Bad txid");

            bc::client::history_row row;
            row.output.hash = hash;
            row.output_height = json.height();
            row.spend.hash = bc::null_hash;
            history.push_back(row);
        }

        onReply(history);
        return Status();
    };
Exemple #2
0
Status
loginServerMessages(JsonPtr &result, const std::list<std::string> &usernames)
{
    const auto url = ABC_SERVER_ROOT "/v2/messages";

    // Compute all userIds:
    JsonArray loginIds;
    std::map<std::string, std::string> loginIdMap;
    for (const auto &username: usernames)
    {
        std::shared_ptr<LoginStore> store;
        ABC_CHECK(LoginStore::create(store, username));
        const auto loginId = base64Encode(store->userId());
        loginIds.append(json_string(loginId.c_str()));
        loginIdMap[loginId] = username;
    }

    JsonObject request;
    ABC_CHECK(request.set("loginIds", loginIds));

    // Make the request:
    HttpReply reply;
    ABC_CHECK(AirbitzRequest().request(reply, url, "POST", request.encode()));
    ServerReplyJson replyJson;
    ABC_CHECK(replyJson.decode(reply));

    // Insert the original usernames into the results:
    JsonArray arrayJson(replyJson.results());
    size_t size = arrayJson.size();
    for (size_t i = 0; i < size; i++)
    {
        JsonObject objectJson(arrayJson[i]);
        const auto loginId = objectJson.getString("loginId", nullptr);
        if (!loginId) continue;
        const auto username = loginIdMap.find(loginId);
        if (username == loginIdMap.end()) continue;
        ABC_CHECK(objectJson.set("username", username->second));
    }

    result = arrayJson;
    return Status();
}