From 8ea2d203897ec1b623d170f27b5b519e072f84e3 Mon Sep 17 00:00:00 2001 From: Stefan Stammberger Date: Tue, 9 Nov 2021 18:15:07 +0100 Subject: [PATCH] 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")