2021-08-30 19:55:02 +02:00
|
|
|
import asyncio
|
2022-08-01 16:20:25 +02:00
|
|
|
import hashlib
|
2020-10-15 04:58:57 +02:00
|
|
|
import json
|
2020-03-31 19:05:25 +02:00
|
|
|
from os import getenv
|
2022-07-16 14:23:03 +02:00
|
|
|
from typing import AsyncGenerator, Dict, Optional
|
2020-01-16 13:58:27 +01:00
|
|
|
|
2022-07-16 14:23:03 +02:00
|
|
|
import httpx
|
2022-07-07 14:30:16 +02:00
|
|
|
from loguru import logger
|
|
|
|
|
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-10 21:26:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
class LntxbotWallet(Wallet):
|
|
|
|
"""https://github.com/fiatjaf/lntxbot/blob/master/api.go"""
|
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
def __init__(self):
|
|
|
|
endpoint = getenv("LNTXBOT_API_ENDPOINT")
|
2020-01-10 21:26:42 +01:00
|
|
|
self.endpoint = endpoint[:-1] if endpoint.endswith("/") else endpoint
|
2020-10-08 21:03:18 +02:00
|
|
|
|
2021-03-24 04:40:32 +01:00
|
|
|
key = (
|
|
|
|
getenv("LNTXBOT_KEY")
|
|
|
|
or getenv("LNTXBOT_ADMIN_KEY")
|
|
|
|
or getenv("LNTXBOT_INVOICE_KEY")
|
|
|
|
)
|
2020-10-08 21:03:18 +02:00
|
|
|
self.auth = {"Authorization": f"Basic {key}"}
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def status(self) -> StatusResponse:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
r = await client.get(
|
2021-10-17 19:33:29 +02:00
|
|
|
f"{self.endpoint}/balance", headers=self.auth, timeout=40
|
2021-03-24 05:01:09 +01:00
|
|
|
)
|
2020-10-13 03:25:55 +02:00
|
|
|
try:
|
|
|
|
data = r.json()
|
|
|
|
except:
|
2021-03-24 04:40:32 +01:00
|
|
|
return StatusResponse(
|
|
|
|
f"Failed to connect to {self.endpoint}, got: '{r.text[:200]}...'", 0
|
|
|
|
)
|
2020-10-13 03:25:55 +02:00
|
|
|
|
|
|
|
if data.get("error"):
|
|
|
|
return StatusResponse(data["message"], 0)
|
|
|
|
|
|
|
|
return StatusResponse(None, data["BTC"]["AvailableBalance"] * 1000)
|
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def create_invoice(
|
2021-03-24 04:40:32 +01: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,
|
|
|
|
**kwargs,
|
2020-08-31 04:48:46 +02:00
|
|
|
) -> InvoiceResponse:
|
|
|
|
data: Dict = {"amt": str(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 ""
|
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
r = await client.post(
|
2021-10-17 19:33:29 +02:00
|
|
|
f"{self.endpoint}/addinvoice", headers=self.auth, json=data, timeout=40
|
2021-03-24 05:01:09 +01:00
|
|
|
)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2020-10-08 21:03:18 +02:00
|
|
|
if r.is_error:
|
|
|
|
try:
|
|
|
|
data = r.json()
|
2020-03-31 19:05:25 +02:00
|
|
|
error_message = data["message"]
|
2020-10-08 21:03:18 +02:00
|
|
|
except:
|
|
|
|
error_message = r.text
|
|
|
|
pass
|
2020-03-31 19:05:25 +02:00
|
|
|
|
2020-10-08 21:03:18 +02:00
|
|
|
return InvoiceResponse(False, None, None, error_message)
|
|
|
|
|
|
|
|
data = r.json()
|
|
|
|
return InvoiceResponse(True, data["payment_hash"], data["pay_req"], None)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2022-03-16 07:20:15 +01:00
|
|
|
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
|
2021-03-24 05:01:09 +01:00
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
r = await client.post(
|
|
|
|
f"{self.endpoint}/payinvoice",
|
|
|
|
headers=self.auth,
|
|
|
|
json={"invoice": bolt11},
|
2022-07-27 15:29:44 +02:00
|
|
|
timeout=None,
|
2021-03-24 05:01:09 +01:00
|
|
|
)
|
2020-01-16 13:37:49 +01:00
|
|
|
|
2021-11-12 11:16:15 +01:00
|
|
|
if "error" in r.json():
|
2020-10-08 21:03:18 +02:00
|
|
|
try:
|
|
|
|
data = r.json()
|
|
|
|
error_message = data["message"]
|
|
|
|
except:
|
|
|
|
error_message = r.text
|
|
|
|
pass
|
2022-08-30 13:28:58 +02:00
|
|
|
return PaymentResponse(False, None, None, None, error_message)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2020-10-08 21:03:18 +02:00
|
|
|
data = r.json()
|
2022-08-30 13:28:58 +02:00
|
|
|
if data.get("type") != "paid_invoice":
|
|
|
|
return PaymentResponse(None)
|
2020-10-13 04:18:37 +02:00
|
|
|
checking_id = data["payment_hash"]
|
2021-01-05 23:50:06 +01:00
|
|
|
fee_msat = -data["fee_msat"]
|
2020-10-13 04:18:37 +02:00
|
|
|
preimage = data["payment_preimage"]
|
|
|
|
return PaymentResponse(True, checking_id, fee_msat, preimage, None)
|
2020-01-16 14:26:51 +01:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
r = await client.post(
|
|
|
|
f"{self.endpoint}/invoicestatus/{checking_id}?wait=false",
|
|
|
|
headers=self.auth,
|
|
|
|
)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
data = r.json()
|
2020-10-08 21:03:18 +02:00
|
|
|
if r.is_error or "error" in data:
|
|
|
|
return PaymentStatus(None)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2020-05-09 18:29:24 +02:00
|
|
|
if "preimage" not in data:
|
2020-03-31 19:05:25 +02:00
|
|
|
return PaymentStatus(False)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2020-03-31 19:05:25 +02:00
|
|
|
return PaymentStatus(True)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2021-03-24 05:01:09 +01:00
|
|
|
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
|
|
|
|
async with httpx.AsyncClient() as client:
|
|
|
|
r = await client.post(
|
2021-10-17 19:33:29 +02:00
|
|
|
url=f"{self.endpoint}/paymentstatus/{checking_id}", headers=self.auth
|
2021-03-24 05:01:09 +01:00
|
|
|
)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2020-10-08 21:03:18 +02:00
|
|
|
data = r.json()
|
|
|
|
if r.is_error or "error" in data:
|
2020-03-31 19:05:25 +02:00
|
|
|
return PaymentStatus(None)
|
2020-01-10 21:26:42 +01:00
|
|
|
|
2020-03-07 22:27:00 +01:00
|
|
|
statuses = {"complete": True, "failed": False, "pending": None, "unknown": None}
|
2020-10-08 21:03:18 +02:00
|
|
|
return PaymentStatus(statuses[data.get("status", "unknown")])
|
2020-10-02 22:13:33 +02:00
|
|
|
|
|
|
|
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
|
2020-10-15 04:58:57 +02:00
|
|
|
url = f"{self.endpoint}/payments/stream"
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
async with httpx.AsyncClient(timeout=None, headers=self.auth) as client:
|
|
|
|
async with client.stream("GET", url) as r:
|
|
|
|
async for line in r.aiter_lines():
|
|
|
|
if line.startswith("data:"):
|
|
|
|
data = json.loads(line[5:])
|
|
|
|
if "payment_hash" in data and data.get("msatoshi") > 0:
|
|
|
|
yield data["payment_hash"]
|
2021-04-18 15:50:07 +02:00
|
|
|
except (OSError, httpx.ReadError, httpx.ReadTimeout, httpx.ConnectError):
|
2020-10-15 04:58:57 +02:00
|
|
|
pass
|
|
|
|
|
2022-07-07 14:30:16 +02:00
|
|
|
logger.error(
|
|
|
|
"lost connection to lntxbot /payments/stream, retrying in 5 seconds"
|
|
|
|
)
|
2021-08-30 19:55:02 +02:00
|
|
|
await asyncio.sleep(5)
|