restoreCryptoIdentity method
Reconnects to an already initialized crypto identity using the provided recovery key or passphrase. Throws if encryption is unavailable, the identity is not initialized, or it is already connected.
keyOrPassphrase is the recovery key or passphrase that unlocks the
secure secret storage. keyIdentifier can select a specific key when
multiple exist.
Implementation
Future<void> restoreCryptoIdentity(
String keyOrPassphrase, {
String? keyIdentifier,
bool selfSign = true,
}) async {
final encryption = this.encryption;
if (encryption == null) {
throw Exception('End to end encryption not available!');
}
final cryptoIdentityState = await getCryptoIdentityState();
if (!cryptoIdentityState.initialized) {
throw Exception(
'Crypto identity is not initalized. Please check with `Client.getCryptoIdentityState()` first and run `Client.initCryptoIdentity()` once for this account.',
);
}
if (cryptoIdentityState.connected) {
throw Exception(
'Crypto identity is already connected. Please check with `Client.getCryptoIdentityState()`.',
);
}
final completer = Completer();
encryption.bootstrap(
onUpdate: (bootstrap) async {
try {
switch (bootstrap.state) {
case BootstrapState.loading:
break;
case BootstrapState.askWipeSsss:
bootstrap.wipeSsss(false);
break;
case BootstrapState.askUseExistingSsss:
bootstrap.useExistingSsss(true, keyIdentifier: keyIdentifier);
break;
case BootstrapState.askUnlockSsss:
bootstrap.unlockedSsss();
break;
case BootstrapState.askBadSsss:
bootstrap.ignoreBadSecrets(false);
break;
case BootstrapState.openExistingSsss:
await bootstrap.newSsssKey!
.unlock(keyOrPassphrase: keyOrPassphrase);
await bootstrap.openExistingSsss();
if (selfSign) {
await bootstrap.client.encryption!.crossSigning
.selfSign(keyOrPassphrase: keyOrPassphrase);
}
break;
case BootstrapState.askWipeCrossSigning:
await bootstrap.wipeCrossSigning(false);
break;
case BootstrapState.askWipeOnlineKeyBackup:
bootstrap.wipeOnlineKeyBackup(false);
break;
// These states should not appear at all:
case BootstrapState.askSetupOnlineKeyBackup:
case BootstrapState.askSetupCrossSigning:
case BootstrapState.askNewSsss:
throw Exception(
'Bootstrap state ${bootstrap.state} should not happen!',
);
case BootstrapState.error:
throw Exception('Bootstrap error!');
case BootstrapState.done:
completer.complete();
break;
}
} catch (e, s) {
if (completer.isCompleted) {
return Logs().e('Bootstrap error after completed', e, s);
}
return completer.completeError(e, s);
}
},
);
await completer.future;
}