Refactror config loading (when entry not set, set default value)
MODIFY
HiddenWallet.Daemon/Config.cs
HiddenWallet.Daemon/Config.cs
@@ -23,20 +23,13 @@ namespace HiddenWallet.Daemon
[JsonProperty(PropertyName = "CanSpendUnconfirmed", Order = 3)]
[JsonConverter(typeof(FunnyBoolConverter))]
- public bool CanSpendUnconfirmed { get; set; }
+ public bool? CanSpendUnconfirmed { get; set; }
public Config()
{
}
- public Config(string walletFilePath, Network network, bool canSpendUnconfirmed)
- {
- WalletFilePath = walletFilePath ?? throw new ArgumentNullException(nameof(walletFilePath));
- Network = network ?? throw new ArgumentNullException(nameof(network));
- CanSpendUnconfirmed = canSpendUnconfirmed;
- }
-
public async Task ToFileAsync(string path, CancellationToken cancel)
{
if (path == null) throw new ArgumentNullException(nameof(path));
@@ -48,16 +41,29 @@ namespace HiddenWallet.Daemon
cancel);
}
- public static async Task<Config> CreateFromFileAsync(string path, CancellationToken cancel)
+ public async Task LoadOrCreateDefaultFileAsync(string path, CancellationToken cancel)
{
if (path == null) throw new ArgumentNullException(nameof(path));
+
+ WalletFilePath = Path.Combine(FullSpvWallet.Global.DataDir, "Wallets", "Wallet.json");
+ Network = Network.Main;
+ CanSpendUnconfirmed = false;
+
if (!File.Exists(path))
{
- throw new ArgumentException($"Config file does not exist at {path}");
+ Console.WriteLine($"Config file did not exist. Created at path: {path}");
}
-
+ else
+ {
string jsonString = await File.ReadAllTextAsync(path, Encoding.UTF8, cancel);
- return JsonConvert.DeserializeObject<Config>(jsonString);
+ var config = JsonConvert.DeserializeObject<Config>(jsonString);
+
+ WalletFilePath = config.WalletFilePath ?? WalletFilePath;
+ Network = config.Network ?? Network;
+ CanSpendUnconfirmed = config.CanSpendUnconfirmed ?? CanSpendUnconfirmed;
+ }
+
+ await ToFileAsync(path, cancel);
}
}
}

MODIFY
HiddenWallet.Daemon/Program.cs
HiddenWallet.Daemon/Program.cs
@@ -20,16 +20,8 @@ namespace HiddenWallet.Daemon
#pragma warning restore IDE1006 // Naming Styles
{
var configFilePath = Path.Combine(FullSpvWallet.Global.DataDir, "Config.json");
- if(File.Exists(configFilePath))
- {
- Global.Config = await Config.CreateFromFileAsync(configFilePath, CancellationToken.None);
- }
- else
- {
- Global.Config = new Config(Path.Combine(FullSpvWallet.Global.DataDir, "Wallets", "Wallet.json"), Network.Main, false);
- await Global.Config.ToFileAsync(configFilePath, CancellationToken.None);
- Console.WriteLine($"Config file did not exist. Created at path: {configFilePath}");
- }
+ Global.Config = new Config();
+ await Global.Config.LoadOrCreateDefaultFileAsync(configFilePath, CancellationToken.None);
var endPoint = "http://localhost:37120/";
var alreadyRunning = false;

MODIFY
HiddenWallet.Daemon/Wrappers/WalletWrapper.cs
HiddenWallet.Daemon/Wrappers/WalletWrapper.cs
@@ -265,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, Global.Config.CanSpendUnconfirmed);
+ var result = await _walletJob.BuildTransactionAsync(address.ScriptPubKey, amount, feeType, safeAccount, (bool)Global.Config.CanSpendUnconfirmed);
if (result.Success)
{

Refactror config loading (when entry not set, set default value)