Search in sources :

Example 1 with RequestUploadPreKey

use of im.actor.core.api.rpc.RequestUploadPreKey in project actor-platform by actorapp.

the class KeyManagerActor method onMainKeysReady.

private void onMainKeysReady() {
    Log.d(TAG, "Main Keys are ready");
    //
    // Generation required pre keys
    //
    int missingKeysCount = Math.max(0, Configuration.EPHEMERAL_KEYS_COUNT - ownKeys.getPreKeys().length);
    if (missingKeysCount > 0) {
        ownKeys = ownKeys.appendPreKeys(ManagedList.of(PrivateKey.GENERATOR, missingKeysCount).toArray(new PrivateKey[0]));
        encryptionKeysStorage.addOrUpdateItem(0, ownKeys.toByteArray());
    }
    //
    // Uploading own pre keys
    //
    final ManagedList<PrivateKey> pendingEphermalKeys = ManagedList.of(ownKeys.getPreKeys()).filter(PrivateKey.NOT_UPLOADED);
    if (pendingEphermalKeys.size() > 0) {
        ArrayList<ApiEncryptionKey> uploadingKeys = pendingEphermalKeys.map(PrivateKey.TO_API);
        ArrayList<ApiEncryptionKeySignature> uploadingSignatures = pendingEphermalKeys.map(PrivateKey.SIGN(ownKeys.getIdentityKey()));
        api(new RequestUploadPreKey(ownKeys.getKeyGroupId(), uploadingKeys, uploadingSignatures)).then(new Consumer<ResponseVoid>() {

            @Override
            public void apply(ResponseVoid responseVoid) {
                ownKeys = ownKeys.markAsUploaded(pendingEphermalKeys.toArray(new PrivateKey[pendingEphermalKeys.size()]));
                encryptionKeysStorage.addOrUpdateItem(0, ownKeys.toByteArray());
                onAllKeysReady();
            }
        }).failure(new Consumer<Exception>() {

            @Override
            public void apply(Exception e) {
                Log.w(TAG, "Ephemeral keys upload error");
                Log.e(TAG, e);
            // Ignore. This will freeze all encryption operations.
            }
        });
    } else {
        onAllKeysReady();
    }
}
Also used : ResponseVoid(im.actor.core.api.rpc.ResponseVoid) PrivateKey(im.actor.core.modules.encryption.entity.PrivateKey) RequestUploadPreKey(im.actor.core.api.rpc.RequestUploadPreKey) IOException(java.io.IOException) Consumer(im.actor.runtime.function.Consumer) ApiEncryptionKeySignature(im.actor.core.api.ApiEncryptionKeySignature) ApiEncryptionKey(im.actor.core.api.ApiEncryptionKey)

Aggregations

ApiEncryptionKey (im.actor.core.api.ApiEncryptionKey)1 ApiEncryptionKeySignature (im.actor.core.api.ApiEncryptionKeySignature)1 RequestUploadPreKey (im.actor.core.api.rpc.RequestUploadPreKey)1 ResponseVoid (im.actor.core.api.rpc.ResponseVoid)1 PrivateKey (im.actor.core.modules.encryption.entity.PrivateKey)1 Consumer (im.actor.runtime.function.Consumer)1 IOException (java.io.IOException)1