send zaps to relays in zap request
This commit is contained in:
parent
c2e58fa1b4
commit
e119f5c4c5
1 changed files with 37 additions and 13 deletions
50
tasks.py
50
tasks.py
|
|
@ -13,6 +13,7 @@ from lnbits.settings import settings
|
||||||
from .crud import get_pay_link
|
from .crud import get_pay_link
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
from . import nostrclient_present, nostr_privatekey
|
from . import nostrclient_present, nostr_privatekey
|
||||||
|
from typing import List
|
||||||
|
|
||||||
if nostrclient_present:
|
if nostrclient_present:
|
||||||
try:
|
try:
|
||||||
|
|
@ -81,7 +82,7 @@ async def on_invoice_paid(payment: Payment):
|
||||||
|
|
||||||
def get_tag(event_json, tag):
|
def get_tag(event_json, tag):
|
||||||
res = [
|
res = [
|
||||||
event_tag[1] for event_tag in event_json["tags"] if event_tag[0] == tag
|
event_tag[1:] for event_tag in event_json["tags"] if event_tag[0] == tag
|
||||||
]
|
]
|
||||||
return res[0] if res else None
|
return res[0] if res else None
|
||||||
|
|
||||||
|
|
@ -89,7 +90,7 @@ async def on_invoice_paid(payment: Payment):
|
||||||
for t in ["p", "e"]:
|
for t in ["p", "e"]:
|
||||||
tag = get_tag(event_json, t)
|
tag = get_tag(event_json, t)
|
||||||
if tag:
|
if tag:
|
||||||
tags.append([t, tag])
|
tags.append([t, tag[0]])
|
||||||
tags.append(["bolt11", payment.bolt11])
|
tags.append(["bolt11", payment.bolt11])
|
||||||
tags.append(["description", nostr])
|
tags.append(["description", nostr])
|
||||||
zap_receipt = Event(
|
zap_receipt = Event(
|
||||||
|
|
@ -97,18 +98,41 @@ async def on_invoice_paid(payment: Payment):
|
||||||
)
|
)
|
||||||
nostr_privatekey.sign_event(zap_receipt)
|
nostr_privatekey.sign_event(zap_receipt)
|
||||||
|
|
||||||
def send_event(_):
|
def send_zap(relay):
|
||||||
logger.debug(f"Sending zap: {zap_receipt.to_message()}")
|
def send_event(_):
|
||||||
ws.send(zap_receipt.to_message())
|
logger.debug(f"Sending zap to {ws.url}")
|
||||||
ws.close()
|
ws.send(zap_receipt.to_message())
|
||||||
|
ws.close()
|
||||||
|
|
||||||
ws = WebSocketApp(
|
ws = WebSocketApp(relay, on_open=send_event)
|
||||||
f"ws://localhost:{settings.port}/nostrclient/api/v1/relay",
|
wst = Thread(target=ws.run_forever, name=f"LNURL zap {relay}")
|
||||||
on_open=send_event,
|
wst.daemon = True
|
||||||
)
|
wst.start()
|
||||||
wst = Thread(target=ws.run_forever)
|
return ws, wst
|
||||||
wst.daemon = True
|
|
||||||
wst.start()
|
# list of all websockets
|
||||||
|
wss: List[WebSocketApp] = []
|
||||||
|
# list of all threads for these websockets
|
||||||
|
wsts: List[Thread] = []
|
||||||
|
|
||||||
|
# send zap via nostrclient
|
||||||
|
ws, wst = send_zap(f"ws://localhost:{settings.port}/nostrclient/api/v1/relay")
|
||||||
|
wss += [ws]
|
||||||
|
wsts += [wst]
|
||||||
|
|
||||||
|
# send zap receipt to relays in zap request
|
||||||
|
relays = get_tag(event_json, "relays")
|
||||||
|
if relays:
|
||||||
|
for i, r in enumerate(relays):
|
||||||
|
ws, wst = send_zap(r)
|
||||||
|
wss += [ws]
|
||||||
|
wsts += [wst]
|
||||||
|
|
||||||
|
await asyncio.sleep(10)
|
||||||
|
for ws, wst in zip(wss, wsts):
|
||||||
|
logger.debug(f"Closing websocket {ws.url}")
|
||||||
|
ws.close()
|
||||||
|
wst.join()
|
||||||
|
|
||||||
|
|
||||||
async def mark_webhook_sent(
|
async def mark_webhook_sent(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue