2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2019-01-07 09:52:27 +01:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Data;
|
2019-02-17 11:30:16 +01:00
|
|
|
using BTCPayServer.Filters;
|
2021-09-23 13:36:42 +02:00
|
|
|
using BTCPayServer.Lightning;
|
2021-10-25 08:18:02 +02:00
|
|
|
using BTCPayServer.Logging;
|
2023-12-01 16:13:44 +01:00
|
|
|
using BTCPayServer.Models;
|
2019-01-07 09:52:27 +01:00
|
|
|
using BTCPayServer.Payments;
|
|
|
|
using BTCPayServer.Payments.Lightning;
|
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-06-28 10:55:27 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2019-01-07 09:52:27 +01:00
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
{
|
|
|
|
[Route("embed/{storeId}/{cryptoCode}/ln")]
|
|
|
|
[AllowAnonymous]
|
2022-01-07 04:32:00 +01:00
|
|
|
public class UIPublicLightningNodeInfoController : Controller
|
2019-01-07 09:52:27 +01:00
|
|
|
{
|
|
|
|
private readonly BTCPayNetworkProvider _BtcPayNetworkProvider;
|
|
|
|
private readonly LightningLikePaymentHandler _LightningLikePaymentHandler;
|
|
|
|
private readonly StoreRepository _StoreRepository;
|
|
|
|
|
2022-01-07 04:32:00 +01:00
|
|
|
public UIPublicLightningNodeInfoController(BTCPayNetworkProvider btcPayNetworkProvider,
|
2019-01-07 09:52:27 +01:00
|
|
|
LightningLikePaymentHandler lightningLikePaymentHandler, StoreRepository storeRepository)
|
|
|
|
{
|
|
|
|
_BtcPayNetworkProvider = btcPayNetworkProvider;
|
|
|
|
_LightningLikePaymentHandler = lightningLikePaymentHandler;
|
|
|
|
_StoreRepository = storeRepository;
|
|
|
|
}
|
2020-06-28 10:55:27 +02:00
|
|
|
|
2019-01-07 09:52:27 +01:00
|
|
|
[HttpGet]
|
2023-03-01 07:27:18 +01:00
|
|
|
[XFrameOptions(XFrameOptionsAttribute.XFrameOptions.Unset)]
|
2019-01-07 09:52:27 +01:00
|
|
|
public async Task<IActionResult> ShowLightningNodeInfo(string storeId, string cryptoCode)
|
|
|
|
{
|
|
|
|
var store = await _StoreRepository.FindStore(storeId);
|
|
|
|
if (store == null)
|
|
|
|
return NotFound();
|
|
|
|
|
2023-11-21 11:53:24 +01:00
|
|
|
var storeBlob = store.GetStoreBlob();
|
|
|
|
var vm = new ShowLightningNodeInfoViewModel
|
|
|
|
{
|
|
|
|
CryptoCode = cryptoCode,
|
|
|
|
StoreName = store.StoreName,
|
2023-12-01 16:13:44 +01:00
|
|
|
StoreBranding = new StoreBrandingViewModel(storeBlob)
|
2023-11-21 11:53:24 +01:00
|
|
|
};
|
2019-01-07 09:52:27 +01:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var paymentMethodDetails = GetExistingLightningSupportedPaymentMethod(cryptoCode, store);
|
2019-05-29 11:43:50 +02:00
|
|
|
var network = _BtcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
2023-11-21 11:53:24 +01:00
|
|
|
var nodeInfo = await _LightningLikePaymentHandler.GetNodeInfo(paymentMethodDetails, network,
|
|
|
|
new InvoiceLogs(), throws: true);
|
2019-01-07 09:52:27 +01:00
|
|
|
|
2023-11-21 11:53:24 +01:00
|
|
|
vm.Available = true;
|
|
|
|
vm.CryptoImage = GetImage(paymentMethodDetails.PaymentId, network);
|
|
|
|
vm.NodeInfo = nodeInfo.Select(n => new ShowLightningNodeInfoViewModel.NodeData(n)).ToArray();
|
2019-01-07 09:52:27 +01:00
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
2023-11-21 11:53:24 +01:00
|
|
|
// ignored
|
2019-01-07 09:52:27 +01:00
|
|
|
}
|
2023-11-21 11:53:24 +01:00
|
|
|
|
|
|
|
return View(vm);
|
2019-01-07 09:52:27 +01:00
|
|
|
}
|
2020-06-28 10:55:27 +02:00
|
|
|
|
2019-01-07 09:52:27 +01:00
|
|
|
private LightningSupportedPaymentMethod GetExistingLightningSupportedPaymentMethod(string cryptoCode, StoreData store)
|
|
|
|
{
|
|
|
|
var id = new PaymentMethodId(cryptoCode, PaymentTypes.LightningLike);
|
|
|
|
var existing = store.GetSupportedPaymentMethods(_BtcPayNetworkProvider)
|
|
|
|
.OfType<LightningSupportedPaymentMethod>()
|
|
|
|
.FirstOrDefault(d => d.PaymentId == id);
|
|
|
|
return existing;
|
|
|
|
}
|
|
|
|
|
|
|
|
private string GetImage(PaymentMethodId paymentMethodId, BTCPayNetwork network)
|
|
|
|
{
|
|
|
|
var res = paymentMethodId.PaymentType == PaymentTypes.BTCLike
|
|
|
|
? Url.Content(network.CryptoImagePath)
|
|
|
|
: Url.Content(network.LightningImagePath);
|
|
|
|
return "/" + res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class ShowLightningNodeInfoViewModel
|
|
|
|
{
|
2021-09-23 13:36:42 +02:00
|
|
|
public class NodeData
|
|
|
|
{
|
|
|
|
string _connection;
|
|
|
|
public NodeData(NodeInfo nodeInfo)
|
|
|
|
{
|
|
|
|
_connection = nodeInfo.ToString();
|
|
|
|
Id = $"{nodeInfo.Host}-{nodeInfo.Port}".Replace(".", "-", StringComparison.OrdinalIgnoreCase);
|
|
|
|
IsTor = nodeInfo.IsTor;
|
|
|
|
}
|
|
|
|
public string Id { get; }
|
|
|
|
public bool IsTor { get; }
|
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return _connection;
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 16:13:44 +01:00
|
|
|
public StoreBrandingViewModel StoreBranding { get; set; }
|
2021-09-23 13:36:42 +02:00
|
|
|
public NodeData[] NodeInfo { get; set; }
|
2019-01-07 09:52:27 +01:00
|
|
|
public bool Available { get; set; }
|
|
|
|
public string CryptoCode { get; set; }
|
|
|
|
public string CryptoImage { get; set; }
|
2020-11-17 08:57:14 +01:00
|
|
|
public string StoreName { get; set; }
|
2019-01-07 09:52:27 +01:00
|
|
|
}
|
|
|
|
}
|