| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.Hosting; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | using NBitcoin; |
| | | 6 | | using NLightning.Domain.Node.Options; |
| | | 7 | | using NLightning.Domain.Protocol.ValueObjects; |
| | | 8 | | using NLightning.Infrastructure.Bitcoin.Managers; |
| | | 9 | | using NLightning.Infrastructure.Bitcoin.Options; |
| | | 10 | | using NLightning.Infrastructure.Bitcoin.Wallet; |
| | | 11 | | using NLightning.Node.Extensions; |
| | | 12 | | using NLightning.Node.Helpers; |
| | | 13 | | using NLightning.Node.Utilities; |
| | | 14 | | using Serilog; |
| | | 15 | | |
| | | 16 | | try |
| | | 17 | | { |
| | | 18 | | // Bootstrap logger for startup messages |
| | 0 | 19 | | Log.Logger = new LoggerConfiguration() |
| | 0 | 20 | | .WriteTo.Console() |
| | 0 | 21 | | .CreateBootstrapLogger(); |
| | | 22 | | |
| | 0 | 23 | | AppDomain.CurrentDomain.UnhandledException += (_, e) => |
| | 0 | 24 | | { |
| | 0 | 25 | | var exception = (Exception)e.ExceptionObject; |
| | 0 | 26 | | Log.Logger.Error("An unhandled exception occurred: {exception}", exception); |
| | 0 | 27 | | }; |
| | | 28 | | |
| | | 29 | | // Get network for the PID file path |
| | 0 | 30 | | var network = CommandLineHelper.GetNetwork(args); |
| | 0 | 31 | | var pidFilePath = DaemonUtils.GetPidFilePath(network); |
| | | 32 | | |
| | | 33 | | // Check for the stop command |
| | 0 | 34 | | if (CommandLineHelper.IsStopRequested(args)) |
| | | 35 | | { |
| | 0 | 36 | | var stopped = DaemonUtils.StopDaemon(pidFilePath, Log.Logger); |
| | 0 | 37 | | return stopped ? 0 : 1; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | // Check for status command |
| | 0 | 41 | | if (CommandLineHelper.IsStatusRequested(args)) |
| | | 42 | | { |
| | 0 | 43 | | ReportDaemonStatus(pidFilePath); |
| | 0 | 44 | | return 0; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | // Check if help is requested |
| | 0 | 48 | | if (CommandLineHelper.IsHelpRequested(args)) |
| | | 49 | | { |
| | 0 | 50 | | CommandLineHelper.ShowUsage(); |
| | 0 | 51 | | return 0; |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // Read the configuration file to check for daemon setting |
| | 0 | 55 | | var initialConfig = NodeConfigurationExtensions.ReadInitialConfiguration(args); |
| | | 56 | | |
| | 0 | 57 | | string? password = null; |
| | | 58 | | |
| | | 59 | | // Try to get password from args or prompt |
| | 0 | 60 | | if (args.Contains("--password")) |
| | | 61 | | { |
| | 0 | 62 | | var idx = Array.IndexOf(args, "--password"); |
| | 0 | 63 | | if (idx >= 0 && idx + 1 < args.Length) |
| | 0 | 64 | | password = args[idx + 1]; |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | if (string.IsNullOrWhiteSpace(password)) |
| | | 68 | | { |
| | 0 | 69 | | password = ConsoleUtils.ReadPassword("Enter password for key encryption: "); |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | if (string.IsNullOrWhiteSpace(password)) |
| | | 73 | | { |
| | 0 | 74 | | Log.Error("Password cannot be empty."); |
| | 0 | 75 | | return 1; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | SecureKeyManager keyManager; |
| | 0 | 79 | | var keyFilePath = SecureKeyManager.GetKeyFilePath(network); |
| | 0 | 80 | | if (!File.Exists(keyFilePath)) |
| | | 81 | | { |
| | | 82 | | // Get current Block Height for key birth |
| | | 83 | | try |
| | | 84 | | { |
| | | 85 | | // Create logger for the wallet service using Serilog |
| | 0 | 86 | | var loggerFactory = LoggerFactory.Create(b => b.AddSerilog(Log.Logger, dispose: false)); |
| | 0 | 87 | | var walletLogger = loggerFactory.CreateLogger<BitcoinWalletService>(); |
| | | 88 | | |
| | | 89 | | // Bind options from initialConfig |
| | 0 | 90 | | var bitcoinOptions = initialConfig.GetSection("Bitcoin").Get<BitcoinOptions>() |
| | 0 | 91 | | ?? throw new InvalidOperationException( |
| | 0 | 92 | | "Bitcoin configuration section is missing or invalid."); |
| | 0 | 93 | | var nodeOptions = initialConfig.GetSection("Node").Get<NodeOptions>() |
| | 0 | 94 | | ?? throw new InvalidOperationException("Node configuration section is missing or invalid."); |
| | | 95 | | |
| | | 96 | | // Instantiate the service |
| | 0 | 97 | | var bitcoinWalletService = new BitcoinWalletService( |
| | 0 | 98 | | Options.Create(bitcoinOptions), |
| | 0 | 99 | | walletLogger, |
| | 0 | 100 | | Options.Create(nodeOptions) |
| | 0 | 101 | | ); |
| | | 102 | | |
| | 0 | 103 | | var heightOfBirth = await bitcoinWalletService.GetCurrentBlockHeightAsync(); |
| | | 104 | | |
| | | 105 | | // Creates new key |
| | 0 | 106 | | var key = new Key(); |
| | 0 | 107 | | keyManager = new SecureKeyManager(key.ToBytes(), new BitcoinNetwork(network), keyFilePath, heightOfBirth); |
| | 0 | 108 | | keyManager.SaveToFile(password); |
| | 0 | 109 | | Console.WriteLine($"New key created and saved to {keyFilePath}"); |
| | 0 | 110 | | } |
| | 0 | 111 | | catch (Exception e) |
| | | 112 | | { |
| | 0 | 113 | | Log.Logger.Error(e, "An error occurred while creating new key."); |
| | 0 | 114 | | return 1; |
| | | 115 | | } |
| | | 116 | | } |
| | | 117 | | else |
| | | 118 | | { |
| | | 119 | | // Load the existing key |
| | 0 | 120 | | keyManager = SecureKeyManager.FromFilePath(keyFilePath, new BitcoinNetwork(network), password); |
| | 0 | 121 | | Console.WriteLine($"Loaded key from {keyFilePath}"); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | // Start as a daemon if requested |
| | 0 | 125 | | if (DaemonUtils.StartDaemonIfRequested(args, initialConfig, pidFilePath, Log.Logger)) |
| | | 126 | | { |
| | | 127 | | // The parent process exits immediately after starting the daemon |
| | 0 | 128 | | return 0; |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | Log.Information("Starting NLTG..."); |
| | | 132 | | |
| | | 133 | | // Create and run host |
| | 0 | 134 | | var host = Host.CreateDefaultBuilder(args) |
| | 0 | 135 | | .ConfigureNltg(initialConfig) |
| | 0 | 136 | | .ConfigureNltgServices(keyManager) |
| | 0 | 137 | | .Build(); |
| | | 138 | | |
| | | 139 | | // Run migrations if configured |
| | 0 | 140 | | await host.MigrateDatabaseIfConfiguredAsync(); |
| | | 141 | | |
| | | 142 | | // Run the host |
| | 0 | 143 | | await host.RunAsync(); |
| | | 144 | | |
| | 0 | 145 | | return 0; |
| | | 146 | | } |
| | | 147 | | catch (Exception e) |
| | | 148 | | { |
| | 0 | 149 | | Log.Fatal(e, "Application terminated unexpectedly"); |
| | 0 | 150 | | return 1; |
| | | 151 | | } |
| | | 152 | | finally |
| | | 153 | | { |
| | 0 | 154 | | Log.CloseAndFlush(); |
| | | 155 | | } |
| | | 156 | | |
| | | 157 | | static void ReportDaemonStatus(string pidFilePath) |
| | | 158 | | { |
| | | 159 | | try |
| | | 160 | | { |
| | 0 | 161 | | if (!File.Exists(pidFilePath)) |
| | | 162 | | { |
| | 0 | 163 | | Console.WriteLine("NLTG daemon is not running"); |
| | 0 | 164 | | return; |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | var pidText = File.ReadAllText(pidFilePath).Trim(); |
| | 0 | 168 | | if (!int.TryParse(pidText, out var pid)) |
| | | 169 | | { |
| | 0 | 170 | | Console.WriteLine("Invalid PID in file, daemon may not be running"); |
| | 0 | 171 | | return; |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | try |
| | | 175 | | { |
| | 0 | 176 | | var process = System.Diagnostics.Process.GetProcessById(pid); |
| | 0 | 177 | | var runTime = DateTime.Now - process.StartTime; |
| | | 178 | | |
| | 0 | 179 | | Console.WriteLine("NLTG daemon is running"); |
| | 0 | 180 | | Console.WriteLine($"PID: {pid}"); |
| | 0 | 181 | | Console.WriteLine($"Started: {process.StartTime}"); |
| | 0 | 182 | | Console.WriteLine($"Uptime: {runTime.Days}d {runTime.Hours}h {runTime.Minutes}m"); |
| | 0 | 183 | | Console.WriteLine($"Memory: {process.WorkingSet64 / (1024 * 1024)} MB"); |
| | 0 | 184 | | } |
| | 0 | 185 | | catch (ArgumentException) |
| | | 186 | | { |
| | 0 | 187 | | Console.WriteLine("NLTG daemon is not running (stale PID file)"); |
| | 0 | 188 | | } |
| | 0 | 189 | | } |
| | 0 | 190 | | catch (Exception e) |
| | | 191 | | { |
| | 0 | 192 | | Console.WriteLine($"Error checking daemon status: {e.Message}"); |
| | 0 | 193 | | } |
| | 0 | 194 | | } |