Split Options in BTCPayOptions (#2175)

* Split Options in BTCPayOptions

* fix spacing
This commit is contained in:
Andrew Camilleri
2021-01-02 13:44:28 +01:00
committed by GitHub
parent 2c87100ffb
commit aaf85216eb
17 changed files with 261 additions and 159 deletions

View File

@@ -50,20 +50,16 @@ namespace BTCPayServer.Tests
[Trait("Fast", "Fast")] [Trait("Fast", "Fast")]
public void LoadSubChainsAlways() public void LoadSubChainsAlways()
{ {
var options = new BTCPayServerOptions(); var config = new ConfigurationRoot(new List<IConfigurationProvider>()
options.LoadArgs(new ConfigurationRoot(new List<IConfigurationProvider>()
{ {
new MemoryConfigurationProvider(new MemoryConfigurationSource() new MemoryConfigurationProvider(new MemoryConfigurationSource()
{ {
InitialData = new[] InitialData = new[] {new KeyValuePair<string, string>("chains", "usdt"),}
{
new KeyValuePair<string, string>("chains", "usdt"),
}
}) })
})); });
var networkProvider = config.ConfigureNetworkProvider();
Assert.NotNull(options.NetworkProvider.GetNetwork("LBTC")); Assert.NotNull(networkProvider.GetNetwork("LBTC"));
Assert.NotNull(options.NetworkProvider.GetNetwork("USDT")); Assert.NotNull(networkProvider.GetNetwork("USDT"));
} }

View File

@@ -28,17 +28,17 @@ namespace BTCPayServer.Tests
[Trait("Altcoins", "Altcoins")] [Trait("Altcoins", "Altcoins")]
public void LoadSubChainsAlways() public void LoadSubChainsAlways()
{ {
var options = new BTCPayServerOptions(); var config = new ConfigurationRoot(new List<IConfigurationProvider>()
options.LoadArgs(new ConfigurationRoot(new List<IConfigurationProvider>()
{ {
new MemoryConfigurationProvider(new MemoryConfigurationSource() new MemoryConfigurationProvider(new MemoryConfigurationSource()
{ {
InitialData = new[] {new KeyValuePair<string, string>("chains", "usdt20"),} InitialData = new[] {new KeyValuePair<string, string>("chains", "usdt20"),}
}) })
})); });
Assert.NotNull(options.NetworkProvider.GetNetwork("ETH")); var networkProvider = config.ConfigureNetworkProvider();
Assert.NotNull(options.NetworkProvider.GetNetwork("USDT20")); Assert.NotNull(networkProvider.GetNetwork("ETH"));
Assert.NotNull(networkProvider.GetNetwork("USDT20"));
} }
[Fact] [Fact]

View File

@@ -1,26 +1,16 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using BTCPayServer.Lightning;
using BTCPayServer.Logging; using BTCPayServer.Logging;
using BTCPayServer.SSH; using BTCPayServer.SSH;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using NBitcoin; using NBitcoin;
using Serilog.Events; using Serilog.Events;
using TwentyTwenty.Storage;
namespace BTCPayServer.Configuration namespace BTCPayServer.Configuration
{ {
public class NBXplorerConnectionSetting
{
public string CryptoCode { get; internal set; }
public Uri ExplorerUri { get; internal set; }
public string CookieFile { get; internal set; }
}
public class BTCPayServerOptions public class BTCPayServerOptions
{ {
public NetworkType NetworkType public NetworkType NetworkType
@@ -40,11 +30,6 @@ namespace BTCPayServer.Configuration
} }
public EndPoint SocksEndpoint { get; set; } public EndPoint SocksEndpoint { get; set; }
public List<NBXplorerConnectionSetting> NBXplorerConnectionSettings
{
get;
set;
} = new List<NBXplorerConnectionSetting>();
public bool DisableRegistration public bool DisableRegistration
{ {
@@ -79,83 +64,7 @@ namespace BTCPayServer.Configuration
if (conf.GetOrDefault<bool>("launchsettings", false) && NetworkType != NetworkType.Regtest) if (conf.GetOrDefault<bool>("launchsettings", false) && NetworkType != NetworkType.Regtest)
throw new ConfigException($"You need to run BTCPayServer with the run.sh or run.ps1 script"); throw new ConfigException($"You need to run BTCPayServer with the run.sh or run.ps1 script");
var supportedChains = conf.GetOrDefault<string>("chains", "btc")
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.ToUpperInvariant()).ToHashSet();
var networkProvider = new BTCPayNetworkProvider(NetworkType);
var filtered = networkProvider.Filter(supportedChains.ToArray());
#if ALTCOINS
supportedChains.AddRange(filtered.GetAllElementsSubChains(networkProvider));
supportedChains.AddRange(filtered.GetAllEthereumSubChains(networkProvider));
#endif
#if !ALTCOINS
var onlyBTC = supportedChains.Count == 1 && supportedChains.First() == "BTC";
if (!onlyBTC)
throw new ConfigException($"This build of BTCPay Server does not support altcoins");
#endif
NetworkProvider = networkProvider.Filter(supportedChains.ToArray());
foreach (var chain in supportedChains)
{
if (NetworkProvider.GetNetwork<BTCPayNetworkBase>(chain) == null)
throw new ConfigException($"Invalid chains \"{chain}\"");
}
var validChains = new List<string>();
foreach (var net in NetworkProvider.GetAll().OfType<BTCPayNetwork>())
{
NBXplorerConnectionSetting setting = new NBXplorerConnectionSetting();
setting.CryptoCode = net.CryptoCode;
setting.ExplorerUri = conf.GetOrDefault<Uri>($"{net.CryptoCode}.explorer.url", net.NBXplorerNetwork.DefaultSettings.DefaultUrl);
setting.CookieFile = conf.GetOrDefault<string>($"{net.CryptoCode}.explorer.cookiefile", net.NBXplorerNetwork.DefaultSettings.DefaultCookieFile);
NBXplorerConnectionSettings.Add(setting);
{
var lightning = conf.GetOrDefault<string>($"{net.CryptoCode}.lightning", string.Empty);
if (lightning.Length != 0)
{
if (!LightningConnectionString.TryParse(lightning, true, out var connectionString, out var error))
{
Logs.Configuration.LogWarning($"Invalid setting {net.CryptoCode}.lightning, " + Environment.NewLine +
$"If you have a c-lightning server use: 'type=clightning;server=/root/.lightning/lightning-rpc', " + Environment.NewLine +
$"If you have a lightning charge server: 'type=charge;server=https://charge.example.com;api-token=yourapitoken'" + Environment.NewLine +
$"If you have a lnd server: 'type=lnd-rest;server=https://lnd:lnd@lnd.example.com;macaroon=abf239...;certthumbprint=2abdf302...'" + Environment.NewLine +
$" lnd server: 'type=lnd-rest;server=https://lnd:lnd@lnd.example.com;macaroonfilepath=/root/.lnd/admin.macaroon;certthumbprint=2abdf302...'" + Environment.NewLine +
$"If you have an eclair server: 'type=eclair;server=http://eclair.com:4570;password=eclairpassword;bitcoin-host=bitcoind:37393;bitcoin-auth=bitcoinrpcuser:bitcoinrpcpassword" + Environment.NewLine +
$" eclair server: 'type=eclair;server=http://eclair.com:4570;password=eclairpassword;bitcoin-host=bitcoind:37393" + Environment.NewLine +
$"Error: {error}" + Environment.NewLine +
"This service will not be exposed through BTCPay Server");
}
else
{
if (connectionString.IsLegacy)
{
Logs.Configuration.LogWarning($"Setting {net.CryptoCode}.lightning is a deprecated format, it will work now, but please replace it for future versions with '{connectionString.ToString()}'");
}
InternalLightningByCryptoCode.Add(net.CryptoCode, connectionString);
}
}
}
ExternalServices.Load(net.CryptoCode, conf);
}
ExternalServices.LoadNonCryptoServices(conf);
Logs.Configuration.LogInformation("Supported chains: " + String.Join(',', supportedChains.ToArray()));
var services = conf.GetOrDefault<string>("externalservices", null);
if (services != null)
{
foreach (var service in services.Split(new[] { ';', ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(p => (p, SeparatorIndex: p.IndexOf(':', StringComparison.OrdinalIgnoreCase)))
.Where(p => p.SeparatorIndex != -1)
.Select(p => (Name: p.p.Substring(0, p.SeparatorIndex),
Link: p.p.Substring(p.SeparatorIndex + 1))))
{
if (Uri.TryCreate(service.Link, UriKind.RelativeOrAbsolute, out var uri))
OtherExternalServices.AddOrReplace(service.Name, uri);
}
}
BundleJsCss = conf.GetOrDefault<bool>("bundlejscss", true); BundleJsCss = conf.GetOrDefault<bool>("bundlejscss", true);
DockerDeployment = conf.GetOrDefault<bool>("dockerdeployment", true); DockerDeployment = conf.GetOrDefault<bool>("dockerdeployment", true);
@@ -273,12 +182,6 @@ namespace BTCPayServer.Configuration
} }
public string RootPath { get; set; } public string RootPath { get; set; }
public Dictionary<string, LightningConnectionString> InternalLightningByCryptoCode { get; set; } = new Dictionary<string, LightningConnectionString>();
public Dictionary<string, Uri> OtherExternalServices { get; set; } = new Dictionary<string, Uri>();
public ExternalServices ExternalServices { get; set; } = new ExternalServices();
public BTCPayNetworkProvider NetworkProvider { get; set; }
public bool DockerDeployment { get; set; } public bool DockerDeployment { get; set; }
public bool BundleJsCss public bool BundleJsCss
{ {

View File

@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
using NBitcoin;
namespace BTCPayServer.Configuration
{
public class ExternalServicesOptions
{
public Dictionary<string, Uri> OtherExternalServices { get; set; } = new Dictionary<string, Uri>();
public ExternalServices ExternalServices { get; set; } = new ExternalServices();
public void Configure(IConfiguration configuration, BTCPayNetworkProvider btcPayNetworkProvider)
{
foreach (var net in btcPayNetworkProvider.GetAll().OfType<BTCPayNetwork>())
{
ExternalServices.Load(net.CryptoCode, configuration);
}
ExternalServices.LoadNonCryptoServices(configuration);
var services = configuration.GetOrDefault<string>("externalservices", null);
if (services != null)
{
foreach (var service in services.Split(new[] {';', ','}, StringSplitOptions.RemoveEmptyEntries)
.Select(p => (p, SeparatorIndex: p.IndexOf(':', StringComparison.OrdinalIgnoreCase)))
.Where(p => p.SeparatorIndex != -1)
.Select(p => (Name: p.p.Substring(0, p.SeparatorIndex),
Link: p.p.Substring(p.SeparatorIndex + 1))))
{
if (Uri.TryCreate(service.Link, UriKind.RelativeOrAbsolute, out var uri))
OtherExternalServices.AddOrReplace(service.Name, uri);
}
}
}
}
}

View File

@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using BTCPayServer.Lightning;
using BTCPayServer.Logging;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
namespace BTCPayServer.Configuration
{
public class LightningNetworkOptions
{
public Dictionary<string, LightningConnectionString> InternalLightningByCryptoCode { get; set; } =
new Dictionary<string, LightningConnectionString>();
public void Configure(IConfiguration conf, BTCPayNetworkProvider networkProvider)
{
foreach (var net in networkProvider.GetAll().OfType<BTCPayNetwork>())
{
var lightning = conf.GetOrDefault<string>($"{net.CryptoCode}.lightning", string.Empty);
if (lightning.Length != 0)
{
if (!LightningConnectionString.TryParse(lightning, true, out var connectionString,
out var error))
{
Logs.Configuration.LogWarning($"Invalid setting {net.CryptoCode}.lightning, " +
Environment.NewLine +
$"If you have a c-lightning server use: 'type=clightning;server=/root/.lightning/lightning-rpc', " +
Environment.NewLine +
$"If you have a lightning charge server: 'type=charge;server=https://charge.example.com;api-token=yourapitoken'" +
Environment.NewLine +
$"If you have a lnd server: 'type=lnd-rest;server=https://lnd:lnd@lnd.example.com;macaroon=abf239...;certthumbprint=2abdf302...'" +
Environment.NewLine +
$" lnd server: 'type=lnd-rest;server=https://lnd:lnd@lnd.example.com;macaroonfilepath=/root/.lnd/admin.macaroon;certthumbprint=2abdf302...'" +
Environment.NewLine +
$"If you have an eclair server: 'type=eclair;server=http://eclair.com:4570;password=eclairpassword;bitcoin-host=bitcoind:37393;bitcoin-auth=bitcoinrpcuser:bitcoinrpcpassword" +
Environment.NewLine +
$" eclair server: 'type=eclair;server=http://eclair.com:4570;password=eclairpassword;bitcoin-host=bitcoind:37393" +
Environment.NewLine +
$"Error: {error}" + Environment.NewLine +
"This service will not be exposed through BTCPay Server");
}
else
{
if (connectionString.IsLegacy)
{
Logs.Configuration.LogWarning(
$"Setting {net.CryptoCode}.lightning is a deprecated format, it will work now, but please replace it for future versions with '{connectionString.ToString()}'");
}
InternalLightningByCryptoCode.Add(net.CryptoCode, connectionString);
}
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
using System;
namespace BTCPayServer.Configuration
{
public class NBXplorerConnectionSetting
{
public string CryptoCode { get; internal set; }
public Uri ExplorerUri { get; internal set; }
public string CookieFile { get; internal set; }
}
}

View File

@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Configuration;
namespace BTCPayServer.Configuration
{
public class NBXplorerOptions
{
public List<NBXplorerConnectionSetting> NBXplorerConnectionSettings
{
get;
set;
} = new List<NBXplorerConnectionSetting>();
public void Configure(IConfiguration conf, BTCPayNetworkProvider provider)
{
foreach (BTCPayNetwork btcPayNetwork in provider.GetAll().OfType<BTCPayNetwork>())
{
NBXplorerConnectionSetting setting = new NBXplorerConnectionSetting();
setting.CryptoCode = btcPayNetwork.CryptoCode;
setting.ExplorerUri = conf.GetOrDefault<Uri>($"{btcPayNetwork.CryptoCode}.explorer.url",
btcPayNetwork.NBXplorerNetwork.DefaultSettings.DefaultUrl);
setting.CookieFile = conf.GetOrDefault<string>($"{btcPayNetwork.CryptoCode}.explorer.cookiefile",
btcPayNetwork.NBXplorerNetwork.DefaultSettings.DefaultCookieFile);
NBXplorerConnectionSettings.Add(setting);
}
}
}
}

View File

@@ -10,6 +10,7 @@ using BTCPayServer.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace BTCPayServer.Controllers.GreenField namespace BTCPayServer.Controllers.GreenField
{ {
@@ -19,19 +20,20 @@ namespace BTCPayServer.Controllers.GreenField
[EnableCors(CorsPolicies.All)] [EnableCors(CorsPolicies.All)]
public class InternalLightningNodeApiController : LightningNodeApiController public class InternalLightningNodeApiController : LightningNodeApiController
{ {
private readonly BTCPayServerOptions _btcPayServerOptions;
private readonly BTCPayNetworkProvider _btcPayNetworkProvider; private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
private readonly LightningClientFactoryService _lightningClientFactory; private readonly LightningClientFactoryService _lightningClientFactory;
private readonly IOptions<LightningNetworkOptions> _lightningNetworkOptions;
public InternalLightningNodeApiController(BTCPayServerOptions btcPayServerOptions, public InternalLightningNodeApiController(
BTCPayNetworkProvider btcPayNetworkProvider, BTCPayServerEnvironment btcPayServerEnvironment, BTCPayNetworkProvider btcPayNetworkProvider, BTCPayServerEnvironment btcPayServerEnvironment,
CssThemeManager cssThemeManager, LightningClientFactoryService lightningClientFactory) : base( CssThemeManager cssThemeManager, LightningClientFactoryService lightningClientFactory,
IOptions<LightningNetworkOptions> lightningNetworkOptions ) : base(
btcPayNetworkProvider, btcPayServerEnvironment, cssThemeManager) btcPayNetworkProvider, btcPayServerEnvironment, cssThemeManager)
{ {
_btcPayServerOptions = btcPayServerOptions;
_btcPayNetworkProvider = btcPayNetworkProvider; _btcPayNetworkProvider = btcPayNetworkProvider;
_lightningClientFactory = lightningClientFactory; _lightningClientFactory = lightningClientFactory;
_lightningNetworkOptions = lightningNetworkOptions;
} }
[Authorize(Policy = Policies.CanUseInternalLightningNode, [Authorize(Policy = Policies.CanUseInternalLightningNode,
@@ -100,7 +102,7 @@ namespace BTCPayServer.Controllers.GreenField
protected override Task<ILightningClient> GetLightningClient(string cryptoCode, bool doingAdminThings) protected override Task<ILightningClient> GetLightningClient(string cryptoCode, bool doingAdminThings)
{ {
_btcPayServerOptions.InternalLightningByCryptoCode.TryGetValue(cryptoCode, _lightningNetworkOptions.Value.InternalLightningByCryptoCode.TryGetValue(cryptoCode,
out var internalLightningNode); out var internalLightningNode);
var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode); var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
if (network == null || !CanUseInternalLightning(doingAdminThings) || internalLightningNode == null) if (network == null || !CanUseInternalLightning(doingAdminThings) || internalLightningNode == null)

View File

@@ -14,6 +14,7 @@ using BTCPayServer.Services;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
namespace BTCPayServer.Controllers.GreenField namespace BTCPayServer.Controllers.GreenField
{ {
@@ -23,17 +24,17 @@ namespace BTCPayServer.Controllers.GreenField
[EnableCors(CorsPolicies.All)] [EnableCors(CorsPolicies.All)]
public class StoreLightningNodeApiController : LightningNodeApiController public class StoreLightningNodeApiController : LightningNodeApiController
{ {
private readonly BTCPayServerOptions _btcPayServerOptions; private readonly IOptions<LightningNetworkOptions> _lightningNetworkOptions;
private readonly LightningClientFactoryService _lightningClientFactory; private readonly LightningClientFactoryService _lightningClientFactory;
private readonly BTCPayNetworkProvider _btcPayNetworkProvider; private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
public StoreLightningNodeApiController( public StoreLightningNodeApiController(
BTCPayServerOptions btcPayServerOptions, IOptions<LightningNetworkOptions> lightningNetworkOptions,
LightningClientFactoryService lightningClientFactory, BTCPayNetworkProvider btcPayNetworkProvider, LightningClientFactoryService lightningClientFactory, BTCPayNetworkProvider btcPayNetworkProvider,
BTCPayServerEnvironment btcPayServerEnvironment, CssThemeManager cssThemeManager) : base( BTCPayServerEnvironment btcPayServerEnvironment, CssThemeManager cssThemeManager) : base(
btcPayNetworkProvider, btcPayServerEnvironment, cssThemeManager) btcPayNetworkProvider, btcPayServerEnvironment, cssThemeManager)
{ {
_btcPayServerOptions = btcPayServerOptions; _lightningNetworkOptions = lightningNetworkOptions;
_lightningClientFactory = lightningClientFactory; _lightningClientFactory = lightningClientFactory;
_btcPayNetworkProvider = btcPayNetworkProvider; _btcPayNetworkProvider = btcPayNetworkProvider;
} }
@@ -102,7 +103,7 @@ namespace BTCPayServer.Controllers.GreenField
protected override Task<ILightningClient> GetLightningClient(string cryptoCode, protected override Task<ILightningClient> GetLightningClient(string cryptoCode,
bool doingAdminThings) bool doingAdminThings)
{ {
_btcPayServerOptions.InternalLightningByCryptoCode.TryGetValue(cryptoCode, _lightningNetworkOptions.Value.InternalLightningByCryptoCode.TryGetValue(cryptoCode,
out var internalLightningNode); out var internalLightningNode);
var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode); var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);

View File

@@ -32,6 +32,7 @@ using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Routing; using Microsoft.AspNetCore.Routing;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NBitcoin; using NBitcoin;
using NBitcoin.DataEncoders; using NBitcoin.DataEncoders;
using Renci.SshNet; using Renci.SshNet;
@@ -54,6 +55,7 @@ namespace BTCPayServer.Controllers
private readonly CheckConfigurationHostedService _sshState; private readonly CheckConfigurationHostedService _sshState;
private readonly EventAggregator _eventAggregator; private readonly EventAggregator _eventAggregator;
private readonly CssThemeManager _cssThemeManager; private readonly CssThemeManager _cssThemeManager;
private readonly IOptions<ExternalServicesOptions> _externalServiceOptions;
private readonly StoredFileRepository _StoredFileRepository; private readonly StoredFileRepository _StoredFileRepository;
private readonly FileService _FileService; private readonly FileService _FileService;
private readonly IEnumerable<IStorageProviderService> _StorageProviderServices; private readonly IEnumerable<IStorageProviderService> _StorageProviderServices;
@@ -72,7 +74,8 @@ namespace BTCPayServer.Controllers
AppService appService, AppService appService,
CheckConfigurationHostedService sshState, CheckConfigurationHostedService sshState,
EventAggregator eventAggregator, EventAggregator eventAggregator,
CssThemeManager cssThemeManager) CssThemeManager cssThemeManager,
IOptions<ExternalServicesOptions> externalServiceOptions)
{ {
_Options = options; _Options = options;
_StoredFileRepository = storedFileRepository; _StoredFileRepository = storedFileRepository;
@@ -89,6 +92,7 @@ namespace BTCPayServer.Controllers
_sshState = sshState; _sshState = sshState;
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_cssThemeManager = cssThemeManager; _cssThemeManager = cssThemeManager;
_externalServiceOptions = externalServiceOptions;
} }
[Route("server/maintenance")] [Route("server/maintenance")]
@@ -338,10 +342,10 @@ namespace BTCPayServer.Controllers
public async Task<IActionResult> Services() public async Task<IActionResult> Services()
{ {
var result = new ServicesViewModel(); var result = new ServicesViewModel();
result.ExternalServices = _Options.ExternalServices.ToList(); result.ExternalServices = _externalServiceOptions.Value.ExternalServices.ToList();
// other services // other services
foreach (var externalService in _Options.OtherExternalServices) foreach (var externalService in _externalServiceOptions.Value.OtherExternalServices)
{ {
result.OtherExternalServices.Add(new ServicesViewModel.OtherExternalService() result.OtherExternalServices.Add(new ServicesViewModel.OtherExternalService()
{ {
@@ -434,7 +438,7 @@ namespace BTCPayServer.Controllers
private ExternalService GetService(string serviceName, string cryptoCode) private ExternalService GetService(string serviceName, string cryptoCode)
{ {
var result = _Options.ExternalServices.GetService(serviceName, cryptoCode); var result = _externalServiceOptions.Value.ExternalServices.GetService(serviceName, cryptoCode);
if (result != null) if (result != null)
return result; return result;
foreach (var torService in _torServices.Services) foreach (var torService in _torServices.Services)

View File

@@ -48,7 +48,7 @@ namespace BTCPayServer.Controllers
private LightningConnectionString GetInternalLighningNode(string cryptoCode) private LightningConnectionString GetInternalLighningNode(string cryptoCode)
{ {
if (_BtcpayServerOptions.InternalLightningByCryptoCode.TryGetValue(cryptoCode, out var connectionString)) if (_lightningNetworkOptions.Value.InternalLightningByCryptoCode.TryGetValue(cryptoCode, out var connectionString))
{ {
return CanUseInternalLightning() ? connectionString : null; return CanUseInternalLightning() ? connectionString : null;
} }

View File

@@ -32,6 +32,7 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering; using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Options;
using NBitcoin; using NBitcoin;
using NBitcoin.DataEncoders; using NBitcoin.DataEncoders;
using NBXplorer; using NBXplorer;
@@ -67,7 +68,8 @@ namespace BTCPayServer.Controllers
CssThemeManager cssThemeManager, CssThemeManager cssThemeManager,
AppService appService, AppService appService,
IWebHostEnvironment webHostEnvironment, IWebHostEnvironment webHostEnvironment,
WebhookNotificationManager webhookNotificationManager) WebhookNotificationManager webhookNotificationManager,
IOptions<LightningNetworkOptions> lightningNetworkOptions)
{ {
_RateFactory = rateFactory; _RateFactory = rateFactory;
_Repo = repo; _Repo = repo;
@@ -82,6 +84,7 @@ namespace BTCPayServer.Controllers
_CssThemeManager = cssThemeManager; _CssThemeManager = cssThemeManager;
_appService = appService; _appService = appService;
_webHostEnvironment = webHostEnvironment; _webHostEnvironment = webHostEnvironment;
_lightningNetworkOptions = lightningNetworkOptions;
WebhookNotificationManager = webhookNotificationManager; WebhookNotificationManager = webhookNotificationManager;
_EventAggregator = eventAggregator; _EventAggregator = eventAggregator;
_NetworkProvider = networkProvider; _NetworkProvider = networkProvider;
@@ -108,6 +111,7 @@ namespace BTCPayServer.Controllers
private readonly CssThemeManager _CssThemeManager; private readonly CssThemeManager _CssThemeManager;
private readonly AppService _appService; private readonly AppService _appService;
private readonly IWebHostEnvironment _webHostEnvironment; private readonly IWebHostEnvironment _webHostEnvironment;
private readonly IOptions<LightningNetworkOptions> _lightningNetworkOptions;
private readonly EventAggregator _EventAggregator; private readonly EventAggregator _EventAggregator;
[TempData] [TempData]

View File

@@ -6,6 +6,7 @@ using BTCPayServer.Configuration;
using BTCPayServer.HostedServices; using BTCPayServer.HostedServices;
using BTCPayServer.Logging; using BTCPayServer.Logging;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NBXplorer; using NBXplorer;
namespace BTCPayServer namespace BTCPayServer
@@ -13,34 +14,40 @@ namespace BTCPayServer
public class ExplorerClientProvider public class ExplorerClientProvider
{ {
readonly BTCPayNetworkProvider _NetworkProviders; readonly BTCPayNetworkProvider _NetworkProviders;
readonly BTCPayServerOptions _Options;
public BTCPayNetworkProvider NetworkProviders => _NetworkProviders; public BTCPayNetworkProvider NetworkProviders => _NetworkProviders;
readonly NBXplorerDashboard _Dashboard; readonly NBXplorerDashboard _Dashboard;
public ExplorerClientProvider(IHttpClientFactory httpClientFactory, BTCPayNetworkProvider networkProviders, BTCPayServerOptions options, NBXplorerDashboard dashboard)
public ExplorerClientProvider(
IHttpClientFactory httpClientFactory,
BTCPayNetworkProvider networkProviders,
IOptions<NBXplorerOptions> nbXplorerOptions,
NBXplorerDashboard dashboard)
{ {
_Dashboard = dashboard; _Dashboard = dashboard;
_NetworkProviders = networkProviders; _NetworkProviders = networkProviders;
_Options = options;
foreach (var setting in options.NBXplorerConnectionSettings) foreach (var setting in nbXplorerOptions.Value.NBXplorerConnectionSettings)
{ {
var cookieFile = setting.CookieFile; var cookieFile = setting.CookieFile;
if (cookieFile.Trim() == "0" || string.IsNullOrEmpty(cookieFile.Trim())) if (cookieFile.Trim() == "0" || string.IsNullOrEmpty(cookieFile.Trim()))
cookieFile = null; cookieFile = null;
Logs.Configuration.LogInformation($"{setting.CryptoCode}: Explorer url is {(setting.ExplorerUri.AbsoluteUri ?? "not set")}"); Logs.Configuration.LogInformation($"{setting.CryptoCode}: Explorer url is {(setting.ExplorerUri.AbsoluteUri)}");
Logs.Configuration.LogInformation($"{setting.CryptoCode}: Cookie file is {(setting.CookieFile ?? "not set")}"); Logs.Configuration.LogInformation($"{setting.CryptoCode}: Cookie file is {(setting.CookieFile ?? "not set")}");
if (setting.ExplorerUri != null) if (setting.ExplorerUri != null)
{ {
_Clients.TryAdd(setting.CryptoCode.ToUpperInvariant(), CreateExplorerClient(httpClientFactory.CreateClient(nameof(ExplorerClientProvider)), _NetworkProviders.GetNetwork<BTCPayNetwork>(setting.CryptoCode), setting.ExplorerUri, setting.CookieFile)); _Clients.TryAdd(setting.CryptoCode.ToUpperInvariant(),
CreateExplorerClient(httpClientFactory.CreateClient(nameof(ExplorerClientProvider)),
_NetworkProviders.GetNetwork<BTCPayNetwork>(setting.CryptoCode), setting.ExplorerUri,
setting.CookieFile));
} }
} }
} }
private static ExplorerClient CreateExplorerClient(HttpClient httpClient, BTCPayNetwork n, Uri uri, string cookieFile) private static ExplorerClient CreateExplorerClient(HttpClient httpClient, BTCPayNetwork n, Uri uri,
string cookieFile)
{ {
var explorer = n.NBXplorerNetwork.CreateExplorerClient(uri); var explorer = n.NBXplorerNetwork.CreateExplorerClient(uri);
explorer.SetClient(httpClient); explorer.SetClient(httpClient);
if (cookieFile == null) if (cookieFile == null)
@@ -48,10 +55,13 @@ namespace BTCPayServer
Logs.Configuration.LogWarning($"{explorer.CryptoCode}: Not using cookie authentication"); Logs.Configuration.LogWarning($"{explorer.CryptoCode}: Not using cookie authentication");
explorer.SetNoAuth(); explorer.SetNoAuth();
} }
if (!explorer.SetCookieAuth(cookieFile)) if (!explorer.SetCookieAuth(cookieFile))
{ {
Logs.Configuration.LogWarning($"{explorer.CryptoCode}: Using cookie auth against NBXplorer, but {cookieFile} is not found"); Logs.Configuration.LogWarning(
$"{explorer.CryptoCode}: Using cookie auth against NBXplorer, but {cookieFile} is not found");
} }
return explorer; return explorer;
} }

View File

@@ -13,6 +13,7 @@ using BTCPayServer.Abstractions.Models;
using BTCPayServer.Configuration; using BTCPayServer.Configuration;
using BTCPayServer.Data; using BTCPayServer.Data;
using BTCPayServer.Lightning; using BTCPayServer.Lightning;
using BTCPayServer.Logging;
using BTCPayServer.Models; using BTCPayServer.Models;
using BTCPayServer.Models.StoreViewModels; using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Payments; using BTCPayServer.Payments;
@@ -25,6 +26,7 @@ using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ViewFeatures; using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NBitcoin; using NBitcoin;
using NBitcoin.Payment; using NBitcoin.Payment;
using NBitpayClient; using NBitpayClient;
@@ -464,6 +466,37 @@ namespace BTCPayServer
string sql = command.CommandText; string sql = command.CommandText;
return sql; return sql;
} }
public static BTCPayNetworkProvider ConfigureNetworkProvider(this IConfiguration configuration)
{
var _networkType = DefaultConfiguration.GetNetworkType(configuration);
var supportedChains = configuration.GetOrDefault<string>("chains", "btc")
.Split(',', StringSplitOptions.RemoveEmptyEntries)
.Select(t => t.ToUpperInvariant()).ToHashSet();
var networkProvider = new BTCPayNetworkProvider(_networkType);
var filtered = networkProvider.Filter(supportedChains.ToArray());
#if ALTCOINS
supportedChains.AddRange(filtered.GetAllElementsSubChains(networkProvider));
supportedChains.AddRange(filtered.GetAllEthereumSubChains(networkProvider));
#endif
#if !ALTCOINS
var onlyBTC = supportedChains.Count == 1 && supportedChains.First() == "BTC";
if (!onlyBTC)
throw new ConfigException($"This build of BTCPay Server does not support altcoins");
#endif
var result = networkProvider.Filter(supportedChains.ToArray());
foreach (var chain in supportedChains)
{
if (result.GetNetwork<BTCPayNetworkBase>(chain) == null)
throw new ConfigException($"Invalid chains \"{chain}\"");
}
Logs.Configuration.LogInformation(
"Supported chains: " + String.Join(',', supportedChains.ToArray()));
return result;
}
private static object Private(this object obj, string privateField) => obj?.GetType().GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj); private static object Private(this object obj, string privateField) => obj?.GetType().GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj);
private static T Private<T>(this object obj, string privateField) => (T)obj?.GetType().GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj); private static T Private<T>(this object obj, string privateField) => (T)obj?.GetType().GetField(privateField, BindingFlags.Instance | BindingFlags.NonPublic)?.GetValue(obj);
} }

View File

@@ -1,8 +1,8 @@
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Configuration;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
namespace BTCPayServer.Filters namespace BTCPayServer.Filters
{ {
@@ -14,14 +14,16 @@ namespace BTCPayServer.Filters
{ {
_cryptoCode = cryptoCode; _cryptoCode = cryptoCode;
} }
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{ {
var options = context.HttpContext.RequestServices.GetService(typeof(BTCPayServerOptions)) as BTCPayServerOptions; var options = context.HttpContext.RequestServices.GetService<BTCPayNetworkProvider>();
if (options.NetworkProvider.GetNetwork(_cryptoCode) == null) if (options.GetNetwork(_cryptoCode) == null)
{ {
context.Result = new NotFoundResult(); context.Result = new NotFoundResult();
return; return;
} }
await next(); await next();
} }
} }

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.InteropServices.ComTypes; using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Threading; using System.Threading;
@@ -117,7 +118,6 @@ namespace BTCPayServer.Hosting
services.TryAddSingleton<DataDirectories>(); services.TryAddSingleton<DataDirectories>();
services.TryAddSingleton<DatabaseOptions>(o => services.TryAddSingleton<DatabaseOptions>(o =>
{ {
var opts = o.GetRequiredService<BTCPayServerOptions>();
try try
{ {
var dbOptions = new DatabaseOptions(o.GetRequiredService<IConfiguration>(), var dbOptions = new DatabaseOptions(o.GetRequiredService<IConfiguration>(),
@@ -144,13 +144,24 @@ namespace BTCPayServer.Hosting
} }
}); });
services.AddSingleton<ApplicationDbContextFactory>(); services.AddSingleton<ApplicationDbContextFactory>();
services.AddOptions<NBXplorerOptions>().Configure<BTCPayNetworkProvider>(
services.TryAddSingleton<BTCPayNetworkProvider>(o => (options, btcPayNetworkProvider) =>
{ {
var opts = o.GetRequiredService<BTCPayServerOptions>(); options.Configure(configuration, btcPayNetworkProvider);
return opts.NetworkProvider;
}); });
services.AddOptions<LightningNetworkOptions>().Configure<BTCPayNetworkProvider>(
(options, btcPayNetworkProvider) =>
{
options.Configure(configuration, btcPayNetworkProvider);
});
services.AddOptions<ExternalServicesOptions>().Configure<BTCPayNetworkProvider>(
(options, btcPayNetworkProvider) =>
{
options.Configure(configuration, btcPayNetworkProvider);
});
services.TryAddSingleton(o => configuration.ConfigureNetworkProvider());
services.TryAddSingleton<AppService>(); services.TryAddSingleton<AppService>();
services.AddSingleton<PluginService>(); services.AddSingleton<PluginService>();
services.AddSingleton<IPluginHookService>(provider => provider.GetService<PluginService>()); services.AddSingleton<IPluginHookService>(provider => provider.GetService<PluginService>());

View File

@@ -2,9 +2,9 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Configuration;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.Extensions.DependencyInjection;
namespace BTCPayServer.Services.Altcoins.Ethereum.Filters namespace BTCPayServer.Services.Altcoins.Ethereum.Filters
{ {
@@ -12,15 +12,15 @@ namespace BTCPayServer.Services.Altcoins.Ethereum.Filters
{ {
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{ {
var options = (BTCPayServerOptions) context.HttpContext.RequestServices.GetService(typeof(BTCPayServerOptions)); var options = context.HttpContext.RequestServices.GetService<BTCPayNetworkProvider>();
if (!options.NetworkProvider.GetAll().OfType<EthereumBTCPayNetwork>().Any()) if (!options.GetAll().OfType<EthereumBTCPayNetwork>().Any())
{ {
context.Result = new NotFoundResult(); context.Result = new NotFoundResult();
return; return;
} }
await next(); await next();
} }
} }
} }
#endif #endif