Pending Transaction Webhook Providers foundation

This commit is contained in:
rockstardev
2025-03-23 10:58:45 -05:00
parent 1751ad7a81
commit acfd6059b1
5 changed files with 168 additions and 22 deletions

View File

@@ -1356,7 +1356,7 @@ namespace BTCPayServer.Controllers
if (vm.SigningContext.PendingTransactionId is not null)
{
var psbt = PSBT.Parse(vm.SigningContext.PSBT, NetworkProvider.GetNetwork<BTCPayNetwork>(walletId.CryptoCode).NBitcoinNetwork);
var pendingTransaction = await _pendingTransactionService.CollectSignature(walletId.CryptoCode, psbt, false, CancellationToken.None);
var pendingTransaction = await _pendingTransactionService.CollectSignature(walletId.CryptoCode, psbt, CancellationToken.None);
if (pendingTransaction != null)
return RedirectToAction(nameof(WalletTransactions), new { walletId = walletId.ToString() });

View File

@@ -20,12 +20,10 @@ using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;
namespace BTCPayServer.HostedServices;
public class PendingTransactionService(
DelayedTransactionBroadcaster broadcaster,
BTCPayNetworkProvider networkProvider,
ApplicationDbContextFactory dbContextFactory,
EventAggregator eventAggregator,
ILogger<PendingTransactionService> logger,
ExplorerClientProvider explorerClientProvider)
ILogger<PendingTransactionService> logger)
: EventHostedServiceBase(eventAggregator, logger), IPeriodicTask
{
protected override void SubscribeToEvents()
@@ -114,11 +112,15 @@ public class PendingTransactionService(
pendingTransaction.SetBlob(new PendingTransactionBlob { PSBT = psbt.ToBase64() });
ctx.PendingTransactions.Add(pendingTransaction);
await ctx.SaveChangesAsync(cancellationToken);
EventAggregator.Publish(new PendingTransactionEvent
{
Data = pendingTransaction,
Type = PendingTransactionEvent.Created
});
return pendingTransaction;
}
public async Task<PendingTransaction?> CollectSignature(string cryptoCode, PSBT psbt, bool broadcastIfComplete,
CancellationToken cancellationToken)
public async Task<PendingTransaction?> CollectSignature(string cryptoCode, PSBT psbt, CancellationToken cancellationToken)
{
var network = networkProvider.GetNetwork<BTCPayNetwork>(cryptoCode);
if (network is null)
@@ -187,23 +189,11 @@ public class PendingTransactionService(
}
await ctx.SaveChangesAsync(cancellationToken);
if (broadcastIfComplete && pendingTransaction.State == PendingTransactionState.Signed)
EventAggregator.Publish(new PendingTransactionEvent
{
var explorerClient = explorerClientProvider.GetExplorerClient(network);
var tx = originalPsbtWorkingCopyWithNewPsbt.ExtractTransaction();
var result = await explorerClient.BroadcastAsync(tx, cancellationToken);
if (result.Success)
{
pendingTransaction.State = PendingTransactionState.Broadcast;
await ctx.SaveChangesAsync(cancellationToken);
}
else
{
await broadcaster.Schedule(DateTimeOffset.Now, tx, network);
}
}
Data = pendingTransaction,
Type = PendingTransactionEvent.SignatureCollected
});
return pendingTransaction;
}
@@ -245,6 +235,21 @@ public class PendingTransactionService(
if (pt is null) return;
pt.State = PendingTransactionState.Broadcast;
await ctx.SaveChangesAsync();
EventAggregator.Publish(new PendingTransactionEvent
{
Data = pt,
Type = PendingTransactionEvent.Broadcast
});
}
public record PendingTransactionEvent
{
public const string Created = nameof(Created);
public const string SignatureCollected = nameof(SignatureCollected);
public const string Broadcast = nameof(Broadcast);
public PendingTransaction Data { get; set; } = null!;
public string Type { get; set; } = null!;
}
}

View File

@@ -0,0 +1,48 @@
using System.Globalization;
using System.Threading.Tasks;
using BTCPayServer.Client.Models;
using BTCPayServer.Controllers;
using BTCPayServer.Data;
using BTCPayServer.Services.Invoices;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;
namespace BTCPayServer.HostedServices.Webhooks;
public class PendingTransactionDeliveryRequest(
PendingTransaction pendingTransaction,
string webhookId,
WebhookEvent webhookEvent,
WebhookDeliveryData delivery,
WebhookBlob webhookBlob)
: WebhookSender.WebhookDeliveryRequest(webhookId, webhookEvent, delivery, webhookBlob)
{
public override Task<SendEmailRequest> Interpolate(SendEmailRequest req,
UIStoresController.StoreEmailRule storeEmailRule)
{
// if (storeEmailRule.CustomerEmail &&
// MailboxAddressValidator.TryParse(Invoice.Metadata.BuyerEmail, out var bmb))
// {
// req.Email ??= string.Empty;
// req.Email += $",{bmb}";
// }
req.Subject = Interpolate(req.Subject);
req.Body = Interpolate(req.Body);
return Task.FromResult(req);
}
private string Interpolate(string str)
{
var res = str.Replace("{PendingTransaction.Id}", pendingTransaction.TransactionId);
// .Replace("{Invoice.StoreId}", Invoice.StoreId)
// .Replace("{Invoice.Price}", Invoice.Price.ToString(CultureInfo.InvariantCulture))
// .Replace("{Invoice.Currency}", Invoice.Currency)
// .Replace("{Invoice.Status}", Invoice.Status.ToString())
// .Replace("{Invoice.AdditionalStatus}", Invoice.ExceptionStatus.ToString())
// .Replace("{Invoice.OrderId}", Invoice.Metadata.OrderId);
// res = InterpolateJsonField(res, "Invoice.Metadata", Invoice.Metadata.ToJObject());
return res;
}
}

