lnbits-legend/lnbits/wallets/spark.py

256 lines
8.5 KiB
Python
Raw Normal View History

import asyncio
import hashlib
2020-09-27 23:12:55 -03:00
import json
2021-03-27 21:24:08 -03:00
import random
2022-07-16 14:23:03 +02:00
from typing import AsyncGenerator, Optional
2020-08-29 12:23:01 -03:00
2022-07-16 14:23:03 +02:00
import httpx
from loguru import logger
2022-10-05 13:01:41 +02:00
from lnbits.settings import settings
from .base import (
InvoiceResponse,
PaymentResponse,
PaymentStatus,
2022-07-16 14:23:03 +02:00
StatusResponse,
Wallet,
)
2020-08-29 12:23:01 -03:00
class SparkError(Exception):
pass
class UnknownError(Exception):
pass
class SparkWallet(Wallet):
def __init__(self):
2023-04-03 12:23:01 +02:00
assert settings.spark_url, "spark url does not exist"
2022-10-05 13:01:41 +02:00
self.url = settings.spark_url.replace("/rpc", "")
self.token = settings.spark_token
assert self.token, "spark wallet token does not exist"
headers = {"X-Access": self.token, "User-Agent": settings.user_agent}
self.client = httpx.AsyncClient(base_url=self.url, headers=headers)
async def cleanup(self):
try:
await self.client.aclose()
except RuntimeError as e:
logger.warning(f"Error closing wallet connection: {e}")
2020-08-29 12:23:01 -03:00
def __getattr__(self, key):
async def call(*args, **kwargs):
2020-08-29 12:23:01 -03:00
if args and kwargs:
raise TypeError(
"must supply either named arguments or a list of arguments, not"
f" both: {args} {kwargs}"
)
2020-08-29 12:23:01 -03:00
elif args:
params = args
elif kwargs:
params = kwargs
2020-10-13 13:57:26 -03:00
else:
params = {}
2020-08-29 12:23:01 -03:00
2021-03-27 21:24:08 -03:00
try:
r = await self.client.post(
"/rpc",
json={"method": key, "params": params},
timeout=60 * 60 * 24,
)
r.raise_for_status()
except (
OSError,
httpx.ConnectError,
httpx.RequestError,
httpx.HTTPError,
httpx.TimeoutException,
) as exc:
raise UnknownError(f"error connecting to spark: {exc}")
2020-08-29 12:23:01 -03:00
try:
data = r.json()
except Exception:
2020-08-29 12:23:01 -03:00
raise UnknownError(r.text)
if r.is_error:
if r.status_code == 401:
raise SparkError("Access key invalid!")
2020-08-29 12:23:01 -03:00
raise SparkError(data["message"])
2020-08-29 12:23:01 -03:00
return data
return call
async def status(self) -> StatusResponse:
try:
funds = await self.listfunds()
except (httpx.ConnectError, httpx.RequestError):
return StatusResponse("Couldn't connect to Spark server", 0)
except (SparkError, UnknownError) as e:
return StatusResponse(str(e), 0)
return StatusResponse(
2021-10-17 18:33:29 +01:00
None, sum([ch["channel_sat"] * 1000 for ch in funds["channels"]])
)
async def create_invoice(
self,
amount: int,
memo: Optional[str] = None,
description_hash: Optional[bytes] = None,
unhashed_description: Optional[bytes] = None,
**kwargs,
) -> InvoiceResponse:
label = f"lbs{random.random()}"
2020-08-29 13:48:16 -03:00
checking_id = label
2020-08-29 12:23:01 -03:00
try:
if description_hash:
r = await self.invoicewithdescriptionhash(
2020-09-03 23:02:15 +02:00
msatoshi=amount * 1000,
label=label,
description_hash=description_hash.hex(),
)
elif unhashed_description:
r = await self.invoicewithdescriptionhash(
msatoshi=amount * 1000,
label=label,
description_hash=hashlib.sha256(unhashed_description).hexdigest(),
2020-08-29 12:23:01 -03:00
)
else:
r = await self.invoice(
msatoshi=amount * 1000,
label=label,
description=memo or "",
exposeprivatechannels=True,
expiry=kwargs.get("expiry"),
)
2020-08-29 13:48:16 -03:00
ok, payment_request, error_message = True, r["bolt11"], ""
2020-08-29 12:23:01 -03:00
except (SparkError, UnknownError) as e:
2020-08-29 13:48:16 -03:00
ok, payment_request, error_message = False, None, str(e)
2020-08-29 12:23:01 -03:00
return InvoiceResponse(ok, checking_id, payment_request, error_message)
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
2020-08-29 12:23:01 -03:00
try:
r = await self.pay(
bolt11=bolt11,
maxfee=fee_limit_msat,
)
fee_msat = -int(r["msatoshi_sent"] - r["msatoshi"])
preimage = r["payment_preimage"]
return PaymentResponse(True, r["payment_hash"], fee_msat, preimage, None)
except (SparkError, UnknownError) as exc:
listpays = await self.listpays(bolt11)
if not listpays:
return PaymentResponse(False, None, None, None, str(exc))
pays = listpays["pays"]
if len(pays) == 0:
return PaymentResponse(False, None, None, None, str(exc))
pay = pays[0]
payment_hash = pay["payment_hash"]
if len(pays) > 1:
raise SparkError(
f"listpays({payment_hash}) returned an unexpected response:"
f" {listpays}"
)
if pay["status"] == "failed":
return PaymentResponse(False, None, None, None, str(exc))
2023-04-05 11:39:01 +02:00
if pay["status"] == "pending":
return PaymentResponse(None, payment_hash, None, None, None)
2023-04-05 11:39:01 +02:00
if pay["status"] == "complete":
r = pay
r["payment_preimage"] = pay["preimage"]
r["msatoshi"] = int(pay["amount_msat"][0:-4])
r["msatoshi_sent"] = int(pay["amount_sent_msat"][0:-4])
# this may result in an error if it was paid previously
# our database won't allow the same payment_hash to be added twice
# this is good
fee_msat = -int(r["msatoshi_sent"] - r["msatoshi"])
preimage = r["payment_preimage"]
return PaymentResponse(
True, r["payment_hash"], fee_msat, preimage, None
)
else:
return PaymentResponse(False, None, None, None, str(exc))
2020-08-29 12:23:01 -03:00
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
2021-03-27 21:24:08 -03:00
try:
r = await self.listinvoices(label=checking_id)
except (SparkError, UnknownError):
return PaymentStatus(None)
2020-08-29 12:23:01 -03:00
if not r or not r.get("invoices"):
return PaymentStatus(None)
if r["invoices"][0]["status"] == "paid":
return PaymentStatus(True)
else:
2020-08-29 12:23:01 -03:00
return PaymentStatus(False)
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
# check if it's 32 bytes hex
if len(checking_id) != 64:
return PaymentStatus(None)
try:
int(checking_id, 16)
except ValueError:
return PaymentStatus(None)
# ask sparko
2021-03-27 21:24:08 -03:00
try:
r = await self.listpays(payment_hash=checking_id)
except (SparkError, UnknownError):
return PaymentStatus(None)
2020-08-29 12:23:01 -03:00
if not r["pays"]:
return PaymentStatus(False)
if r["pays"][0]["payment_hash"] == checking_id:
status = r["pays"][0]["status"]
if status == "complete":
2023-01-06 15:43:35 +01:00
fee_msat = -(
int(r["pays"][0]["amount_sent_msat"][0:-4])
- int(r["pays"][0]["amount_msat"][0:-4])
)
return PaymentStatus(True, fee_msat, r["pays"][0]["preimage"])
2023-04-05 11:39:01 +02:00
if status == "failed":
2020-08-29 12:23:01 -03:00
return PaymentStatus(False)
return PaymentStatus(None)
raise KeyError("supplied an invalid checking_id")
2020-09-27 23:12:55 -03:00
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
url = f"/stream?access-key={self.token}"
while True:
try:
async with self.client.stream("GET", url, timeout=None) as r:
async for line in r.aiter_lines():
if line.startswith("data:"):
data = json.loads(line[5:])
if "pay_index" in data and data.get("status") == "paid":
yield data["label"]
except (
OSError,
httpx.ReadError,
httpx.ConnectError,
httpx.ReadTimeout,
httpx.HTTPError,
):
pass
logger.error("lost connection to spark /stream, retrying in 5 seconds")
await asyncio.sleep(5)