2020-06-28 21:44:35 -05:00
|
|
|
using System;
|
2024-04-04 16:31:04 +09:00
|
|
|
using System.Collections.Generic;
|
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;
|
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;
|
2024-05-09 02:18:02 +02:00
|
|
|
using BTCPayServer.Services;
|
2024-04-04 16:31:04 +09:00
|
|
|
using BTCPayServer.Services.Invoices;
|
2019-01-07 09:52:27 +01:00
|
|
|
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;
|
2024-10-07 19:58:08 +09:00
|
|
|
private readonly Dictionary<PaymentMethodId, ICheckoutModelExtension> _paymentModelExtensions;
|
2024-05-09 02:18:02 +02:00
|
|
|
private readonly UriResolver _uriResolver;
|
2024-04-04 16:31:04 +09:00
|
|
|
private readonly PaymentMethodHandlerDictionary _handlers;
|
2019-01-07 09:52:27 +01:00
|
|
|
private readonly StoreRepository _StoreRepository;
|
|
|
|
|
2022-01-07 12:32:00 +09:00
|
|
|
public UIPublicLightningNodeInfoController(BTCPayNetworkProvider btcPayNetworkProvider,
|
2024-10-07 19:58:08 +09:00
|
|
|
Dictionary<PaymentMethodId, ICheckoutModelExtension> paymentModelExtensions,
|
2024-05-09 02:18:02 +02:00
|
|
|
UriResolver uriResolver,
|
2024-04-04 16:31:04 +09:00
|
|
|
PaymentMethodHandlerDictionary handlers,
|
2024-04-24 10:12:58 +09:00
|
|
|
StoreRepository storeRepository)
|
2019-01-07 09:52:27 +01:00
|
|
|
{
|
|
|
|
_BtcPayNetworkProvider = btcPayNetworkProvider;
|
2024-04-04 16:31:04 +09:00
|
|
|
_paymentModelExtensions = paymentModelExtensions;
|
2024-05-09 02:18:02 +02:00
|
|
|
_uriResolver = uriResolver;
|
2024-04-04 16:31:04 +09:00
|
|
|
_handlers = handlers;
|
2019-01-07 09:52:27 +01:00
|
|
|
_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);
|
2024-04-24 10:12:58 +09:00
|
|
|
var pmi = PaymentTypes.LN.GetPaymentMethodId(cryptoCode);
|
|
|
|
if (store == null || _handlers.TryGet(pmi) is not LightningLikePaymentHandler handler)
|
2019-01-07 09:52:27 +01:00
|
|
|
return NotFound();
|
|
|
|
|
2023-11-21 11:53:24 +01:00
|
|
|
var storeBlob = store.GetStoreBlob();
|
|
|
|
var vm = new ShowLightningNodeInfoViewModel
|
|
|
|
{
|
|
|
|
CryptoCode = cryptoCode,
|
|
|
|
StoreName = store.StoreName,
|
2024-05-09 02:18:02 +02:00
|
|
|
StoreBranding = await StoreBrandingViewModel.CreateAsync(Request, _uriResolver, storeBlob)
|
2023-11-21 11:53:24 +01:00
|
|
|
};
|
2019-01-07 09:52:27 +01:00
|
|
|
try
|
|
|
|
{
|
2024-04-04 16:31:04 +09:00
|
|
|
var paymentMethodDetails = store.GetPaymentMethodConfig<LightningPaymentMethodConfig>(pmi, _handlers);
|
2024-04-24 10:12:58 +09:00
|
|
|
var nodeInfo = await handler.GetNodeInfo(paymentMethodDetails, null, throws: true);
|
2019-01-07 09:52:27 +01:00
|
|
|
|
2023-11-21 11:53:24 +01:00
|
|
|
vm.Available = true;
|
2024-04-04 16:31:04 +09:00
|
|
|
vm.CryptoImage = GetImage(pmi);
|
2023-11-21 11:53:24 +01:00
|
|
|
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 17:55:27 +09:00
|
|
|
|
2024-04-04 16:31:04 +09:00
|
|
|
private string GetImage(PaymentMethodId paymentMethodId)
|
2019-01-07 09:52:27 +01:00
|
|
|
{
|
2024-04-04 16:31:04 +09:00
|
|
|
if (_paymentModelExtensions.TryGetValue(paymentMethodId, out var paymentModelExtension))
|
|
|
|
{
|
|
|
|
return "/" + Url.Content(paymentModelExtension.Image);
|
|
|
|
}
|
|
|
|
return null;
|
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;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|