2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2019-01-07 09:52:27 +01:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using BTCPayServer.Data;
|
2019-02-17 19:30:16 +09: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;
|
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 17:55:27 +09: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 12:32:00 +09: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 12:32:00 +09: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 17:55:27 +09: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();
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var paymentMethodDetails = GetExistingLightningSupportedPaymentMethod(cryptoCode, store);
|
2019-05-29 09:43:50 +00:00
|
|
|
var network = _BtcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
2021-10-25 08:18:02 +02:00
|
|
|
var nodeInfo =
|
2022-11-05 12:21:24 +01:00
|
|
|
await _LightningLikePaymentHandler.GetNodeInfo(paymentMethodDetails, network, new InvoiceLogs(), throws: true);
|
2019-01-07 09:52:27 +01:00
|
|
|
|
2020-11-17 08:57:14 +01:00
|
|
|
return View(new ShowLightningNodeInfoViewModel
|
2019-01-07 09:52:27 +01:00
|
|
|
{
|
2022-11-05 12:21:24 +01:00
|
|
|
Available = true,
|
2021-09-23 13:36:42 +02:00
|
|
|
NodeInfo = nodeInfo.Select(n => new ShowLightningNodeInfoViewModel.NodeData(n)).ToArray(),
|
2019-01-07 09:52:27 +01:00
|
|
|
CryptoCode = cryptoCode,
|
2020-11-17 08:57:14 +01:00
|
|
|
CryptoImage = GetImage(paymentMethodDetails.PaymentId, network),
|
|
|
|
StoreName = store.StoreName
|
2019-01-07 09:52:27 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
2021-09-23 13:36:42 +02:00
|
|
|
return View(new ShowLightningNodeInfoViewModel
|
|
|
|
{
|
2021-12-31 16:59:02 +09:00
|
|
|
Available = false,
|
2021-09-23 13:36:42 +02:00
|
|
|
CryptoCode = cryptoCode,
|
|
|
|
StoreName = store.StoreName
|
|
|
|
});
|
2019-01-07 09:52:27 +01:00
|
|
|
}
|
|
|
|
}
|
2020-06-28 17:55:27 +09: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-23 13:36:42 +02:00
|
|
|
|
2019-01-07 09:52:27 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|