View File

@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using BTCPayServer.Client.Models;
using BTCPayServer.Controllers.Greenfield;
using BTCPayServer.Data;
using BTCPayServer.Events;
using BTCPayServer.Services.Invoices;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using WebhookDeliveryData = BTCPayServer.Data.WebhookDeliveryData;
namespace BTCPayServer.HostedServices.Webhooks;
public class PendingTransactionWebhookProvider : WebhookProvider<PendingTransactionService.PendingTransactionEvent>
{
public PendingTransactionWebhookProvider(WebhookSender webhookSender, EventAggregator eventAggregator,
ILogger<InvoiceWebhookProvider> logger) : base(
eventAggregator, logger, webhookSender)
{
}
public const string PendingTransactionCreated = nameof(PendingTransactionCreated);
public const string PendingTransactionSignatureCollected = nameof(PendingTransactionSignatureCollected);
public const string PendingTransactionBroadcast = nameof(PendingTransactionBroadcast);
public override Dictionary<string, string> GetSupportedWebhookTypes()
{
return new Dictionary<string, string>
{
{PendingTransactionCreated, "Pending Transaction - Created"},
{PendingTransactionSignatureCollected, "Pending Transaction - Signature Collected"},
{PendingTransactionBroadcast, "Pending Transaction - Broadcast"}
};
}
protected override WebhookSender.WebhookDeliveryRequest CreateDeliveryRequest(PendingTransactionService.PendingTransactionEvent evt,
WebhookData webhook)
{
var webhookBlob = webhook?.GetBlob();
var webhookEvent = GetWebhookEvent(evt)!;
webhookEvent.StoreId = evt.Data.StoreId;
webhookEvent.WebhookId = webhook?.Id;
webhookEvent.IsRedelivery = false;
WebhookDeliveryData delivery = webhook is null? null: WebhookExtensions.NewWebhookDelivery(webhook.Id);
if (delivery is not null)
{
webhookEvent.DeliveryId = delivery.Id;
webhookEvent.OriginalDeliveryId = delivery.Id;
webhookEvent.Timestamp = delivery.Timestamp;
}
return new PendingTransactionDeliveryRequest(evt.Data, webhook?.Id, webhookEvent, delivery, webhookBlob);
}
protected override WebhookPendingTransactionEvent GetWebhookEvent(PendingTransactionService.PendingTransactionEvent evt)
{
return evt.Type switch
{
PendingTransactionService.PendingTransactionEvent.Created => new WebhookPendingTransactionEvent(
PendingTransactionCreated, evt.Data.StoreId),
PendingTransactionService.PendingTransactionEvent.SignatureCollected => new WebhookPendingTransactionEvent(
PendingTransactionSignatureCollected, evt.Data.StoreId),
PendingTransactionService.PendingTransactionEvent.Broadcast => new WebhookPendingTransactionEvent(
PendingTransactionBroadcast, evt.Data.StoreId),
_ => null
};
}
public override WebhookEvent CreateTestEvent(string type, params object[] args)
{
var storeId = args[0].ToString();
return new WebhookInvoiceEvent(type, storeId)
{
InvoiceId = "__test__" + Guid.NewGuid() + "__test__"
};
}
public class WebhookPendingTransactionEvent : StoreWebhookEvent
{
public WebhookPendingTransactionEvent(string type, string storeId)
{
if (!type.StartsWith(PendingTransactionCreated.Replace("Created", "").ToLower(), StringComparison.InvariantCultureIgnoreCase))
throw new ArgumentException("Invalid event type", nameof(type));
Type = type;
StoreId = storeId;
}
[JsonProperty(Order = 2)] public string PendingTransactionId { get; set; }
}
}

View File

@@ -354,6 +354,7 @@ namespace BTCPayServer.Hosting
services.TryAddSingleton<PaymentRequestRepository>();
services.TryAddSingleton<BTCPayWalletProvider>();
services.AddSingleton<PendingTransactionService>();
services.AddSingleton<IWebhookProvider>(o => o.GetRequiredService<PendingTransactionWebhookProvider>());
services.AddScheduledTask<PendingTransactionService>(TimeSpan.FromMinutes(10));
services.TryAddSingleton<WalletReceiveService>();
services.AddSingleton<IHostedService>(provider => provider.GetService<WalletReceiveService>());