This commit is contained in:
2023-07-27 19:05:45 +01:00
parent dfa9188146
commit 9053e3eb1f
4 changed files with 71 additions and 0 deletions

View File

@ -17,6 +17,11 @@ public static class Extensions
{
return NostrPrivateKey.FromBech32(cfg.PrivateKey).DerivePublicKey().Hex;
}
public static NostrPrivateKey GetPrivateKey(this Config cfg)
{
return NostrPrivateKey.FromBech32(cfg.PrivateKey);
}
public static List<Variant> GetVariants(this IngestEndpoint ep)
{

View File

@ -46,6 +46,7 @@ internal static class Program
services.AddSingleton<INostrClient>(s => s.GetRequiredService<NostrMultiWebsocketClient>());
services.AddSingleton<NostrListener>();
services.AddHostedService<NostrListenerLifetime>();
services.AddTransient<ZapService>();
// streaming services
services.AddTransient<SrsApi>();

View File

@ -43,6 +43,7 @@ public class LndInvoicesStream : BackgroundService
{
using var scope = _scopeFactory.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<StreamerContext>();
var zapService = scope.ServiceProvider.GetRequiredService<ZapService>();
try
{
@ -56,6 +57,10 @@ public class LndInvoicesStream : BackgroundService
payment.IsPaid = true;
payment.User.Balance += (long)(payment.Amount * 1000L);
await db.SaveChangesAsync(stoppingToken);
if (!string.IsNullOrEmpty(payment.Nostr))
{
zapService.HandlePaid(payment.Invoice, payment.Nostr);
}
}
}
catch (Exception ex)

View File

@ -0,0 +1,60 @@
using Newtonsoft.Json;
using Nostr.Client.Client;
using Nostr.Client.Messages;
using Nostr.Client.Requests;
namespace NostrStreamer.Services;
public class ZapService
{
private readonly ILogger<ZapService> _logger;
private readonly Config _config;
private readonly INostrClient _nostrClient;
public ZapService(ILogger<ZapService> logger, Config config, INostrClient nostrClient)
{
_logger = logger;
_config = config;
_nostrClient = nostrClient;
}
public void HandlePaid(string pr, string? zapRequest)
{
try
{
if (string.IsNullOrEmpty(zapRequest)) return;
var zapNote = JsonConvert.DeserializeObject<NostrEvent>(zapRequest);
if (zapNote == default)
{
_logger.LogWarning("Could not parse zap note {note}", zapRequest);
return;
}
var key = _config.GetPrivateKey();
var pubKey = key.DerivePublicKey().Hex;
var tags = zapNote.Tags!.Where(a => a.TagIdentifier?.Length == 1).ToList();
tags.Add(new("bolt11", pr));
tags.Add(new("description", zapRequest));
var zapReceipt = new NostrEvent()
{
Kind = NostrKind.Zap,
CreatedAt = DateTime.UtcNow,
Pubkey = pubKey,
Content = zapNote.Content,
Tags = new(tags.ToArray())
};
var zapReceiptSigned = zapReceipt.Sign(key);
var jsonZap = JsonConvert.SerializeObject(zapReceiptSigned);
_logger.LogInformation("Created tip receipt {json}", jsonZap);
_nostrClient.Send(new NostrEventRequest(zapReceiptSigned));
}
catch (Exception e)
{
_logger.LogError(e, "Failed to handle zap");
}
}
}