find, rename and delete the backups of a wallet left in a previous wallets folder when its password is added, it is renamed or it is deleted
What changed, and why it matters
This commit fixes a cleanup issue in the Sparrow Bitcoin wallet. Previously, if a wallet file was moved out of the default 'wallets' folder, its automatic backups could be left behind in the old folder. When the user later added a password, renamed, or deleted that wallet, Sparrow would not find or remove those leftover backups. The change makes Sparrow look in the wallet's previous folder for any orphaned backups and offers to delete or rename them alongside the main wallet. It is a privacy/data-residue fix rather than a remote hack vulnerability.
Treat as a low-severity privacy hardening fix. Users who previously moved wallet files out of the default wallets folder should manually check any old 'backup' subdirectories for leftover wallet backups and securely delete them if desired. No immediate action required for typical usage; ensure the fix is included in the next release.
Security signals we found
Leftover wallet backup files not deleted/renamed when wallet moved out of wallets directory
Potential data residue of wallet metadata/keys in backup folder after wallet deletion or rename
UI warning text updated to mention backups when backups exist outside wallets dir
Secure deletion helper (IOUtils.secureDelete) already used for backup cleanup
No cryptographic, network, or privilege-escalation changes
Evidence from the diff
The patch extends Storage backup management so it no longer assumes backups only exist when isInWalletsDir() is true. It introduces getBackupDirs() which, for wallets outside the wallets folder, also checks a sibling ‘backup’ directory (the previous wallets folder’s backup dir). deleteBackups(), hasBackups(), and renameBackups() are updated to operate over all discovered backup dirs. UI flows in AppController, WalletPasswordDialog, and SettingsController now warn the user and offer deletion when backups exist even for wallets stored elsewhere. A new constructor flag backupsDeletable controls whether the delete-backups checkbox is shown when adding/updating a password. Unit tests verify deletion and renaming of orphaned backups.
Changed components
src/main/java/com/sparrowwallet/sparrow/io/Storage.javasrc/main/java/com/sparrowwallet/sparrow/AppController.javasrc/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.javasrc/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.javaWallet backup deletion and rename logicWallet password/rename/delete UI dialogsInspect captured patch +77 / −16
### src/main/java/com/sparrowwallet/sparrow/AppController.java
@@ -2489,7 +2489,9 @@ private void renameWallet(WalletForm selectedWalletForm) {
}
private void deleteWallet(WalletForm selectedWalletForm) {
- Optional<ButtonType> optButtonType = AppServices.showWarningDialog("Delete " + selectedWalletForm.getWallet().getMasterName() + "?", "The wallet file" + (selectedWalletForm.getStorage().isInWalletsDir() ? " and any backups" : "") + " will be deleted. Are you sure?", ButtonType.NO, ButtonType.YES);
+ boolean deletesBackups = selectedWalletForm.getStorage().isInWalletsDir() || selectedWalletForm.getStorage().hasBackups();
+ Optional<ButtonType> optButtonType = AppServices.showWarningDialog("Delete " + selectedWalletForm.getWallet().getMasterName() + "?",
+ "The wallet file" + (deletesBackups ? " and any backups" : "") + " will be deleted. Are you sure?", ButtonType.NO, ButtonType.YES);
if(optButtonType.isPresent() && optButtonType.get() == ButtonType.YES) {
Storage storage = selectedWalletForm.getStorage();
if(selectedWalletForm.getMasterWallet().isEncrypted()) {
### src/main/java/com/sparrowwallet/sparrow/control/WalletPasswordDialog.java
@@ -29,6 +29,10 @@ public WalletPasswordDialog(String walletName, PasswordRequirement requirement)
}
public WalletPasswordDialog(String walletName, PasswordRequirement requirement, boolean suggestChangePassword, boolean backupSupported) {
+ this(walletName, requirement, suggestChangePassword, backupSupported, backupSupported);
+ }
+
+ public WalletPasswordDialog(String walletName, PasswordRequirement requirement, boolean suggestChangePassword, boolean backupSupported, boolean backupsDeletable) {
this.requirement = requirement;
this.password = new ViewPasswordField();
this.passwordConfirm = new ViewPasswordField();
@@ -70,6 +74,12 @@ public WalletPasswordDialog(String walletName, PasswordRequirement requirement,
content.getChildren().addAll(backupExisting, deleteBackups);
backupExisting.setSelected(true);
deleteBackups.setSelected(true);
+ } else if(backupsDeletable && (requirement == PasswordRequirement.UPDATE_EMPTY || requirement == PasswordRequirement.UPDATE_SET)) {
+ //A wallet outside the wallets folder takes no new backups, but may still have ones made there before
+ deleteBackups.managedProperty().bind(deleteBackups.visibleProperty());
+ deleteBackups.visibleProperty().bind(backupExisting.visibleProperty().not());
+ content.getChildren().add(deleteBackups);
+ deleteBackups.setSelected(true);
}
dialogPane.setContent(content);
### src/main/java/com/sparrowwallet/sparrow/io/Storage.java
@@ -283,34 +283,47 @@ public boolean delete(boolean deleteBackups) {
}
public void deleteBackups() {
- //Backups are only written for wallets in the wallets folder, so any backups matching the name of a wallet elsewhere belong to another wallet
- if(isInWalletsDir()) {
- deleteBackups(null);
+ for(File backupDir : getBackupDirs()) {
+ for(File backup : getBackups(backupDir, null)) {
+ IOUtils.secureDelete(backup);
+ }
}
}
- private void deleteBackups(String prefix) {
- File[] backups = getBackups(prefix);
- for(File backup : backups) {
- IOUtils.secureDelete(backup);
- }
+ public boolean hasBackups() {
+ return getBackupDirs().stream().anyMatch(backupDir -> getBackups(backupDir, null).length > 0);
}
- File[] getBackups(String prefix) {
- return getBackups(getWalletsBackupDir(), prefix);
+ /**
+ * Backups are written to the backup folder of the wallets folder. A wallet outside it may have been left in a previous wallets folder, whose backup
+ * folder still holds its backups, since Sparrow creates a backup folder only inside a wallets folder.
+ */
+ private List<File> getBackupDirs() {
+ if(isInWalletsDir()) {
+ return List.of(getWalletsBackupDir());
+ }
+
+ File previousBackupDir = new File(walletFile.getAbsoluteFile().getParentFile(), WALLETS_BACKUP_DIR);
+ return previousBackupDir.isDirectory() ? List.of(previousBackupDir) : Collections.emptyList();
}
public void renameBackups(Storage renamedStorage) {
- if(isInWalletsDir()) {
- renameBackups(getWalletsBackupDir(), renamedStorage);
+ //A renamed wallet is written to the wallets folder, so backups from a previous wallets folder are moved to where it will look for them
+ File targetDir = renamedStorage.isInWalletsDir() ? getWalletsBackupDir() : null;
+ for(File backupDir : getBackupDirs()) {
+ renameBackups(backupDir, targetDir == null ? backupDir : targetDir, renamedStorage);
}
}
void renameBackups(File backupDir, Storage renamedStorage) {
+ renameBackups(backupDir, backupDir, renamedStorage);
+ }
+
+ void renameBackups(File sourceDir, File targetDir, Storage renamedStorage) {
String walletName = persistence.getWalletName(walletFile, null);
String renamedWalletName = renamedStorage.persistence.getWalletName(renamedStorage.walletFile, null);
- for(File backup : getBackups(backupDir, null)) {
- File renamedBackup = new File(backupDir, renamedWalletName + backup.getName().substring(walletName.length()));
+ for(File backup : getBackups(sourceDir, null)) {
+ File renamedBackup = new File(targetDir, renamedWalletName + backup.getName().substring(walletName.length()));
try {
Files.move(backup.toPath(), renamedBackup.toPath());
} catch(IOException e) {
### src/main/java/com/sparrowwallet/sparrow/wallet/SettingsController.java
@@ -1048,7 +1048,8 @@ private boolean saveWallet(boolean changePassword, boolean suggestChangePassword
}
}
- WalletPasswordDialog dlg = new WalletPasswordDialog(null, requirement, suggestChangePassword, walletForm.getStorage().isInWalletsDir());
+ WalletPasswordDialog dlg = new WalletPasswordDialog(null, requirement, suggestChangePassword, walletForm.getStorage().isInWalletsDir(),
+ walletForm.getStorage().isInWalletsDir() || walletForm.getStorage().hasBackups());
dlg.initOwner(apply.getScene().getWindow());
Optional<SecureString> password = walletForm.isLocked() ? Optional.empty() : dlg.showAndWait();
if(password.isPresent() && walletForm.isLocked()) {
### src/test/java/com/sparrowwallet/sparrow/io/StorageTest.java
@@ -168,6 +168,41 @@ public void getBackupsExcludesBackupsOfAnExistingJsonWallet() throws IOException
assertBackups(backupDir, PersistenceType.JSON, "Separate", "Separate-20200101120000");
}
+ @Test
+ public void deleteBackupsInPreviousWalletsFolder() throws IOException {
+ File walletFile = new File(tempDir.toFile(), "Savings.mv.db");
+ Storage storage = new Storage(PersistenceType.DB, walletFile);
+ Assertions.assertFalse(storage.isInWalletsDir());
+ Assertions.assertFalse(storage.hasBackups());
+
+ File backupDir = new File(tempDir.toFile(), Storage.WALLETS_BACKUP_DIR);
+ Assertions.assertTrue(backupDir.mkdir());
+ File backup = new File(backupDir, "Savings-20250101120000.mv.db");
+ File otherBackup = new File(backupDir, "Other-20250101120000.mv.db");
+ Assertions.assertTrue(backup.createNewFile());
+ Assertions.assertTrue(otherBackup.createNewFile());
+
+ Assertions.assertTrue(storage.hasBackups());
+ storage.deleteBackups();
+ Assertions.assertFalse(backup.exists());
+ Assertions.assertTrue(otherBackup.exists());
+ Assertions.assertFalse(storage.hasBackups());
+ }
+
+ @Test
+ public void renameBackupsMovesBackupsToTheRenamedWalletsBackupFolder() throws IOException {
+ File previousBackupDir = createBackupDir("Old-20250101120000.mv.db", "Other-20250101120000.mv.db");
+ File walletsBackupDir = createBackupDir();
+
+ Storage oldStorage = new Storage(PersistenceType.DB, new File(previousBackupDir.getParentFile(), "Old.mv.db"));
+ Storage newStorage = new Storage(PersistenceType.DB, new File(walletsBackupDir.getParentFile(), "New.mv.db"));
+ oldStorage.renameBackups(previousBackupDir, walletsBackupDir, newStorage);
+
+ assertBackups(previousBackupDir, PersistenceType.DB, "Old.mv.db");
+ assertBackups(previousBackupDir, PersistenceType.DB, "Other.mv.db", "Other-20250101120000.mv.db");
+ assertBackups(walletsBackupDir, PersistenceType.DB, "New.mv.db", "New-20250101120000.mv.db");
+ }
+
@Test
public void renameBackupsMovesBackupsToTheRenamedWallet() throws IOException {
File backupDir = createBackupDir("Old-20250101120000.mv.db", "Old-20210101120000.json", "Old-20200101120000",Why this scored 36/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.