2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2023-11-21 10:55:02 +01:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2019-04-11 05:41:38 +02:00
|
|
|
using System.Net.Http;
|
|
|
|
using BTCPayServer.Lightning;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Services
|
|
|
|
{
|
|
|
|
public class LightningClientFactoryService
|
|
|
|
{
|
|
|
|
private readonly IHttpClientFactory _httpClientFactory;
|
|
|
|
|
2023-11-21 10:55:02 +01:00
|
|
|
private readonly IEnumerable<Func<HttpClient, ILightningConnectionStringHandler>>
|
|
|
|
_lightningConnectionStringHandlersFactories;
|
|
|
|
|
|
|
|
private readonly IEnumerable<ILightningConnectionStringHandler> _lightningConnectionStringHandlers;
|
|
|
|
|
|
|
|
public LightningClientFactoryService(IHttpClientFactory httpClientFactory,
|
|
|
|
IEnumerable<Func<HttpClient, ILightningConnectionStringHandler>> lightningConnectionStringHandlersFactories, IEnumerable<ILightningConnectionStringHandler> lightningConnectionStringHandlers)
|
2019-04-11 05:41:38 +02:00
|
|
|
{
|
|
|
|
_httpClientFactory = httpClientFactory;
|
2023-11-21 10:55:02 +01:00
|
|
|
_lightningConnectionStringHandlersFactories = lightningConnectionStringHandlersFactories;
|
|
|
|
_lightningConnectionStringHandlers = lightningConnectionStringHandlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
private LightningClientFactory GetFactory(string namedClient, BTCPayNetwork network)
|
|
|
|
{
|
|
|
|
var httpClient = _httpClientFactory.CreateClient(namedClient);
|
|
|
|
|
|
|
|
return new LightningClientFactory(_lightningConnectionStringHandlersFactories
|
|
|
|
.Select(handler => handler(httpClient)).Concat(_lightningConnectionStringHandlers)
|
|
|
|
.ToArray(), network.NBitcoinNetwork);
|
2019-04-11 05:41:38 +02:00
|
|
|
}
|
|
|
|
|
2022-06-13 09:22:26 +02:00
|
|
|
public static string OnionNamedClient { get; set; } = "lightning.onion";
|
|
|
|
|
2023-11-21 10:55:02 +01:00
|
|
|
public ILightningClient Create(string lightningConnectionString, BTCPayNetwork network)
|
2019-04-11 05:41:38 +02:00
|
|
|
{
|
2021-12-28 09:39:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(lightningConnectionString);
|
|
|
|
ArgumentNullException.ThrowIfNull(network);
|
2022-06-13 09:22:26 +02:00
|
|
|
|
2023-11-21 10:55:02 +01:00
|
|
|
var httpClient = lightningConnectionString.Contains(".onion")
|
|
|
|
? OnionNamedClient
|
|
|
|
: $"{network.CryptoCode}: Lightning client";
|
|
|
|
|
|
|
|
return GetFactory(httpClient, network).Create(lightningConnectionString);
|
2019-04-11 05:41:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|