@@ -43,11 +43,6 @@ class StorageNotifier extends AutoDisposeAsyncNotifier<Storage> {
4343 return const ResultSuccess ();
4444 }
4545
46- Salt ? oldSalt = await currentStorage.readSecretsSalt ();
47- if (oldSalt == null ) {
48- throw SaltError ();
49- }
50-
5146 if (backupPassword != null ) {
5247 Result <Backup > backupResult = await ref.read (backupStoreProvider.notifier).doBackup (backupPassword);
5348 if (backupResult is ! ResultSuccess ) {
@@ -74,17 +69,22 @@ class StorageNotifier extends AutoDisposeAsyncNotifier<Storage> {
7469 }
7570 }
7671
77- CryptoStore currentCryptoStore = await CryptoStore .fromPassword (masterPassword, oldSalt);
78- Salt ? salt = await newStorage.readSecretsSalt ();
79- List <Totp > totps = await currentStorage.listTotps ();
72+ List <Totp > currentTotps = await currentStorage.listTotps ();
73+ Totp ? firstTotp = (await newStorage.listTotps (limit: 1 )).firstOrNull;
8074 List <Totp > toAdd = [];
81- if (salt == null ) {
82- await newStorage.saveSecretsSalt (oldSalt);
83- toAdd.addAll (totps);
75+ if (firstTotp == null ) {
76+ toAdd.addAll (currentTotps);
8477 } else {
85- CryptoStore newCryptoStore = await CryptoStore .fromPassword (masterPassword, salt);
86- for (Totp totp in totps) {
87- DecryptedTotp ? decryptedTotp = await totp.changeEncryptionKey (currentCryptoStore, newCryptoStore);
78+ CryptoStore ? currentCryptoStore = ref.read (cryptoStoreProvider).value;
79+ CryptoStore newCryptoStore = await CryptoStore .fromPassword (masterPassword, firstTotp.encryptedData.encryptionSalt);
80+ for (Totp totp in currentTotps) {
81+ CryptoStore oldCryptoStore = currentCryptoStore? .salt == totp.encryptedData.encryptionSalt
82+ ? currentCryptoStore!
83+ : await CryptoStore .fromPassword (
84+ masterPassword,
85+ totp.encryptedData.encryptionSalt,
86+ );
87+ DecryptedTotp ? decryptedTotp = await totp.changeEncryptionKey (oldCryptoStore, newCryptoStore);
8888 toAdd.add (decryptedTotp ?? totp);
8989 }
9090 await ref.read (cryptoStoreProvider.notifier).saveAndUse (newCryptoStore);
@@ -135,7 +135,7 @@ class GenericMigrationError extends StorageMigrationException {
135135/// Whether we should ask for a different [StorageMigrationDeletedTotpPolicy] .
136136class ShouldAskForDifferentDeletedTotpPolicyException extends StorageMigrationException {
137137 /// The error code.
138- static const String _code = 'genericError ' ;
138+ static const String _code = 'shouldAskForDifferentDeletedTotpPolicy ' ;
139139
140140 /// Creates a new storage migration policy exception instance.
141141 ShouldAskForDifferentDeletedTotpPolicyException ()
@@ -144,28 +144,13 @@ class ShouldAskForDifferentDeletedTotpPolicyException extends StorageMigrationEx
144144 );
145145
146146 @override
147- String toString () => 'StorageMigrationDeletedTotpPolicy error' ;
148- }
149-
150- /// When there is a salt error.
151- class SaltError extends StorageMigrationException {
152- /// The error code.
153- static const String _code = 'genericError' ;
154-
155- /// Creates a new salt error instance.
156- SaltError ()
157- : super (
158- code: _code,
159- );
160-
161- @override
162- String toString () => 'Salt error' ;
147+ String toString () => 'Another deleted TOTP policy should be used' ;
163148}
164149
165150/// When we haven't succeeded to do the asked backup.
166151class BackupException extends StorageMigrationException {
167152 /// The error code.
168- static const String _code = 'genericError ' ;
153+ static const String _code = 'backupError ' ;
169154
170155 /// Creates a new backup exception instance.
171156 BackupException ()
@@ -180,7 +165,7 @@ class BackupException extends StorageMigrationException {
180165/// When the provided password don't match the one that has been using on the old storage.
181166class CurrentStoragePasswordMismatchException extends StorageMigrationException {
182167 /// The error code.
183- static const String _code = 'genericError ' ;
168+ static const String _code = 'currentStoragePasswordMismatch ' ;
184169
185170 /// Creates a new current storage password mismatch exception instance.
186171 CurrentStoragePasswordMismatchException ()
@@ -192,25 +177,10 @@ class CurrentStoragePasswordMismatchException extends StorageMigrationException
192177 String toString () => 'Current storage password is incorrect' ;
193178}
194179
195- /// When the provided password don't match the one that has been using on the new storage.
196- class NewStoragePasswordMismatchException extends StorageMigrationException {
197- /// The error code.
198- static const String _code = 'genericError' ;
199-
200- /// Creates a new new storage password mismatch exception instance.
201- NewStoragePasswordMismatchException ()
202- : super (
203- code: _code,
204- );
205-
206- @override
207- String toString () => 'New storage password is incorrect' ;
208- }
209-
210180/// When there is an error while trying to change the encryption key of the old storage.
211181class EncryptionKeyChangeFailedError extends StorageMigrationException {
212182 /// The error code.
213- static const String _code = 'genericError ' ;
183+ static const String _code = 'encryptionKeyChangeFailed ' ;
214184
215185 /// Creates a new encryption key change error instance.
216186 EncryptionKeyChangeFailedError ()
@@ -264,26 +234,17 @@ mixin Storage {
264234 Future <Totp ?> getTotp (String uuid);
265235
266236 /// Lists all TOTPs.
267- Future <List <Totp >> listTotps ();
237+ Future <List <Totp >> listTotps ({ int ? limit} );
268238
269239 /// Lists all TOTPs UUID.
270- Future <List <String >> listUuids ();
240+ Future <List <String >> listUuids ({ int ? limit} );
271241
272242 /// Replace all current TOTPs by [newTotps] .
273243 Future <void > replaceTotps (List <Totp > newTotps) async {
274244 await clearTotps ();
275245 await addTotps (newTotps);
276246 }
277247
278- /// Loads the salt that allows to encrypt secrets.
279- Future <Salt ?> readSecretsSalt ();
280-
281- /// Saves the salt that allows to encrypt secrets.
282- Future <void > saveSecretsSalt (Salt salt);
283-
284- /// Deletes the salt that allows to encrypt secrets.
285- Future <void > deleteSecretsSalt ();
286-
287248 /// Closes this storage instance.
288249 Future <void > close ();
289250
0 commit comments