Make nostr great again

This commit is contained in:
Kukks
2023-09-07 13:59:50 +02:00
parent 00c8f6003b
commit cdbf4ccc21
5 changed files with 64 additions and 22 deletions

View File

@@ -11,7 +11,7 @@
<PropertyGroup> <PropertyGroup>
<Product>Nostr </Product> <Product>Nostr </Product>
<Description>Allows you to verify your nostr account with NIP5 and zap like the rest of the crazies</Description> <Description>Allows you to verify your nostr account with NIP5 and zap like the rest of the crazies</Description>
<Version>1.0.16</Version> <Version>1.1.0</Version>
</PropertyGroup> </PropertyGroup>
<!-- Plugin development properties --> <!-- Plugin development properties -->
<PropertyGroup> <PropertyGroup>

View File

@@ -47,11 +47,7 @@ public class LnurlDescriptionFilter : PluginHookFilter<string>
var username = metadata var username = metadata
.FirstOrDefault(strings => strings.FirstOrDefault()?.Equals("text/identifier") is true) .FirstOrDefault(strings => strings.FirstOrDefault()?.Equals("text/identifier") is true)
?.ElementAtOrDefault(1)?.ToLowerInvariant().Split("@")[0]; ?.ElementAtOrDefault(1)?.ToLowerInvariant().Split("@")[0];
if (string.IsNullOrEmpty(username)) if (!string.IsNullOrEmpty(username))
{
return arg;
}
else
{ {
var lnAddress = await _lightningAddressService.ResolveByAddress(username); var lnAddress = await _lightningAddressService.ResolveByAddress(username);
if (lnAddress is null) if (lnAddress is null)
@@ -59,9 +55,6 @@ public class LnurlDescriptionFilter : PluginHookFilter<string>
return arg; return arg;
} }
} }
var parsedNote = System.Text.Json.JsonSerializer.Deserialize<NostrEvent>(nostr); var parsedNote = System.Text.Json.JsonSerializer.Deserialize<NostrEvent>(nostr);
if (parsedNote?.Kind != 9734) if (parsedNote?.Kind != 9734)
{ {

View File

@@ -23,20 +23,21 @@ public class LnurlFilter : PluginHookFilter<LNURLPayRequest>
{ {
var name = arg.ParsedMetadata.FirstOrDefault(pair => pair.Key == "text/identifier").Value var name = arg.ParsedMetadata.FirstOrDefault(pair => pair.Key == "text/identifier").Value
?.ToLowerInvariant().Split("@")[0]; ?.ToLowerInvariant().Split("@")[0];
if (string.IsNullOrEmpty(name)) if (!string.IsNullOrEmpty(name))
{ {
return arg; var lnAddress = await _lightningAddressService.ResolveByAddress(name);
if (lnAddress is null)
{
return arg;
}
var nip5 = await _nip5Controller.GetForStore(lnAddress.StoreDataId);
arg.NostrPubkey = nip5?.PubKey;
} }
var lnAddress = await _lightningAddressService.ResolveByAddress(name);
if (lnAddress is null)
{
return arg;
}
var nip5 = await _nip5Controller.GetForStore(lnAddress.StoreDataId);
arg.NostrPubkey = nip5?.PubKey ?? (await _zapper.GetSettings()).ZappingPublicKeyHex; arg.NostrPubkey ??= (await _zapper.GetSettings()).ZappingPublicKeyHex;
arg.AllowsNostr = true; arg.AllowsNostr = true;
return arg; return arg;
} }

View File

@@ -2,14 +2,18 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net.Http;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Abstractions.Constants; using BTCPayServer.Abstractions.Constants;
using BTCPayServer.Client; using BTCPayServer.Client;
using BTCPayServer.Filters;
using BTCPayServer.Services.Stores; using BTCPayServer.Services.Stores;
using LNURL;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors; using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using NBitcoin;
using NBitcoin.DataEncoders; using NBitcoin.DataEncoders;
using NBitcoin.Secp256k1; using NBitcoin.Secp256k1;
using NNostr.Client; using NNostr.Client;
@@ -205,4 +209,33 @@ public class Nip5Controller : Controller
: null : null
}); });
} }
[CheatModeRoute]
[HttpGet("~/nostr-fake")]
[EnableCors(CorsPolicies.All)]
[IgnoreAntiforgeryToken]
[AllowAnonymous]
public async Task<IActionResult> FakeNostr(string lnurl)
{
if (lnurl.Contains("@"))
{
lnurl = LNURL.LNURL.ExtractUriFromInternetIdentifier(lnurl).ToString();
}
var lnurlRequest = (LNURLPayRequest) await LNURL.LNURL.FetchInformation(new Uri(lnurl), new HttpClient());
var nKey = ECPrivKey.Create(RandomUtils.GetBytes(32));
var nostrEvent = new NostrEvent()
{
Kind = 9734,
Content = "",
};
var lnurlBech32x = LNURL.LNURL.EncodeBech32(new Uri(lnurl));
nostrEvent.SetTag("relays", "wss://btcpay.kukks.org/nostr");
nostrEvent.SetTag("lnurl", lnurlBech32x);
nostrEvent.SetTag("amount", lnurlRequest.MinSendable.MilliSatoshi.ToString());
nostrEvent = await nostrEvent.ComputeIdAndSignAsync(nKey);
var response = await new HttpClient().GetAsync(lnurlRequest.Callback + "?amount=" + lnurlRequest.MinSendable.MilliSatoshi +
"&nostr=" +System.Text.Json.JsonSerializer.Serialize(nostrEvent));
return Content(await response.Content.ReadAsStringAsync());
}
} }

