Hooks for Zaps (#4826)

This commit is contained in:
Andrew Camilleri
2023-03-29 12:27:04 +02:00
committed by GitHub
parent 83ea898780
commit 824e779eb2
2 changed files with 49 additions and 30 deletions

View File

@@ -53,7 +53,7 @@
<PackageReference Include="Fido2" Version="2.0.2" /> <PackageReference Include="Fido2" Version="2.0.2" />
<PackageReference Include="Fido2.AspNet" Version="2.0.2" /> <PackageReference Include="Fido2.AspNet" Version="2.0.2" />
<PackageReference Include="HtmlSanitizer" Version="5.0.372" /> <PackageReference Include="HtmlSanitizer" Version="5.0.372" />
<PackageReference Include="LNURL" Version="0.0.28" /> <PackageReference Include="LNURL" Version="0.0.29" />
<PackageReference Include="MailKit" Version="3.3.0" /> <PackageReference Include="MailKit" Version="3.3.0" />
<PackageReference Include="BTCPayServer.NETCore.Plugins.Mvc" Version="1.4.4" /> <PackageReference Include="BTCPayServer.NETCore.Plugins.Mvc" Version="1.4.4" />
<PackageReference Include="QRCoder" Version="1.4.3" /> <PackageReference Include="QRCoder" Version="1.4.3" />

View File

@@ -6,6 +6,7 @@ using System.Linq;
using System.Threading; 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.Extensions; using BTCPayServer.Abstractions.Extensions;
using BTCPayServer.Abstractions.Models; using BTCPayServer.Abstractions.Models;
using BTCPayServer.Client; using BTCPayServer.Client;
@@ -55,6 +56,7 @@ namespace BTCPayServer
private readonly LightningLikePayoutHandler _lightningLikePayoutHandler; private readonly LightningLikePayoutHandler _lightningLikePayoutHandler;
private readonly PullPaymentHostedService _pullPaymentHostedService; private readonly PullPaymentHostedService _pullPaymentHostedService;
private readonly BTCPayNetworkJsonSerializerSettings _btcPayNetworkJsonSerializerSettings; private readonly BTCPayNetworkJsonSerializerSettings _btcPayNetworkJsonSerializerSettings;
private readonly IPluginHookService _pluginHookService;
public UILNURLController(InvoiceRepository invoiceRepository, public UILNURLController(InvoiceRepository invoiceRepository,
EventAggregator eventAggregator, EventAggregator eventAggregator,
@@ -67,7 +69,8 @@ namespace BTCPayServer
LightningAddressService lightningAddressService, LightningAddressService lightningAddressService,
LightningLikePayoutHandler lightningLikePayoutHandler, LightningLikePayoutHandler lightningLikePayoutHandler,
PullPaymentHostedService pullPaymentHostedService, PullPaymentHostedService pullPaymentHostedService,
BTCPayNetworkJsonSerializerSettings btcPayNetworkJsonSerializerSettings) BTCPayNetworkJsonSerializerSettings btcPayNetworkJsonSerializerSettings,
IPluginHookService pluginHookService)
{ {
_invoiceRepository = invoiceRepository; _invoiceRepository = invoiceRepository;
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
@@ -81,6 +84,7 @@ namespace BTCPayServer
_lightningLikePayoutHandler = lightningLikePayoutHandler; _lightningLikePayoutHandler = lightningLikePayoutHandler;
_pullPaymentHostedService = pullPaymentHostedService; _pullPaymentHostedService = pullPaymentHostedService;
_btcPayNetworkJsonSerializerSettings = btcPayNetworkJsonSerializerSettings; _btcPayNetworkJsonSerializerSettings = btcPayNetworkJsonSerializerSettings;
_pluginHookService = pluginHookService;
} }
[HttpGet("withdraw/pp/{pullPaymentId}")] [HttpGet("withdraw/pp/{pullPaymentId}")]
@@ -453,17 +457,18 @@ namespace BTCPayServer
await _invoiceRepository.UpdateInvoicePaymentMethod(i.Id, pm); await _invoiceRepository.UpdateInvoicePaymentMethod(i.Id, pm);
} }
var description = blob.LightningDescriptionTemplate var invoiceDescription = blob.LightningDescriptionTemplate
.Replace("{StoreName}", store.StoreName ?? "", StringComparison.OrdinalIgnoreCase) .Replace("{StoreName}", store.StoreName ?? "", StringComparison.OrdinalIgnoreCase)
.Replace("{ItemDescription}", i.Metadata.ItemDesc ?? "", StringComparison.OrdinalIgnoreCase) .Replace("{ItemDescription}", i.Metadata.ItemDesc ?? "", StringComparison.OrdinalIgnoreCase)
.Replace("{OrderId}", i.Metadata.OrderId ?? "", StringComparison.OrdinalIgnoreCase); .Replace("{OrderId}", i.Metadata.OrderId ?? "", StringComparison.OrdinalIgnoreCase);
lnurlMetadata.Add(new[] { "text/plain", description }); lnurlMetadata.Add(new[] { "text/plain", invoiceDescription });
if (!string.IsNullOrEmpty(username)) if (!string.IsNullOrEmpty(username))
{ {
lnurlMetadata.Add(new[] { "text/identifier", lnAddress }); lnurlMetadata.Add(new[] { "text/identifier", lnAddress });
} }
return Ok(new LNURLPayRequest
if (await _pluginHookService.ApplyFilter("modify-lnurlp-request", new LNURLPayRequest
{ {
Tag = "payRequest", Tag = "payRequest",
MinSendable = new LightMoney(min ?? 1m, LightMoneyUnit.Satoshi), MinSendable = new LightMoney(min ?? 1m, LightMoneyUnit.Satoshi),
@@ -476,8 +481,12 @@ namespace BTCPayServer
Callback = new Uri(_linkGenerator.GetUriByAction( Callback = new Uri(_linkGenerator.GetUriByAction(
action: nameof(GetLNURLForInvoice), action: nameof(GetLNURLForInvoice),
controller: "UILNURL", controller: "UILNURL",
values: new { cryptoCode, invoiceId = i.Id }, Request.Scheme, Request.Host, Request.PathBase)) values: new {cryptoCode, invoiceId = i.Id}, Request.Scheme, Request.Host, Request.PathBase))
}); }) is not LNURLPayRequest lnurlp)
{
return NotFound();
}
return Ok(lnurlp);
} }
[HttpGet("pay/i/{invoiceId}")] [HttpGet("pay/i/{invoiceId}")]
@@ -530,12 +539,12 @@ namespace BTCPayServer
List<string[]> lnurlMetadata = new(); List<string[]> lnurlMetadata = new();
var blob = store.GetStoreBlob(); var blob = store.GetStoreBlob();
var description = blob.LightningDescriptionTemplate var invoiceDescription = blob.LightningDescriptionTemplate
.Replace("{StoreName}", store.StoreName ?? "", StringComparison.OrdinalIgnoreCase) .Replace("{StoreName}", store.StoreName ?? "", StringComparison.OrdinalIgnoreCase)
.Replace("{ItemDescription}", i.Metadata.ItemDesc ?? "", StringComparison.OrdinalIgnoreCase) .Replace("{ItemDescription}", i.Metadata.ItemDesc ?? "", StringComparison.OrdinalIgnoreCase)
.Replace("{OrderId}", i.Metadata.OrderId ?? "", StringComparison.OrdinalIgnoreCase); .Replace("{OrderId}", i.Metadata.OrderId ?? "", StringComparison.OrdinalIgnoreCase);
lnurlMetadata.Add(new[] { "text/plain", description }); lnurlMetadata.Add(new[] { "text/plain", invoiceDescription });
if (!string.IsNullOrEmpty(paymentMethodDetails.ConsumedLightningAddress)) if (!string.IsNullOrEmpty(paymentMethodDetails.ConsumedLightningAddress))
{ {
lnurlMetadata.Add(new[] { "text/identifier", paymentMethodDetails.ConsumedLightningAddress }); lnurlMetadata.Add(new[] { "text/identifier", paymentMethodDetails.ConsumedLightningAddress });
@@ -567,7 +576,7 @@ namespace BTCPayServer
if (amt is null) if (amt is null)
{ {
return Ok(new LNURLPayRequest if (await _pluginHookService.ApplyFilter("modify-lnurlp-request", new LNURLPayRequest
{ {
Tag = "payRequest", Tag = "payRequest",
MinSendable = min, MinSendable = min,
@@ -575,7 +584,12 @@ namespace BTCPayServer
CommentAllowed = lnurlSupportedPaymentMethod.LUD12Enabled ? 2000 : 0, CommentAllowed = lnurlSupportedPaymentMethod.LUD12Enabled ? 2000 : 0,
Metadata = metadata, Metadata = metadata,
Callback = new Uri(Request.GetCurrentUrl()) Callback = new Uri(Request.GetCurrentUrl())
}); }) is not LNURLPayRequest lnurlp)
{
return NotFound();
}
return Ok(lnurlp);
} }
if (string.IsNullOrEmpty(paymentMethodDetails.BOLT11) || paymentMethodDetails.GeneratedBoltAmount != amt) if (string.IsNullOrEmpty(paymentMethodDetails.BOLT11) || paymentMethodDetails.GeneratedBoltAmount != amt)
@@ -599,14 +613,19 @@ namespace BTCPayServer
try try
{ {
var expiry = i.ExpirationTime.ToUniversalTime() - DateTimeOffset.UtcNow; var expiry = i.ExpirationTime.ToUniversalTime() - DateTimeOffset.UtcNow;
var param = new CreateInvoiceParams(amt, metadata, expiry) var description = (await _pluginHookService.ApplyFilter("modify-lnurlp-description", metadata)) as string;
if (description is null)
{
return NotFound();
}
var param = new CreateInvoiceParams(amt, description, expiry)
{ {
PrivateRouteHints = blob.LightningPrivateRouteHints, PrivateRouteHints = blob.LightningPrivateRouteHints,
DescriptionHashOnly = true DescriptionHashOnly = true
}; };
invoice = await client.CreateInvoice(param); invoice = await client.CreateInvoice(param);
if (!BOLT11PaymentRequest.Parse(invoice.BOLT11, network.NBitcoinNetwork) if (!BOLT11PaymentRequest.Parse(invoice.BOLT11, network.NBitcoinNetwork)
.VerifyDescriptionHash(metadata)) .VerifyDescriptionHash(description))
{ {
return BadRequest(new LNUrlStatusResponse return BadRequest(new LNUrlStatusResponse
{ {