mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-24 14:50:50 +01:00
* Add store name to LN info unavailable pae * Display multiple node info items if available Allows to view clearnet and Tor connection info side by side. * Re-add preferOnion for certain cases * HTML compatible node ids * Display more node connection failure details * Fix syntax error * Update BTCPayServer/Payments/Lightning/LightningLikePaymentHandler.cs Co-authored-by: Nicolas Dorier <nicolas.dorier@gmail.com> * View updates * Revert previous variable change * Keep logic out of the view Co-authored-by: Nicolas Dorier <nicolas.dorier@gmail.com>
110 lines
4.1 KiB
C#
110 lines
4.1 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BTCPayServer.Data;
|
|
using BTCPayServer.Filters;
|
|
using BTCPayServer.Lightning;
|
|
using BTCPayServer.Payments;
|
|
using BTCPayServer.Payments.Lightning;
|
|
using BTCPayServer.Services.Stores;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
namespace BTCPayServer.Controllers
|
|
{
|
|
|
|
[Route("embed/{storeId}/{cryptoCode}/ln")]
|
|
[AllowAnonymous]
|
|
public class PublicLightningNodeInfoController : Controller
|
|
{
|
|
private readonly BTCPayNetworkProvider _BtcPayNetworkProvider;
|
|
private readonly LightningLikePaymentHandler _LightningLikePaymentHandler;
|
|
private readonly StoreRepository _StoreRepository;
|
|
|
|
public PublicLightningNodeInfoController(BTCPayNetworkProvider btcPayNetworkProvider,
|
|
LightningLikePaymentHandler lightningLikePaymentHandler, StoreRepository storeRepository)
|
|
{
|
|
_BtcPayNetworkProvider = btcPayNetworkProvider;
|
|
_LightningLikePaymentHandler = lightningLikePaymentHandler;
|
|
_StoreRepository = storeRepository;
|
|
}
|
|
|
|
[HttpGet]
|
|
[XFrameOptions(XFrameOptionsAttribute.XFrameOptions.AllowAll)]
|
|
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);
|
|
var network = _BtcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
|
var nodeInfo = await _LightningLikePaymentHandler.GetNodeInfo(paymentMethodDetails, network);
|
|
|
|
return View(new ShowLightningNodeInfoViewModel
|
|
{
|
|
Available = true,
|
|
NodeInfo = nodeInfo.Select(n => new ShowLightningNodeInfoViewModel.NodeData(n)).ToArray(),
|
|
CryptoCode = cryptoCode,
|
|
CryptoImage = GetImage(paymentMethodDetails.PaymentId, network),
|
|
StoreName = store.StoreName
|
|
});
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return View(new ShowLightningNodeInfoViewModel
|
|
{
|
|
Available = false,
|
|
CryptoCode = cryptoCode,
|
|
StoreName = store.StoreName
|
|
});
|
|
}
|
|
}
|
|
|
|
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
|
|
{
|
|
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; }
|
|
public bool Available { get; set; }
|
|
public string CryptoCode { get; set; }
|
|
public string CryptoImage { get; set; }
|
|
public string StoreName { get; set; }
|
|
}
|
|
}
|