Refactor Config.cs serialization
MODIFY
HiddenWallet.Daemon/Global.cs
HiddenWallet.Daemon/Global.cs
@@ -10,5 +10,6 @@ namespace HiddenWallet.Daemon
public static class Global
{
public static WalletWrapper WalletWrapper;
+ public static Config Config;
}
}

MODIFY
HiddenWallet.Daemon/Program.cs
HiddenWallet.Daemon/Program.cs
@@ -19,7 +19,18 @@ namespace HiddenWallet.Daemon
public static async Task Main(string[] args)
#pragma warning restore IDE1006 // Naming Styles
{
- await Config.InitializeAsync();
+ var configFilePath = "Config.json";
+ if(File.Exists(configFilePath))
+ {
+ Global.Config = await Config.CreateFromFileAsync(configFilePath, CancellationToken.None);
+ }
+ else
+ {
+ Global.Config = new Config(Path.Combine("Wallets", "Wallet.json"), Network.Main, false);
+ await Global.Config.ToFileAsync(configFilePath, CancellationToken.None);
+ Console.WriteLine($"Config file did not exist. Created at path: {configFilePath}");
+ }
+
var endPoint = "http://localhost:37120/";
var alreadyRunning = false;
using (var client = new HttpClient())
@@ -73,7 +84,6 @@ namespace HiddenWallet.Daemon
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Global.WalletWrapper = new WalletWrapper();
- await Global.WalletWrapper.InitializeAsync();
var host = new WebHostBuilder()
.UseKestrel()

MODIFY
HiddenWallet.Daemon/Wrappers/WalletWrapper.cs
HiddenWallet.Daemon/Wrappers/WalletWrapper.cs
@@ -33,7 +33,7 @@ namespace HiddenWallet.Daemon.Wrappers
private readonly CancellationTokenSource _cts = new CancellationTokenSource();
private Task _walletJobTask = Task.CompletedTask;
- public bool WalletExists => File.Exists(Config.WalletFilePath);
+ public bool WalletExists => File.Exists(Global.Config.WalletFilePath);
public bool IsDecrypted => !_walletJobTask.IsCompleted && _password != null;
public Network Network => _walletJob.Safe.Network;
@@ -60,17 +60,10 @@ namespace HiddenWallet.Daemon.Wrappers
}
- public async Task InitializeAsync()
- {
- // Loads the config file
- // It also creates it with default settings if doesn't exist
- await Config.LoadAsync();
- }
-
#region SafeOperations
public async Task<WalletCreateResponse> CreateAsync(string password)
{
- var result = await Safe.CreateAsync(password, Config.WalletFilePath, Config.Network);
+ var result = await Safe.CreateAsync(password, Global.Config.WalletFilePath, Global.Config.Network);
return new WalletCreateResponse
{
Mnemonic = result.Mnemonic.ToString(),
@@ -80,8 +73,8 @@ namespace HiddenWallet.Daemon.Wrappers
public async Task LoadAsync(string password)
{
- Safe safe = await Safe.LoadAsync(password, Config.WalletFilePath);
- if (safe.Network != Config.Network) throw new NotSupportedException("Network in the config file differs from the netwrok in the wallet file");
+ Safe safe = await Safe.LoadAsync(password, Global.Config.WalletFilePath);
+ if (safe.Network != Global.Config.Network) throw new NotSupportedException("Network in the config file differs from the network in the wallet file");
if (!_walletJobTask.IsCompleted)
{
@@ -113,8 +106,8 @@ namespace HiddenWallet.Daemon.Wrappers
await Safe.RecoverAsync(
new Mnemonic(mnemonic),
password,
- Config.WalletFilePath,
- Config.Network,
+ Global.Config.WalletFilePath,
+ Global.Config.Network,
DateTimeOffset.ParseExact(creationTime, "yyyy-MM-dd", CultureInfo.InvariantCulture));
}
@@ -272,7 +265,7 @@ namespace HiddenWallet.Daemon.Wrappers
public async Task<BaseResponse> BuildTransactionAsync(string password, SafeAccount safeAccount, BitcoinAddress address, Money amount, FeeType feeType)
{
if (password != _password) throw new InvalidOperationException("Wrong password");
- var result = await _walletJob.BuildTransactionAsync(address.ScriptPubKey, amount, feeType, safeAccount, Config.CanSpendUnconfirmed);
+ var result = await _walletJob.BuildTransactionAsync(address.ScriptPubKey, amount, feeType, safeAccount, Global.Config.CanSpendUnconfirmed);
if (result.Success)
{

Refactor Config.cs serialization