2017-03-01 02:49:10 +03:00
|
|
|
# How to write a Python gRPC client for the Lightning Network Daemon #
|
|
|
|
|
2017-03-01 03:09:35 +03:00
|
|
|
This section enumerates what you need to do to write a client that communicates
|
|
|
|
with lnd in Python.
|
2017-03-01 02:49:10 +03:00
|
|
|
|
2017-03-01 03:09:35 +03:00
|
|
|
Lnd uses the gRPC protocol for communication with clients like lncli. gRPC is
|
|
|
|
based on protocol buffers and as such, you will need to compile the lnd proto
|
|
|
|
file in Python before you can use it to communicate with lnd. The following are
|
|
|
|
the steps to take.
|
2017-03-01 02:49:10 +03:00
|
|
|
|
|
|
|
* Create a virtual environment for your project
|
|
|
|
```
|
|
|
|
$ virtualenv lnd
|
|
|
|
```
|
|
|
|
* Activate the virtual environment
|
|
|
|
```
|
|
|
|
$ source lnd/bin/activate
|
|
|
|
```
|
2017-03-01 03:09:35 +03:00
|
|
|
* Install dependencies (googleapis-common-protos is required due to the use of
|
|
|
|
google/api/annotations.proto)
|
2017-03-01 02:49:10 +03:00
|
|
|
```
|
|
|
|
(lnd)$ pip install grpcio grpcio-tools googleapis-common-protos
|
|
|
|
```
|
2017-03-01 03:09:35 +03:00
|
|
|
* Clone the google api's repository (required due to the use of
|
|
|
|
google/api/annotations.proto)
|
2017-03-01 02:49:10 +03:00
|
|
|
```
|
|
|
|
(lnd)$ git clone https://github.com/googleapis/googleapis.git
|
|
|
|
```
|
2017-03-01 03:09:35 +03:00
|
|
|
* Copy the lnd rpc.proto file (you'll find this in lnrpc/rpc.proto) or just
|
|
|
|
download it
|
2017-03-01 02:49:10 +03:00
|
|
|
```
|
|
|
|
(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
|
|
|
|
```
|
|
|
|
|
2017-03-01 03:09:35 +03:00
|
|
|
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.
|
2017-03-01 02:49:10 +03:00
|
|
|
|
2017-03-01 03:09:35 +03:00
|
|
|
```python
|
2017-03-01 02:49:10 +03:00
|
|
|
# python-based lnd client to retrieve and display wallet balance
|
|
|
|
|
|
|
|
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)
|
|
|
|
```
|
2017-03-01 03:09:35 +03:00
|
|
|
|
|
|
|
With the above, you should have all the `lnd` related `gRPC` dependencies
|
|
|
|
installed locally into your virtual environment. In order to get up to speed
|
|
|
|
with `protofbuf` usage from Python, see [this official `protobuf` tutorial for
|
|
|
|
Python](https://developers.google.com/protocol-buffers/docs/pythontutorial).
|
|
|
|
Additionally, [this official gRPC
|
|
|
|
resource](http://www.grpc.io/docs/tutorials/basic/python.html) details how to
|
|
|
|
drive `gRPC` from Python including the basics of making RPC calls, streaming
|
|
|
|
RPC's (bi-directional and uni-directional), etc.
|