Greenfield: Allow for cancellation of Lightning method calls (#3674)

This commit is contained in:
d11n 2022-04-26 03:29:20 +02:00 committed by GitHub
parent 8f0ac61634
commit 261a3ecee3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 75 additions and 85 deletions

View File

@ -1,3 +1,4 @@
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants; using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Abstractions.Contracts; using BTCPayServer.Abstractions.Contracts;
@ -40,9 +41,9 @@ namespace BTCPayServer.Controllers.Greenfield
[Authorize(Policy = Policies.CanUseInternalLightningNode, [Authorize(Policy = Policies.CanUseInternalLightningNode,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/server/lightning/{cryptoCode}/info")] [HttpGet("~/api/v1/server/lightning/{cryptoCode}/info")]
public override Task<IActionResult> GetInfo(string cryptoCode) public override Task<IActionResult> GetInfo(string cryptoCode, CancellationToken cancellationToken = default)
{ {
return base.GetInfo(cryptoCode); return base.GetInfo(cryptoCode, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseInternalLightningNode, [Authorize(Policy = Policies.CanUseInternalLightningNode,
@ -56,17 +57,17 @@ namespace BTCPayServer.Controllers.Greenfield
[Authorize(Policy = Policies.CanUseInternalLightningNode, [Authorize(Policy = Policies.CanUseInternalLightningNode,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/server/lightning/{cryptoCode}/channels")] [HttpGet("~/api/v1/server/lightning/{cryptoCode}/channels")]
public override Task<IActionResult> GetChannels(string cryptoCode) public override Task<IActionResult> GetChannels(string cryptoCode, CancellationToken cancellationToken = default)
{ {
return base.GetChannels(cryptoCode); return base.GetChannels(cryptoCode, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseInternalLightningNode, [Authorize(Policy = Policies.CanUseInternalLightningNode,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPost("~/api/v1/server/lightning/{cryptoCode}/channels")] [HttpPost("~/api/v1/server/lightning/{cryptoCode}/channels")]
public override Task<IActionResult> OpenChannel(string cryptoCode, OpenLightningChannelRequest request) public override Task<IActionResult> OpenChannel(string cryptoCode, OpenLightningChannelRequest request, CancellationToken cancellationToken = default)
{ {
return base.OpenChannel(cryptoCode, request); return base.OpenChannel(cryptoCode, request, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseInternalLightningNode, [Authorize(Policy = Policies.CanUseInternalLightningNode,
@ -80,33 +81,33 @@ namespace BTCPayServer.Controllers.Greenfield
[Authorize(Policy = Policies.CanUseInternalLightningNode, [Authorize(Policy = Policies.CanUseInternalLightningNode,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/server/lightning/{cryptoCode}/payments/{paymentHash}")] [HttpGet("~/api/v1/server/lightning/{cryptoCode}/payments/{paymentHash}")]
public override Task<IActionResult> GetPayment(string cryptoCode, string paymentHash) public override Task<IActionResult> GetPayment(string cryptoCode, string paymentHash, CancellationToken cancellationToken = default)
{ {
return base.GetPayment(cryptoCode, paymentHash); return base.GetPayment(cryptoCode, paymentHash, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseInternalLightningNode, [Authorize(Policy = Policies.CanUseInternalLightningNode,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/server/lightning/{cryptoCode}/invoices/{id}")] [HttpGet("~/api/v1/server/lightning/{cryptoCode}/invoices/{id}")]
public override Task<IActionResult> GetInvoice(string cryptoCode, string id) public override Task<IActionResult> GetInvoice(string cryptoCode, string id, CancellationToken cancellationToken = default)
{ {
return base.GetInvoice(cryptoCode, id); return base.GetInvoice(cryptoCode, id, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseInternalLightningNode, [Authorize(Policy = Policies.CanUseInternalLightningNode,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPost("~/api/v1/server/lightning/{cryptoCode}/invoices/pay")] [HttpPost("~/api/v1/server/lightning/{cryptoCode}/invoices/pay")]
public override Task<IActionResult> PayInvoice(string cryptoCode, PayLightningInvoiceRequest lightningInvoice) public override Task<IActionResult> PayInvoice(string cryptoCode, PayLightningInvoiceRequest lightningInvoice, CancellationToken cancellationToken = default)
{ {
return base.PayInvoice(cryptoCode, lightningInvoice); return base.PayInvoice(cryptoCode, lightningInvoice, cancellationToken);
} }
[Authorize(Policy = Policies.CanCreateLightningInvoiceInternalNode, [Authorize(Policy = Policies.CanCreateLightningInvoiceInternalNode,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPost("~/api/v1/server/lightning/{cryptoCode}/invoices")] [HttpPost("~/api/v1/server/lightning/{cryptoCode}/invoices")]
public override Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request) public override Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request, CancellationToken cancellationToken = default)
{ {
return base.CreateInvoice(cryptoCode, request); return base.CreateInvoice(cryptoCode, request, cancellationToken);
} }
protected override async Task<ILightningClient> GetLightningClient(string cryptoCode, bool doingAdminThings) protected override async Task<ILightningClient> GetLightningClient(string cryptoCode, bool doingAdminThings)

View File

@ -1,4 +1,5 @@
using System.Linq; using System.Linq;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants; using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Abstractions.Contracts; using BTCPayServer.Abstractions.Contracts;
@ -42,9 +43,9 @@ namespace BTCPayServer.Controllers.Greenfield
[Authorize(Policy = Policies.CanUseLightningNodeInStore, [Authorize(Policy = Policies.CanUseLightningNodeInStore,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/info")] [HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/info")]
public override Task<IActionResult> GetInfo(string cryptoCode) public override Task<IActionResult> GetInfo(string cryptoCode, CancellationToken cancellationToken = default)
{ {
return base.GetInfo(cryptoCode); return base.GetInfo(cryptoCode, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseLightningNodeInStore, [Authorize(Policy = Policies.CanUseLightningNodeInStore,
@ -57,16 +58,16 @@ namespace BTCPayServer.Controllers.Greenfield
[Authorize(Policy = Policies.CanUseLightningNodeInStore, [Authorize(Policy = Policies.CanUseLightningNodeInStore,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/channels")] [HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/channels")]
public override Task<IActionResult> GetChannels(string cryptoCode) public override Task<IActionResult> GetChannels(string cryptoCode, CancellationToken cancellationToken = default)
{ {
return base.GetChannels(cryptoCode); return base.GetChannels(cryptoCode, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseLightningNodeInStore, [Authorize(Policy = Policies.CanUseLightningNodeInStore,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/channels")] [HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/channels")]
public override Task<IActionResult> OpenChannel(string cryptoCode, OpenLightningChannelRequest request) public override Task<IActionResult> OpenChannel(string cryptoCode, OpenLightningChannelRequest request, CancellationToken cancellationToken = default)
{ {
return base.OpenChannel(cryptoCode, request); return base.OpenChannel(cryptoCode, request, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseLightningNodeInStore, [Authorize(Policy = Policies.CanUseLightningNodeInStore,
@ -80,33 +81,33 @@ namespace BTCPayServer.Controllers.Greenfield
[Authorize(Policy = Policies.CanUseLightningNodeInStore, [Authorize(Policy = Policies.CanUseLightningNodeInStore,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/payments/{paymentHash}")] [HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/payments/{paymentHash}")]
public override Task<IActionResult> GetPayment(string cryptoCode, string paymentHash) public override Task<IActionResult> GetPayment(string cryptoCode, string paymentHash, CancellationToken cancellationToken = default)
{ {
return base.GetPayment(cryptoCode, paymentHash); return base.GetPayment(cryptoCode, paymentHash, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseLightningNodeInStore, [Authorize(Policy = Policies.CanUseLightningNodeInStore,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/pay")] [HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/pay")]
public override Task<IActionResult> PayInvoice(string cryptoCode, PayLightningInvoiceRequest lightningInvoice) public override Task<IActionResult> PayInvoice(string cryptoCode, PayLightningInvoiceRequest lightningInvoice, CancellationToken cancellationToken = default)
{ {
return base.PayInvoice(cryptoCode, lightningInvoice); return base.PayInvoice(cryptoCode, lightningInvoice, cancellationToken);
} }
[Authorize(Policy = Policies.CanUseLightningNodeInStore, [Authorize(Policy = Policies.CanUseLightningNodeInStore,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/{id}")] [HttpGet("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices/{id}")]
public override Task<IActionResult> GetInvoice(string cryptoCode, string id) public override Task<IActionResult> GetInvoice(string cryptoCode, string id, CancellationToken cancellationToken = default)
{ {
return base.GetInvoice(cryptoCode, id); return base.GetInvoice(cryptoCode, id, cancellationToken);
} }
[Authorize(Policy = Policies.CanCreateLightningInvoiceInStore, [Authorize(Policy = Policies.CanCreateLightningInvoiceInStore,
AuthenticationSchemes = AuthenticationSchemes.Greenfield)] AuthenticationSchemes = AuthenticationSchemes.Greenfield)]
[HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices")] [HttpPost("~/api/v1/stores/{storeId}/lightning/{cryptoCode}/invoices")]
public override Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request) public override Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request, CancellationToken cancellationToken = default)
{ {
return base.CreateInvoice(cryptoCode, request); return base.CreateInvoice(cryptoCode, request, cancellationToken);
} }
protected override Task<ILightningClient> GetLightningClient(string cryptoCode, protected override Task<ILightningClient> GetLightningClient(string cryptoCode,

View File

@ -39,11 +39,11 @@ namespace BTCPayServer.Controllers.Greenfield
_authorizationService = authorizationService; _authorizationService = authorizationService;
} }
public virtual async Task<IActionResult> GetInfo(string cryptoCode) public virtual async Task<IActionResult> GetInfo(string cryptoCode, CancellationToken cancellationToken = default)
{ {
var lightningClient = await GetLightningClient(cryptoCode, true); var lightningClient = await GetLightningClient(cryptoCode, true);
var info = await lightningClient.GetInfo(); var info = await lightningClient.GetInfo(cancellationToken);
return Ok(new LightningNodeInformationData() return Ok(new LightningNodeInformationData
{ {
BlockHeight = info.BlockHeight, BlockHeight = info.BlockHeight,
NodeURIs = info.NodeInfoList.Select(nodeInfo => nodeInfo).ToArray() NodeURIs = info.NodeInfoList.Select(nodeInfo => nodeInfo).ToArray()
@ -75,12 +75,12 @@ namespace BTCPayServer.Controllers.Greenfield
return Ok(); return Ok();
} }
public virtual async Task<IActionResult> GetChannels(string cryptoCode) public virtual async Task<IActionResult> GetChannels(string cryptoCode, CancellationToken cancellationToken = default)
{ {
var lightningClient = await GetLightningClient(cryptoCode, true); var lightningClient = await GetLightningClient(cryptoCode, true);
var channels = await lightningClient.ListChannels(); var channels = await lightningClient.ListChannels(cancellationToken);
return Ok(channels.Select(channel => new LightningChannelData() return Ok(channels.Select(channel => new LightningChannelData
{ {
Capacity = channel.Capacity, Capacity = channel.Capacity,
ChannelPoint = channel.ChannelPoint.ToString(), ChannelPoint = channel.ChannelPoint.ToString(),
@ -92,7 +92,7 @@ namespace BTCPayServer.Controllers.Greenfield
} }
public virtual async Task<IActionResult> OpenChannel(string cryptoCode, OpenLightningChannelRequest request) public virtual async Task<IActionResult> OpenChannel(string cryptoCode, OpenLightningChannelRequest request, CancellationToken cancellationToken = default)
{ {
var lightningClient = await GetLightningClient(cryptoCode, true); var lightningClient = await GetLightningClient(cryptoCode, true);
if (request?.NodeURI is null) if (request?.NodeURI is null)
@ -124,12 +124,12 @@ namespace BTCPayServer.Controllers.Greenfield
return this.CreateValidationError(ModelState); return this.CreateValidationError(ModelState);
} }
var response = await lightningClient.OpenChannel(new Lightning.OpenChannelRequest() var response = await lightningClient.OpenChannel(new OpenChannelRequest
{ {
ChannelAmount = request.ChannelAmount, ChannelAmount = request.ChannelAmount,
FeeRate = request.FeeRate, FeeRate = request.FeeRate,
NodeInfo = request.NodeURI NodeInfo = request.NodeURI
}); }, cancellationToken);
string errorCode, errorMessage; string errorCode, errorMessage;
switch (response.Result) switch (response.Result)
@ -164,14 +164,14 @@ namespace BTCPayServer.Controllers.Greenfield
return Ok(new JValue((await lightningClient.GetDepositAddress()).ToString())); return Ok(new JValue((await lightningClient.GetDepositAddress()).ToString()));
} }
public virtual async Task<IActionResult> GetPayment(string cryptoCode, string paymentHash) public virtual async Task<IActionResult> GetPayment(string cryptoCode, string paymentHash, CancellationToken cancellationToken = default)
{ {
var lightningClient = await GetLightningClient(cryptoCode, false); var lightningClient = await GetLightningClient(cryptoCode, false);
var payment = await lightningClient.GetPayment(paymentHash); var payment = await lightningClient.GetPayment(paymentHash, cancellationToken);
return payment == null ? this.CreateAPIError(404, "payment-not-found", "Impossible to find a lightning payment with this payment hash") : Ok(ToModel(payment)); return payment == null ? this.CreateAPIError(404, "payment-not-found", "Impossible to find a lightning payment with this payment hash") : Ok(ToModel(payment));
} }
public virtual async Task<IActionResult> PayInvoice(string cryptoCode, PayLightningInvoiceRequest lightningInvoice) public virtual async Task<IActionResult> PayInvoice(string cryptoCode, PayLightningInvoiceRequest lightningInvoice, CancellationToken cancellationToken = default)
{ {
var lightningClient = await GetLightningClient(cryptoCode, true); var lightningClient = await GetLightningClient(cryptoCode, true);
var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode); var network = _btcPayNetworkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
@ -190,7 +190,7 @@ namespace BTCPayServer.Controllers.Greenfield
var param = lightningInvoice?.MaxFeeFlat != null || lightningInvoice?.MaxFeePercent != null var param = lightningInvoice?.MaxFeeFlat != null || lightningInvoice?.MaxFeePercent != null
? new PayInvoiceParams { MaxFeePercent = lightningInvoice.MaxFeePercent, MaxFeeFlat = lightningInvoice.MaxFeeFlat } ? new PayInvoiceParams { MaxFeePercent = lightningInvoice.MaxFeePercent, MaxFeeFlat = lightningInvoice.MaxFeeFlat }
: null; : null;
var result = await lightningClient.Pay(lightningInvoice.BOLT11, param); var result = await lightningClient.Pay(lightningInvoice.BOLT11, param, cancellationToken);
return result.Result switch return result.Result switch
{ {
@ -205,14 +205,14 @@ namespace BTCPayServer.Controllers.Greenfield
}; };
} }
public virtual async Task<IActionResult> GetInvoice(string cryptoCode, string id) public virtual async Task<IActionResult> GetInvoice(string cryptoCode, string id, CancellationToken cancellationToken = default)
{ {
var lightningClient = await GetLightningClient(cryptoCode, false); var lightningClient = await GetLightningClient(cryptoCode, false);
var inv = await lightningClient.GetInvoice(id); var inv = await lightningClient.GetInvoice(id, cancellationToken);
return inv == null ? this.CreateAPIError(404, "invoice-not-found", "Impossible to find a lightning invoice with this id") : Ok(ToModel(inv)); return inv == null ? this.CreateAPIError(404, "invoice-not-found", "Impossible to find a lightning invoice with this id") : Ok(ToModel(inv));
} }
public virtual async Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request) public virtual async Task<IActionResult> CreateInvoice(string cryptoCode, CreateLightningInvoiceRequest request, CancellationToken cancellationToken = default)
{ {
var lightningClient = await GetLightningClient(cryptoCode, false); var lightningClient = await GetLightningClient(cryptoCode, false);
if (request.Amount < LightMoney.Zero) if (request.Amount < LightMoney.Zero)
@ -241,7 +241,7 @@ namespace BTCPayServer.Controllers.Greenfield
{ {
PrivateRouteHints = request.PrivateRouteHints, DescriptionHash = request.DescriptionHash PrivateRouteHints = request.PrivateRouteHints, DescriptionHash = request.DescriptionHash
}; };
var invoice = await lightningClient.CreateInvoice(param, CancellationToken.None); var invoice = await lightningClient.CreateInvoice(param, cancellationToken);
return Ok(ToModel(invoice)); return Ok(ToModel(invoice));
} }
catch (Exception ex) catch (Exception ex)

View File

@ -480,8 +480,7 @@ namespace BTCPayServer.Controllers.Greenfield
} }
public override async Task<PayoutData> ApprovePayout(string storeId, string payoutId, public override async Task<PayoutData> ApprovePayout(string storeId, string payoutId,
ApprovePayoutRequest request, ApprovePayoutRequest request, CancellationToken cancellationToken = default)
CancellationToken cancellationToken = default)
{ {
return GetFromActionResult<PayoutData>( return GetFromActionResult<PayoutData>(
await _greenfieldPullPaymentController.ApprovePayout(storeId, payoutId, request, cancellationToken)); await _greenfieldPullPaymentController.ApprovePayout(storeId, payoutId, request, cancellationToken));
@ -491,12 +490,11 @@ namespace BTCPayServer.Controllers.Greenfield
CancellationToken token = default) CancellationToken token = default)
{ {
return GetFromActionResult<LightningNodeInformationData>( return GetFromActionResult<LightningNodeInformationData>(
await _storeLightningNodeApiController.GetInfo(cryptoCode)); await _storeLightningNodeApiController.GetInfo(cryptoCode, token));
} }
public override async Task ConnectToLightningNode(string storeId, string cryptoCode, public override async Task ConnectToLightningNode(string storeId, string cryptoCode,
ConnectToNodeRequest request, ConnectToNodeRequest request, CancellationToken token = default)
CancellationToken token = default)
{ {
HandleActionResult(await _storeLightningNodeApiController.ConnectToNode(cryptoCode, request)); HandleActionResult(await _storeLightningNodeApiController.ConnectToNode(cryptoCode, request));
} }
@ -505,14 +503,14 @@ namespace BTCPayServer.Controllers.Greenfield
string cryptoCode, CancellationToken token = default) string cryptoCode, CancellationToken token = default)
{ {
return GetFromActionResult<IEnumerable<LightningChannelData>>( return GetFromActionResult<IEnumerable<LightningChannelData>>(
await _storeLightningNodeApiController.GetChannels(cryptoCode)); await _storeLightningNodeApiController.GetChannels(cryptoCode, token));
} }
public override async Task OpenLightningChannel(string storeId, string cryptoCode, public override async Task OpenLightningChannel(string storeId, string cryptoCode,
OpenLightningChannelRequest request, OpenLightningChannelRequest request,
CancellationToken token = default) CancellationToken token = default)
{ {
HandleActionResult(await _storeLightningNodeApiController.OpenChannel(cryptoCode, request)); HandleActionResult(await _storeLightningNodeApiController.OpenChannel(cryptoCode, request, token));
} }
public override async Task<string> GetLightningDepositAddress(string storeId, string cryptoCode, public override async Task<string> GetLightningDepositAddress(string storeId, string cryptoCode,
@ -523,32 +521,30 @@ namespace BTCPayServer.Controllers.Greenfield
} }
public override async Task PayLightningInvoice(string storeId, string cryptoCode, public override async Task PayLightningInvoice(string storeId, string cryptoCode,
PayLightningInvoiceRequest request, PayLightningInvoiceRequest request, CancellationToken token = default)
CancellationToken token = default)
{ {
HandleActionResult(await _storeLightningNodeApiController.PayInvoice(cryptoCode, request)); HandleActionResult(await _storeLightningNodeApiController.PayInvoice(cryptoCode, request, token));
} }
public override async Task<LightningInvoiceData> GetLightningInvoice(string storeId, string cryptoCode, public override async Task<LightningInvoiceData> GetLightningInvoice(string storeId, string cryptoCode,
string invoiceId, CancellationToken token = default) string invoiceId, CancellationToken token = default)
{ {
return GetFromActionResult<LightningInvoiceData>( return GetFromActionResult<LightningInvoiceData>(
await _storeLightningNodeApiController.GetInvoice(cryptoCode, invoiceId)); await _storeLightningNodeApiController.GetInvoice(cryptoCode, invoiceId, token));
} }
public override async Task<LightningPaymentData> GetLightningPayment(string storeId, string cryptoCode, public override async Task<LightningPaymentData> GetLightningPayment(string storeId, string cryptoCode,
string paymentHash, CancellationToken token = default) string paymentHash, CancellationToken token = default)
{ {
return GetFromActionResult<LightningPaymentData>( return GetFromActionResult<LightningPaymentData>(
await _storeLightningNodeApiController.GetPayment(cryptoCode, paymentHash)); await _storeLightningNodeApiController.GetPayment(cryptoCode, paymentHash, token));
} }
public override async Task<LightningInvoiceData> CreateLightningInvoice(string storeId, string cryptoCode, public override async Task<LightningInvoiceData> CreateLightningInvoice(string storeId, string cryptoCode,
CreateLightningInvoiceRequest request, CreateLightningInvoiceRequest request, CancellationToken token = default)
CancellationToken token = default)
{ {
return GetFromActionResult<LightningInvoiceData>( return GetFromActionResult<LightningInvoiceData>(
await _storeLightningNodeApiController.CreateInvoice(cryptoCode, request)); await _storeLightningNodeApiController.CreateInvoice(cryptoCode, request, token));
} }
public override async Task<LightningNodeInformationData> GetLightningNodeInfo(string cryptoCode, public override async Task<LightningNodeInformationData> GetLightningNodeInfo(string cryptoCode,
@ -568,13 +564,13 @@ namespace BTCPayServer.Controllers.Greenfield
CancellationToken token = default) CancellationToken token = default)
{ {
return GetFromActionResult<IEnumerable<LightningChannelData>>( return GetFromActionResult<IEnumerable<LightningChannelData>>(
await _lightningNodeApiController.GetChannels(cryptoCode)); await _lightningNodeApiController.GetChannels(cryptoCode, token));
} }
public override async Task OpenLightningChannel(string cryptoCode, OpenLightningChannelRequest request, public override async Task OpenLightningChannel(string cryptoCode, OpenLightningChannelRequest request,
CancellationToken token = default) CancellationToken token = default)
{ {
HandleActionResult(await _lightningNodeApiController.OpenChannel(cryptoCode, request)); HandleActionResult(await _lightningNodeApiController.OpenChannel(cryptoCode, request, token));
} }
public override async Task<string> GetLightningDepositAddress(string cryptoCode, public override async Task<string> GetLightningDepositAddress(string cryptoCode,
@ -585,25 +581,24 @@ namespace BTCPayServer.Controllers.Greenfield
} }
public override async Task<LightningPaymentData> PayLightningInvoice(string cryptoCode, public override async Task<LightningPaymentData> PayLightningInvoice(string cryptoCode,
PayLightningInvoiceRequest request, PayLightningInvoiceRequest request, CancellationToken token = default)
CancellationToken token = default)
{ {
return GetFromActionResult<LightningPaymentData>( return GetFromActionResult<LightningPaymentData>(
await _lightningNodeApiController.PayInvoice(cryptoCode, request)); await _lightningNodeApiController.PayInvoice(cryptoCode, request, token));
} }
public override async Task<LightningInvoiceData> GetLightningInvoice(string cryptoCode, string invoiceId, public override async Task<LightningInvoiceData> GetLightningInvoice(string cryptoCode, string invoiceId,
CancellationToken token = default) CancellationToken token = default)
{ {
return GetFromActionResult<LightningInvoiceData>( return GetFromActionResult<LightningInvoiceData>(
await _lightningNodeApiController.GetInvoice(cryptoCode, invoiceId)); await _lightningNodeApiController.GetInvoice(cryptoCode, invoiceId, token));
} }
public override async Task<LightningPaymentData> GetLightningPayment(string cryptoCode, public override async Task<LightningPaymentData> GetLightningPayment(string cryptoCode,
string paymentHash, CancellationToken token = default) string paymentHash, CancellationToken token = default)
{ {
return GetFromActionResult<LightningPaymentData>( return GetFromActionResult<LightningPaymentData>(
await _lightningNodeApiController.GetPayment(cryptoCode, paymentHash)); await _lightningNodeApiController.GetPayment(cryptoCode, paymentHash, token));
} }
public override async Task<LightningInvoiceData> CreateLightningInvoice(string cryptoCode, public override async Task<LightningInvoiceData> CreateLightningInvoice(string cryptoCode,
@ -611,21 +606,18 @@ namespace BTCPayServer.Controllers.Greenfield
CancellationToken token = default) CancellationToken token = default)
{ {
return GetFromActionResult<LightningInvoiceData>( return GetFromActionResult<LightningInvoiceData>(
await _lightningNodeApiController.CreateInvoice(cryptoCode, request)); await _lightningNodeApiController.CreateInvoice(cryptoCode, request, token));
} }
private T GetFromActionResult<T>(IActionResult result) private T GetFromActionResult<T>(IActionResult result)
{ {
HandleActionResult(result); HandleActionResult(result);
switch (result) return result switch
{ {
case JsonResult jsonResult: JsonResult jsonResult => (T)jsonResult.Value,
return (T)jsonResult.Value; OkObjectResult { Value: T res } => res,
case OkObjectResult { Value: T res }: _ => default
return res; };
default:
return default;
}
} }
private void HandleActionResult(IActionResult result) private void HandleActionResult(IActionResult result)
@ -654,7 +646,7 @@ namespace BTCPayServer.Controllers.Greenfield
} }
public override Task<IEnumerable<OnChainPaymentMethodData>> GetStoreOnChainPaymentMethods(string storeId, public override Task<IEnumerable<OnChainPaymentMethodData>> GetStoreOnChainPaymentMethods(string storeId,
bool? enabled, CancellationToken token) bool? enabled, CancellationToken token = default)
{ {
return Task.FromResult( return Task.FromResult(
GetFromActionResult(_chainPaymentMethodsController.GetOnChainPaymentMethods(storeId, enabled))); GetFromActionResult(_chainPaymentMethodsController.GetOnChainPaymentMethods(storeId, enabled)));
@ -674,8 +666,7 @@ namespace BTCPayServer.Controllers.Greenfield
} }
public override async Task<OnChainPaymentMethodData> UpdateStoreOnChainPaymentMethod(string storeId, public override async Task<OnChainPaymentMethodData> UpdateStoreOnChainPaymentMethod(string storeId,
string cryptoCode, UpdateOnChainPaymentMethodRequest paymentMethod, string cryptoCode, UpdateOnChainPaymentMethodRequest paymentMethod, CancellationToken token = default)
CancellationToken token = default)
{ {
return GetFromActionResult<OnChainPaymentMethodData>( return GetFromActionResult<OnChainPaymentMethodData>(
await _chainPaymentMethodsController.UpdateOnChainPaymentMethod(storeId, cryptoCode, await _chainPaymentMethodsController.UpdateOnChainPaymentMethod(storeId, cryptoCode,
@ -698,8 +689,7 @@ namespace BTCPayServer.Controllers.Greenfield
} }
public override Task<OnChainPaymentMethodPreviewResultData> PreviewStoreOnChainPaymentMethodAddresses( public override Task<OnChainPaymentMethodPreviewResultData> PreviewStoreOnChainPaymentMethodAddresses(
string storeId, string cryptoCode, int offset = 0, int amount = 10, string storeId, string cryptoCode, int offset = 0, int amount = 10, CancellationToken token = default)
CancellationToken token = default)
{ {
return Task.FromResult(GetFromActionResult<OnChainPaymentMethodPreviewResultData>( return Task.FromResult(GetFromActionResult<OnChainPaymentMethodPreviewResultData>(
_chainPaymentMethodsController.GetOnChainPaymentMethodPreview(storeId, cryptoCode, offset, _chainPaymentMethodsController.GetOnChainPaymentMethodPreview(storeId, cryptoCode, offset,
@ -836,8 +826,7 @@ namespace BTCPayServer.Controllers.Greenfield
} }
public override async Task<OnChainWalletTransactionData> GetOnChainWalletTransaction(string storeId, public override async Task<OnChainWalletTransactionData> GetOnChainWalletTransaction(string storeId,
string cryptoCode, string transactionId, string cryptoCode, string transactionId, CancellationToken token = default)
CancellationToken token = default)
{ {
return GetFromActionResult<OnChainWalletTransactionData>( return GetFromActionResult<OnChainWalletTransactionData>(
await _storeOnChainWalletsController.GetOnChainWalletTransaction(storeId, cryptoCode, transactionId)); await _storeOnChainWalletsController.GetOnChainWalletTransaction(storeId, cryptoCode, transactionId));
@ -851,8 +840,7 @@ namespace BTCPayServer.Controllers.Greenfield
} }
public override async Task<OnChainWalletTransactionData> CreateOnChainTransaction(string storeId, public override async Task<OnChainWalletTransactionData> CreateOnChainTransaction(string storeId,
string cryptoCode, CreateOnChainTransactionRequest request, string cryptoCode, CreateOnChainTransactionRequest request, CancellationToken token = default)
CancellationToken token = default)
{ {
if (!request.ProceedWithBroadcast) if (!request.ProceedWithBroadcast)
{ {