using BTCPayServer.Logging; using System.Linq; using Microsoft.Extensions.Logging; using NBitcoin; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; using StandardConfiguration; using Microsoft.Extensions.Configuration; using NBXplorer; using BTCPayServer.Payments.Lightning; 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 NetworkType NetworkType { get; set; } public string ConfigurationFile { get; private set; } public string DataDir { get; private set; } public List Listen { get; set; } public List NBXplorerConnectionSettings { get; set; } = new List(); public void LoadArgs(IConfiguration conf) { NetworkType = DefaultConfiguration.GetNetworkType(conf); var defaultSettings = BTCPayDefaultSettings.GetDefaultSettings(NetworkType); DataDir = conf.GetOrDefault("datadir", defaultSettings.DefaultDataDirectory); Logs.Configuration.LogInformation("Network: " + NetworkType.ToString()); var supportedChains = conf.GetOrDefault("chains", "btc") .Split(',', StringSplitOptions.RemoveEmptyEntries) .Select(t => t.ToUpperInvariant()); NetworkProvider = new BTCPayNetworkProvider(NetworkType).Filter(supportedChains.ToArray()); foreach (var chain in supportedChains) { if (NetworkProvider.GetNetwork(chain) == null) throw new ConfigException($"Invalid chains \"{chain}\""); } var validChains = new List(); foreach (var net in NetworkProvider.GetAll()) { NBXplorerConnectionSetting setting = new NBXplorerConnectionSetting(); setting.CryptoCode = net.CryptoCode; setting.ExplorerUri = conf.GetOrDefault($"{net.CryptoCode}.explorer.url", net.NBXplorerNetwork.DefaultSettings.DefaultUrl); setting.CookieFile = conf.GetOrDefault($"{net.CryptoCode}.explorer.cookiefile", net.NBXplorerNetwork.DefaultSettings.DefaultCookieFile); NBXplorerConnectionSettings.Add(setting); { var lightning = conf.GetOrDefault($"{net.CryptoCode}.lightning", string.Empty); if (lightning.Length != 0) { if (!LightningConnectionString.TryParse(lightning, true, out var connectionString, out var error)) { throw new ConfigException($"Invalid setting {net.CryptoCode}.lightning, " + Environment.NewLine + $"If you have a 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 + error); } if (connectionString.IsLegacy) { Logs.Configuration.LogWarning($"Setting {net.CryptoCode}.lightning will work but use an deprecated format, please replace it by '{connectionString.ToString()}'"); } InternalLightningByCryptoCode.Add(net.CryptoCode, connectionString); } } { var lightning = conf.GetOrDefault($"{net.CryptoCode}.external.lnd.grpc", string.Empty); if (lightning.Length != 0) { if (!LightningConnectionString.TryParse(lightning, false, out var connectionString, out var error)) { throw new ConfigException($"Invalid setting {net.CryptoCode}.external.lnd.grpc, " + Environment.NewLine + $"lnd server: 'type=lnd-grpc;server=https://lnd.example.com;macaroon=abf239...;certthumbprint=2abdf302...'" + Environment.NewLine + $"lnd server: 'type=lnd-grpc;server=https://lnd.example.com;macaroonfilepath=/root/.lnd/admin.macaroon;certthumbprint=2abdf302...'" + Environment.NewLine + error); } ExternalServicesByCryptoCode.Add(net.CryptoCode, new ExternalLNDGRPC(connectionString)); } } } Logs.Configuration.LogInformation("Supported chains: " + String.Join(',', supportedChains.ToArray())); PostgresConnectionString = conf.GetOrDefault("postgres", null); BundleJsCss = conf.GetOrDefault("bundlejscss", true); ExternalUrl = conf.GetOrDefault("externalurl", null); RootPath = conf.GetOrDefault("rootpath", "/"); if (!RootPath.StartsWith("/", StringComparison.InvariantCultureIgnoreCase)) RootPath = "/" + RootPath; var old = conf.GetOrDefault("internallightningnode", null); if (old != null) throw new ConfigException($"internallightningnode should not be used anymore, use btclightning instead"); } public string RootPath { get; set; } public Dictionary InternalLightningByCryptoCode { get; set; } = new Dictionary(); public ExternalServices ExternalServicesByCryptoCode { get; set; } = new ExternalServices(); public BTCPayNetworkProvider NetworkProvider { get; set; } public string PostgresConnectionString { get; set; } public Uri ExternalUrl { get; set; } public bool BundleJsCss { get; set; } internal string GetRootUri() { if (ExternalUrl == null) return null; UriBuilder builder = new UriBuilder(ExternalUrl); builder.Path = RootPath; return builder.ToString(); } } public class ExternalServices : MultiValueDictionary { public IEnumerable GetServices(string cryptoCode) where T : ExternalService { if (!this.TryGetValue(cryptoCode.ToUpperInvariant(), out var services)) return Array.Empty(); return services.OfType(); } } public class ExternalService { } public class ExternalLNDGRPC : ExternalService { public ExternalLNDGRPC(LightningConnectionString connectionString) { ConnectionString = connectionString; } public LightningConnectionString ConnectionString { get; set; } } }