Exemplo n.º 1
0
Status
loginServerGetLoginPackage(const Lobby &lobby,
                           DataSlice LP1, DataSlice LRA1, LoginPackage &result, JsonPtr &rootKeyBox)
{
    const auto url = ABC_SERVER_ROOT "/account/loginpackage/get";
    ServerRequestJson json;
    ABC_CHECK(json.setup(lobby));
    if (LP1.size())
        ABC_CHECK(json.authKeySet(base64Encode(LP1)));
    if (LRA1.size())
        ABC_CHECK(json.recoveryAuthKeySet(base64Encode(LRA1)));

    HttpReply reply;
    ABC_CHECK(AirbitzRequest().post(reply, url, json.encode()));
    ServerReplyJson replyJson;
    ABC_CHECK(replyJson.decode(reply.body));
    ABC_CHECK(replyJson.ok());

    struct ResultJson:
        public JsonObject
    {
        ABC_JSON_CONSTRUCTORS(ResultJson, JsonObject)
        ABC_JSON_STRING(package, "login_package", nullptr)
        ABC_JSON_VALUE(rootKeyBox, "rootKeyBox", JsonPtr)
    } resultJson(replyJson.results());

    ABC_CHECK(resultJson.packageOk());
    ABC_CHECK(result.decode(resultJson.package()));
    if (json_is_object(resultJson.rootKeyBox().get()))
        rootKeyBox = resultJson.rootKeyBox();
    return Status();
}
Exemplo n.º 2
0
Status
Login::passwordAuthSet(DataSlice passwordAuth)
{
    std::lock_guard<std::mutex> lock(mutex_);
    passwordAuth_ = DataChunk(passwordAuth.begin(), passwordAuth.end());
    return Status();
}
Exemplo n.º 3
0
/**
 * Encodes data in an arbitrary power-of-2 base.
 * @param Bytes number of bytes per chunk of characters.
 * @param Chars number of characters per chunk.
 */
