This commit is contained in:
ben 2022-07-31 23:51:57 +01:00
parent cac7e1e3c0
commit 940ef26003
3 changed files with 27 additions and 28 deletions

View File

@ -42,8 +42,8 @@ LNBITS_BACKEND_WALLET_CLASS=ClicheWallet
# Set one of these blocks depending on the wallet kind you chose above: # Set one of these blocks depending on the wallet kind you chose above:
#ClicheWallet # ClicheWallet
SPARK_ENDPOINT=ws://127.0.0.1:12000 CLICHE_ENDPOINT=ws://127.0.0.1:12000
# SparkWallet # SparkWallet
SPARK_URL=http://localhost:9737/rpc SPARK_URL=http://localhost:9737/rpc

View File

@ -1,9 +1,9 @@
# flake8: noqa # flake8: noqa
from .cliche import ClicheWallet
from .clightning import CLightningWallet from .clightning import CLightningWallet
from .eclair import EclairWallet from .eclair import EclairWallet
from .fake import FakeWallet from .fake import FakeWallet
from .cliche import ClicheWallet
from .lnbits import LNbitsWallet from .lnbits import LNbitsWallet
from .lndrest import LndRestWallet from .lndrest import LndRestWallet
from .lnpay import LNPayWallet from .lnpay import LNPayWallet

View File

@ -5,6 +5,7 @@ from typing import AsyncGenerator, Dict, Optional
import httpx import httpx
from loguru import logger from loguru import logger
from websocket import create_connection
from .base import ( from .base import (
InvoiceResponse, InvoiceResponse,
@ -13,7 +14,6 @@ from .base import (
StatusResponse, StatusResponse,
Wallet, Wallet,
) )
from websocket import create_connection
class ClicheWallet(Wallet): class ClicheWallet(Wallet):
@ -26,7 +26,7 @@ class ClicheWallet(Wallet):
try: try:
ws = create_connection(self.endpoint) ws = create_connection(self.endpoint)
ws.send("get-info") ws.send("get-info")
r = ws.recv() r = ws.recv()
except Exception as exc: except Exception as exc:
return StatusResponse( return StatusResponse(
f"Failed to connect to {self.endpoint} due to: {exc}", 0 f"Failed to connect to {self.endpoint} due to: {exc}", 0
@ -48,42 +48,45 @@ class ClicheWallet(Wallet):
) -> InvoiceResponse: ) -> InvoiceResponse:
if description_hash: if description_hash:
ws = create_connection(self.endpoint) ws = create_connection(self.endpoint)
ws.send(f"create-invoice --msatoshi {amount*1000} --description_hash {description_hash}") ws.send(
r = ws.recv() f"create-invoice --msatoshi {amount*1000} --description_hash {description_hash}"
)
r = ws.recv()
else: else:
ws = create_connection(self.endpoint) ws = create_connection(self.endpoint)
ws.send(f"create-invoice --msatoshi {amount*1000} --description {memo}") ws.send(f"create-invoice --msatoshi {amount*1000} --description {memo}")
r = ws.recv() r = ws.recv()
data = json.loads(r) data = json.loads(r)
checking_id = None checking_id = None
payment_request = None payment_request = None
error_message = None error_message = None
try: try:
if data["error"]["message"]: if data["error"]["message"]:
logger.error( logger.error(data["error"]["message"])
data["error"]["message"]
)
error_message = data["error"]["message"] error_message = data["error"]["message"]
return InvoiceResponse(False, checking_id, payment_request, error_message) return InvoiceResponse(
False, checking_id, payment_request, error_message
)
except: except:
checking_id, payment_request = data["result"]["payment_hash"], data["result"]["invoice"] checking_id, payment_request = (
return InvoiceResponse(True, checking_id, payment_request, error_message) data["result"]["payment_hash"],
data["result"]["invoice"],
)
return InvoiceResponse(True, checking_id, payment_request, error_message)
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse: async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
ws = create_connection(self.endpoint) ws = create_connection(self.endpoint)
ws.send(f"pay-invoice --invoice {bolt11}") ws.send(f"pay-invoice --invoice {bolt11}")
r = ws.recv() r = ws.recv()
data = json.loads(r) data = json.loads(r)
checking_id = None checking_id = None
error_message = None error_message = None
try: try:
if data["error"]["message"]: if data["error"]["message"]:
logger.error( logger.error(data["error"]["message"])
data["error"]["message"]
)
error_message = data["error"]["message"] error_message = data["error"]["message"]
return PaymentResponse(False, None, 0, error_message) return PaymentResponse(False, None, 0, error_message)
except: except:
checking_id = data["result"]["payment_hash"] checking_id = data["result"]["payment_hash"]
return PaymentResponse(True, checking_id, 0, error_message) return PaymentResponse(True, checking_id, 0, error_message)
@ -91,13 +94,11 @@ class ClicheWallet(Wallet):
async def get_invoice_status(self, checking_id: str) -> PaymentStatus: async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
ws = create_connection(self.endpoint) ws = create_connection(self.endpoint)
ws.send(f"check-payment --hash {checking_id}") ws.send(f"check-payment --hash {checking_id}")
r = ws.recv() r = ws.recv()
data = json.loads(r) data = json.loads(r)
try: try:
if data["error"]["message"]: if data["error"]["message"]:
logger.error( logger.error(data["error"]["message"])
data["error"]["message"]
)
return PaymentStatus(None) return PaymentStatus(None)
except: except:
statuses = {"pending": None, "complete": True, "failed": False} statuses = {"pending": None, "complete": True, "failed": False}
@ -106,13 +107,11 @@ class ClicheWallet(Wallet):
async def get_payment_status(self, checking_id: str) -> PaymentStatus: async def get_payment_status(self, checking_id: str) -> PaymentStatus:
ws = create_connection(self.endpoint) ws = create_connection(self.endpoint)
ws.send(f"check-payment --hash {checking_id}") ws.send(f"check-payment --hash {checking_id}")
r = ws.recv() r = ws.recv()
data = json.loads(r) data = json.loads(r)
try: try:
if data["error"]["message"]: if data["error"]["message"]:
logger.error( logger.error(data["error"]["message"])
data["error"]["message"]
)
return PaymentStatus(None) return PaymentStatus(None)
except: except:
statuses = {"pending": None, "complete": True, "failed": False} statuses = {"pending": None, "complete": True, "failed": False}
@ -122,4 +121,4 @@ class ClicheWallet(Wallet):
self.queue: asyncio.Queue = asyncio.Queue(0) self.queue: asyncio.Queue = asyncio.Queue(0)
while True: while True:
value = await self.queue.get() value = await self.queue.get()
yield value yield value