Skip to main content

An rpc client for LND (lightning network deamon)

Project description

lndgrpc

A python grpc client for LND (Lightning Network Daemon) ⚡⚡⚡

This is a wrapper around the default grpc interface that handles setting up credentials (including macaroons). An async client is also available to do fun async stuff like listening for invoices in the background.

Dependencies

  • Python 3.6+
  • Working LND lightning node, take note of its ip address.
  • Copy your admin.macaroon and tls.cert files from your node to a directoy on your machine.

Installation

pip install py-lnd-grpc

# Test it is working
# Set these values as needed!
export CRED_PATH=/path/to/macaroon/and/tls/cert
export LND_NODE_IP=192.168.1.xx

# This will run a get_info() request on your node, checking its connection.
python3 -m lndgrpc

Environment Variables

These environment variables are only used when testing node connectivity and/or correct module installation from the command line. This library is primarily used through Python scripting.

export CRED_PATH=/path/to/macaroon/and/tls/cert
export LND_NODE_IP=192.168.1.xx

python3 -m lndgrpc

# You should expect to see:
#
# .....
# .....
# .....
# lndgrpc package is installed... Wow it works!

Basic Usage

The api mirrors the underlying lnd grpc api (http://api.lightning.community/) but methods will be in pep8 style. ie. .GetInfo() becomes .get_info().

from lndgrpc import LNDClient

# pass in the ip-address with RPC port and network ('mainnet', 'testnet', 'simnet')
# the client defaults to 127.0.0.1:10009 and mainnet if no args provided
lnd = LNDClient("127.0.0.1:10009", network='simnet')

# Unlock you wallet
lnd.unlock_wallet(wallet_password=b"your_wallet_password")

# Get general data about your node
lnd.get_info()

print('Listening for invoices...')
for invoice in lnd.subscribe_invoices():
    print(invoice)

Async

import asyncio
from lndgrpc import AsyncLNDClient

async_lnd = AsyncLNDClient()

async def subscribe_invoices():
    print('Listening for invoices...')
    async for invoice in async_lnd.subscribe_invoices():
        print(invoice)

async def get_info():
    while True:
        info = await async_lnd.get_info()
        print(info)
        await asyncio.sleep(5)

async def run():
    coros = [subscribe_invoices(), get_info()]
    await asyncio.gather(*coros)

loop = asyncio.get_event_loop()
loop.run_until_complete(run())

Specifying Macaroon/Cert files

By default the client will attempt to lookup the readonly.macaron and tls.cert files in the mainnet directory. However if you want to specify a different macaroon or different path you can pass in the filepath explicitly.

lnd = LNDClient(
    macaroon_filepath='~/.lnd/invoice.macaroon', 
    cert_filepath='path/to/tls.cert'
)

Generating LND Proto Files

virtualenv lnd
source lnd/bin/activate
pip install grpcio grpcio-tools googleapis-common-protos
git clone https://github.com/googleapis/googleapis.git
git clone https://github.com/lightningnetwork/lnd.git
# python -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. rpc.proto
from pathlib import Path
import shutil

for proto in list(Path("lnd/lnrpc").rglob("*.proto")):
    shutil.copy(proto,Path.cwd())

for proto in list(Path(".").rglob("*.proto")):
    sh.python("-m","grpc_tools.protoc","--proto_path=.","--python_out=.","--grpc_python_out=.", str(proto))

Last Step: In File: verrpc_pb2_grpc.py Change: import verrpc_pb2 as verrpc__pb2 To: from lndgrpc import verrpc_pb2 as verrpc__pb2

Deploy to Test-PyPi

poetry build
twine check dist/*
twine upload --repository-url https://test.pypi.org/legacy/ dist/*

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

py-lnd-grpc-0.2.4.tar.gz (102.7 kB view details)

Uploaded Source

Built Distribution

py_lnd_grpc-0.2.4-py3-none-any.whl (108.3 kB view details)

Uploaded Python 3

File details

Details for the file py-lnd-grpc-0.2.4.tar.gz.

File metadata

  • Download URL: py-lnd-grpc-0.2.4.tar.gz
  • Upload date:
  • Size: 102.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.0 pkginfo/1.7.0 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5

File hashes

Hashes for py-lnd-grpc-0.2.4.tar.gz
Algorithm Hash digest
SHA256 67d0f128eb6ecd2e82fe03292b0449cc9d00772fb72b86164a81f0308d9dabaa
MD5 5f58f92792da37f2ce123af82db610e2
BLAKE2b-256 400c34e656272b7cca84bdf001aa64b5f4d029bab3647e8f5a917f6d293cd8cd

See more details on using hashes here.

File details

Details for the file py_lnd_grpc-0.2.4-py3-none-any.whl.

File metadata

  • Download URL: py_lnd_grpc-0.2.4-py3-none-any.whl
  • Upload date:
  • Size: 108.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.0 pkginfo/1.7.0 requests/2.25.0 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.8.5

File hashes

Hashes for py_lnd_grpc-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 fec6383c08a4a26dc0e169a14578f317aaacdeeff67cb825b37e123f2ca212c7
MD5 ad04e7e8c64c0a62d0c03ab788748dd5
BLAKE2b-256 8b548c854dd6d8d030b9038caf186e14e5f6fa5a7981f9c8e1c13904ade9f571

See more details on using hashes here.

Supported by

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