2021-08-30 19:55:02 +02:00
|
|
|
import asyncio
|
2022-08-01 16:20:25 +02:00
|
|
|
import hashlib
|
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
|
2023-02-06 10:07:40 +01:00
|
|
|
from loguru import logger
|
2022-07-07 14:30:16 +02:00
|
|
|
|
2022-10-05 13:01:41 +02:00
|
|
|
from lnbits.settings import settings
|
|
|
|
|
2021-03-24 04:40:32 +01:00
|
|
|
from .base import (
|
|
|
|
InvoiceResponse,
|
|
|
|
PaymentResponse,
|
|
|
|
PaymentStatus,
|
2022-07-16 14:23:03 +02:00
|
|
|
StatusResponse,
|
2021-03-24 04:40:32 +01:00
|
|
|
Wallet,
|
|
|
|
)
|
2020-01-18 11:24:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LNPayWallet(Wallet):
|
|
|
|
"""https://docs.lnpay.co/"""
|
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
def __init__(self):
|
2023-12-24 14:23:21 +01:00
|
|
|
if not settings.lnpay_api_endpoint:
|
|
|
|
raise ValueError(
|
|
|
|
"cannot initialize LNPayWallet: missing lnpay_api_endpoint"
|
|
|
|
)
|
|
|
|
if not settings.lnpay_api_key:
|
|
|
|
raise ValueError("cannot initialize LNPayWallet: missing lnpay_api_key")
|
2023-02-02 13:57:36 +01:00
|
|
|
|
2023-12-24 14:23:21 +01:00
|
|
|
wallet_key = settings.lnpay_wallet_key or settings.lnpay_admin_key
|
|
|
|
if not wallet_key:
|
|
|
|
raise ValueError(
|
|
|
|
"cannot initialize LNPayWallet: "
|
|
|
|
"missing lnpay_wallet_key or lnpay_admin_key"
|
|
|
|
)
|
2023-02-02 13:57:36 +01:00
|
|
|
self.wallet_key = wallet_key
|
2023-12-24 14:23:21 +01:00
|
|
|
|
2023-12-24 15:03:58 +01:00
|
|
|
self.endpoint = self.normalize_endpoint(settings.lnpay_api_endpoint)
|
|
|
|
|
2023-11-30 13:54:07 +01:00
|
|
|
headers = {
|
|
|
|
"X-Api-Key": settings.lnpay_api_key,
|
|
|
|
"User-Agent": settings.user_agent,
|
|
|
|
}
|
|
|
|
self.client = httpx.AsyncClient(base_url=self.endpoint, headers=headers)
|
2023-06-19 12:12:00 +02:00
|
|
|
|
|
|
|
async def cleanup(self):
|
2023-07-21 09:50:50 +02:00
|
|
|
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
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def status(self) -> StatusResponse:
|
2023-06-19 12:12:00 +02:00
|
|
|
url = f"/wallet/{self.wallet_key}"
|
2020-10-13 03:25:55 +02:00
|
|
|
try:
|
2023-06-19 12:12:00 +02:00
|
|
|
r = await self.client.get(url, timeout=60)
|
2020-10-13 03:25:55 +02:00
|
|
|
except (httpx.ConnectError, httpx.RequestError):
|
2020-10-13 19:46:23 +02:00
|
|
|
return StatusResponse(f"Unable to connect to '{url}'", 0)
|
2020-10-13 03:25:55 +02:00
|
|
|
|
|
|
|
if r.is_error:
|
|
|
|
return StatusResponse(r.text[:250], 0)
|
|
|
|
|
|
|
|
data = r.json()
|
|
|
|
if data["statusType"]["name"] != "active":
|
|
|
|
return StatusResponse(
|
2023-08-24 11:26:09 +02:00
|
|
|
f"Wallet {data['user_label']} (data['id']) not active, but"
|
|
|
|
f" {data['statusType']['name']}",
|
2021-03-24 04:40:32 +01:00
|
|
|
0,
|
2020-10-13 03:25:55 +02:00
|
|
|
)
|
|
|
|
|
2020-10-13 18:57:26 +02:00
|
|
|
return StatusResponse(None, data["balance"] * 1000)
|
2020-10-13 03:25:55 +02:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def create_invoice(
|
2020-09-28 04:12:55 +02:00
|
|
|
self,
|
|
|
|
amount: int,
|
|
|
|
memo: Optional[str] = None,
|
|
|
|
description_hash: Optional[bytes] = None,
|
2022-08-13 14:29:04 +02:00
|
|
|
unhashed_description: Optional[bytes] = None,
|
2023-08-16 12:22:14 +02:00
|
|
|
**_,
|
2020-08-31 04:48:46 +02:00
|
|
|
) -> InvoiceResponse:
|
|
|
|
data: Dict = {"num_satoshis": f"{amount}"}
|
|
|
|
if description_hash:
|
2022-08-13 14:29:04 +02:00
|
|
|
data["description_hash"] = description_hash.hex()
|
|
|
|
elif unhashed_description:
|
|
|
|
data["description_hash"] = hashlib.sha256(unhashed_description).hexdigest()
|
2020-08-31 04:48:46 +02:00
|
|
|
else:
|
|
|
|
data["memo"] = memo or ""
|
|
|
|
|
2023-06-19 12:12:00 +02:00
|
|
|
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,
|
|
|
|
)
|
2020-01-31 21:07:05 +01:00
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
if ok:
|
2020-01-18 11:24:22 +01:00
|
|
|
data = r.json()
|
2020-03-31 19:05:25 +02:00
|
|
|
checking_id, payment_request = data["id"], data["payment_request"]
|
2020-01-18 11:24:22 +01:00
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
return InvoiceResponse(ok, checking_id, payment_request, error_message)
|
2020-01-18 11:24:22 +01:00
|
|
|
|
2022-03-16 07:20:15 +01:00
|
|
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
2023-06-19 12:12:00 +02:00
|
|
|
r = await self.client.post(
|
|
|
|
f"/wallet/{self.wallet_key}/withdraw",
|
|
|
|
json={"payment_request": bolt11},
|
|
|
|
timeout=None,
|
|
|
|
)
|
2020-03-31 19:05:25 +02:00
|
|
|
|
2020-10-13 04:18:37 +02:00
|
|
|
try:
|
|
|
|
data = r.json()
|
2023-08-16 12:17:54 +02:00
|
|
|
except Exception:
|
2021-03-24 04:40:32 +01:00
|
|
|
return PaymentResponse(
|
|
|
|
False, None, 0, None, f"Got invalid JSON: {r.text[:200]}"
|
|
|
|
)
|
2020-10-13 04:18:37 +02:00
|
|
|
|
|
|
|
if r.is_error:
|
2022-08-30 13:28:58 +02:00
|
|
|
return PaymentResponse(False, None, None, None, data["message"])
|
2020-01-18 11:24:22 +01:00
|
|
|
|
2020-10-13 04:18:37 +02: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
|
|
|
|
2021-03-24 05:01:09 +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
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
2023-06-19 12:12:00 +02:00
|
|
|
r = await self.client.get(
|
|
|
|
url=f"/lntx/{checking_id}",
|
|
|
|
)
|
2020-01-18 11:24:22 +01:00
|
|
|
|
2020-10-02 22:13:33 +02:00
|
|
|
if r.is_error:
|
2020-03-31 19:05:25 +02:00
|
|
|
return PaymentStatus(None)
|
2020-01-18 11:24:22 +01:00
|
|
|
|
2022-08-30 13:28:58 +02: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}
|
2022-08-30 13:28:58 +02:00
|
|
|
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)
|
2021-08-30 19:55:02 +02:00
|
|
|
while True:
|
|
|
|
value = await self.queue.get()
|
2020-10-04 02:57:14 +02:00
|
|
|
yield value
|