Improve locking
MODIFY
WalletWasabi/KeyManagement/KeyManager.cs
WalletWasabi/KeyManagement/KeyManager.cs
@@ -28,15 +28,17 @@ namespace WalletWasabi.KeyManagement
[JsonProperty(Order = 4)]
private List<HdPubKey> HdPubKeys { get; }
+ private readonly object HdPubKeysLock;
+
private List<byte[]> HdPubKeyScriptBytes { get; }
+ private readonly object HdPubKeyScriptBytesLock;
+
// BIP84-ish derivation scheme
// m / purpose' / coin_type' / account' / change / address_index
// https://github.com/bitcoin/bips/blob/master/bip-0084.mediawiki
private static readonly KeyPath AccountKeyPath = new KeyPath("m/84'/0'/0'");
- private readonly object HdPubKeysLock;
-
public string FilePath { get; private set; }
private object ToFileLock { get; }
@@ -46,6 +48,7 @@ namespace WalletWasabi.KeyManagement
HdPubKeys = new List<HdPubKey>();
HdPubKeyScriptBytes = new List<byte[]>();
HdPubKeysLock = new object();
+ HdPubKeyScriptBytesLock = new object();
EncryptedSecret = Guard.NotNull(nameof(encryptedSecret), encryptedSecret);
ChainCode = Guard.NotNull(nameof(chainCode), chainCode);
@@ -60,6 +63,7 @@ namespace WalletWasabi.KeyManagement
HdPubKeys = new List<HdPubKey>();
HdPubKeyScriptBytes = new List<byte[]>();
HdPubKeysLock = new object();
+ HdPubKeyScriptBytesLock = new object();
if (password is null)
{
@@ -140,7 +144,10 @@ namespace WalletWasabi.KeyManagement
string jsonString = File.ReadAllText(safestFile, Encoding.UTF8);
var km = JsonConvert.DeserializeObject<KeyManager>(jsonString);
km.SetFilePath(filePath);
+ lock (km.HdPubKeyScriptBytesLock)
+ {
km.HdPubKeyScriptBytes.AddRange(km.GetKeys().Select(x => x.GetP2wpkhScript().ToCompressedBytes()));
+ }
return km;
}
@@ -187,7 +194,10 @@ namespace WalletWasabi.KeyManagement
var hdPubKey = new HdPubKey(pubKey, fullPath, label, keyState);
HdPubKeys.Add(hdPubKey);
+ lock (HdPubKeyScriptBytesLock)
+ {
HdPubKeyScriptBytes.Add(hdPubKey.GetP2wpkhScript().ToCompressedBytes());
+ }
if (toFile)
{
@@ -222,7 +232,7 @@ namespace WalletWasabi.KeyManagement
public IEnumerable<byte[]> GetPubKeyScriptBytes()
{
- lock (HdPubKeysLock)
+ lock (HdPubKeyScriptBytesLock)
{
return HdPubKeyScriptBytes;
}

Improve locking