Skip to content

Commit e9108b8

Browse files
committed
fix possible db corruption on password removal by reordering database file and row updates
1 parent 3766585 commit e9108b8

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/main/java/com/sparrowwallet/sparrow/io/db/DbPersistence.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,20 @@ public void updateWallet(Storage storage, Wallet wallet) throws StorageException
162162

163163
@Override
164164
public void updateWallet(Storage storage, Wallet wallet, ECKey encryptionPubKey) throws StorageException {
165-
updatePassword(storage, encryptionPubKey);
165+
String newPassword = getFilePassword(encryptionPubKey);
166+
String currentPassword = getDatasourcePassword();
166167

167168
updateExecutor.execute(() -> {
168169
try {
169-
update(storage, wallet, getFilePassword(encryptionPubKey));
170+
if(dataSource != null && currentPassword != null && newPassword == null) {
171+
//Removing encryption: write data first
172+
update(storage, wallet, currentPassword);
173+
updatePassword(storage, encryptionPubKey);
174+
} else {
175+
//Adding encryption or no change: change file first
176+
updatePassword(storage, encryptionPubKey);
177+
update(storage, wallet, newPassword);
178+
}
170179
} catch(Exception e) {
171180
log.error("Error updating wallet db", e);
172181
}

0 commit comments

Comments
 (0)