Zaps
This commit is contained in:
@ -18,6 +18,11 @@ public static class Extensions
|
|||||||
return NostrPrivateKey.FromBech32(cfg.PrivateKey).DerivePublicKey().Hex;
|
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)
|
public static List<Variant> GetVariants(this IngestEndpoint ep)
|
||||||
{
|
{
|
||||||
return ep.Capabilities
|
return ep.Capabilities
|
||||||
|
@ -46,6 +46,7 @@ internal static class Program
|
|||||||
services.AddSingleton<INostrClient>(s => s.GetRequiredService<NostrMultiWebsocketClient>());
|
services.AddSingleton<INostrClient>(s => s.GetRequiredService<NostrMultiWebsocketClient>());
|
||||||
services.AddSingleton<NostrListener>();
|
services.AddSingleton<NostrListener>();
|
||||||
services.AddHostedService<NostrListenerLifetime>();
|
services.AddHostedService<NostrListenerLifetime>();
|
||||||
|
services.AddTransient<ZapService>();
|
||||||
|
|
||||||
// streaming services
|
// streaming services
|
||||||
services.AddTransient<SrsApi>();
|
services.AddTransient<SrsApi>();
|
||||||
|
@ -43,6 +43,7 @@ public class LndInvoicesStream : BackgroundService
|
|||||||
{
|
{
|
||||||
using var scope = _scopeFactory.CreateScope();
|
using var scope = _scopeFactory.CreateScope();
|
||||||
var db = scope.ServiceProvider.GetRequiredService<StreamerContext>();
|
var db = scope.ServiceProvider.GetRequiredService<StreamerContext>();
|
||||||
|
var zapService = scope.ServiceProvider.GetRequiredService<ZapService>();
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
@ -56,6 +57,10 @@ public class LndInvoicesStream : BackgroundService
|
|||||||
payment.IsPaid = true;
|
payment.IsPaid = true;
|
||||||
payment.User.Balance += (long)(payment.Amount * 1000L);
|
payment.User.Balance += (long)(payment.Amount * 1000L);
|
||||||
await db.SaveChangesAsync(stoppingToken);
|
await db.SaveChangesAsync(stoppingToken);
|
||||||
|
if (!string.IsNullOrEmpty(payment.Nostr))
|
||||||
|
{
|
||||||
|
zapService.HandlePaid(payment.Invoice, payment.Nostr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
60
NostrStreamer/Services/ZapService.cs
Normal file
60
NostrStreamer/Services/ZapService.cs
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user