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;
|
2020-05-29 02:00:13 +02:00
|
|
|
using BTCPayServer.Client;
|
|
|
|
using BTCPayServer.Client.Models;
|
|
|
|
using BTCPayServer.Configuration;
|
|
|
|
using BTCPayServer.HostedServices;
|
|
|
|
using BTCPayServer.Lightning;
|
|
|
|
using BTCPayServer.Security;
|
|
|
|
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 13:05:23 +09:00
|
|
|
namespace BTCPayServer.Controllers.Greenfield
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
|
|
|
[ApiController]
|
|
|
|
[Authorize(AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-06-08 23:40:58 +09:00
|
|
|
[LightningUnavailableExceptionFilter]
|
2020-06-30 08:26:19 +02:00
|
|
|
[EnableCors(CorsPolicies.All)]
|
2022-01-07 12:17:59 +09:00
|
|
|
public class GreenfieldInternalLightningNodeApiController : GreenfieldLightningNodeApiController
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
|
|
|
private readonly BTCPayNetworkProvider _btcPayNetworkProvider;
|
|
|
|
private readonly LightningClientFactoryService _lightningClientFactory;
|
2021-01-02 13:44:28 +01:00
|
|
|
private readonly IOptions<LightningNetworkOptions> _lightningNetworkOptions;
|
2020-05-29 02:00:13 +02:00
|
|
|
|
2022-01-07 12:17:59 +09:00
|
|
|
public GreenfieldInternalLightningNodeApiController(
|
2022-05-24 13:18:16 +09:00
|
|
|
BTCPayNetworkProvider btcPayNetworkProvider, PoliciesSettings policiesSettings, LightningClientFactoryService lightningClientFactory,
|
2021-03-02 11:11:58 +09:00
|
|
|
IOptions<LightningNetworkOptions> lightningNetworkOptions,
|
|
|
|
IAuthorizationService authorizationService) : base(
|
2022-05-24 13:18:16 +09:00
|
|
|
btcPayNetworkProvider, policiesSettings, authorizationService)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
|
|
|
_btcPayNetworkProvider = btcPayNetworkProvider;
|
|
|
|
_lightningClientFactory = lightningClientFactory;
|
2021-01-02 13:44:28 +01:00
|
|
|
_lightningNetworkOptions = lightningNetworkOptions;
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/server/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.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/server/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.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/server/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.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/server/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.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/server/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.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-06-08 23:40:58 +09:00
|
|
|
[HttpPost("~/api/v1/server/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
|
|
|
}
|
2020-06-28 17:55:27 +09:00
|
|
|
|
2022-04-12 11:01:58 +02:00
|
|
|
[Authorize(Policy = Policies.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/server/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.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/server/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
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/server/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.CanCreateLightningInvoiceInternalNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/server/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-03-02 11:11:58 +09:00
|
|
|
protected override async Task<ILightningClient> GetLightningClient(string cryptoCode, bool doingAdminThings)
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
|
|
|
var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
|
2021-12-16 12:32:13 +09:00
|
|
|
if (network is null)
|
|
|
|
throw ErrorCryptoCodeNotFound();
|
|
|
|
if (!_lightningNetworkOptions.Value.InternalLightningByCryptoCode.TryGetValue(network.CryptoCode,
|
|
|
|
out var internalLightningNode))
|
2020-05-29 02:00:13 +02:00
|
|
|
{
|
2021-12-16 12:32:13 +09:00
|
|
|
throw ErrorInternalLightningNodeNotConfigured();
|
|
|
|
}
|
|
|
|
if (!await CanUseInternalLightning(doingAdminThings))
|
|
|
|
{
|
|
|
|
throw ErrorShouldBeAdminForInternalNode();
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
2021-03-02 11:11:58 +09:00
|
|
|
return _lightningClientFactory.Create(internalLightningNode, network);
|
2020-05-29 02:00:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|