lnbits-legend/lnbits/wallets/cliche.py

151 lines
5.2 KiB
Python
Raw Normal View History

2022-07-31 23:46:42 +01:00
import asyncio
import hashlib
2022-07-31 23:46:42 +01:00
import json
from os import getenv
from typing import AsyncGenerator, Dict, Optional
import httpx
from loguru import logger
2022-07-31 23:51:57 +01:00
from websocket import create_connection
2022-07-31 23:46:42 +01:00
from .base import (
InvoiceResponse,
PaymentResponse,
PaymentStatus,
StatusResponse,
Wallet,
)
class ClicheWallet(Wallet):
"""https://github.com/fiatjaf/cliche"""
def __init__(self):
self.endpoint = getenv("CLICHE_ENDPOINT")
async def status(self) -> StatusResponse:
try:
ws = create_connection(self.endpoint)
ws.send("get-info")
2022-07-31 23:51:57 +01:00
r = ws.recv()
2022-07-31 23:46:42 +01:00
except Exception as exc:
return StatusResponse(
f"Failed to connect to {self.endpoint} due to: {exc}", 0
)
try:
data = json.loads(r)
except:
return StatusResponse(
f"Failed to connect to {self.endpoint}, got: '{r.text[:200]}...'", 0
)
return StatusResponse(None, data["result"]["wallets"][0]["balance"])
async def create_invoice(
self,
amount: int,
memo: Optional[str] = None,
description_hash: Optional[bytes] = None,
unhashed_description: Optional[bytes] = None,
2022-07-31 23:46:42 +01:00
) -> InvoiceResponse:
if unhashed_description or description_hash:
description_hash_str = (
description_hash.hex()
if description_hash
else hashlib.sha256(unhashed_description).hexdigest()
if unhashed_description
else None
)
2022-07-31 23:46:42 +01:00
ws = create_connection(self.endpoint)
2022-07-31 23:51:57 +01:00
ws.send(
f"create-invoice --msatoshi {amount*1000} --description_hash {description_hash_str}"
2022-07-31 23:51:57 +01:00
)
r = ws.recv()
2022-07-31 23:46:42 +01:00
else:
ws = create_connection(self.endpoint)
ws.send(f"create-invoice --msatoshi {amount*1000} --description {memo}")
2022-07-31 23:51:57 +01:00
r = ws.recv()
2022-07-31 23:46:42 +01:00
data = json.loads(r)
checking_id = None
payment_request = None
error_message = None
2022-08-01 10:19:07 +01:00
if data.get("error") is not None and data["error"].get("message"):
logger.error(data["error"]["message"])
error_message = data["error"]["message"]
2022-08-01 10:22:51 +01:00
return InvoiceResponse(False, checking_id, payment_request, error_message)
2022-08-01 10:19:07 +01:00
if data.get("result") is not None:
2022-07-31 23:51:57 +01:00
checking_id, payment_request = (
data["result"]["payment_hash"],
data["result"]["invoice"],
)
2022-08-01 10:19:07 +01:00
else:
2022-08-01 10:22:51 +01:00
return InvoiceResponse(
False, checking_id, payment_request, "Could not get payment hash"
)
2022-08-01 10:19:07 +01:00
2022-08-01 09:41:03 +01:00
return InvoiceResponse(True, checking_id, payment_request, error_message)
2022-07-31 23:46:42 +01:00
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
ws = create_connection(self.endpoint)
ws.send(f"pay-invoice --invoice {bolt11}")
2022-07-31 23:51:57 +01:00
r = ws.recv()
2022-07-31 23:46:42 +01:00
data = json.loads(r)
checking_id = None
error_message = None
2022-08-01 10:19:07 +01:00
if data.get("error") is not None and data["error"].get("message"):
logger.error(data["error"]["message"])
error_message = data["error"]["message"]
return PaymentResponse(False, None, 0, error_message)
if data.get("result") is not None and data["result"].get("payment_hash"):
2022-07-31 23:46:42 +01:00
checking_id = data["result"]["payment_hash"]
2022-08-01 10:19:07 +01:00
else:
return PaymentResponse(False, checking_id, 0, "Could not get payment hash")
2022-08-01 09:41:03 +01:00
return PaymentResponse(True, checking_id, 0, error_message)
2022-07-31 23:46:42 +01:00
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
ws = create_connection(self.endpoint)
ws.send(f"check-payment --hash {checking_id}")
2022-07-31 23:51:57 +01:00
r = ws.recv()
2022-07-31 23:46:42 +01:00
data = json.loads(r)
2022-08-01 10:19:07 +01:00
if data.get("error") is not None and data["error"].get("message"):
logger.error(data["error"]["message"])
return PaymentStatus(None)
statuses = {"pending": None, "complete": True, "failed": False}
2022-08-01 09:41:03 +01:00
return PaymentStatus(statuses[data["result"]["status"]])
2022-07-31 23:46:42 +01:00
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
ws = create_connection(self.endpoint)
ws.send(f"check-payment --hash {checking_id}")
2022-07-31 23:51:57 +01:00
r = ws.recv()
2022-07-31 23:46:42 +01:00
data = json.loads(r)
2022-08-01 10:19:07 +01:00
if data.get("error") is not None and data["error"].get("message"):
logger.error(data["error"]["message"])
return PaymentStatus(None)
statuses = {"pending": None, "complete": True, "failed": False}
2022-08-01 09:41:03 +01:00
return PaymentStatus(statuses[data["result"]["status"]])
2022-07-31 23:46:42 +01:00
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
2022-08-01 08:56:37 +01:00
try:
ws = await create_connection(self.endpoint)
while True:
r = await ws.recv()
data = json.loads(r)
try:
if data["result"]["status"]:
yield data["result"]["payment_hash"]
except:
continue
except:
pass
logger.error("lost connection to cliche's websocket, retrying in 5 seconds")
await asyncio.sleep(5)