Make existing keymanager modifier functions to tofile
MODIFY
WalletWasabi.Tests/KeyManagementTests.cs
WalletWasabi.Tests/KeyManagementTests.cs
@@ -93,12 +93,14 @@ namespace WalletWasabi.Tests
var filePath = Path.Combine(SharedFixture.DataDir, nameof(CanSerialize), "Wallet.json");
DeleteFileAndDirectoryIfExists(filePath);
- var manager = KeyManager.CreateNew(out _, password, filePath);
Logger.TurnOff();
Assert.Throws<FileNotFoundException>(() => KeyManager.FromFile(filePath));
Logger.TurnOn();
+ var manager = KeyManager.CreateNew(out _, password, filePath);
+ KeyManager.FromFile(filePath);
+
manager.ToFile();
manager.ToFile(); // assert it doesn't throw

MODIFY
WalletWasabi/KeyManagement/KeyManager.cs
WalletWasabi/KeyManagement/KeyManager.cs
@@ -36,6 +36,7 @@ namespace WalletWasabi.KeyManagement
private readonly object HdPubKeysLock;
public string FilePath { get; private set; }
+ private object ToFileLock { get; }
[JsonConstructor]
public KeyManager(BitcoinEncryptedSecretNoEC encryptedSecret, byte[] chainCode, ExtPubKey extPubKey, string filePath = null)
@@ -47,6 +48,8 @@ namespace WalletWasabi.KeyManagement
ChainCode = Guard.NotNull(nameof(chainCode), chainCode);
ExtPubKey = Guard.NotNull(nameof(extPubKey), extPubKey);
SetFilePath(filePath);
+ ToFileLock = new object();
+ ToFile();
}
public KeyManager(BitcoinEncryptedSecretNoEC encryptedSecret, byte[] chainCode, string password, string filePath = null)
@@ -66,6 +69,8 @@ namespace WalletWasabi.KeyManagement
ExtPubKey = extKey.Derive(AccountKeyPath).Neuter();
SetFilePath(filePath);
+ ToFileLock = new object();
+ ToFile();
}
public static KeyManager CreateNew(out Mnemonic mnemonic, string password, string filePath = null)
@@ -109,6 +114,8 @@ namespace WalletWasabi.KeyManagement
public void ToFile()
{
if (FilePath != null)
+ {
+ lock (ToFileLock)
{
string jsonString = JsonConvert.SerializeObject(this, Formatting.Indented);
File.WriteAllText(FilePath,
@@ -116,6 +123,7 @@ namespace WalletWasabi.KeyManagement
Encoding.UTF8);
}
}
+ }
public static KeyManager FromFile(string filePath)
{
@@ -166,6 +174,8 @@ namespace WalletWasabi.KeyManagement
var hdPubKey = new HdPubKey(pubKey, fullPath, label, keyState);
HdPubKeys.Add(hdPubKey);
+ ToFile();
+
return hdPubKey;
}
}

Make existing keymanager modifier functions to tofile