From 8ea2d203897ec1b623d170f27b5b519e072f84e3 Mon Sep 17 00:00:00 2001 From: Stefan Stammberger Date: Tue, 9 Nov 2021 18:15:07 +0100 Subject: [PATCH 1/2] fix: wallets not loading when protobuf is missing --- docs/guide/wallets.md | 2 +- lnbits/wallets/lndgrpc.py | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/guide/wallets.md b/docs/guide/wallets.md index d53909427..e6431ffd7 100644 --- a/docs/guide/wallets.md +++ b/docs/guide/wallets.md @@ -29,7 +29,7 @@ Using this wallet requires the installation of the `pylightning` Python package. ### LND (gRPC) -Using this wallet requires the installation of the `grpcio` Python packages. +Using this wallet requires the installation of the `grpcio` and `protobuf` Python packages. - `LNBITS_BACKEND_WALLET_CLASS`: **LndWallet** - `LND_GRPC_ENDPOINT`: ip_address diff --git a/lnbits/wallets/lndgrpc.py b/lnbits/wallets/lndgrpc.py index 6eaafce57..3e030677b 100644 --- a/lnbits/wallets/lndgrpc.py +++ b/lnbits/wallets/lndgrpc.py @@ -1,7 +1,10 @@ +imports_ok = True try: + from google import protobuf import grpc except ImportError: # pragma: nocover - grpc = None + imports_ok = False + import binascii import base64 @@ -9,8 +12,9 @@ import hashlib from os import environ, error, getenv from typing import Optional, Dict, AsyncGenerator -import lnbits.wallets.lnd_grpc_files.lightning_pb2 as ln -import lnbits.wallets.lnd_grpc_files.lightning_pb2_grpc as lnrpc +if imports_ok: + import lnbits.wallets.lnd_grpc_files.lightning_pb2 as ln + import lnbits.wallets.lnd_grpc_files.lightning_pb2_grpc as lnrpc from .base import ( StatusResponse, @@ -76,9 +80,9 @@ environ["GRPC_SSL_CIPHER_SUITES"] = "HIGH+ECDSA" class LndWallet(Wallet): def __init__(self): - if grpc is None: # pragma: nocover + if not imports_ok: # pragma: nocover raise ImportError( - "The `grpcio` library must be installed to use `GRPC LndWallet`. Alternatively try using the LndRESTWallet." + "The `grpcio` and `protobuf` library must be installed to use `GRPC LndWallet`. Alternatively try using the LndRESTWallet." ) endpoint = getenv("LND_GRPC_ENDPOINT") From 81d8d4e2180a57086974b3b0200943084839fe1c Mon Sep 17 00:00:00 2001 From: Ben Arc Date: Tue, 9 Nov 2021 17:44:05 +0000 Subject: [PATCH 2/2] plugged few key issues --- lnbits/core/views/api.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index 683afe287..4d9f2d2dd 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -47,16 +47,22 @@ from ..tasks import api_invoice_listeners @core_app.get("/api/v1/wallet") async def api_wallet(wallet: WalletTypeInfo = Depends(get_key_type)): - return { - "id": wallet.wallet.id, - "name": wallet.wallet.name, - "balance": wallet.wallet.balance_msat, - } + if wallet.wallet_type == 0: + return { + "id": wallet.wallet.id, + "name": wallet.wallet.name, + "balance": wallet.wallet.balance_msat, + } + else: + return { + "name": wallet.wallet.name, + "balance": wallet.wallet.balance_msat, + } @core_app.put("/api/v1/wallet/{new_name}") async def api_update_wallet( - new_name: str, wallet: WalletTypeInfo = Depends(get_key_type) + new_name: str, wallet: WalletTypeInfo = Depends(WalletAdminKeyChecker()) ): await update_wallet(wallet.wallet.id, new_name) return { @@ -193,7 +199,6 @@ async def api_payments_create( wallet: WalletTypeInfo = Depends(get_key_type), invoiceData: CreateInvoiceData = Body(...), ): - if wallet.wallet_type < 0 or wallet.wallet_type > 2: raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Key is invalid") @@ -315,7 +320,7 @@ async def subscribe(request: Request, wallet: Wallet): @core_app.get("/api/v1/payments/sse") async def api_payments_sse( - request: Request, wallet: WalletTypeInfo = Depends(get_key_type) + request: Request, wallet: WalletTypeInfo = Depends(WalletAdminKeyChecker()) ): return EventSourceResponse( subscribe(request, wallet), ping=20, media_type="text/event-stream"