Skip to content
This repository has been archived by the owner on Aug 28, 2019. It is now read-only.

Commit

Permalink
Upload one-time keys. Issue quotient-im#88
Browse files Browse the repository at this point in the history
  • Loading branch information
a-andreyev committed Jun 15, 2019
1 parent efc815c commit a11d25a
Showing 1 changed file with 72 additions and 10 deletions.
82 changes: 72 additions & 10 deletions lib/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
#include <QtCore/QRegularExpression>
#include <QtCore/QMimeDatabase>
#include <QtCore/QCoreApplication>
#include <QtCore/QHash>

using namespace QMatrixClient;

Expand Down Expand Up @@ -103,7 +104,38 @@ class Connection::Private
GetCapabilitiesJob* capabilitiesJob = nullptr;
GetCapabilitiesJob::Capabilities capabilities;

QtOlm::Account* olmAccount = nullptr;
UploadKeysJob* uploadKeysJob = nullptr;
float signedKeysProportion = 1; // TODO: to constructor with Q_ASSERT
float oneTimeKeyThreshold = 0.5; // TODO: to constructor with Q_ASSERT
int target_keys_number = olmAccount->maxOneTimeKeys();
QHash<QString, int> oneTimeKeyCounts;
QHash<QString, int> oneTimeKeysToUploadCounts;
// TODO: literals to names
QHash<QString, int> targetOneTimeKeyCounts { {QStringLiteral("signed_curve25519"), qRound(signedKeysProportion * target_keys_number)},
{QStringLiteral("curve25519"), qRound((1-signedKeysProportion) * target_keys_number)}
};
void updateKeysToUpload()
{
for (auto it = targetOneTimeKeyCounts.cbegin(); it != targetOneTimeKeyCounts.cend(); ++it)
{
int numKeys = oneTimeKeyCounts.value(it.key(),0);
int numToCreate = qMax(it.value() - numKeys, 0);
oneTimeKeysToUploadCounts.insert(it.key(), numToCreate);
}
}

bool oneTimeKeyShouldUpload()
{
if (oneTimeKeyCounts.empty())
return true;
for (auto it = targetOneTimeKeyCounts.cbegin(); it != targetOneTimeKeyCounts.cend(); ++it)
{
if (oneTimeKeyCounts.value(it.key(),0) < it.value() * oneTimeKeyThreshold)
return true;
}
return false;
}

SyncJob* syncJob = nullptr;

Expand Down Expand Up @@ -242,9 +274,7 @@ void Connection::doConnectToServer(const QString& user, const QString& password,

// Uploading device keys:
AccountSettings accountSettings(loginJob->userId());

using namespace QtOlm;
Account* olmAccount = accountSettings.loadOlmAccount();
d->olmAccount = accountSettings.loadOlmAccount();

DeviceKeys deviceKeys;

Expand All @@ -257,9 +287,9 @@ void Connection::doConnectToServer(const QString& user, const QString& password,

QHash<QString, QString> keys;
keys.insert(QStringLiteral("curve25519") + QStringLiteral(":") + loginJob->deviceId(),
olmAccount->curve25519IdentityKey());
d->olmAccount->curve25519IdentityKey());
keys.insert(QStringLiteral("ed25519") + QStringLiteral(":") + loginJob->deviceId(),
olmAccount->ed25519IdentityKey());
d->olmAccount->ed25519IdentityKey());
deviceKeys.keys = keys;

QJsonObject message;
Expand All @@ -268,14 +298,46 @@ void Connection::doConnectToServer(const QString& user, const QString& password,
QHash<QString, QHash<QString, QString>> signatures;
QHash<QString, QString> signatureDict;
signatureDict.insert(QStringLiteral("ed25519") + QStringLiteral(":") + loginJob->deviceId(),
olmAccount->sign(message));
d->olmAccount->sign(message));
signatures.insert(loginJob->userId(),signatureDict);

connect(d->uploadKeysJob, &BaseJob::success, this, [this] {
d->oneTimeKeyCounts = d->uploadKeysJob->oneTimeKeyCounts();
d->updateKeysToUpload();
// Uploading one-time keys
if (d->olmAccount && d->oneTimeKeyShouldUpload())
{
int signedKeysToUpload = d->oneTimeKeysToUploadCounts.value(QStringLiteral("signed_curve25519"), 0);
int unsignedKeysToUpload = d->oneTimeKeysToUploadCounts.value(QStringLiteral("curve25519"), 0);
d->olmAccount->generateOneTimeKeys(signedKeysToUpload + unsignedKeysToUpload);
/*
one_time_keys = {}
keys = self.olm_account.one_time_keys['curve25519']
for i, key_id in enumerate(keys):
if i < signed_keys_to_upload:
key = self.sign_json({'key': keys[key_id]})
key_type = 'signed_curve25519'
else:
key = keys[key_id]
key_type = 'curve25519'
one_time_keys['{}:{}'.format(key_type, key_id)] = key
*/

// d->uploadKeysJob = callApi<UploadKeysJob>(oneTimeKeys);
/* self.one_time_keys_manager.server_counts = ret['one_time_key_counts'] */
d->olmAccount->markKeysAsPublished();

/*
keys_uploaded = {}
if unsigned_keys_to_upload:
keys_uploaded['curve25519'] = unsigned_keys_to_upload
if signed_keys_to_upload:
keys_uploaded['signed_curve25519'] = signed_keys_to_upload
logger.info('Uploaded new one-time keys: %s.', keys_uploaded)
*/
}
});
d->uploadKeysJob = callApi<UploadKeysJob>(deviceKeys);

// TODO: Upload one-time keys:
// QHash<QString, QVariant> oneTimeKeys;
// d->uploadKeysJob = callApi<UploadKeysJob>(deviceKeys);
});
connect(loginJob, &BaseJob::failure, this,
[this, loginJob] {
Expand Down

0 comments on commit a11d25a

Please sign in to comment.