2019-03-01 05:20:21 +01:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Lightning;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Configuration
|
|
|
|
|
{
|
|
|
|
|
public class ExternalServices : List<ExternalService>
|
|
|
|
|
{
|
|
|
|
|
public void Load(string cryptoCode, IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Load(configuration, cryptoCode, "lndgrpc", ExternalServiceTypes.LNDGRPC, "Invalid setting {0}, " + Environment.NewLine +
|
|
|
|
|
"lnd server: 'server=https://lnd.example.com;macaroon=abf239...;certthumbprint=2abdf302...'" + Environment.NewLine +
|
|
|
|
|
"lnd server: 'server=https://lnd.example.com;macaroonfilepath=/root/.lnd/admin.macaroon;certthumbprint=2abdf302...'" + Environment.NewLine +
|
|
|
|
|
"lnd server: 'server=https://lnd.example.com;macaroondirectorypath=/root/.lnd;certthumbprint=2abdf302...'" + Environment.NewLine +
|
|
|
|
|
"Error: {1}",
|
|
|
|
|
"LND (gRPC server)");
|
|
|
|
|
Load(configuration, cryptoCode, "lndrest", ExternalServiceTypes.LNDRest, "Invalid setting {0}, " + Environment.NewLine +
|
|
|
|
|
"lnd server: 'server=https://lnd.example.com;macaroon=abf239...;certthumbprint=2abdf302...'" + Environment.NewLine +
|
|
|
|
|
"lnd server: 'server=https://lnd.example.com;macaroonfilepath=/root/.lnd/admin.macaroon;certthumbprint=2abdf302...'" + Environment.NewLine +
|
|
|
|
|
"lnd server: 'server=https://lnd.example.com;macaroondirectorypath=/root/.lnd;certthumbprint=2abdf302...'" + Environment.NewLine +
|
|
|
|
|
"Error: {1}",
|
|
|
|
|
"LND (REST server)");
|
2019-11-05 05:04:35 +01:00
|
|
|
|
Load(configuration, cryptoCode, "lndseedbackup", ExternalServiceTypes.LNDSeedBackup, "Invalid setting {0}, " + Environment.NewLine +
|
|
|
|
|
"lnd seed backup: /etc/merchant_lnd/data/chain/bitcoin/regtest/walletunlock.json'" + Environment.NewLine +
|
|
|
|
|
"Error: {1}",
|
|
|
|
|
"LND Seed Backup");
|
2019-03-01 05:20:21 +01:00
|
|
|
|
Load(configuration, cryptoCode, "spark", ExternalServiceTypes.Spark, "Invalid setting {0}, " + Environment.NewLine +
|
|
|
|
|
$"Valid example: 'server=https://btcpay.example.com/spark/btc/;cookiefile=/etc/clightning_bitcoin_spark/.cookie'" + Environment.NewLine +
|
|
|
|
|
"Error: {1}",
|
|
|
|
|
"C-Lightning (Spark server)");
|
|
|
|
|
Load(configuration, cryptoCode, "rtl", ExternalServiceTypes.RTL, "Invalid setting {0}, " + Environment.NewLine +
|
|
|
|
|
$"Valid example: 'server=https://btcpay.example.com/rtl/btc/;cookiefile=/etc/clightning_bitcoin_rtl/.cookie'" + Environment.NewLine +
|
|
|
|
|
"Error: {1}",
|
2020-01-23 14:20:37 +01:00
|
|
|
|
"Ride the Lightning server");
|
|
|
|
|
Load(configuration, cryptoCode, "clightningrest", ExternalServiceTypes.CLightningRest, "Invalid setting {0}, " + Environment.NewLine +
|
|
|
|
|
$"Valid example: 'server=https://btcpay.example.com/clightning-rest/btc/;cookiefile=/etc/clightning_bitcoin_rtl/.cookie'" + Environment.NewLine +
|
|
|
|
|
"Error: {1}",
|
|
|
|
|
"C-Lightning REST");
|
2019-03-01 05:20:21 +01:00
|
|
|
|
Load(configuration, cryptoCode, "charge", ExternalServiceTypes.Charge, "Invalid setting {0}, " + Environment.NewLine +
|
|
|
|
|
$"lightning charge server: 'type=charge;server=https://charge.example.com;api-token=2abdf302...'" + Environment.NewLine +
|
|
|
|
|
$"lightning charge server: 'type=charge;server=https://charge.example.com;cookiefilepath=/root/.charge/.cookie'" + Environment.NewLine +
|
|
|
|
|
"Error: {1}",
|
|
|
|
|
"C-Lightning (Charge server)");
|
2020-01-21 10:27:10 +01:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LoadNonCryptoServices(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
Load(configuration, null, "configurator", ExternalServiceTypes.Configurator, "Invalid setting {0}, " + Environment.NewLine +
|
2020-01-22 07:16:32 +01:00
|
|
|
|
$"configurator: 'cookiefilepathfile=/etc/configurator/cookie'" + Environment.NewLine +
|
2020-01-21 10:27:10 +01:00
|
|
|
|
"Error: {1}",
|
|
|
|
|
"Configurator");
|
2019-03-01 05:20:21 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Load(IConfiguration configuration, string cryptoCode, string serviceName, ExternalServiceTypes type, string errorMessage, string displayName)
|
|
|
|
|
{
|
2020-01-21 10:27:10 +01:00
|
|
|
|
var setting = $"{(!string.IsNullOrEmpty(cryptoCode)? $"{cryptoCode}.": string.Empty)}external.{serviceName}";
|
2019-03-01 05:20:21 +01:00
|
|
|
|
var connStr = configuration.GetOrDefault<string>(setting, string.Empty);
|
|
|
|
|
if (connStr.Length != 0)
|
|
|
|
|
{
|
2019-11-05 20:40:06 +01:00
|
|
|
|
ExternalConnectionString serviceConnection;
|
|
|
|
|
if (type == ExternalServiceTypes.LNDSeedBackup)
|
|
|
|
|
{
|
|
|
|
|
// just using CookieFilePath to hold variable instead of refactoring whole holder class to better conform
|
|
|
|
|
serviceConnection = new ExternalConnectionString { CookieFilePath = connStr };
|
|
|
|
|
}
|
|
|
|
|
else if (!ExternalConnectionString.TryParse(connStr, out serviceConnection, out var error))
|
2019-03-01 05:20:21 +01:00
|
|
|
|
{
|
|
|
|
|
throw new ConfigException(string.Format(CultureInfo.InvariantCulture, errorMessage, setting, error));
|
|
|
|
|
}
|
2019-11-05 20:40:06 +01:00
|
|
|
|
this.Add(new ExternalService() { Type = type, ConnectionString = serviceConnection, CryptoCode = cryptoCode, DisplayName = displayName, ServiceName = serviceName });
|
2019-03-01 05:20:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExternalService GetService(string serviceName, string cryptoCode)
|
|
|
|
|
{
|
2020-01-21 10:27:10 +01:00
|
|
|
|
return this.FirstOrDefault(o =>
|
|
|
|
|
(cryptoCode == null && o.CryptoCode == null) ||
|
|
|
|
|
(o.CryptoCode != null && o.CryptoCode.Equals(cryptoCode, StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
&&
|
|
|
|
|
o.ServiceName.Equals(serviceName, StringComparison.OrdinalIgnoreCase));
|
2019-03-01 05:20:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ExternalService
|
|
|
|
|
{
|
|
|
|
|
public string DisplayName { get; set; }
|
|
|
|
|
public ExternalServiceTypes Type { get; set; }
|
|
|
|
|
public ExternalConnectionString ConnectionString { get; set; }
|
|
|
|
|
public string CryptoCode { get; set; }
|
|
|
|
|
public string ServiceName { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum ExternalServiceTypes
|
|
|
|
|
{
|
|
|
|
|
LNDRest,
|
|
|
|
|
LNDGRPC,
|
2019-11-05 05:04:35 +01:00
|
|
|
|
LNDSeedBackup,
|
2019-03-01 05:20:21 +01:00
|
|
|
|
Spark,
|
|
|
|
|
RTL,
|
2019-05-07 07:07:36 +02:00
|
|
|
|
Charge,
|
2019-11-07 06:33:10 +01:00
|
|
|
|
P2P,
|
2020-01-21 10:27:10 +01:00
|
|
|
|
RPC,
|
2020-01-23 14:20:37 +01:00
|
|
|
|
Configurator,
|
|
|
|
|
CLightningRest
|
2019-03-01 05:20:21 +01:00
|
|
|
|
}
|
|
|
|
|
}
|