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

add high-level export/import methods

not sure how to test these high-level methods though
This commit is contained in:
Cédric Van Rompay
2020-01-22 16:40:38 +01:00
parent ed01b3b8cf
commit 7b6dabbe9c
3 changed files with 48 additions and 5 deletions

View File

@@ -97,6 +97,11 @@ function keyFromRecoverySession(session, decryptionKey) {
* @param {string} opts.accessToken The access_token for this user.
*
* @param {string} opts.userId The user ID for this user.
*
* @param {Object} opts.deviceToImport Device data exported with
* (TODO link to export method) that must be imported to recreate this device.
* Should only be useful for deviced with end-to-end crypto enabled.
* If provided, opts.userId should **not** be provided.
*
* @param {IdentityServerProvider} [opts.identityServer]
* Optional. A provider object with one function `getAccessToken`, which is a
@@ -243,6 +248,18 @@ export function MatrixClient(opts) {
this.deviceId = opts.deviceId || null;
if (opts.deviceToImport) {
if (this.deviceId) {
logger.warn('not importing device because device ID is provided to constructor independently of exported data');
} else if (!(opts.deviceToImport.deviceId)){
logger.warn('not importing device because no device ID in exported data');
} else {
this.deviceId = opts.deviceToImport.deviceId;
// will be used during async initialization of the crypto
this._exportedOlmDeviceToImport = opts.deviceToImport.olmDevice;
}
}
const userId = (opts.userId || null);
this.credentials = {
userId: userId,
@@ -391,6 +408,17 @@ export function MatrixClient(opts) {
utils.inherits(MatrixClient, EventEmitter);
utils.extend(MatrixClient.prototype, MatrixBaseApis.prototype);
MatrixClient.prototype.exportDevice = async function() {
if (!(this._crypto)) {
logger.warn('not exporting device if crypto is not enabled');
return;
}
return {
deviceId: this.deviceId,
olmDevice: await this._crypto._olmDevice.export(),
};
};
/**
* Clear any data out of the persistent stores used by the client.
*
@@ -684,7 +712,10 @@ MatrixClient.prototype.initCrypto = async function() {
]);
logger.log("Crypto: initialising crypto object...");
await crypto.init();
await crypto.init({
exportedOlmDevice: this._exportedOlmDeviceToImport,
});
delete this._exportedOlmDeviceToImport;
this.olmVersion = Crypto.getOlmVersion();