2020-05-29 02:00:13 +02:00
|
|
|
using System.Linq;
|
2022-04-26 03:29:20 +02:00
|
|
|
using System.Threading;
|
2020-05-29 02:00:13 +02:00
|
|
|
using System.Threading.Tasks;
|
2020-11-17 13:46:23 +01:00
|
|
|
using BTCPayServer.Abstractions.Constants;
|
2021-07-27 14:08:54 +02:00
|
|
|
using BTCPayServer.Abstractions.Contracts;
|
2022-04-22 12:17:20 +02:00
|
|
|
using BTCPayServer.Abstractions.Extensions;
|
2020-05-29 02:00:13 +02:00
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Client.Models;
|
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
using BTCPayServer.Lightning;
|
|
|
|
using BTCPayServer.Payments;
|
|
|
|
using BTCPayServer.Payments.Lightning;
|
|
|
|
using BTCPayServer.Services;
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2020-06-30 08:26:19 +02:00
|
|
|
using Microsoft.AspNetCore.Cors;
|
2020-05-29 02:00:13 +02:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2021-01-02 13:44:28 +01:00
|
|
|
using Microsoft.Extensions.Options;
|
2020-05-29 02:00:13 +02:00
|
|
|
|
2022-01-14 05:05:23 +01:00
|
|
|
namespace BTCPayServer.Controllers.Greenfield
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-06-08 16:40:58 +02:00
|
|
|
[LightningUnavailableExceptionFilter]
|
2020-06-30 08:26:19 +02:00
|
|
|
[EnableCors(CorsPolicies.All)]
|
2022-01-07 04:17:59 +01:00
|
|
|
public class GreenfieldStoreLightningNodeApiController : GreenfieldLightningNodeApiController
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2021-01-02 13:44:28 +01:00
|
|
|
private readonly IOptions<LightningNetworkOptions> _lightningNetworkOptions;
|
2020-05-29 02:00:13 +02:00
|
|
|
private readonly LightningClientFactoryService _lightningClientFactory;
|
|
|
|
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
|
|
|
|
|
2022-01-07 04:17:59 +01:00
|
|
|
public GreenfieldStoreLightningNodeApiController(
|
2021-01-02 13:44:28 +01:00
|
|
|
IOptions<LightningNetworkOptions> lightningNetworkOptions,
|
2020-05-29 02:00:13 +02:00
|
|
|
LightningClientFactoryService lightningClientFactory, BTCPayNetworkProvider btcPayNetworkProvider,
|
2022-05-24 06:18:16 +02:00
|
|
|
PoliciesSettings policiesSettings,
|
2021-03-02 03:11:58 +01:00
|
|
|
IAuthorizationService authorizationService) : base(
|
2022-05-24 06:18:16 +02:00
|
|
|
btcPayNetworkProvider, policiesSettings, authorizationService)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2021-01-02 13:44:28 +01:00
|
|
|
_lightningNetworkOptions = lightningNetworkOptions;
|
2020-05-29 02:00:13 +02:00
|
|
|
_lightningClientFactory = lightningClientFactory;
|
|
|
|
_btcPayNetworkProvider = btcPayNetworkProvider;
|
|
|
|
}
|
2022-06-23 06:42:28 +02:00
|
|
|
|
2020-05-29 02:00:13 +02:00
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/info")]
|
2022-04-26 03:29:20 +02:00
|
|
|
public override Task<IActionResult> GetInfo(string cryptoCode, CancellationToken cancellationToken = default)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2022-04-26 03:29:20 +02:00
|
|
|
return base.GetInfo(cryptoCode, cancellationToken);
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
|
2022-06-23 06:42:28 +02:00
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/balance")]
|
|
|
|
public override Task<IActionResult> GetBalance(string cryptoCode, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
return base.GetBalance(cryptoCode, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2020-05-29 02:00:13 +02:00
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/connect")]
|
2022-05-18 15:22:08 +02:00
|
|
|
public override Task<IActionResult> ConnectToNode(string cryptoCode, ConnectToNodeRequest request, CancellationToken cancellationToken = default)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2022-05-18 15:22:08 +02:00
|
|
|
return base.ConnectToNode(cryptoCode, request, cancellationToken);
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/channels")]
|
2022-04-26 03:29:20 +02:00
|
|
|
public override Task<IActionResult> GetChannels(string cryptoCode, CancellationToken cancellationToken = default)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2022-04-26 03:29:20 +02:00
|
|
|
return base.GetChannels(cryptoCode, cancellationToken);
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/channels")]
|
2022-04-26 03:29:20 +02:00
|
|
|
public override Task<IActionResult> OpenChannel(string cryptoCode, OpenLightningChannelRequest request, CancellationToken cancellationToken = default)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2022-04-26 03:29:20 +02:00
|
|
|
return base.OpenChannel(cryptoCode, request, cancellationToken);
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-06-08 16:40:58 +02:00
|
|
|
[HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/address")]
|
2022-05-18 15:22:08 +02:00
|
|
|
public override Task<IActionResult> GetDepositAddress(string cryptoCode, CancellationToken cancellationToken = default)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2022-05-18 15:22:08 +02:00
|
|
|
return base.GetDepositAddress(cryptoCode, cancellationToken);
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
|
2022-04-12 11:01:58 +02:00
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/payments/{paymentHash}")]
|
2022-04-26 03:29:20 +02:00
|
|
|
public override Task<IActionResult> GetPayment(string cryptoCode, string paymentHash, CancellationToken cancellationToken = default)
|
2022-04-12 11:01:58 +02:00
|
|
|
{
|
2022-04-26 03:29:20 +02:00
|
|
|
return base.GetPayment(cryptoCode, paymentHash, cancellationToken);
|
2022-04-12 11:01:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-29 02:00:13 +02:00
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/pay")]
|
2022-04-26 03:29:20 +02:00
|
|
|
public override Task<IActionResult> PayInvoice(string cryptoCode, PayLightningInvoiceRequest lightningInvoice, CancellationToken cancellationToken = default)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2022-04-26 03:29:20 +02:00
|
|
|
return base.PayInvoice(cryptoCode, lightningInvoice, cancellationToken);
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-06-08 16:40:58 +02:00
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/{id}")]
|
2022-04-26 03:29:20 +02:00
|
|
|
public override Task<IActionResult> GetInvoice(string cryptoCode, string id, CancellationToken cancellationToken = default)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2022-04-26 03:29:20 +02:00
|
|
|
return base.GetInvoice(cryptoCode, id, cancellationToken);
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
|
2022-10-17 09:51:15 +02:00
|
|
|
[Authorize(Policy = Policies.CanUseLightningNodeInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices")]
|
|
|
|
public override Task<IActionResult> GetInvoices(string cryptoCode, [FromQuery] bool? pendingOnly, [FromQuery] long? offsetIndex, CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
return base.GetInvoices(cryptoCode, pendingOnly, offsetIndex, cancellationToken);
|
|
|
|
}
|
|
|
|
|
2020-05-29 02:00:13 +02:00
|
|
|
[Authorize(Policy = Policies.CanCreateLightningInvoiceInStore,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices")]
|
2022-04-26 03:29:20 +02:00
|
|
|
public override Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request, CancellationToken cancellationToken = default)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2022-04-26 03:29:20 +02:00
|
|
|
return base.CreateInvoice(cryptoCode, request, cancellationToken);
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
|
2021-12-16 04:32:13 +01:00
|
|
|
protected override Task<ILightningClient> GetLightningClient(string cryptoCode,
|
2020-05-29 02:00:13 +02:00
|
|
|
bool doingAdminThings)
|
|
|
|
{
|
|
|
|
var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
2022-04-22 12:17:20 +02:00
|
|
|
if (network == null)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2021-12-16 04:32:13 +01:00
|
|
|
throw ErrorCryptoCodeNotFound();
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
2022-04-22 12:17:20 +02:00
|
|
|
|
|
|
|
var store = HttpContext.GetStoreData();
|
|
|
|
if (store == null)
|
|
|
|
{
|
|
|
|
throw new JsonHttpException(StoreNotFound());
|
|
|
|
}
|
|
|
|
|
2020-05-29 02:00:13 +02:00
|
|
|
var id = new PaymentMethodId(cryptoCode, PaymentTypes.LightningLike);
|
|
|
|
var existing = store.GetSupportedPaymentMethods(_btcPayNetworkProvider)
|
|
|
|
.OfType<LightningSupportedPaymentMethod>()
|
|
|
|
.FirstOrDefault(d => d.PaymentId == id);
|
2021-03-02 03:11:58 +01:00
|
|
|
if (existing == null)
|
2021-12-16 04:32:13 +01:00
|
|
|
throw ErrorLightningNodeNotConfiguredForStore();
|
2021-03-02 03:11:58 +01:00
|
|
|
if (existing.GetExternalLightningUrl() is LightningConnectionString connectionString)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2021-12-16 04:32:13 +01:00
|
|
|
return Task.FromResult(_lightningClientFactory.Create(connectionString, network));
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
2021-12-16 04:32:13 +01:00
|
|
|
else if (existing.IsInternalNode &&
|
|
|
|
_lightningNetworkOptions.Value.InternalLightningByCryptoCode.TryGetValue(network.CryptoCode,
|
|
|
|
out var internalLightningNode))
|
2021-03-02 03:11:58 +01:00
|
|
|
{
|
2022-01-11 09:22:10 +01:00
|
|
|
if (!User.IsInRole(Roles.ServerAdmin) && doingAdminThings)
|
2021-12-16 04:32:13 +01:00
|
|
|
{
|
|
|
|
throw ErrorShouldBeAdminForInternalNode();
|
|
|
|
}
|
|
|
|
return Task.FromResult(_lightningClientFactory.Create(internalLightningNode, network));
|
2021-03-02 03:11:58 +01:00
|
|
|
}
|
2021-12-16 04:32:13 +01:00
|
|
|
throw ErrorLightningNodeNotConfiguredForStore();
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
2022-04-22 12:17:20 +02:00
|
|
|
|
|
|
|
private IActionResult StoreNotFound()
|
|
|
|
{
|
|
|
|
return this.CreateAPIError(404, "store-not-found", "The store was not found");
|
|
|
|
}
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
}
|