| | 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 | | using NLightning.Application; |
| | 7 | | using NLightning.Domain.Bitcoin.Interfaces; |
| | 8 | | using NLightning.Domain.Bitcoin.Transactions.Factories; |
| | 9 | | using NLightning.Domain.Bitcoin.Transactions.Interfaces; |
| | 10 | | using NLightning.Domain.Channels.Factories; |
| | 11 | | using NLightning.Domain.Channels.Interfaces; |
| | 12 | | using NLightning.Domain.Crypto.Hashes; |
| | 13 | | using NLightning.Domain.Protocol.Interfaces; |
| | 14 | | using NLightning.Domain.Protocol.ValueObjects; |
| | 15 | | using NLightning.Infrastructure; |
| | 16 | | using NLightning.Infrastructure.Bitcoin; |
| | 17 | | using NLightning.Infrastructure.Bitcoin.Builders; |
| | 18 | | using NLightning.Infrastructure.Bitcoin.Managers; |
| | 19 | | using NLightning.Infrastructure.Bitcoin.Options; |
| | 20 | | using NLightning.Infrastructure.Bitcoin.Services; |
| | 21 | | using NLightning.Infrastructure.Bitcoin.Signers; |
| | 22 | | using NLightning.Infrastructure.Persistence; |
| | 23 | | using NLightning.Infrastructure.Repositories; |
| | 24 | | using NLightning.Infrastructure.Serialization; |
| | 25 | |
|
| | 26 | | namespace NLightning.Node.Extensions; |
| | 27 | |
|
| | 28 | | using Domain.Node.Options; |
| | 29 | | using Services; |
| | 30 | |
|
| | 31 | | public static class NodeServiceExtensions |
| | 32 | | { |
| | 33 | | /// <summary> |
| | 34 | | /// Registers all NLTG application services for dependency injection |
| | 35 | | /// </summary> |
| | 36 | | public static IHostBuilder ConfigureNltgServices(this IHostBuilder hostBuilder, SecureKeyManager secureKeyManager) |
| | 37 | | { |
| 0 | 38 | | return hostBuilder.ConfigureServices((hostContext, services) => |
| 0 | 39 | | { |
| 0 | 40 | | // Get configuration |
| 0 | 41 | | var configuration = hostContext.Configuration; |
| 0 | 42 | |
|
| 0 | 43 | | // Register configuration as a service |
| 0 | 44 | | services.AddSingleton(configuration); |
| 0 | 45 | |
|
| 0 | 46 | | // Register the main daemon service |
| 0 | 47 | | services.AddHostedService<NltgDaemonService>(); |
| 0 | 48 | |
|
| 0 | 49 | | // Add HttpClient for FeeService with configuration |
| 0 | 50 | | services.AddHttpClient<IFeeService, FeeService>(client => |
| 0 | 51 | | { |
| 0 | 52 | | client.Timeout = TimeSpan.FromSeconds(30); |
| 0 | 53 | | client.DefaultRequestHeaders.Add("Accept", "application/json"); |
| 0 | 54 | | }); |
| 0 | 55 | |
|
| 0 | 56 | | // Singleton services (one instance throughout the application) |
| 0 | 57 | | services.AddSingleton<ISecureKeyManager>(secureKeyManager); |
| 0 | 58 | | services.AddSingleton<IChannelFactory>(sp => |
| 0 | 59 | | { |
| 0 | 60 | | var feeService = sp.GetRequiredService<IFeeService>(); |
| 0 | 61 | | var lightningSigner = sp.GetRequiredService<ILightningSigner>(); |
| 0 | 62 | | var nodeOptions = sp.GetRequiredService<IOptions<NodeOptions>>().Value; |
| 0 | 63 | | var sha256 = sp.GetRequiredService<ISha256>(); |
| 0 | 64 | | return new ChannelFactory(feeService, lightningSigner, nodeOptions, sha256); |
| 0 | 65 | | }); |
| 0 | 66 | | services.AddSingleton<ICommitmentTransactionModelFactory, CommitmentTransactionModelFactory>(); |
| 0 | 67 | |
|
| 0 | 68 | | // Add the Signer |
| 0 | 69 | | services.AddSingleton<ILightningSigner>(serviceProvider => |
| 0 | 70 | | { |
| 0 | 71 | | var fundingOutputBuilder = serviceProvider.GetRequiredService<IFundingOutputBuilder>(); |
| 0 | 72 | | var keyDerivationService = serviceProvider.GetRequiredService<IKeyDerivationService>(); |
| 0 | 73 | | var logger = serviceProvider.GetRequiredService<ILogger<LocalLightningSigner>>(); |
| 0 | 74 | | var nodeOptions = serviceProvider.GetRequiredService<IOptions<NodeOptions>>().Value; |
| 0 | 75 | |
|
| 0 | 76 | | // Create the signer with the correct network |
| 0 | 77 | | return new LocalLightningSigner(fundingOutputBuilder, keyDerivationService, logger, nodeOptions, |
| 0 | 78 | | secureKeyManager); |
| 0 | 79 | | }); |
| 0 | 80 | |
|
| 0 | 81 | | // Add the Application services |
| 0 | 82 | | services.AddApplicationServices(); |
| 0 | 83 | |
|
| 0 | 84 | | // Add the Infrastructure services |
| 0 | 85 | | services.AddBitcoinInfrastructure(); |
| 0 | 86 | | services.AddInfrastructureServices(); |
| 0 | 87 | | services.AddPersistenceInfrastructureServices(configuration); |
| 0 | 88 | | services.AddRepositoriesInfrastructureServices(); |
| 0 | 89 | | services.AddSerializationInfrastructureServices(); |
| 0 | 90 | |
|
| 0 | 91 | | // Scoped services (one instance per scope) |
| 0 | 92 | |
|
| 0 | 93 | | // Transient services (new instance each time) |
| 0 | 94 | |
|
| 0 | 95 | | // Register options with values from configuration |
| 0 | 96 | | services.AddOptions<BitcoinOptions>().BindConfiguration("Bitcoin").ValidateOnStart(); |
| 0 | 97 | | services.AddOptions<FeeEstimationOptions>().BindConfiguration("FeeEstimation").ValidateOnStart(); |
| 0 | 98 | | services.AddOptions<NodeOptions>() |
| 0 | 99 | | .BindConfiguration("Node") |
| 0 | 100 | | .PostConfigure(options => |
| 0 | 101 | | { |
| 0 | 102 | | var configuredAddresses = configuration.GetSection("Node:ListenAddresses").Get<string[]?>(); |
| 0 | 103 | | if (configuredAddresses is { Length: > 0 }) |
| 0 | 104 | | { |
| 0 | 105 | | options.ListenAddresses = configuredAddresses.ToList(); |
| 0 | 106 | | } |
| 0 | 107 | |
|
| 0 | 108 | | var networkString = configuration.GetValue<string>("Node:Network"); |
| 0 | 109 | | if (!string.IsNullOrWhiteSpace(networkString)) |
| 0 | 110 | | { |
| 0 | 111 | | options.BitcoinNetwork = new BitcoinNetwork(networkString); |
| 0 | 112 | | } |
| 0 | 113 | |
|
| 0 | 114 | | options.Features.ChainHashes = [options.BitcoinNetwork.ChainHash]; |
| 0 | 115 | | }) |
| 0 | 116 | | .ValidateOnStart(); |
| 0 | 117 | | }); |
| | 118 | | } |
| | 119 | | } |