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(
|
2021-12-31 16:59:02 +09:00
|
|
|
BTCPayNetworkProvider btcPayNetworkProvider, ISettingsRepository settingsRepository, LightningClientFactoryService lightningClientFactory,
|
2021-03-02 11:11:58 +09:00
|
|
|
IOptions<LightningNetworkOptions> lightningNetworkOptions,
|
|
|
|
IAuthorizationService authorizationService) : base(
|
2021-07-27 14:08:54 +02:00
|
|
|
btcPayNetworkProvider, settingsRepository, 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")]
|
|
|
|
public override Task<IActionResult> GetInfo(string cryptoCode)
|
|
|
|
{
|
|
|
|
return base.GetInfo(cryptoCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/server/lightning/{cryptoCode}/connect")]
|
|
|
|
public override Task<IActionResult> ConnectToNode(string cryptoCode, ConnectToNodeRequest request)
|
|
|
|
{
|
|
|
|
return base.ConnectToNode(cryptoCode, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/server/lightning/{cryptoCode}/channels")]
|
|
|
|
public override Task<IActionResult> GetChannels(string cryptoCode)
|
|
|
|
{
|
|
|
|
return base.GetChannels(cryptoCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/server/lightning/{cryptoCode}/channels")]
|
|
|
|
public override Task<IActionResult> OpenChannel(string cryptoCode, OpenLightningChannelRequest request)
|
|
|
|
{
|
|
|
|
return base.OpenChannel(cryptoCode, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
2020-06-08 23:40:58 +09:00
|
|
|
[HttpPost("~/api/v1/server/lightning/{cryptoCode}/address")]
|
2020-05-29 02:00:13 +02:00
|
|
|
public override Task<IActionResult> GetDepositAddress(string cryptoCode)
|
|
|
|
{
|
|
|
|
return base.GetDepositAddress(cryptoCode);
|
|
|
|
}
|
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}")]
|
|
|
|
public override Task<IActionResult> GetPayment(string cryptoCode, string paymentHash)
|
|
|
|
{
|
|
|
|
return base.GetPayment(cryptoCode, paymentHash);
|
|
|
|
}
|
|
|
|
|
2020-05-29 02:00:13 +02:00
|
|
|
[Authorize(Policy = Policies.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpGet("~/api/v1/server/lightning/{cryptoCode}/invoices/{id}")]
|
|
|
|
public override Task<IActionResult> GetInvoice(string cryptoCode, string id)
|
|
|
|
{
|
|
|
|
return base.GetInvoice(cryptoCode, id);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanUseInternalLightningNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/server/lightning/{cryptoCode}/invoices/pay")]
|
|
|
|
public override Task<IActionResult> PayInvoice(string cryptoCode, PayLightningInvoiceRequest lightningInvoice)
|
|
|
|
{
|
|
|
|
return base.PayInvoice(cryptoCode, lightningInvoice);
|
|
|
|
}
|
|
|
|
|
|
|
|
[Authorize(Policy = Policies.CanCreateLightningInvoiceInternalNode,
|
|
|
|
AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
|
|
|
|
[HttpPost("~/api/v1/server/lightning/{cryptoCode}/invoices")]
|
|
|
|
public override Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request)
|
|
|
|
{
|
|
|
|
return base.CreateInvoice(cryptoCode, request);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|