View File

@@ -5,8 +5,10 @@ using System.Linq;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using BTCPayServer.Events; using BTCPayServer.Events;
using BTCPayServer.Models.InvoicingModels;
using BTCPayServer.Payments; using BTCPayServer.Payments;
using BTCPayServer.Services; using BTCPayServer.Services;
using BTCPayServer.Services.Invoices;
using BTCPayServer.Services.Stores; using BTCPayServer.Services.Stores;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
@@ -49,6 +51,7 @@ public class Zapper : IHostedService
private readonly IMemoryCache _memoryCache; private readonly IMemoryCache _memoryCache;
private readonly ILogger<Zapper> _logger; private readonly ILogger<Zapper> _logger;
private readonly SettingsRepository _settingsRepository; private readonly SettingsRepository _settingsRepository;
private readonly InvoiceRepository _invoiceRepository;
private IEventAggregatorSubscription _subscription; private IEventAggregatorSubscription _subscription;
private readonly ConcurrentBag<PendingZapEvent> _pendingZapEvents = new(); private readonly ConcurrentBag<PendingZapEvent> _pendingZapEvents = new();
@@ -70,13 +73,18 @@ public class Zapper : IHostedService
public Zapper(EventAggregator eventAggregator, public Zapper(EventAggregator eventAggregator,
Nip5Controller nip5Controller, IMemoryCache memoryCache, ILogger<Zapper> logger, SettingsRepository settingsRepository, StoreRepository storeRepository) Nip5Controller nip5Controller,
IMemoryCache memoryCache,
ILogger<Zapper> logger,
SettingsRepository settingsRepository,
InvoiceRepository invoiceRepository)
{ {
_eventAggregator = eventAggregator; _eventAggregator = eventAggregator;
_nip5Controller = nip5Controller; _nip5Controller = nip5Controller;
_memoryCache = memoryCache; _memoryCache = memoryCache;
_logger = logger; _logger = logger;
_settingsRepository = settingsRepository; _settingsRepository = settingsRepository;
_invoiceRepository = invoiceRepository;
} }
public Task StartAsync(CancellationToken cancellationToken) public Task StartAsync(CancellationToken cancellationToken)
@@ -202,9 +210,16 @@ public class Zapper : IHostedService
Tags = tags Tags = tags
}; };
zapReceipt = await zapReceipt.ComputeIdAndSignAsync(key);
await zapReceipt.ComputeIdAndSignAsync(key); relays = relays.Concat(userNostrSettings?.Relays ?? Array.Empty<string>()).Distinct().ToArray();
_pendingZapEvents.Add(new PendingZapEvent(relays.Concat(userNostrSettings?.Relays?? Array.Empty<string>()).Distinct().ToArray(), zapReceipt)); arg.Invoice.Metadata.SetAdditionalData("Nostr", new Dictionary<string,string>()
{
{"Zap Request", zapRequestEvent.Id},
{"Zap Receipt", zapReceipt.Id},
{"Relays", string.Join(',', relays)}
});
await _invoiceRepository.UpdateInvoiceMetadata(arg.InvoiceId, arg.Invoice.StoreId, arg.Invoice.Metadata.ToJObject());
_pendingZapEvents.Add(new PendingZapEvent(relays, zapReceipt));
} }
public Task StopAsync(CancellationToken cancellationToken) public Task StopAsync(CancellationToken cancellationToken)