Fix environment folder initialized twice bug
MODIFY
HiddenWallet.ChaumianTumbler/Global.cs
HiddenWallet.ChaumianTumbler/Global.cs
@@ -8,6 +8,8 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
+using System.Net;
+using System.Text;
using System.Threading;
using System.Threading.Tasks;
@@ -15,13 +17,109 @@ namespace HiddenWallet.ChaumianTumbler
{
public static class Global
{
+ public async static Task InitializeAsync()
+ {
+ _dataDir = null;
+ CoinJoinStorePath = Path.Combine(DataDir, "CoinJoins.json");
+ UtxoRefereePath = Path.Combine(DataDir, "BannedUtxos.json");
+
+ string configFilePath = Path.Combine(DataDir, "Config.json");
+ Config = new Config();
+ await Config.LoadOrCreateDefaultFileAsync(configFilePath, CancellationToken.None);
+
+ string rsaPath = Path.Combine(DataDir, "RsaKey.json");
+ if (File.Exists(rsaPath))
+ {
+ string rsaKeyJson = await File.ReadAllTextAsync(rsaPath, Encoding.UTF8);
+ RsaKey = BlindingRsaKey.CreateFromJson(rsaKeyJson);
+ }
+ else
+ {
+ RsaKey = new BlindingRsaKey();
+ await File.WriteAllTextAsync(rsaPath, RsaKey.ToJson(), Encoding.UTF8);
+ Console.WriteLine($"Created RSA key at: {rsaPath}");
+ }
+
+ RpcClient = new RPCClient(
+ credentials: new RPCCredentialString
+ {
+ UserPassword = new NetworkCredential(Config.BitcoinRpcUser, Config.BitcoinRpcPassword)
+ },
+ network: Config.Network);
+ await AssertRpcNodeFullyInitializedAsync();
+
+ if (File.Exists(CoinJoinStorePath))
+ {
+ CoinJoinStore = await CoinJoinStore.CreateFromFileAsync(CoinJoinStorePath);
+ }
+ else
+ {
+ CoinJoinStore = new CoinJoinStore();
+ }
+
+ if (File.Exists(UtxoRefereePath))
+ {
+ UtxoReferee = await UtxoReferee.CreateFromFileAsync(UtxoRefereePath);
+ }
+ else
+ {
+ UtxoReferee = new UtxoReferee();
+ }
+ UtxoRefereeJobCancel = new CancellationTokenSource();
+ UtxoRefereeJob = UtxoReferee.StartAsync(UtxoRefereeJobCancel.Token);
+
+ StateMachine = new TumblerStateMachine();
+ StateMachineJobCancel = new CancellationTokenSource();
+ StateMachineJob = StateMachine.StartAsync(StateMachineJobCancel.Token);
+ }
+
+ private static async Task AssertRpcNodeFullyInitializedAsync()
+ {
+ RPCResponse blockchainInfo = await RpcClient.SendCommandAsync(RPCOperations.getblockchaininfo);
+ try
+ {
+ if (blockchainInfo.Error != null)
+ {
+ throw new NotSupportedException("blockchainInfo.Error != null");
+ }
+ if (blockchainInfo.Result == null)
+ {
+ throw new NotSupportedException("blockchainInfo.Result == null");
+ }
+ int blocks = blockchainInfo.Result.Value<int>("blocks");
+ if (blocks == 0)
+ {
+ throw new NotSupportedException("blocks == 0");
+ }
+ int headers = blockchainInfo.Result.Value<int>("headers");
+ if (headers == 0)
+ {
+ throw new NotSupportedException("headers == 0");
+ }
+ if (blocks != headers)
+ {
+ throw new NotSupportedException("blocks != headers");
+ }
+
+ if (await RpcClient.SendCommandAsync("estimatesmartfee", 1, "ECONOMICAL") == null)
+ {
+ throw new NotSupportedException("estimatesmartfee 1 ECONOMICAL == null");
+ }
+ }
+ catch
+ {
+ Console.WriteLine("Bitcoin Core is not yet fully initialized.");
+ throw;
+ }
+ }
+
public static Config Config;
public static CoinJoinStore CoinJoinStore;
- public static string CoinJoinStorePath = Path.Combine(DataDir, "CoinJoins.json");
+ public static string CoinJoinStorePath;
public static UtxoReferee UtxoReferee;
- public static string UtxoRefereePath = Path.Combine(DataDir, "BannedUtxos.json");
+ public static string UtxoRefereePath;
public static Task UtxoRefereeJob;
public static CancellationTokenSource UtxoRefereeJobCancel;
@@ -33,7 +131,7 @@ namespace HiddenWallet.ChaumianTumbler
public static Task StateMachineJob;
public static CancellationTokenSource StateMachineJobCancel;
- private static string _dataDir = null;
+ private static string _dataDir;
public static string DataDir
{
get

MODIFY
HiddenWallet.ChaumianTumbler/Program.cs
HiddenWallet.ChaumianTumbler/Program.cs
@@ -27,54 +27,7 @@ namespace HiddenWallet.ChaumianTumbler
{
try
{
- string configFilePath = Path.Combine(Global.DataDir, "Config.json");
- Global.Config = new Config();
- await Global.Config.LoadOrCreateDefaultFileAsync(configFilePath, CancellationToken.None);
-
- string rsaPath = Path.Combine(Global.DataDir, "RsaKey.json");
- if (File.Exists(rsaPath))
- {
- string rsaKeyJson = await File.ReadAllTextAsync(rsaPath, Encoding.UTF8);
- Global.RsaKey = BlindingRsaKey.CreateFromJson(rsaKeyJson);
- }
- else
- {
- Global.RsaKey = new BlindingRsaKey();
- await File.WriteAllTextAsync(rsaPath, Global.RsaKey.ToJson(), Encoding.UTF8);
- Console.WriteLine($"Created RSA key at: {rsaPath}");
- }
-
- Global.RpcClient = new RPCClient(
- credentials: new RPCCredentialString
- {
- UserPassword = new NetworkCredential(Global.Config.BitcoinRpcUser, Global.Config.BitcoinRpcPassword)
- },
- network: Global.Config.Network);
- await AssertRpcNodeFullyInitializedAsync();
-
- if(File.Exists(Global.CoinJoinStorePath))
- {
- Global.CoinJoinStore = await CoinJoinStore.CreateFromFileAsync(Global.CoinJoinStorePath);
- }
- else
- {
- Global.CoinJoinStore = new CoinJoinStore();
- }
-
- if (File.Exists(Global.UtxoRefereePath))
- {
- Global.UtxoReferee = await UtxoReferee.CreateFromFileAsync(Global.UtxoRefereePath);
- }
- else
- {
- Global.UtxoReferee = new UtxoReferee();
- }
- Global.UtxoRefereeJobCancel = new CancellationTokenSource();
- Global.UtxoRefereeJob = Global.UtxoReferee.StartAsync(Global.UtxoRefereeJobCancel.Token);
-
- Global.StateMachine = new TumblerStateMachine();
- Global.StateMachineJobCancel = new CancellationTokenSource();
- Global.StateMachineJob = Global.StateMachine.StartAsync(Global.StateMachineJobCancel.Token);
+ await Global.InitializeAsync();
using (var host = WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
@@ -90,45 +43,5 @@ namespace HiddenWallet.ChaumianTumbler
Console.ReadKey();
}
}
-
- private static async Task AssertRpcNodeFullyInitializedAsync()
- {
- RPCResponse blockchainInfo = await Global.RpcClient.SendCommandAsync(RPCOperations.getblockchaininfo);
- try
- {
- if (blockchainInfo.Error != null)
- {
- throw new NotSupportedException("blockchainInfo.Error != null");
- }
- if (blockchainInfo.Result == null)
- {
- throw new NotSupportedException("blockchainInfo.Result == null");
- }
- int blocks = blockchainInfo.Result.Value<int>("blocks");
- if (blocks == 0)
- {
- throw new NotSupportedException("blocks == 0");
- }
- int headers = blockchainInfo.Result.Value<int>("headers");
- if (headers == 0)
- {
- throw new NotSupportedException("headers == 0");
- }
- if (blocks != headers)
- {
- throw new NotSupportedException("blocks != headers");
- }
-
- if (await Global.RpcClient.SendCommandAsync("estimatesmartfee", 1, "ECONOMICAL") == null)
- {
- throw new NotSupportedException("estimatesmartfee 1 ECONOMICAL == null");
- }
- }
- catch
- {
- Console.WriteLine("Bitcoin Core is not yet fully initialized.");
- throw;
- }
- }
}
}

Fix environment folder initialized twice bug