btcpayserver/BTCPayServer/Controllers/PublicLightningNodeInfoController.cs

90 lines
3.4 KiB
C#
Raw Normal View History

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;
using BTCPayServer.Filters;
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
{
2020-06-28 10:55:27 +02:00
2019-01-07 09:52:27 +01:00
[Route("embed/{storeId}/{cryptoCode}/ln")]
[AllowAnonymous]
public class PublicLightningNodeInfoController : Controller
{
private readonly BTCPayNetworkProvider _BtcPayNetworkProvider;
private readonly LightningLikePaymentHandler _LightningLikePaymentHandler;
private readonly StoreRepository _StoreRepository;
2020-06-28 10:55:27 +02:00
public PublicLightningNodeInfoController(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]
[XFrameOptions(XFrameOptionsAttribute.XFrameOptions.AllowAll)]
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);
var network = _BtcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
2019-01-07 09:52:27 +01:00
var nodeInfo =
await _LightningLikePaymentHandler.GetNodeInfo(Request.IsOnion(), paymentMethodDetails,
2019-01-07 09:52:27 +01:00
network);
return View(new ShowLightningNodeInfoViewModel
2019-01-07 09:52:27 +01:00
{
Available = true,
NodeInfo = nodeInfo.ToString(),
CryptoCode = cryptoCode,
CryptoImage = GetImage(paymentMethodDetails.PaymentId, network),
StoreName = store.StoreName
2019-01-07 09:52:27 +01:00
});
}
catch (Exception)
{
return View(new ShowLightningNodeInfoViewModel { Available = false, CryptoCode = cryptoCode });
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
{
public string NodeInfo { get; set; }
public bool Available { get; set; }
public string CryptoCode { get; set; }
public string CryptoImage { get; set; }
public string StoreName { get; set; }
2019-01-07 09:52:27 +01:00
}
}