Skip to main content

An LND gRPC client for Python 3.6

Project description

lnd-grpc

Version 0.3.1

Requires python >=3.6

Build Status CodeFactor License: MIT

A simple library to provide a Python 3 interface to the lnd lightning client gRPC.

This version of the library has been compiled with lnd proto files from the v0.6.1-beta tag on github. This version has been tested using Bitcoin Core v0.18.0 as a backend

Install requires:

  • grpcio
  • grpcio-tools
  • googleapis-common-protos

Note: Configuration for coins other than bitcoin will require modifying the source code directly.

Installation

Via pip:

pip install lnd-grpc

Cloning and installing source as editable package:

git clone https://github.com/willcl-ark/lnd_grpc.git

cd lnd_grpc

Activate virtual env as required

pip install -e .

Bitcoin setup

bitcoind or btcd must be running and be ready to accept rpc connections from lnd.

LND setup

lnd daemon must be running on the host machine. This can typically be accomplished in a screen/tmux session.

If lnd.conf is not already configured to communicate with your bitcoin client, an example lnd daemon startup command for bitcoind connection might look like:

lnd --bitcoin.active \
--bitcoin.mainnet \
--debuglevel=debug \
--bitcoin.node=bitcoind \
--bitcoind.rpcuser=xxxxx \
--bitcoind.rpcpass=xxxxxxxxxxxxxx \
--externalip=xx.xx.xx.xx \
--bitcoind.zmqpubrawblock=tcp://host:port \
--bitcoind.zmqpubrawtx=tcp://host:port \
--rpclisten=host:port

Using

Import the module into your project:

import lnd_grpc

Create an instance of the client class:

lnd_rpc = lnd_grpc.Client()

Note: The class is instantiated to work with default bitcoind rpc port and lnd in default installation directory, on mainnet, unless additional arguments are passed.

The class instantiation takes the the following arguments which you can change as required:

    (
    lnd_dir: str = None, \
    macaroon_path: str = None, \
    tls_cert_path: str = None \
    network: str = 'mainnet', \
    grpc_host: str = 'localhost', \
    grpc_port: str = '10009'
    )

Initialization of a new lnd installation

Note: If you have already created a wallet during lnd setup/installation you can skip this section.

If this is the first time you have run lnd you will not have a wallet created. 'Macaroons', the authentication technique used to communicate securely with lnd, are tied to a wallet (seed) and therefore an alternative connection must be made with lnd to create the wallet, before recreating the connection stub using the wallet's macaroon.

Initialization requires the following steps:

  1. Generate a new seed lnd_rpc.gen_seed()
  2. Initialize a new wallet lnd_rpc.init_wallet()

Connecting and re-connecting after wallet created

If you did not run the initialization sequence above, you will only need to unlock your wallet before issuing further RPC commands:

lnd_rpc.unlock_wallet(password='wallet_password')

Interface conventions

Further RPC commands can then be issued to the lnd gRPC interface using the following convention, where LND gRPC commands are converted from CamelCase to lowercase_with_underscores and keyword arguments named to exactly match the parameters the gRPC uses:

lnd_rpc.grpc_command(keyword_arg=value)

Valid gRPC commands and their keyword arguments can be found here

Connection stubs will be generated dynamically as required to ensure channel freshness.

Iterables

Response-streaming RPCs now return the python iterators themselves to be operated on, e.g. with .__next__() or for resp in response:

Threading

The backend LND server (Golang) has asynchronous capability so any limitations are on the client side. The Python gRPC Client is not natively async-compatible (e.g. using asyncio). There are wrappers which exist that can 'wrap' python gRPC Client methods into async methods, but using threading is the officially support technique at this moment.

For Python client threading to work correctly you must use the same channel for each thread. This is easy with this library if you use a single Client() instance in your application, as the same channel is used for each RPC for that Client object. This makes threading relatively easy, e.g.:

# get a queue to add responses to
queue = queue.Queue()

# create a function to perform the work you want the thread to target:
def inv_sub_worker(_hash):
    for _response in lnd_rpc.subscribe_single_invoice(_hash):
        queue.put(_response)

# create the thread
# useful to use daemon mode for subscriptions
inv_sub = threading.Thread(target=inv_sub_worker, args=[_hash, ], daemon=True)

# start the thread
inv_sub.start()

Loop

LND must be re-built and installed as per the loop instructions found at the Loop Readme.

Loopd should then be installed as per the same instructions and started manually.

Then you can import and use the RPC client using the following code:

import loop_rpc

loop = loop_rpc.LoopClient()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lnd_grpc-0.3.1.tar.gz (71.6 kB view details)

Uploaded Source

Built Distribution

lnd_grpc-0.3.1-py3-none-any.whl (71.3 kB view details)

Uploaded Python 3

File details

Details for the file lnd_grpc-0.3.1.tar.gz.

File metadata

  • Download URL: lnd_grpc-0.3.1.tar.gz
  • Upload date:
  • Size: 71.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2

File hashes

Hashes for lnd_grpc-0.3.1.tar.gz
Algorithm Hash digest
SHA256 2cc8a2da4459d7efcf078ff3f300f566f2f8bec9ec5a51750ed5d5bb61af4be8
MD5 0e1fce9c6e21c393a052315ac8d94ea0
BLAKE2b-256 b62aa7a7a8aaa967c939d75e37d04836c94d475247e1179877ace36f0285e6c1

See more details on using hashes here.

File details

Details for the file lnd_grpc-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: lnd_grpc-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 71.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.7.3 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2

File hashes

Hashes for lnd_grpc-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2b733a8b6f2fa890063c1d16a79fec41328258904bf5f1d83f282c1f1ed76cf
MD5 065a03ca503f804bca2e2b42ababc15d
BLAKE2b-256 71a780f0ce942c43c2d9aa58885d7bb988409acdc8bd5f22e41a80dc74f480a7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page