You've already forked matrix-js-sdk
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:
@@ -98,6 +98,11 @@ function keyFromRecoverySession(session, decryptionKey) {
|
||||
*
|
||||
* @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
|
||||
* callback that returns a Promise<String> of an identity access token to supply
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -225,7 +225,7 @@ OlmDevice.prototype._storeAccount = function(txn, account) {
|
||||
* Export data for re-creating the Olm device later.
|
||||
* TODO export data other than just account and (P2P) sessions.
|
||||
*
|
||||
* @return {Promise<object>} The export data (see specs for structure)
|
||||
* @return {Promise<object>} The exported data
|
||||
*/
|
||||
OlmDevice.prototype.export = async function() {
|
||||
const result = {
|
||||
|
||||
@@ -228,12 +228,24 @@ utils.inherits(Crypto, EventEmitter);
|
||||
* Initialise the crypto module so that it is ready for use
|
||||
*
|
||||
* Returns a promise which resolves once the crypto module is ready for use.
|
||||
*
|
||||
* @param {Object} kwargs keyword arguments.
|
||||
* @param {string} kwargs.exportedOlmDevice (Optional) data from exported device
|
||||
* that must be re-created.
|
||||
*/
|
||||
Crypto.prototype.init = async function() {
|
||||
Crypto.prototype.init = async function(kwargs) {
|
||||
const {
|
||||
exportedOlmDevice,
|
||||
} = kwargs;
|
||||
|
||||
logger.log("Crypto: initialising Olm...");
|
||||
await global.Olm.init();
|
||||
logger.log("Crypto: initialising Olm device...");
|
||||
await this._olmDevice.init();
|
||||
logger.log(
|
||||
exportedOlmDevice
|
||||
? "Crypto: initialising Olm device from exported device..."
|
||||
: "Crypto: initialising Olm device..."
|
||||
);
|
||||
await this._olmDevice.init(exportedOlmDevice);
|
||||
logger.log("Crypto: loading device list...");
|
||||
await this._deviceList.load();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user