1
0
mirror of https://github.com/matrix-org/matrix-js-sdk.git synced 2025-11-26 17:03:12 +03:00

Make OlmDevice key generation async

* OlmDevice.generateOneTimeKeys becomes async
* Stash maxOneTimeKeys at init so that maxNumberOfOneTimeKeys can remain sync
This commit is contained in:
Richard van der Hoff
2017-08-10 15:01:56 +01:00
parent a5f397b26d
commit 7d2bc12bb7
2 changed files with 9 additions and 7 deletions

View File

@@ -86,6 +86,7 @@ function OlmDevice(sessionStore) {
// don't know these until we load the account from storage in init() // don't know these until we load the account from storage in init()
this.deviceCurve25519Key = null; this.deviceCurve25519Key = null;
this.deviceEd25519Key = null; this.deviceEd25519Key = null;
this._maxOneTimeKeys = null;
// we don't bother stashing outboundgroupsessions in the sessionstore - // we don't bother stashing outboundgroupsessions in the sessionstore -
// instead we keep them here. // instead we keep them here.
@@ -119,6 +120,8 @@ OlmDevice.prototype.init = async function() {
try { try {
_initialise_account(this._sessionStore, this._pickleKey, account); _initialise_account(this._sessionStore, this._pickleKey, account);
e2eKeys = JSON.parse(account.identity_keys()); e2eKeys = JSON.parse(account.identity_keys());
this._maxOneTimeKeys = account.max_number_of_one_time_keys();
} finally { } finally {
account.free(); account.free();
} }
@@ -266,9 +269,7 @@ OlmDevice.prototype.getOneTimeKeys = async function() {
* @return {number} number of keys * @return {number} number of keys
*/ */
OlmDevice.prototype.maxNumberOfOneTimeKeys = function() { OlmDevice.prototype.maxNumberOfOneTimeKeys = function() {
return this._getAccount(function(account) { return this._maxOneTimeKeys;
return account.max_number_of_one_time_keys();
});
}; };
/** /**
@@ -287,7 +288,7 @@ OlmDevice.prototype.markKeysAsPublished = async function() {
* *
* @param {number} numKeys number of keys to generate * @param {number} numKeys number of keys to generate
*/ */
OlmDevice.prototype.generateOneTimeKeys = function(numKeys) { OlmDevice.prototype.generateOneTimeKeys = async function(numKeys) {
const self = this; const self = this;
this._getAccount(function(account) { this._getAccount(function(account) {
account.generate_one_time_keys(numKeys); account.generate_one_time_keys(numKeys);

View File

@@ -308,14 +308,15 @@ function _maybeUploadOneTimeKeys(crypto) {
function uploadLoop(keyCount) { function uploadLoop(keyCount) {
if (keyLimit <= keyCount) { if (keyLimit <= keyCount) {
// If we don't need to generate any more keys then we are done. // If we don't need to generate any more keys then we are done.
return; return Promise.resolve();
} }
const keysThisLoop = Math.min(keyLimit - keyCount, maxKeysPerCycle); const keysThisLoop = Math.min(keyLimit - keyCount, maxKeysPerCycle);
// Ask olm to generate new one time keys, then upload them to synapse. // Ask olm to generate new one time keys, then upload them to synapse.
crypto._olmDevice.generateOneTimeKeys(keysThisLoop); return crypto._olmDevice.generateOneTimeKeys(keysThisLoop).then(() => {
return _uploadOneTimeKeys(crypto).then((res) => { return _uploadOneTimeKeys(crypto);
}).then((res) => {
if (res.one_time_key_counts && res.one_time_key_counts.signed_curve25519) { if (res.one_time_key_counts && res.one_time_key_counts.signed_curve25519) {
// if the response contains a more up to date value use this // if the response contains a more up to date value use this
// for the next loop // for the next loop