2021-10-27 18:38:53 +05:30
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-14 22:23:59 +05:30
|
|
|
import {ViewModel} from "./ViewModel";
|
2021-11-29 13:54:44 +05:30
|
|
|
import {KeyType} from "../matrix/ssss/index";
|
2022-01-26 14:21:48 +05:30
|
|
|
import {Status} from "./session/settings/KeyBackupViewModel.js";
|
2021-10-27 18:38:53 +05:30
|
|
|
|
|
|
|
export class AccountSetupViewModel extends ViewModel {
|
2022-02-17 13:54:18 +05:30
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this._accountSetup = options.accountSetup;
|
2021-10-27 18:38:53 +05:30
|
|
|
this._dehydratedDevice = undefined;
|
2021-10-29 20:10:35 +05:30
|
|
|
this._decryptDehydratedDeviceViewModel = undefined;
|
|
|
|
if (this._accountSetup.encryptedDehydratedDevice) {
|
|
|
|
this._decryptDehydratedDeviceViewModel = new DecryptDehydratedDeviceViewModel(this, dehydratedDevice => {
|
|
|
|
this._dehydratedDevice = dehydratedDevice;
|
|
|
|
this._decryptDehydratedDeviceViewModel = undefined;
|
|
|
|
this.emitChange("deviceDecrypted");
|
|
|
|
});
|
|
|
|
}
|
2021-10-27 18:38:53 +05:30
|
|
|
}
|
|
|
|
|
2021-10-29 20:10:35 +05:30
|
|
|
get decryptDehydratedDeviceViewModel() {
|
|
|
|
return this._decryptDehydratedDeviceViewModel;
|
2021-10-27 18:38:53 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get deviceDecrypted() {
|
|
|
|
return !!this._dehydratedDevice;
|
|
|
|
}
|
|
|
|
|
|
|
|
get dehydratedDeviceId() {
|
|
|
|
return this._accountSetup.encryptedDehydratedDevice.deviceId;
|
|
|
|
}
|
|
|
|
|
2021-10-29 20:10:35 +05:30
|
|
|
finish() {
|
|
|
|
this._accountSetup.finish(this._dehydratedDevice);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 14:21:48 +05:30
|
|
|
// this vm adopts the same shape as KeyBackupViewModel so the same view can be reused.
|
2021-10-29 20:10:35 +05:30
|
|
|
class DecryptDehydratedDeviceViewModel extends ViewModel {
|
|
|
|
constructor(accountSetupViewModel, decryptedCallback) {
|
2022-02-17 13:54:18 +05:30
|
|
|
super(accountSetupViewModel.options);
|
2021-10-29 20:10:35 +05:30
|
|
|
this._accountSetupViewModel = accountSetupViewModel;
|
|
|
|
this._isBusy = false;
|
|
|
|
this._status = Status.SetupKey;
|
|
|
|
this._error = undefined;
|
|
|
|
this._decryptedCallback = decryptedCallback;
|
|
|
|
}
|
|
|
|
|
|
|
|
get decryptAction() {
|
|
|
|
return this.i18n`Restore`;
|
|
|
|
}
|
|
|
|
|
|
|
|
get purpose() {
|
|
|
|
return this.i18n`claim your dehydrated device`;
|
|
|
|
}
|
|
|
|
|
|
|
|
get offerDehydratedDeviceSetup() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
get dehydratedDeviceId() {
|
|
|
|
return this._accountSetupViewModel._dehydratedDevice?.deviceId;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isBusy() {
|
|
|
|
return this._isBusy;
|
|
|
|
}
|
|
|
|
|
|
|
|
get backupVersion() { return 0; }
|
|
|
|
|
|
|
|
get status() {
|
|
|
|
return this._status;
|
|
|
|
}
|
|
|
|
|
|
|
|
get error() {
|
|
|
|
return this._error?.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
showPhraseSetup() {
|
|
|
|
if (this._status === Status.SetupKey) {
|
|
|
|
this._status = Status.SetupPhrase;
|
|
|
|
this.emitChange("status");
|
2021-10-27 18:38:53 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-29 20:10:35 +05:30
|
|
|
showKeySetup() {
|
|
|
|
if (this._status === Status.SetupPhrase) {
|
|
|
|
this._status = Status.SetupKey;
|
|
|
|
this.emitChange("status");
|
|
|
|
}
|
2021-10-27 18:38:53 +05:30
|
|
|
}
|
2021-10-29 20:10:35 +05:30
|
|
|
|
|
|
|
async _enterCredentials(keyType, credential) {
|
|
|
|
if (credential) {
|
|
|
|
try {
|
|
|
|
this._isBusy = true;
|
|
|
|
this.emitChange("isBusy");
|
|
|
|
const {encryptedDehydratedDevice} = this._accountSetupViewModel._accountSetup;
|
|
|
|
const dehydratedDevice = await encryptedDehydratedDevice.decrypt(keyType, credential);
|
|
|
|
this._decryptedCallback(dehydratedDevice);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
this._error = err;
|
|
|
|
this.emitChange("error");
|
|
|
|
} finally {
|
|
|
|
this._isBusy = false;
|
|
|
|
this.emitChange("");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enterSecurityPhrase(passphrase) {
|
|
|
|
this._enterCredentials(KeyType.Passphrase, passphrase);
|
|
|
|
}
|
|
|
|
|
|
|
|
enterSecurityKey(securityKey) {
|
|
|
|
this._enterCredentials(KeyType.RecoveryKey, securityKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
disable() {}
|
2021-10-27 18:38:53 +05:30
|
|
|
}
|