Skip to content

Commit

Permalink
Mint: enable LNbitsWallet invoice stream (cashubtc#594)
Browse files Browse the repository at this point in the history
* enable lnbits invoice stream

* fix stream
  • Loading branch information
callebtc authored Jul 20, 2024
1 parent 8675745 commit 40e60c0
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion cashu/lightning/lnbits.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# type: ignore
import asyncio
import json
from typing import AsyncGenerator, Optional

import httpx
Expand All @@ -25,6 +27,7 @@ class LNbitsWallet(LightningBackend):

supported_units = set([Unit.sat])
unit = Unit.sat
supports_incoming_payment_stream: bool = True

def __init__(self, unit: Unit = Unit.sat, **kwargs):
self.assert_unit_supported(unit)
Expand Down Expand Up @@ -184,4 +187,40 @@ async def get_payment_quote(
)

async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
raise NotImplementedError("paid_invoices_stream not implemented")
url = f"{self.endpoint}/api/v1/payments/sse"

try:
sse_headers = self.client.headers.copy()
sse_headers.update(
{
"accept": "text/event-stream",
"cache-control": "no-cache",
"connection": "keep-alive",
}
)
async with self.client.stream(
"GET",
url,
content="text/event-stream",
timeout=None,
headers=sse_headers,
) as r:
sse_trigger = False
async for line in r.aiter_lines():
# The data we want to listen to is of this shape:
# event: payment-received
# data: {.., "payment_hash" : "asd"}
if line.startswith("event: payment-received"):
sse_trigger = True
continue
elif sse_trigger and line.startswith("data:"):
data = json.loads(line[len("data:") :])
sse_trigger = False
yield data["payment_hash"]
else:
sse_trigger = False

except (OSError, httpx.ReadError, httpx.ConnectError, httpx.ReadTimeout):
pass

await asyncio.sleep(1)

0 comments on commit 40e60c0

Please sign in to comment.