Buckets:
| import asyncio | |
| import hashlib | |
| import json | |
| from collections.abc import AsyncGenerator | |
| from loguru import logger | |
| from websocket import create_connection | |
| from lnbits.helpers import normalize_endpoint | |
| from lnbits.settings import settings | |
| from .base import ( | |
| InvoiceResponse, | |
| PaymentPendingStatus, | |
| PaymentResponse, | |
| PaymentStatus, | |
| StatusResponse, | |
| Wallet, | |
| ) | |
| class ClicheWallet(Wallet): | |
| """https://github.com/fiatjaf/cliche""" | |
| def __init__(self): | |
| if not settings.cliche_endpoint: | |
| raise ValueError("cannot initialize ClicheWallet: missing cliche_endpoint") | |
| self.endpoint = normalize_endpoint(settings.cliche_endpoint) | |
| async def cleanup(self): | |
| pass | |
| async def status(self) -> StatusResponse: | |
| try: | |
| ws = create_connection(self.endpoint) | |
| ws.send("get-info") | |
| r = ws.recv() | |
| except Exception as exc: | |
| return StatusResponse( | |
| f"Failed to connect to {self.endpoint} due to: {exc}", 0 | |
| ) | |
| try: | |
| data = json.loads(r) | |
| except Exception: | |
| return StatusResponse( | |
| f"Failed to connect to {self.endpoint}, got: {r[:200]!r}...", 0 | |
| ) | |
| return StatusResponse(None, data["result"]["wallets"][0]["balance"]) | |
| async def create_invoice( | |
| self, | |
| amount: int, | |
| memo: str | None = None, | |
| description_hash: bytes | None = None, | |
| unhashed_description: bytes | None = None, | |
| **_, | |
| ) -> 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 | |
| ) | |
| ) | |
| ws = create_connection(self.endpoint) | |
| ws.send( | |
| f"create-invoice --msatoshi {amount*1000} --description_hash" | |
| f" {description_hash_str}" | |
| ) | |
| r = ws.recv() | |
| else: | |
| ws = create_connection(self.endpoint) | |
| ws.send(f"create-invoice --msatoshi {amount*1000} --description {memo}") | |
| r = ws.recv() | |
| data = json.loads(r) | |
| checking_id = None | |
| payment_request = None | |
| if data.get("error") is not None and data["error"].get("message"): | |
| logger.error(data["error"]["message"]) | |
| error_message = data["error"]["message"] | |
| return InvoiceResponse( | |
| ok=False, checking_id=checking_id, error_message=error_message | |
| ) | |
| if data.get("result") is not None: | |
| checking_id, payment_request = ( | |
| data["result"]["payment_hash"], | |
| data["result"]["invoice"], | |
| ) | |
| else: | |
| return InvoiceResponse(ok=False, error_message="Could not get payment hash") | |
| return InvoiceResponse( | |
| ok=True, | |
| checking_id=checking_id, | |
| payment_request=payment_request, | |
| preimage=data["result"].get("preimage"), | |
| ) | |
| async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse: | |
| try: | |
| ws = create_connection(self.endpoint) | |
| ws.send(f"pay-invoice --invoice {bolt11}") | |
| checking_id, fee_msat, preimage, payment_ok = ( | |
| None, | |
| None, | |
| None, | |
| None, | |
| ) | |
| for _ in range(2): | |
| r = ws.recv() | |
| data = json.loads(r) | |
| if data.get("error") is not None: | |
| error_message = data["error"].get("message") | |
| return PaymentResponse(error_message=error_message) | |
| if data.get("method") == "payment_succeeded": | |
| payment_ok = True | |
| checking_id = data["params"]["payment_hash"] | |
| fee_msat = data["params"]["fee_msatoshi"] | |
| preimage = data["params"]["preimage"] | |
| continue | |
| if data.get("result") is None: | |
| return PaymentResponse(error_message="result is None") | |
| return PaymentResponse( | |
| ok=payment_ok, | |
| checking_id=checking_id, | |
| fee_msat=fee_msat, | |
| preimage=preimage, | |
| ) | |
| except Exception as exc: | |
| logger.warning(exc) | |
| return PaymentResponse(error_message=f"Unable to query {self.endpoint}.") | |
| async def get_invoice_status(self, checking_id: str) -> PaymentStatus: | |
| ws = create_connection(self.endpoint) | |
| ws.send(f"check-payment --hash {checking_id}") | |
| r = ws.recv() | |
| data = json.loads(r) | |
| if data.get("error") is not None and data["error"].get("message"): | |
| logger.error(data["error"]["message"]) | |
| return PaymentPendingStatus() | |
| statuses = {"pending": None, "complete": True, "failed": False} | |
| return PaymentStatus(statuses[data["result"]["status"]]) | |
| async def get_payment_status(self, checking_id: str) -> PaymentStatus: | |
| try: | |
| ws = create_connection(self.endpoint) | |
| ws.send(f"check-payment --hash {checking_id}") | |
| r = ws.recv() | |
| data = json.loads(r) | |
| if data.get("error") is not None and data["error"].get("message"): | |
| logger.error(data["error"]["message"]) | |
| return PaymentPendingStatus() | |
| payment = data["result"] | |
| statuses = {"pending": None, "complete": True, "failed": False} | |
| return PaymentStatus( | |
| statuses.get(payment.get("status")), | |
| payment.get("fee_msatoshi"), | |
| payment.get("preimage"), | |
| ) | |
| except Exception as exc: | |
| logger.warning(exc) | |
| return PaymentPendingStatus() | |
| async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: | |
| while settings.lnbits_running: | |
| try: | |
| ws = create_connection(self.endpoint) | |
| while settings.lnbits_running: | |
| r = ws.recv() | |
| data = json.loads(r) | |
| print(data) | |
| try: | |
| if data["result"]["status"]: | |
| yield data["result"]["payment_hash"] | |
| except Exception as exc: | |
| logger.debug(exc) | |
| continue | |
| except Exception as exc: | |
| logger.error( | |
| f"lost connection to cliche's invoices stream: '{exc}', retrying in" | |
| " 5 seconds" | |
| ) | |
| await asyncio.sleep(5) | |
| continue | |
Xet Storage Details
- Size:
- 6.9 kB
- Xet hash:
- 146a7cbcefe41cccb2bfac80f5c97c5c4e6db008007c9d474c0504b8acbde320
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.