lnbits-legend/lnbits/wallets/lnpay.py

164 lines
5.2 KiB
Python
Raw Normal View History

import asyncio
import hashlib
import json
from http import HTTPStatus
2022-07-16 14:23:03 +02:00
from typing import AsyncGenerator, Dict, Optional
2020-01-18 11:24:22 +01:00
2022-07-16 14:23:03 +02:00
import httpx
from fastapi import HTTPException
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-01-18 11:24:22 +01:00
class LNPayWallet(Wallet):
"""https://docs.lnpay.co/"""
def __init__(self):
2022-10-05 13:01:41 +02:00
endpoint = settings.lnpay_api_endpoint
wallet_key = settings.lnpay_wallet_key or settings.lnpay_admin_key
if not endpoint or not wallet_key or not settings.lnpay_api_key:
raise Exception("cannot initialize lnpay")
self.wallet_key = wallet_key
2020-01-18 11:24:22 +01:00
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
2022-10-05 13:01:41 +02:00
self.auth = {"X-Api-Key": settings.lnpay_api_key}
self.client = httpx.AsyncClient(base_url=self.endpoint, headers=self.auth)
async def cleanup(self):
try:
await self.client.aclose()
except RuntimeError as e:
logger.warning(f"Error closing wallet connection: {e}")
2020-01-18 11:24:22 +01:00
async def status(self) -> StatusResponse:
url = f"/wallet/{self.wallet_key}"
try:
r = await self.client.get(url, timeout=60)
except (httpx.ConnectError, httpx.RequestError):
2020-10-13 19:46:23 +02:00
return StatusResponse(f"Unable to connect to '{url}'", 0)
if r.is_error:
return StatusResponse(r.text[:250], 0)
data = r.json()
if data["statusType"]["name"] != "active":
return StatusResponse(
f"Wallet {data['user_label']} (data['id']) not active, but {data['statusType']['name']}",
0,
)
2020-10-13 18:57:26 +02:00
return StatusResponse(None, data["balance"] * 1000)
async def create_invoice(
2020-09-28 04:12:55 +02:00
self,
amount: int,
memo: Optional[str] = None,
description_hash: Optional[bytes] = None,
unhashed_description: Optional[bytes] = None,
**kwargs,
) -> InvoiceResponse:
data: Dict = {"num_satoshis": f"{amount}"}
if description_hash:
data["description_hash"] = description_hash.hex()
elif unhashed_description:
data["description_hash"] = hashlib.sha256(unhashed_description).hexdigest()
else:
data["memo"] = memo or ""
r = await self.client.post(
f"/wallet/{self.wallet_key}/invoice",
json=data,
timeout=60,
)
2020-09-28 04:12:55 +02:00
ok, checking_id, payment_request, error_message = (
r.status_code == 201,
None,
None,
r.text,
)
if ok:
2020-01-18 11:24:22 +01:00
data = r.json()
checking_id, payment_request = data["id"], data["payment_request"]
2020-01-18 11:24:22 +01:00
return InvoiceResponse(ok, checking_id, payment_request, error_message)
2020-01-18 11:24:22 +01:00
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
r = await self.client.post(
f"/wallet/{self.wallet_key}/withdraw",
json={"payment_request": bolt11},
timeout=None,
)
try:
data = r.json()
except:
return PaymentResponse(
False, None, 0, None, f"Got invalid JSON: {r.text[:200]}"
)
if r.is_error:
return PaymentResponse(False, None, None, None, data["message"])
2020-01-18 11:24:22 +01:00
checking_id = data["lnTx"]["id"]
fee_msat = 0
preimage = data["lnTx"]["payment_preimage"]
return PaymentResponse(True, checking_id, fee_msat, preimage, None)
2020-01-18 11:24:22 +01:00
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
return await self.get_payment_status(checking_id)
2020-01-18 11:24:22 +01:00
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
r = await self.client.get(
url=f"/lntx/{checking_id}",
)
2020-01-18 11:24:22 +01:00
if r.is_error:
return PaymentStatus(None)
2020-01-18 11:24:22 +01:00
data = r.json()
preimage = data["payment_preimage"]
fee_msat = data["fee_msat"]
2020-01-18 11:24:22 +01:00
statuses = {0: None, 1: True, -1: False}
return PaymentStatus(statuses[data["settled"]], fee_msat, preimage)
2020-09-28 04:12:55 +02:00
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
2022-07-19 18:51:35 +02:00
self.queue: asyncio.Queue = asyncio.Queue(0)
while True:
value = await self.queue.get()
yield value
2020-09-28 04:12:55 +02:00
async def webhook_listener(self):
# TODO: request.get_data is undefined, was it something with Flask or quart?
# probably issue introduced when refactoring?
text: str = await request.get_data() # type: ignore
try:
data = json.loads(text)
except json.decoder.JSONDecodeError:
logger.error(f"got something wrong on lnpay webhook endpoint: {text[:200]}")
data = None
if (
type(data) is not dict
or "event" not in data
or data["event"].get("name") != "wallet_receive"
):
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)
lntx_id = data["data"]["wtx"]["lnTx"]["id"]
r = await self.client.get(f"/lntx/{lntx_id}?fields=settled")
data = r.json()
if data["settled"]:
await self.queue.put(lntx_id)
raise HTTPException(status_code=HTTPStatus.NO_CONTENT)