You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-11-29 16:43:09 +03:00
MatrixClient: refactor uploadKeys
rewrite uploadKeys to require less looping and not to use deferreds.
This commit is contained in:
@@ -308,22 +308,54 @@ MatrixClient.prototype.isCryptoEnabled = function() {
|
|||||||
* @return {object} A promise that will resolve when the keys are uploaded.
|
* @return {object} A promise that will resolve when the keys are uploaded.
|
||||||
*/
|
*/
|
||||||
MatrixClient.prototype.uploadKeys = function(maxKeys, deferred) {
|
MatrixClient.prototype.uploadKeys = function(maxKeys, deferred) {
|
||||||
if (!CRYPTO_ENABLED || this.sessionStore === null) {
|
var self = this;
|
||||||
|
return _doKeyUpload(this).then(function(res) {
|
||||||
|
var keyCount = res.one_time_key_counts.curve25519 || 0;
|
||||||
|
|
||||||
|
var pickled = self.sessionStore.getEndToEndAccount();
|
||||||
|
|
||||||
|
var numberToGenerate;
|
||||||
|
var account = new Olm.Account();
|
||||||
|
try {
|
||||||
|
account.unpickle(self.accountKey, pickled);
|
||||||
|
|
||||||
|
var maxOneTimeKeys = account.max_number_of_one_time_keys();
|
||||||
|
var keyLimit = Math.floor(maxOneTimeKeys / 2);
|
||||||
|
numberToGenerate = Math.max(keyLimit - keyCount, 0);
|
||||||
|
if (maxKeys !== undefined) {
|
||||||
|
numberToGenerate = Math.min(numberToGenerate, maxKeys);
|
||||||
|
}
|
||||||
|
if (numberToGenerate > 0) {
|
||||||
|
account.generate_one_time_keys(numberToGenerate);
|
||||||
|
}
|
||||||
|
pickled = account.pickle(self.accountKey);
|
||||||
|
self.sessionStore.storeEndToEndAccount(pickled);
|
||||||
|
} finally {
|
||||||
|
account.free();
|
||||||
|
}
|
||||||
|
if (numberToGenerate > 0) {
|
||||||
|
return _doKeyUpload(self);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// build the upload request, and return a promise which resolves to the response
|
||||||
|
function _doKeyUpload(client) {
|
||||||
|
if (!CRYPTO_ENABLED || client.sessionStore === null) {
|
||||||
return q.reject(new Error("End-to-end encryption disabled"));
|
return q.reject(new Error("End-to-end encryption disabled"));
|
||||||
}
|
}
|
||||||
var first_time = deferred === undefined;
|
|
||||||
deferred = deferred || q.defer();
|
var pickled = client.sessionStore.getEndToEndAccount();
|
||||||
var path = "/keys/upload/" + this.deviceId;
|
|
||||||
var pickled = this.sessionStore.getEndToEndAccount();
|
|
||||||
if (!pickled) {
|
if (!pickled) {
|
||||||
return q.reject(new Error("End-to-end account not found"));
|
return q.reject(new Error("End-to-end account not found"));
|
||||||
}
|
}
|
||||||
var account = new Olm.Account();
|
var account = new Olm.Account();
|
||||||
var oneTimeKeys;
|
var oneTimeKeys;
|
||||||
try {
|
try {
|
||||||
account.unpickle(this.accountKey, pickled);
|
account.unpickle(client.accountKey, pickled);
|
||||||
oneTimeKeys = JSON.parse(account.one_time_keys());
|
oneTimeKeys = JSON.parse(account.one_time_keys());
|
||||||
var maxOneTimeKeys = account.max_number_of_one_time_keys();
|
|
||||||
} finally {
|
} finally {
|
||||||
account.free();
|
account.free();
|
||||||
}
|
}
|
||||||
@@ -335,42 +367,26 @@ MatrixClient.prototype.uploadKeys = function(maxKeys, deferred) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
var content = {
|
var content = {
|
||||||
device_keys: this.deviceKeys,
|
device_keys: client.deviceKeys,
|
||||||
one_time_keys: oneTimeJson
|
one_time_keys: oneTimeJson
|
||||||
};
|
};
|
||||||
var self = this;
|
var path = "/keys/upload/" + client.deviceId;
|
||||||
this._http.authedRequestWithPrefix(
|
return client._http.authedRequestWithPrefix(
|
||||||
undefined, "POST", path, undefined, content, httpApi.PREFIX_UNSTABLE
|
undefined, "POST", path, undefined, content, httpApi.PREFIX_UNSTABLE
|
||||||
).then(function(res) {
|
).then(function(res) {
|
||||||
var keyLimit = Math.floor(maxOneTimeKeys / 2);
|
|
||||||
var keyCount = res.one_time_key_counts.curve25519 || 0;
|
|
||||||
var generateKeys = (keyCount < keyLimit);
|
|
||||||
var pickled = self.sessionStore.getEndToEndAccount();
|
|
||||||
|
|
||||||
var account = new Olm.Account();
|
var account = new Olm.Account();
|
||||||
try {
|
try {
|
||||||
account.unpickle(self.accountKey, pickled);
|
account.unpickle(client.accountKey, pickled);
|
||||||
account.mark_keys_as_published();
|
account.mark_keys_as_published();
|
||||||
if (generateKeys) {
|
pickled = account.pickle(client.accountKey);
|
||||||
var numberToGenerate = keyLimit - keyCount;
|
client.sessionStore.storeEndToEndAccount(pickled);
|
||||||
if (maxKeys) {
|
|
||||||
numberToGenerate = Math.min(numberToGenerate, maxKeys);
|
|
||||||
}
|
|
||||||
account.generate_one_time_keys(numberToGenerate);
|
|
||||||
}
|
|
||||||
pickled = account.pickle(self.accountKey);
|
|
||||||
self.sessionStore.storeEndToEndAccount(pickled);
|
|
||||||
} finally {
|
} finally {
|
||||||
account.free();
|
account.free();
|
||||||
}
|
}
|
||||||
if (generateKeys && first_time) {
|
return res;
|
||||||
self.uploadKeys(maxKeys, deferred);
|
|
||||||
} else {
|
|
||||||
deferred.resolve();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
return deferred.promise;
|
}
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user