Skip to content

Commit 0f450dd

Browse files
committed
chore: Improved storage migration.
1 parent 97d2609 commit 0f450dd

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

lib/model/storage/storage.dart

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,35 @@ class StorageNotifier extends AutoDisposeAsyncNotifier<Storage> {
6969
}
7070
}
7171

72-
List<Totp> currentTotps = await currentStorage.listTotps();
73-
Totp? firstTotp = (await newStorage.listTotps(limit: 1)).firstOrNull;
72+
List<Totp> currentStorageTotps = await currentStorage.listTotps();
73+
List<Totp> newStorageTotps = await newStorage.listTotps();
7474
List<Totp> toAdd = [];
75-
if (firstTotp == null) {
76-
toAdd.addAll(currentTotps);
75+
if (newStorageTotps.isEmpty) {
76+
toAdd.addAll(currentStorageTotps);
7777
} else {
7878
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-
);
79+
CryptoStore? newCryptoStore;
80+
for (Totp totp in newStorageTotps) {
81+
CryptoStore cryptoStore = await CryptoStore.fromPassword(masterPassword, totp.encryptedData.encryptionSalt);
82+
if (await totp.encryptedData.canDecryptData(cryptoStore)) {
83+
newCryptoStore = cryptoStore;
84+
break;
85+
}
86+
}
87+
newCryptoStore ??= await CryptoStore.fromPassword(masterPassword, newStorageTotps.first.encryptedData.encryptionSalt);
88+
89+
for (Totp totp in currentStorageTotps) {
90+
CryptoStore oldCryptoStore;
91+
if (currentCryptoStore != null && await totp.encryptedData.canDecryptData(currentCryptoStore)) {
92+
oldCryptoStore = currentCryptoStore;
93+
} else if (await totp.encryptedData.canDecryptData(newCryptoStore)) {
94+
oldCryptoStore = newCryptoStore;
95+
} else {
96+
oldCryptoStore = await CryptoStore.fromPassword(
97+
masterPassword,
98+
totp.encryptedData.encryptionSalt,
99+
);
100+
}
87101
DecryptedTotp? decryptedTotp = await totp.changeEncryptionKey(oldCryptoStore, newCryptoStore);
88102
toAdd.add(decryptedTotp ?? totp);
89103
}

0 commit comments

Comments
 (0)