Order KeyManager
MODIFY
WalletWasabi/Blockchain/Keys/KeyManager.cs
WalletWasabi/Blockchain/Keys/KeyManager.cs
@@ -110,18 +110,18 @@ namespace WalletWasabi.Blockchain.Keys
[JsonConverter(typeof(KeyPathJsonConverter))]
public KeyPath AccountKeyPath { get; private set; }
- [JsonProperty(Order = 8)]
- private BlockchainState BlockchainState { get; }
-
- [JsonProperty(Order = 9)]
- private List<HdPubKey> HdPubKeys { get; }
-
public string FilePath { get; private set; }
public bool IsWatchOnly => EncryptedSecret is null;
public bool IsHardwareWallet => EncryptedSecret is null && MasterFingerprint != null;
+ [JsonProperty(Order = 8)]
+ private BlockchainState BlockchainState { get; }
+
+ [JsonProperty(Order = 9)]
+ private List<HdPubKey> HdPubKeys { get; }
+
private object BlockchainStateLock { get; }
private object HdPubKeysLock { get; }
@@ -180,89 +180,6 @@ namespace WalletWasabi.Blockchain.Keys
return new KeyManager(encryptedSecret, extKey.ChainCode, masterFingerprint, extPubKey, true, minGapLimit, new BlockchainState(), filePath, keyPath);
}
- private void SetMinGapLimit(int? minGapLimit)
- {
- MinGapLimit = minGapLimit is int val ? Math.Max(AbsoluteMinGapLimit, val) : AbsoluteMinGapLimit;
- // AssertCleanKeysIndexed(); Do not do this. Wallet file is null yet.
- }
-
- public void SetFilePath(string filePath)
- {
- FilePath = string.IsNullOrWhiteSpace(filePath) ? null : filePath;
- if (FilePath is null)
- {
- return;
- }
-
- IoHelpers.EnsureContainingDirectoryExists(FilePath);
- }
-
- public void ToFile()
- {
- lock (HdPubKeysLock)
- {
- lock (BlockchainStateLock)
- {
- lock (ToFileLock)
- {
- ToFileNoLock();
- }
- }
- }
- }
-
- private void ToFileNoBlockchainStateLock()
- {
- lock (HdPubKeysLock)
- {
- lock (ToFileLock)
- {
- ToFileNoLock();
- }
- }
- }
-
- public void ToFile(string filePath)
- {
- lock (HdPubKeysLock)
- {
- lock (BlockchainStateLock)
- {
- lock (ToFileLock)
- {
- ToFileNoLock(filePath);
- }
- }
- }
- }
-
- private void ToFileNoLock()
- {
- if (FilePath is null)
- {
- return;
- }
-
- ToFileNoLock(FilePath);
- }
-
- private void ToFileNoLock(string filePath)
- {
- IoHelpers.EnsureContainingDirectoryExists(filePath);
- // Remove the last 100 blocks to ensure verification on the next run. This is needed of reorg.
- int maturity = 101;
- Height prevHeight = BlockchainState.Height;
- int matureHeight = Math.Max(0, prevHeight.Value - maturity);
-
- BlockchainState.Height = new Height(matureHeight);
-
- string jsonString = JsonConvert.SerializeObject(this, Formatting.Indented);
- File.WriteAllText(filePath, jsonString, Encoding.UTF8);
-
- // Re-add removed items for further operations.
- BlockchainState.Height = prevHeight;
- }
-
public static KeyManager FromFile(string filePath)
{
filePath = Guard.NotNullOrEmptyOrWhitespace(nameof(filePath), filePath);
@@ -407,6 +324,45 @@ namespace WalletWasabi.Blockchain.Keys
return true;
}
+ public void SetFilePath(string filePath)
+ {
+ FilePath = string.IsNullOrWhiteSpace(filePath) ? null : filePath;
+ if (FilePath is null)
+ {
+ return;
+ }
+
+ IoHelpers.EnsureContainingDirectoryExists(FilePath);
+ }
+
+ public void ToFile()
+ {
+ lock (HdPubKeysLock)
+ {
+ lock (BlockchainStateLock)
+ {
+ lock (ToFileLock)
+ {
+ ToFileNoLock();
+ }
+ }
+ }
+ }
+
+ public void ToFile(string filePath)
+ {
+ lock (HdPubKeysLock)
+ {
+ lock (BlockchainStateLock)
+ {
+ lock (ToFileLock)
+ {
+ ToFileNoLock(filePath);
+ }
+ }
+ }
+ }
+
public HdPubKey GenerateNewKey(SmartLabel label, KeyState keyState, bool isInternal, bool toFile = true)
{
// BIP44-ish derivation scheme
@@ -701,6 +657,50 @@ namespace WalletWasabi.Blockchain.Keys
return generated;
}
+ private void SetMinGapLimit(int? minGapLimit)
+ {
+ MinGapLimit = minGapLimit is int val ? Math.Max(AbsoluteMinGapLimit, val) : AbsoluteMinGapLimit;
+ // AssertCleanKeysIndexed(); Do not do this. Wallet file is null yet.
+ }
+
+ private void ToFileNoBlockchainStateLock()
+ {
+ lock (HdPubKeysLock)
+ {
+ lock (ToFileLock)
+ {
+ ToFileNoLock();
+ }
+ }
+ }
+
+ private void ToFileNoLock()
+ {
+ if (FilePath is null)
+ {
+ return;
+ }
+
+ ToFileNoLock(FilePath);
+ }
+
+ private void ToFileNoLock(string filePath)
+ {
+ IoHelpers.EnsureContainingDirectoryExists(filePath);
+ // Remove the last 100 blocks to ensure verification on the next run. This is needed of reorg.
+ int maturity = 101;
+ Height prevHeight = BlockchainState.Height;
+ int matureHeight = Math.Max(0, prevHeight.Value - maturity);
+
+ BlockchainState.Height = new Height(matureHeight);
+
+ string jsonString = JsonConvert.SerializeObject(this, Formatting.Indented);
+ File.WriteAllText(filePath, jsonString, Encoding.UTF8);
+
+ // Re-add removed items for further operations.
+ BlockchainState.Height = prevHeight;
+ }
+
#region BlockchainState
public Height GetBestHeight()

Order KeyManager