2018-02-25 16:48:12 +01:00
|
|
|
|
using System;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
|
|
|
|
using BTCPayServer.Payments;
|
|
|
|
|
using BTCPayServer.Payments.Lightning.CLightning;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using BTCPayServer.Payments.Lightning;
|
2018-03-20 16:31:19 +01:00
|
|
|
|
using System.Net;
|
2018-02-25 16:48:12 +01:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
|
{
|
|
|
|
|
public partial class StoresController
|
|
|
|
|
{
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{storeId}/lightning")]
|
|
|
|
|
public async Task<IActionResult> AddLightningNode(string storeId, string selectedCrypto = null)
|
|
|
|
|
{
|
|
|
|
|
selectedCrypto = selectedCrypto ?? "BTC";
|
|
|
|
|
var store = await _Repo.FindStore(storeId, GetUserId());
|
|
|
|
|
if (store == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
LightningNodeViewModel vm = new LightningNodeViewModel();
|
|
|
|
|
vm.SetCryptoCurrencies(_NetworkProvider, selectedCrypto);
|
2018-03-20 18:09:25 +01:00
|
|
|
|
vm.InternalLightningNode = GetInternalLighningNode(selectedCrypto)?.ToUri(true)?.AbsoluteUri;
|
2018-02-25 16:48:12 +01:00
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-20 18:09:25 +01:00
|
|
|
|
private LightningConnectionString GetInternalLighningNode(string selectedCrypto)
|
|
|
|
|
{
|
|
|
|
|
selectedCrypto = "BTC";
|
|
|
|
|
if (_BtcpayServerOptions.InternalLightningByCryptoCode.TryGetValue(selectedCrypto, out var connectionString))
|
|
|
|
|
{
|
|
|
|
|
return CanUseInternalLightning() ? connectionString : null;
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-25 16:48:12 +01:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("{storeId}/lightning")]
|
|
|
|
|
public async Task<IActionResult> AddLightningNode(string storeId, LightningNodeViewModel vm, string command)
|
|
|
|
|
{
|
|
|
|
|
var store = await _Repo.FindStore(storeId, GetUserId());
|
|
|
|
|
if (store == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
var network = vm.CryptoCurrency == null ? null : _ExplorerProvider.GetNetwork(vm.CryptoCurrency);
|
|
|
|
|
vm.SetCryptoCurrencies(_NetworkProvider, vm.CryptoCurrency);
|
2018-03-20 18:09:25 +01:00
|
|
|
|
|
|
|
|
|
var internalLightning = GetInternalLighningNode(network.CryptoCode);
|
|
|
|
|
vm.InternalLightningNode = internalLightning?.ToUri(true)?.AbsoluteUri;
|
2018-03-20 03:59:43 +01:00
|
|
|
|
if (network == null)
|
2018-02-25 16:48:12 +01:00
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(vm.CryptoCurrency), "Invalid network");
|
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PaymentMethodId paymentMethodId = new PaymentMethodId(network.CryptoCode, PaymentTypes.LightningLike);
|
|
|
|
|
Payments.Lightning.LightningSupportedPaymentMethod paymentMethod = null;
|
|
|
|
|
if (!string.IsNullOrEmpty(vm.Url))
|
|
|
|
|
{
|
2018-03-20 18:09:25 +01:00
|
|
|
|
if (!LightningConnectionString.TryParse(vm.Url, out var connectionString, out var error))
|
2018-02-25 16:48:12 +01:00
|
|
|
|
{
|
2018-03-20 16:31:19 +01:00
|
|
|
|
ModelState.AddModelError(nameof(vm.Url), $"Invalid URL ({error})");
|
2018-02-25 16:48:12 +01:00
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
2018-02-26 10:58:02 +01:00
|
|
|
|
|
2018-03-20 18:09:25 +01:00
|
|
|
|
var internalDomain = internalLightning?.ToUri(false)?.DnsSafeHost;
|
2018-03-20 16:31:19 +01:00
|
|
|
|
bool isLocal = (internalDomain == "127.0.0.1" || internalDomain == "localhost");
|
2018-03-20 18:09:25 +01:00
|
|
|
|
|
2018-03-20 16:31:19 +01:00
|
|
|
|
bool isInternalNode = connectionString.ConnectionType == LightningConnectionType.CLightning ||
|
|
|
|
|
connectionString.BaseUri.DnsSafeHost == internalDomain ||
|
|
|
|
|
isLocal;
|
|
|
|
|
|
|
|
|
|
if (connectionString.BaseUri.Scheme == "http" && !isLocal)
|
2018-02-25 16:48:12 +01:00
|
|
|
|
{
|
2018-03-20 16:31:19 +01:00
|
|
|
|
if (!isInternalNode || (isInternalNode && !CanUseInternalLightning()))
|
2018-02-25 16:48:12 +01:00
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(vm.Url), "The url must be HTTPS");
|
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-26 10:58:02 +01:00
|
|
|
|
|
2018-03-20 16:31:19 +01:00
|
|
|
|
if (isInternalNode && !CanUseInternalLightning())
|
2018-02-26 10:58:02 +01:00
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(vm.Url), "Unauthorized url");
|
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-25 16:48:12 +01:00
|
|
|
|
paymentMethod = new Payments.Lightning.LightningSupportedPaymentMethod()
|
|
|
|
|
{
|
|
|
|
|
CryptoCode = paymentMethodId.CryptoCode
|
|
|
|
|
};
|
2018-03-20 16:31:19 +01:00
|
|
|
|
paymentMethod.SetLightningUrl(connectionString);
|
2018-02-25 16:48:12 +01:00
|
|
|
|
}
|
|
|
|
|
if (command == "save")
|
|
|
|
|
{
|
|
|
|
|
store.SetSupportedPaymentMethod(paymentMethodId, paymentMethod);
|
|
|
|
|
await _Repo.UpdateStore(store);
|
|
|
|
|
StatusMessage = $"Lightning node modified ({network.CryptoCode})";
|
|
|
|
|
return RedirectToAction(nameof(UpdateStore), new { storeId = storeId });
|
|
|
|
|
}
|
|
|
|
|
else // if(command == "test")
|
|
|
|
|
{
|
|
|
|
|
if (paymentMethod == null)
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(vm.Url), "Missing url parameter");
|
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
|
|
|
|
var handler = (LightningLikePaymentHandler)_ServiceProvider.GetRequiredService<IPaymentMethodHandler<Payments.Lightning.LightningSupportedPaymentMethod>>();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await handler.Test(paymentMethod, network);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
vm.StatusMessage = $"Error: {ex.Message}";
|
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
|
|
|
|
vm.StatusMessage = "Connection to the lightning node succeed";
|
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-02-26 10:58:02 +01:00
|
|
|
|
|
|
|
|
|
private bool CanUseInternalLightning()
|
|
|
|
|
{
|
|
|
|
|
return (_BTCPayEnv.IsDevelopping || User.IsInRole(Roles.ServerAdmin));
|
|
|
|
|
}
|
2018-02-25 16:48:12 +01:00
|
|
|
|
}
|
|
|
|
|
}
|