| | | 1 | | using Microsoft.Extensions.Configuration; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | using Microsoft.Extensions.Hosting; |
| | | 4 | | using Microsoft.Extensions.Logging; |
| | | 5 | | using Microsoft.Extensions.Options; |
| | | 6 | | |
| | | 7 | | namespace NLightning.Daemon.Extensions; |
| | | 8 | | |
| | | 9 | | using Application; |
| | | 10 | | using Contracts.Utilities; |
| | | 11 | | using Daemon.Ipc.Handlers; |
| | | 12 | | using Daemon.Ipc.Interfaces; |
| | | 13 | | using Domain.Bitcoin.Interfaces; |
| | | 14 | | using Domain.Bitcoin.Transactions.Factories; |
| | | 15 | | using Domain.Bitcoin.Transactions.Interfaces; |
| | | 16 | | using Domain.Channels.Factories; |
| | | 17 | | using Domain.Channels.Interfaces; |
| | | 18 | | using Domain.Channels.Validators; |
| | | 19 | | using Domain.Client.Interfaces; |
| | | 20 | | using Domain.Client.Requests; |
| | | 21 | | using Domain.Client.Responses; |
| | | 22 | | using Domain.Crypto.Hashes; |
| | | 23 | | using Domain.Node.Options; |
| | | 24 | | using Domain.Protocol.Interfaces; |
| | | 25 | | using Domain.Protocol.ValueObjects; |
| | | 26 | | using Handlers; |
| | | 27 | | using Infrastructure; |
| | | 28 | | using Infrastructure.Bitcoin; |
| | | 29 | | using Infrastructure.Bitcoin.Builders; |
| | | 30 | | using Infrastructure.Bitcoin.Managers; |
| | | 31 | | using Infrastructure.Bitcoin.Options; |
| | | 32 | | using Infrastructure.Bitcoin.Services; |
| | | 33 | | using Infrastructure.Bitcoin.Signers; |
| | | 34 | | using Infrastructure.Persistence; |
| | | 35 | | using Infrastructure.Repositories; |
| | | 36 | | using Infrastructure.Serialization; |
| | | 37 | | using Interfaces; |
| | | 38 | | using Services; |
| | | 39 | | using Services.Ipc; |
| | | 40 | | |
| | | 41 | | public static class NodeServiceExtensions |
| | | 42 | | { |
| | | 43 | | /// <summary> |
| | | 44 | | /// Registers all NLTG application services for dependency injection |
| | | 45 | | /// </summary> |
| | | 46 | | public static IHostBuilder ConfigureNltgServices(this IHostBuilder hostBuilder, SecureKeyManager secureKeyManager, |
| | | 47 | | string configPath) |
| | | 48 | | { |
| | 0 | 49 | | return hostBuilder.ConfigureServices((hostContext, services) => |
| | 0 | 50 | | { |
| | 0 | 51 | | // Get configuration |
| | 0 | 52 | | var configuration = hostContext.Configuration; |
| | 0 | 53 | | |
| | 0 | 54 | | // Register configuration as a service |
| | 0 | 55 | | services.AddSingleton(configuration); |
| | 0 | 56 | | |
| | 0 | 57 | | // Register the main daemon service |
| | 0 | 58 | | services.AddHostedService<NltgDaemonService>(); |
| | 0 | 59 | | |
| | 0 | 60 | | // Register Client Handlers |
| | 0 | 61 | | services |
| | 0 | 62 | | .AddScoped<IClientCommandHandler<OpenChannelClientRequest, OpenChannelClientResponse>, |
| | 0 | 63 | | OpenChannelClientHandler>(); |
| | 0 | 64 | | services |
| | 0 | 65 | | .AddScoped<IClientCommandHandler<OpenChannelClientSubscriptionRequest, |
| | 0 | 66 | | OpenChannelClientSubscriptionResponse>, |
| | 0 | 67 | | OpenChannelClientSubscriptionHandler>(); |
| | 0 | 68 | | |
| | 0 | 69 | | // Register IPC server and handlers |
| | 0 | 70 | | services.AddSingleton<INamedPipeIpcService>(sp => |
| | 0 | 71 | | { |
| | 0 | 72 | | var ipcAuthenticator = sp.GetRequiredService<IIpcAuthenticator>(); |
| | 0 | 73 | | var ipcFraming = sp.GetRequiredService<IIpcFraming>(); |
| | 0 | 74 | | var logger = sp.GetRequiredService<ILogger<NamedPipeIpcService>>(); |
| | 0 | 75 | | var ipcRequestRouter = sp.GetRequiredService<IIpcRequestRouter>(); |
| | 0 | 76 | | return new NamedPipeIpcService(ipcAuthenticator, configPath, ipcFraming, logger, ipcRequestRouter); |
| | 0 | 77 | | }); |
| | 0 | 78 | | services.AddSingleton<IIpcFraming, LengthPrefixedIpcFraming>(); |
| | 0 | 79 | | services.AddSingleton<IIpcRequestRouter, IpcRequestRouter>(); |
| | 0 | 80 | | services.AddSingleton<INodeInfoQueryService, NodeInfoQueryService>(); |
| | 0 | 81 | | services.AddSingleton<IIpcCommandHandler, NodeInfoIpcHandler>(); |
| | 0 | 82 | | services.AddSingleton<IIpcCommandHandler, ConnectPeerIpcHandler>(); |
| | 0 | 83 | | services.AddSingleton<IIpcCommandHandler, ListPeersIpcHandler>(); |
| | 0 | 84 | | services.AddSingleton<IIpcCommandHandler, GetAddressIpcHandler>(); |
| | 0 | 85 | | services.AddSingleton<IIpcCommandHandler, GetWalletBalanceIpcHandler>(); |
| | 0 | 86 | | services.AddSingleton<IIpcCommandHandler, OpenChannelIpcHandler>(); |
| | 0 | 87 | | services.AddSingleton<IIpcCommandHandler, OpenChannelSubscriptionIpcHandler>(); |
| | 0 | 88 | | services.AddSingleton<IIpcAuthenticator>(sp => |
| | 0 | 89 | | { |
| | 0 | 90 | | var cookiePath = NodeUtils.GetCookieFilePath(configPath); |
| | 0 | 91 | | var logger = sp.GetRequiredService<ILogger<CookieFileAuthenticator>>(); |
| | 0 | 92 | | return new CookieFileAuthenticator(cookiePath, logger); |
| | 0 | 93 | | }); |
| | 0 | 94 | | |
| | 0 | 95 | | // Add HttpClient for FeeService with configuration |
| | 0 | 96 | | services.AddHttpClient<IFeeService, FeeService>(client => |
| | 0 | 97 | | { |
| | 0 | 98 | | client.Timeout = TimeSpan.FromSeconds(30); |
| | 0 | 99 | | client.DefaultRequestHeaders.Add("Accept", "application/json"); |
| | 0 | 100 | | }); |
| | 0 | 101 | | |
| | 0 | 102 | | // Singleton services (one instance throughout the application) |
| | 0 | 103 | | services.AddSingleton<IChannelOpenValidator>(sp => |
| | 0 | 104 | | { |
| | 0 | 105 | | var nodeOptions = sp.GetRequiredService<IOptions<NodeOptions>>().Value; |
| | 0 | 106 | | return new ChannelOpenValidator(nodeOptions); |
| | 0 | 107 | | }); |
| | 0 | 108 | | services.AddSingleton<ISecureKeyManager>(secureKeyManager); |
| | 0 | 109 | | services.AddSingleton<IChannelFactory>(sp => |
| | 0 | 110 | | { |
| | 0 | 111 | | var channelIdFactory = sp.GetRequiredService<IChannelIdFactory>(); |
| | 0 | 112 | | var channelOpenValidator = sp.GetRequiredService<IChannelOpenValidator>(); |
| | 0 | 113 | | var feeService = sp.GetRequiredService<IFeeService>(); |
| | 0 | 114 | | var lightningSigner = sp.GetRequiredService<ILightningSigner>(); |
| | 0 | 115 | | var nodeOptions = sp.GetRequiredService<IOptions<NodeOptions>>().Value; |
| | 0 | 116 | | var sha256 = sp.GetRequiredService<ISha256>(); |
| | 0 | 117 | | return new ChannelFactory(channelIdFactory, channelOpenValidator, feeService, lightningSigner, |
| | 0 | 118 | | nodeOptions, sha256); |
| | 0 | 119 | | }); |
| | 0 | 120 | | services.AddSingleton<ICommitmentTransactionModelFactory, CommitmentTransactionModelFactory>(); |
| | 0 | 121 | | services.AddSingleton<IFundingTransactionModelFactory, FundingTransactionModelFactory>(); |
| | 0 | 122 | | |
| | 0 | 123 | | // Add the Signer |
| | 0 | 124 | | services.AddSingleton<ILightningSigner>(serviceProvider => |
| | 0 | 125 | | { |
| | 0 | 126 | | var fundingOutputBuilder = serviceProvider.GetRequiredService<IFundingOutputBuilder>(); |
| | 0 | 127 | | var keyDerivationService = serviceProvider.GetRequiredService<IKeyDerivationService>(); |
| | 0 | 128 | | var logger = serviceProvider.GetRequiredService<ILogger<LocalLightningSigner>>(); |
| | 0 | 129 | | var nodeOptions = serviceProvider.GetRequiredService<IOptions<NodeOptions>>().Value; |
| | 0 | 130 | | var utxoMemoryRepository = serviceProvider.GetRequiredService<IUtxoMemoryRepository>(); |
| | 0 | 131 | | |
| | 0 | 132 | | // Create the signer with the correct network |
| | 0 | 133 | | return new LocalLightningSigner(fundingOutputBuilder, keyDerivationService, logger, nodeOptions, |
| | 0 | 134 | | secureKeyManager, utxoMemoryRepository); |
| | 0 | 135 | | }); |
| | 0 | 136 | | |
| | 0 | 137 | | // Add the Application services |
| | 0 | 138 | | services.AddApplicationServices(); |
| | 0 | 139 | | |
| | 0 | 140 | | // Add the Infrastructure services |
| | 0 | 141 | | services.AddBitcoinInfrastructure(); |
| | 0 | 142 | | services.AddInfrastructureServices(); |
| | 0 | 143 | | services.AddPersistenceInfrastructureServices(configuration); |
| | 0 | 144 | | services.AddRepositoriesInfrastructureServices(); |
| | 0 | 145 | | services.AddSerializationInfrastructureServices(); |
| | 0 | 146 | | |
| | 0 | 147 | | // Scoped services (one instance per scope) |
| | 0 | 148 | | |
| | 0 | 149 | | // Transient services (new instance each time) |
| | 0 | 150 | | |
| | 0 | 151 | | // Register options with values from configuration |
| | 0 | 152 | | services.AddOptions<BitcoinOptions>().BindConfiguration("Bitcoin").ValidateOnStart(); |
| | 0 | 153 | | services.AddOptions<FeeEstimationOptions>().BindConfiguration("FeeEstimation").ValidateOnStart(); |
| | 0 | 154 | | services.AddOptions<NodeOptions>() |
| | 0 | 155 | | .BindConfiguration("Node") |
| | 0 | 156 | | .PostConfigure(options => |
| | 0 | 157 | | { |
| | 0 | 158 | | var configuredAddresses = configuration.GetSection("Node:ListenAddresses").Get<string[]?>(); |
| | 0 | 159 | | if (configuredAddresses is { Length: > 0 }) |
| | 0 | 160 | | { |
| | 0 | 161 | | options.ListenAddresses = configuredAddresses.ToList(); |
| | 0 | 162 | | } |
| | 0 | 163 | | |
| | 0 | 164 | | var networkString = configuration.GetValue<string>("Node:Network"); |
| | 0 | 165 | | if (!string.IsNullOrWhiteSpace(networkString)) |
| | 0 | 166 | | { |
| | 0 | 167 | | options.BitcoinNetwork = new BitcoinNetwork(networkString); |
| | 0 | 168 | | } |
| | 0 | 169 | | |
| | 0 | 170 | | options.Features.ChainHashes = [options.BitcoinNetwork.ChainHash]; |
| | 0 | 171 | | }) |
| | 0 | 172 | | .ValidateOnStart(); |
| | 0 | 173 | | }); |
| | | 174 | | } |
| | | 175 | | } |