template<unsigned Bytes, unsigned Chars> std::string
chunkEncode(DataSlice data, const char *alphabet)
{
    std::string out;
    auto chunks = (data.size() + Bytes - 1) / Bytes; // Rounding up
    out.reserve(Chars * chunks);

    constexpr unsigned shift = 8 * Bytes / Chars; // Bits per character
    uint16_t buffer = 0; // Bits waiting to be written out, MSB first
    int bits = 0; // Number of bits currently in the buffer
    auto i = data.begin();
    while (i != data.end() || 0 < bits)
    {
        // Reload the buffer if we need more bits:
        if (i != data.end() && bits < shift)
        {
            buffer |= *i++ << (8 - bits);
            bits += 8;
        }

        // Write out the most-significant bits in the buffer:
        out += alphabet[buffer >> (16 - shift)];
        buffer <<= shift;
        bits -= shift;
    }

    // Pad the final string to a multiple of the chunk size:
    out.append(-out.size() % Chars, '=');
    return out;
}
Exemplo n.º 4
0
Status
broadcastTx(Wallet &self, DataSlice rawTx)
{
    // Create communication resources:
    auto syncer = std::make_shared<Syncer>();
    auto s1 = std::make_shared<DelayedStatus>();
    auto s2 = std::make_shared<DelayedStatus>();
    auto s3 = std::make_shared<DelayedStatus>();

    // Launch the broadcasts:
    DataChunk tx(rawTx.begin(), rawTx.end());

    if (!self.bOverrideBitcoinServers)
    {
        std::thread(broadcastTask<blockchainPostTx>, syncer, s1, tx).detach();
        std::thread(broadcastTask<insightPostTx>, syncer, s2, tx).detach();
    }

    // Queue up an async broadcast over the TxUpdater:
    auto updaterDone = [syncer, s3](Status s)
    {
        {
            std::lock_guard<std::mutex> lock(syncer->mutex);
            s3->status = s;
            s3->done = true;
            if (s)
                ABC_DebugLog("Stratum broadcast OK");
            else
                s.log();
        }
        syncer->cv.notify_all();
    };
    watcherSend(self, updaterDone, rawTx).log();

    // Loop as long as any thread is still running:
    while (true)
    {
        // Wait for the condition variable, which also acquires the lock:
        std::unique_lock<std::mutex> lock(syncer->mutex);
        syncer->cv.wait(lock);

        // Stop waiting if any broadcast has succeeded:
        if (s1->done && s1->status)
            break;
        if (s2->done && s2->status)
            break;
        if (s3->done && s3->status)
            break;

        // If they are all done, we have an error:
        if (s1->done && s2->done && s3->done)
            return s1->status;
    }

    return Status();
}
Exemplo n.º 5
0
std::string
toString(DataSlice slice)
{
    // Due to a bug, lots of AirBitz encrypted blobs end with a null byte.
    // Get rid of those:
    auto size = slice.size();
    if (0 < size && !slice.data()[size - 1])
        size--;

    return std::string(reinterpret_cast<const char *>(slice.data()), size);
}
Exemplo n.º 6
0
Status
ScryptSnrp::hash(DataChunk &result, DataSlice data, size_t size) const
{
    DataChunk out(size);

    int rc = crypto_scrypt(data.data(), data.size(),
                           salt.data(), salt.size(), n, r, p, out.data(), size);
    if (rc)
        return ABC_ERROR(ABC_CC_ScryptError, "Error calculating Scrypt hash");

    result = std::move(out);
    return Status();
}
Exemplo n.º 7
0
Status
loginServerChangePassword(const Login &login,
                          DataSlice newLP1, DataSlice newLRA1,
                          const CarePackage &carePackage, const LoginPackage &loginPackage)
{
    const auto url = ABC_SERVER_ROOT "/account/password/update";
    JsonPtr json(json_pack("{ss, ss, ss, ss, ss}",
                           ABC_SERVER_JSON_L1_FIELD,      base64Encode(login.lobby.authId()).c_str(),
                           ABC_SERVER_JSON_LP1_FIELD,     base64Encode(login.authKey()).c_str(),
                           ABC_SERVER_JSON_NEW_LP1_FIELD, base64Encode(newLP1).c_str(),
                           ABC_SERVER_JSON_CARE_PACKAGE_FIELD,  carePackage.encode().c_str(),
                           ABC_SERVER_JSON_LOGIN_PACKAGE_FIELD, loginPackage.encode().c_str()));
    if (newLRA1.size())
    {
        json_object_set_new(json.get(), ABC_SERVER_JSON_NEW_LRA1_FIELD,
                            json_string(base64Encode(newLRA1).c_str()));
    }

    HttpReply reply;
    ABC_CHECK(AirbitzRequest().post(reply, url, json.encode()));
    ServerReplyJson replyJson;
    ABC_CHECK(replyJson.decode(reply.body));
    ABC_CHECK(replyJson.ok());

    return Status();
}
Exemplo n.º 8
0
Status
loginServerChangePassword(const Login &login,
                          DataSlice newLP1, DataSlice newLRA1,
                          const CarePackage &carePackage,
                          const LoginPackage &loginPackage)
{
    const auto url = ABC_SERVER_ROOT "/v1/account/password/update";
    ServerRequestJson json;
    ABC_CHECK(json.setup(login));
    ABC_CHECK(json.set(ABC_SERVER_JSON_NEW_LP1_FIELD, base64Encode(newLP1)));
    ABC_CHECK(json.set(ABC_SERVER_JSON_CARE_PACKAGE_FIELD, carePackage.encode()));
    ABC_CHECK(json.set(ABC_SERVER_JSON_LOGIN_PACKAGE_FIELD, loginPackage.encode()));
    if (newLRA1.size())
    {
        ABC_CHECK(json.set(ABC_SERVER_JSON_NEW_LRA1_FIELD, base64Encode(newLRA1)));
    }

    HttpReply reply;
    ABC_CHECK(AirbitzRequest().post(reply, url, json.encode()));
    ServerReplyJson replyJson;
    ABC_CHECK(replyJson.decode(reply));

    return Status();
}
Exemplo n.º 9
0
/**
 * Casts new-style data to an old-style U08Buf type.
 * DANGER! THIS IS NOT CONST-CORRECT!
 */
inline U08Buf
toU08Buf(DataSlice slice)
{
    return U08Buf(const_cast<uint8_t *>(slice.data()), slice.size());
}
Exemplo n.º 10
0
Login::Login(LoginStore &store, DataSlice dataKey):
    store(store),
    parent_(store.shared_from_this()),
    dataKey_(dataKey.begin(), dataKey.end())
{}