lnbits-legend/lnbits/wallets/lnpay.py

101 lines
3.3 KiB
Python
Raw Normal View History

import json
import trio # type: ignore
import httpx
from os import getenv
from http import HTTPStatus
2020-09-27 23:12:55 -03:00
from typing import Optional, Dict, AsyncGenerator
from quart import request
2020-01-18 10:24:22 +00:00
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
2020-01-18 10:24:22 +00:00
class LNPayWallet(Wallet):
"""https://docs.lnpay.co/"""
def __init__(self):
endpoint = getenv("LNPAY_API_ENDPOINT", "https://lnpay.co/v1")
2020-01-18 10:24:22 +00:00
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
self.wallet_key = getenv("LNPAY_WALLET_KEY") or getenv("LNPAY_ADMIN_KEY")
self.auth = {"X-Api-Key": getenv("LNPAY_API_KEY")}
2020-01-18 10:24:22 +00:00
def create_invoice(
2020-09-27 23:12:55 -03:00
self,
amount: int,
memo: Optional[str] = None,
description_hash: Optional[bytes] = None,
) -> InvoiceResponse:
data: Dict = {"num_satoshis": f"{amount}"}
if description_hash:
data["description_hash"] = description_hash.hex()
else:
data["memo"] = memo or ""
r = httpx.post(
url=f"{self.endpoint}/user/wallet/{self.wallet_key}/invoice",
headers=self.auth,
2020-09-03 23:02:15 +02:00
json=data,
)
2020-09-27 23:12:55 -03:00
ok, checking_id, payment_request, error_message = (
r.status_code == 201,
None,
None,
r.text,
)
if ok:
2020-01-18 10:24:22 +00:00
data = r.json()
checking_id, payment_request = data["id"], data["payment_request"]
2020-01-18 10:24:22 +00:00
return InvoiceResponse(ok, checking_id, payment_request, error_message)
2020-01-18 10:24:22 +00:00
def pay_invoice(self, bolt11: str) -> PaymentResponse:
r = httpx.post(
url=f"{self.endpoint}/user/wallet/{self.wallet_key}/withdraw",
headers=self.auth,
json={"payment_request": bolt11},
)
ok, checking_id, fee_msat, error_message = r.status_code == 201, None, 0, None
if ok:
checking_id = r.json()["lnTx"]["id"]
2020-01-18 10:24:22 +00:00
return PaymentResponse(ok, checking_id, fee_msat, error_message)
2020-01-18 10:24:22 +00:00
def get_invoice_status(self, checking_id: str) -> PaymentStatus:
return self.get_payment_status(checking_id)
2020-01-18 10:24:22 +00:00
def get_payment_status(self, checking_id: str) -> PaymentStatus:
r = httpx.get(
2020-09-27 23:12:55 -03:00
url=f"{self.endpoint}/user/lntx/{checking_id}?fields=settled",
headers=self.auth,
2020-09-27 23:12:55 -03:00
)
2020-01-18 10:24:22 +00:00
if r.is_error:
return PaymentStatus(None)
2020-01-18 10:24:22 +00:00
statuses = {0: None, 1: True, -1: False}
return PaymentStatus(statuses[r.json()["settled"]])
2020-09-27 23:12:55 -03:00
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
self.send, receive = trio.open_memory_channel(0)
async for value in receive:
yield value
2020-09-27 23:12:55 -03:00
async def webhook_listener(self):
text: str = await request.get_data()
data = json.loads(text)
if type(data) is not dict or "event" not in data or data["event"].get("name") != "wallet_receive":
return "", HTTPStatus.NO_CONTENT
2020-09-27 23:12:55 -03:00
lntx_id = data["data"]["wtx"]["lnTx"]["id"]
async with httpx.AsyncClient() as client:
r = await client.get(
f"{self.endpoint}/user/lntx/{lntx_id}?fields=settled",
headers=self.auth,
)
data = r.json()
if data["settled"]:
await self.send.send(lntx_id)
return "", HTTPStatus.NO_CONTENT