btcpayserver/BTCPayServer/Services/LightningClientFactoryService.cs
Andrew Camilleri 2f23bad3bc
Support the new LN lib (#5422)
* Support the new LN lib

* fix test

* do not cache factories

* try without useless userinfo in lnd

* Remove monero wallet files

* support simpler DI too

---------

Co-authored-by: Dennis Reimann <mail@dennisreimann.de>
2023-11-21 18:55:02 +09:00

50 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using BTCPayServer.Lightning;
namespace BTCPayServer.Services
{
public class LightningClientFactoryService
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IEnumerable<Func<HttpClient, ILightningConnectionStringHandler>>
_lightningConnectionStringHandlersFactories;
private readonly IEnumerable<ILightningConnectionStringHandler> _lightningConnectionStringHandlers;
public LightningClientFactoryService(IHttpClientFactory httpClientFactory,
IEnumerable<Func<HttpClient, ILightningConnectionStringHandler>> lightningConnectionStringHandlersFactories, IEnumerable<ILightningConnectionStringHandler> lightningConnectionStringHandlers)
{
_httpClientFactory = httpClientFactory;
_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);
}
public static string OnionNamedClient { get; set; } = "lightning.onion";
public ILightningClient Create(string lightningConnectionString, BTCPayNetwork network)
{
ArgumentNullException.ThrowIfNull(lightningConnectionString);
ArgumentNullException.ThrowIfNull(network);
var httpClient = lightningConnectionString.Contains(".onion")
? OnionNamedClient
: $"{network.CryptoCode}: Lightning client";
return GetFactory(httpClient, network).Create(lightningConnectionString);
}
}
}