Make sure EnumerateWalletFiles can enumerate on backups too.
MODIFY
WalletWasabi.Gui/Tabs/WalletManager/LoadWallets/LoadWalletViewModel.cs
WalletWasabi.Gui/Tabs/WalletManager/LoadWallets/LoadWalletViewModel.cs
@@ -562,11 +562,7 @@ namespace WalletWasabi.Gui.Tabs.WalletManager.LoadWallets
}
}
- public void OpenWalletsFolder()
- {
- var path = Global.WalletsDir;
- IoHelpers.OpenFolderInFileExplorer(path);
- }
+ public void OpenWalletsFolder() => IoHelpers.OpenFolderInFileExplorer(Global.WalletManager.WalletDirectories.WalletsDir);
private bool TrySetWalletStates()
{
@@ -632,40 +628,12 @@ namespace WalletWasabi.Gui.Tabs.WalletManager.LoadWallets
private bool TryFindWalletByExtPubKey(ExtPubKey extPubKey, out string walletName)
{
- // Start searching for the real wallet name.
- walletName = null;
-
- var walletFiles = new DirectoryInfo(Global.WalletsDir);
- var walletBackupFiles = new DirectoryInfo(Global.WalletBackupsDir);
-
- List<FileInfo> walletFileNames = new List<FileInfo>();
-
- if (walletFiles.Exists)
- {
- walletFileNames.AddRange(walletFiles.EnumerateFiles());
- }
-
- if (walletBackupFiles.Exists)
- {
- walletFileNames.AddRange(walletFiles.EnumerateFiles());
- }
-
- walletFileNames = walletFileNames.OrderByDescending(x => x.LastAccessTimeUtc).ToList();
-
- foreach (FileInfo walletFile in walletFileNames)
- {
- if (walletFile?.Extension?.Equals(".json", StringComparison.OrdinalIgnoreCase) is true
- && KeyManager.TryGetExtPubKeyFromFile(walletFile.FullName, out ExtPubKey epk))
- {
- if (epk == extPubKey) // We already had it.
- {
- walletName = walletFile.Name;
- return true;
- }
- }
- }
+ walletName = Global.WalletManager.WalletDirectories
+ .EnumerateWalletFiles(includeBackupDir: true)
+ .FirstOrDefault(fi => KeyManager.TryGetExtPubKeyFromFile(fi.FullName, out ExtPubKey epk) && epk == extPubKey)
+ ?.Name;
- return false;
+ return walletName is { };
}
}
}

MODIFY
WalletWasabi.Tests/UnitTests/WalletDirTests.cs
WalletWasabi.Tests/UnitTests/WalletDirTests.cs
@@ -91,19 +91,25 @@ namespace WalletWasabi.Tests.UnitTests
var walletDirectories = new WalletDirectories(baseDir);
- List<string> wallets = new List<string>();
+ var wallets = new List<string>();
+ var walletBackups = new List<string>();
const int NumberOfWallets = 4;
for (int i = 0; i < NumberOfWallets; i++)
{
var walletFile = Path.Combine(walletDirectories.WalletsDir, $"FooWallet{i}.json");
var dummyFile = Path.Combine(walletDirectories.WalletsDir, $"FooWallet{i}.dummy");
- wallets.Add(walletFile);
+ var backupFile = Path.Combine(walletDirectories.WalletsBackupDir, $"FooWallet{i}.json");
+
File.Create(walletFile);
File.Create(dummyFile);
- File.Create(Path.Combine(walletDirectories.WalletsBackupDir, $"FooWallet{i}.json"));
+ File.Create(backupFile);
+
+ wallets.Add(walletFile);
+ walletBackups.Add(backupFile);
}
Assert.True(wallets.ToHashSet().SetEquals(walletDirectories.EnumerateWalletFiles().Select(x => x.FullName).ToHashSet()));
+ Assert.True(wallets.Concat(walletBackups).ToHashSet().SetEquals(walletDirectories.EnumerateWalletFiles(true).Select(x => x.FullName).ToHashSet()));
}
[Fact]

MODIFY
WalletWasabi/Wallets/WalletDirectories.cs
WalletWasabi/Wallets/WalletDirectories.cs
@@ -34,15 +34,37 @@ namespace WalletWasabi.Wallets
return (Path.Combine(WalletsDir, walletName), Path.Combine(WalletsBackupDir, walletName));
}
- public IEnumerable<FileInfo> EnumerateWalletFiles()
+ public IEnumerable<FileInfo> EnumerateWalletFiles(bool includeBackupDir = false)
{
- if (!Directory.Exists(WalletsDir))
+ var walletsDirInfo = new DirectoryInfo(WalletsDir);
+ var walletsDirExists = walletsDirInfo.Exists;
+ var searchPattern = $"*.{WalletFileExtension}";
+ var searchOption = SearchOption.TopDirectoryOnly;
+ IEnumerable<FileInfo> result = null;
+
+ if (includeBackupDir)
+ {
+ var backupsDirInfo = new DirectoryInfo(WalletsBackupDir);
+ if (!walletsDirExists && !backupsDirInfo.Exists)
+ {
+ return Enumerable.Empty<FileInfo>();
+ }
+
+ result = walletsDirInfo
+ .EnumerateFiles(searchPattern, searchOption)
+ .Concat(backupsDirInfo.EnumerateFiles(searchPattern, searchOption));
+ }
+ else
+ {
+ if (!walletsDirExists)
{
return Enumerable.Empty<FileInfo>();
}
- var directoryInfo = new DirectoryInfo(WalletsDir);
- return directoryInfo.GetFiles($"*.{WalletFileExtension}", SearchOption.TopDirectoryOnly).OrderByDescending(t => t.LastAccessTimeUtc);
+ result = walletsDirInfo.EnumerateFiles(searchPattern, searchOption);
+ }
+
+ return result.OrderByDescending(t => t.LastAccessTimeUtc);
}
}
}

Make sure EnumerateWalletFiles can enumerate on backups too.