|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:matrix/encryption/utils/bootstrap.dart'; |
| 4 | +import 'package:matrix/matrix.dart'; |
| 5 | + |
| 6 | +extension CryptoSetupExtension on Client { |
| 7 | + /// Returns the current state of the crypto identity. |
| 8 | + Future<({bool initialized, bool connected})> getCryptoIdentityState() async => |
| 9 | + ( |
| 10 | + initialized: (encryption?.keyManager.enabled ?? false) && |
| 11 | + (encryption?.crossSigning.enabled ?? false), |
| 12 | + connected: ((await encryption?.keyManager.isCached()) ?? false) && |
| 13 | + ((await encryption?.crossSigning.isCached()) ?? false), |
| 14 | + ); |
| 15 | + |
| 16 | + Future<void> restoreCryptoIdentity(String keyOrPassphrase) async { |
| 17 | + final encryption = this.encryption; |
| 18 | + if (encryption == null) { |
| 19 | + throw Exception('End to end encryption not available!'); |
| 20 | + } |
| 21 | + final cryptoIdentityState = await getCryptoIdentityState(); |
| 22 | + if (!cryptoIdentityState.initialized) { |
| 23 | + throw Exception( |
| 24 | + 'Crypto identity is not initalized. Please check with `Client.getCryptoIdentityState()` first and run `Client.initCryptoIdentity()` once for this account.', |
| 25 | + ); |
| 26 | + } |
| 27 | + if (cryptoIdentityState.connected) { |
| 28 | + throw Exception( |
| 29 | + 'Crypto identity is already connected. Please check with `Client.getCryptoIdentityState()`.', |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + final completer = Completer(); |
| 34 | + encryption.bootstrap( |
| 35 | + onUpdate: (bootstrap) async { |
| 36 | + try { |
| 37 | + switch (bootstrap.state) { |
| 38 | + case BootstrapState.loading: |
| 39 | + break; |
| 40 | + case BootstrapState.askWipeSsss: |
| 41 | + bootstrap.wipeSsss(false); |
| 42 | + break; |
| 43 | + case BootstrapState.askUseExistingSsss: |
| 44 | + bootstrap.useExistingSsss(true); |
| 45 | + break; |
| 46 | + case BootstrapState.askUnlockSsss: |
| 47 | + bootstrap.unlockedSsss(); |
| 48 | + break; |
| 49 | + case BootstrapState.askBadSsss: |
| 50 | + bootstrap.ignoreBadSecrets(false); |
| 51 | + break; |
| 52 | + case BootstrapState.openExistingSsss: |
| 53 | + await bootstrap.newSsssKey! |
| 54 | + .unlock(keyOrPassphrase: keyOrPassphrase); |
| 55 | + await bootstrap.openExistingSsss(); |
| 56 | + await bootstrap.client.encryption!.crossSigning |
| 57 | + .selfSign(recoveryKey: keyOrPassphrase); |
| 58 | + break; |
| 59 | + case BootstrapState.askWipeCrossSigning: |
| 60 | + await bootstrap.wipeCrossSigning(false); |
| 61 | + break; |
| 62 | + case BootstrapState.askWipeOnlineKeyBackup: |
| 63 | + bootstrap.wipeOnlineKeyBackup(false); |
| 64 | + break; |
| 65 | + // These states should not appear at all: |
| 66 | + case BootstrapState.askSetupOnlineKeyBackup: |
| 67 | + case BootstrapState.askSetupCrossSigning: |
| 68 | + case BootstrapState.askNewSsss: |
| 69 | + throw Exception( |
| 70 | + 'Bootstrap state ${bootstrap.state} should not happen!', |
| 71 | + ); |
| 72 | + case BootstrapState.error: |
| 73 | + throw Exception('Bootstrap error!'); |
| 74 | + case BootstrapState.done: |
| 75 | + completer.complete(); |
| 76 | + break; |
| 77 | + } |
| 78 | + } catch (e, s) { |
| 79 | + if (completer.isCompleted) { |
| 80 | + return Logs().e('Bootstrap error after completed', e, s); |
| 81 | + } |
| 82 | + return completer.completeError(e, s); |
| 83 | + } |
| 84 | + }, |
| 85 | + ); |
| 86 | + |
| 87 | + await completer.future; |
| 88 | + } |
| 89 | + |
| 90 | + Future<String> initCryptoIdentity({ |
| 91 | + String? passphrase, |
| 92 | + bool wipeSecureStorage = true, |
| 93 | + bool wipeKeyBackup = true, |
| 94 | + bool wipeCrossSigning = true, |
| 95 | + bool setupMasterKey = true, |
| 96 | + bool setupSelfSigningKey = true, |
| 97 | + bool setupUserSigningKey = true, |
| 98 | + bool setupOnlineKeyBackup = true, |
| 99 | + }) async { |
| 100 | + final encryption = this.encryption; |
| 101 | + if (encryption == null) { |
| 102 | + throw Exception('End to end encryption not available!'); |
| 103 | + } |
| 104 | + |
| 105 | + String? newSsssKey; |
| 106 | + final completer = Completer(); |
| 107 | + encryption.bootstrap( |
| 108 | + onUpdate: (bootstrap) async { |
| 109 | + try { |
| 110 | + newSsssKey ??= bootstrap.newSsssKey?.recoveryKey; |
| 111 | + switch (bootstrap.state) { |
| 112 | + case BootstrapState.loading: |
| 113 | + break; |
| 114 | + case BootstrapState.askWipeSsss: |
| 115 | + bootstrap.wipeSsss(wipeSecureStorage); |
| 116 | + break; |
| 117 | + case BootstrapState.askUseExistingSsss: |
| 118 | + bootstrap.useExistingSsss(false); |
| 119 | + break; |
| 120 | + case BootstrapState.askUnlockSsss: |
| 121 | + bootstrap.unlockedSsss(); |
| 122 | + break; |
| 123 | + case BootstrapState.askBadSsss: |
| 124 | + bootstrap.ignoreBadSecrets(true); |
| 125 | + break; |
| 126 | + case BootstrapState.askWipeCrossSigning: |
| 127 | + await bootstrap.wipeCrossSigning(wipeCrossSigning); |
| 128 | + break; |
| 129 | + case BootstrapState.askWipeOnlineKeyBackup: |
| 130 | + bootstrap.wipeOnlineKeyBackup(wipeKeyBackup); |
| 131 | + break; |
| 132 | + case BootstrapState.askSetupOnlineKeyBackup: |
| 133 | + await bootstrap.askSetupOnlineKeyBackup(setupOnlineKeyBackup); |
| 134 | + break; |
| 135 | + case BootstrapState.askSetupCrossSigning: |
| 136 | + await bootstrap.askSetupCrossSigning( |
| 137 | + setupMasterKey: setupMasterKey, |
| 138 | + setupSelfSigningKey: setupSelfSigningKey, |
| 139 | + setupUserSigningKey: setupUserSigningKey, |
| 140 | + ); |
| 141 | + break; |
| 142 | + case BootstrapState.askNewSsss: |
| 143 | + await bootstrap.newSsss(passphrase); |
| 144 | + break; |
| 145 | + case BootstrapState.openExistingSsss: |
| 146 | + throw Exception( |
| 147 | + 'Bootstrap state ${bootstrap.state} should not happen!', |
| 148 | + ); |
| 149 | + case BootstrapState.error: |
| 150 | + throw Exception('Bootstrap error!'); |
| 151 | + case BootstrapState.done: |
| 152 | + completer.complete(); |
| 153 | + break; |
| 154 | + } |
| 155 | + } catch (e, s) { |
| 156 | + if (completer.isCompleted) { |
| 157 | + return Logs().e('Bootstrap error after completed', e, s); |
| 158 | + } |
| 159 | + return completer.completeError(e, s); |
| 160 | + } |
| 161 | + }, |
| 162 | + ); |
| 163 | + |
| 164 | + await completer.future; |
| 165 | + return newSsssKey!; |
| 166 | + } |
| 167 | +} |
0 commit comments