Skip to content

Instantly share code, notes, and snippets.

@takinbo
Created January 29, 2017 23:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takinbo/27b58c84dcb58abf8c556caad3949aea to your computer and use it in GitHub Desktop.
Save takinbo/27b58c84dcb58abf8c556caad3949aea to your computer and use it in GitHub Desktop.
Writing a Python gRPC client for the Lightning Network Daemon

How to write a Python gRPC client for the Lightning Network Daemon

  • Create a virtual environment for your project
$ virtualenv lnd
  • Activate the virtual environment
$ source lnd/bin/activate
  • Install dependencies (googleapis-common-protos is required due to the use of google/api/annotations.proto)
(lnd)$ pip install grpcio grpcio-tools googleapis-common-protos
  • Clone the google api's repository (required due to the use of google/api/annotations.proto)
(lnd)$ git clone https://github.com/googleapis/googleapis.git
  • Copy the lnd rpc.proto file (you'll find this in lnrpc/rpc.proto) or just download it
(lnd)$ curl -o rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
  • Compile the proto file
(lnd)$ python -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. rpc.proto

After following these steps, two files rpc_pb2.py and rpc_pb2_grpc.py will be generated. These files will be imported in your project while writing your clients. Here's an example of a simple client that was built using these.

import grpc

import rpc_pb2 as ln
import rpc_pb2_grpc as lnrpc

channel = grpc.insecure_channel('localhost:10009')
stub = lnrpc.LightningStub(channel)

response = stub.WalletBalance(ln.WalletBalanceRequest(witness_only=True))
print response.balance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment