2017-08-02 18:53:53 -07:00
|
|
|
# How to write a Python gRPC client for the Lightning Network Daemon
|
2017-02-28 15:49:10 -08:00
|
|
|
|
2017-02-28 16:09:35 -08:00
|
|
|
This section enumerates what you need to do to write a client that communicates
|
2017-12-19 17:28:38 -08:00
|
|
|
with `lnd` in Python.
|
2017-02-28 15:49:10 -08:00
|
|
|
|
2017-08-02 18:53:53 -07:00
|
|
|
### Setup and Installation
|
|
|
|
|
2017-02-28 16:09:35 -08: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
|
2017-08-02 18:53:53 -07:00
|
|
|
file in Python before you can use it to communicate with lnd.
|
2017-02-28 15:49:10 -08:00
|
|
|
|
|
|
|
* Create a virtual environment for your project
|
|
|
|
```
|
|
|
|
$ virtualenv lnd
|
|
|
|
```
|
|
|
|
* Activate the virtual environment
|
|
|
|
```
|
|
|
|
$ source lnd/bin/activate
|
|
|
|
```
|
2017-02-28 16:09:35 -08:00
|
|
|
* Install dependencies (googleapis-common-protos is required due to the use of
|
|
|
|
google/api/annotations.proto)
|
2017-02-28 15:49:10 -08:00
|
|
|
```
|
|
|
|
(lnd)$ pip install grpcio grpcio-tools googleapis-common-protos
|
|
|
|
```
|
2017-02-28 16:09:35 -08:00
|
|
|
* Clone the google api's repository (required due to the use of
|
|
|
|
google/api/annotations.proto)
|
2017-02-28 15:49:10 -08:00
|
|
|
```
|
|
|
|
(lnd)$ git clone https://github.com/googleapis/googleapis.git
|
|
|
|
```
|
2017-08-02 18:53:53 -07:00
|
|
|
* Copy the lnd rpc.proto file (you'll find this at
|
|
|
|
[lnrpc/rpc.proto](https://github.com/lightningnetwork/lnd/blob/master/lnrpc/rpc.proto))
|
|
|
|
or just download it
|
2017-02-28 15:49:10 -08: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-02-28 16:09:35 -08:00
|
|
|
After following these steps, two files `rpc_pb2.py` and `rpc_pb2_grpc.py` will
|
2017-08-02 18:53:53 -07:00
|
|
|
be generated. These files will be imported in your project anytime you use
|
|
|
|
Python gRPC.
|
2017-02-28 15:49:10 -08:00
|
|
|
|
2017-08-02 18:53:53 -07:00
|
|
|
#### Imports and Client
|
2017-02-28 15:49:10 -08:00
|
|
|
|
2017-08-08 22:30:51 -07:00
|
|
|
Everytime you use Python gRPC, you will have to import the generated rpc modules
|
2017-08-02 18:53:53 -07:00
|
|
|
and set up a channel and stub to your connect to your `lnd` node:
|
2017-02-28 15:49:10 -08:00
|
|
|
|
2017-08-02 18:53:53 -07:00
|
|
|
```python
|
2017-02-28 15:49:10 -08:00
|
|
|
import rpc_pb2 as ln
|
|
|
|
import rpc_pb2_grpc as lnrpc
|
2017-08-02 18:53:53 -07:00
|
|
|
import grpc
|
2017-12-28 06:04:01 -08:00
|
|
|
import os
|
2017-02-28 15:49:10 -08:00
|
|
|
|
2017-08-08 22:30:51 -07:00
|
|
|
# Lnd cert is at ~/.lnd/tls.cert on Linux and
|
|
|
|
# ~/Library/Application Support/Lnd/tls.cert on Mac
|
2017-12-13 15:32:25 -08:00
|
|
|
cert = open(os.path.expanduser('~/.lnd/tls.cert')).read()
|
2017-08-08 22:30:51 -07:00
|
|
|
creds = grpc.ssl_channel_credentials(cert)
|
|
|
|
channel = grpc.secure_channel('localhost:10009', creds)
|
2017-02-28 15:49:10 -08:00
|
|
|
stub = lnrpc.LightningStub(channel)
|
2017-08-02 18:53:53 -07:00
|
|
|
```
|
2017-02-28 15:49:10 -08:00
|
|
|
|
2017-08-02 18:53:53 -07:00
|
|
|
### Examples
|
|
|
|
|
2017-08-08 22:30:51 -07:00
|
|
|
Let's walk through some examples of Python gRPC clients. These examples assume
|
|
|
|
that you have at least two `lnd` nodes running, the RPC location of one of which
|
|
|
|
is at the default `localhost:10009`, with an open channel between the two nodes.
|
2017-08-02 18:53:53 -07:00
|
|
|
|
|
|
|
#### Simple RPC
|
|
|
|
|
|
|
|
```python
|
|
|
|
# Retrieve and display the wallet balance
|
2017-02-28 15:49:10 -08:00
|
|
|
response = stub.WalletBalance(ln.WalletBalanceRequest(witness_only=True))
|
2017-11-26 05:33:32 -08:00
|
|
|
print response.total_balance
|
2017-02-28 15:49:10 -08:00
|
|
|
```
|
2017-02-28 16:09:35 -08:00
|
|
|
|
2017-08-02 18:53:53 -07:00
|
|
|
#### Response-streaming RPC
|
|
|
|
|
|
|
|
```python
|
|
|
|
request = ln.InvoiceSubscription()
|
2017-12-28 06:04:01 -08:00
|
|
|
for invoice in stub.SubscribeInvoices(request):
|
2017-08-02 18:53:53 -07:00
|
|
|
print invoice
|
|
|
|
```
|
2017-08-08 22:30:51 -07:00
|
|
|
|
|
|
|
Now, create an invoice for your node at `localhost:10009`and send a payment to
|
|
|
|
it from another node.
|
|
|
|
```bash
|
|
|
|
$ lncli addinvoice --value=100
|
|
|
|
{
|
|
|
|
"r_hash": <R_HASH>,
|
|
|
|
"pay_req": <PAY_REQ>
|
|
|
|
}
|
|
|
|
$ lncli sendpayment --pay_req=<PAY_REQ>
|
|
|
|
```
|
|
|
|
|
|
|
|
Your Python console should now display the details of the recently satisfied
|
2017-08-02 18:53:53 -07:00
|
|
|
invoice.
|
|
|
|
|
|
|
|
#### Bidirectional-streaming RPC
|
|
|
|
|
|
|
|
```python
|
|
|
|
from time import sleep
|
|
|
|
import codecs
|
|
|
|
|
|
|
|
def request_generator(dest, amt):
|
|
|
|
# Initialization code here
|
|
|
|
counter = 0
|
|
|
|
print("Starting up")
|
|
|
|
while True:
|
|
|
|
request = ln.SendRequest(
|
|
|
|
dest=dest,
|
|
|
|
amt=amt,
|
|
|
|
)
|
|
|
|
yield request
|
|
|
|
# Alter parameters here
|
|
|
|
counter += 1
|
|
|
|
sleep(2)
|
|
|
|
|
|
|
|
# Outputs from lncli are hex-encoded
|
|
|
|
dest_hex = <RECEIVER_ID_PUBKEY>
|
|
|
|
dest_bytes = codecs.decode(dest_hex, 'hex')
|
|
|
|
|
|
|
|
request_iterable = request_generator(dest=dest_bytes, amt=100)
|
|
|
|
|
|
|
|
for payment in stub.SendPayment(request_iterable):
|
|
|
|
print payment
|
|
|
|
```
|
|
|
|
This example will send a payment of 100 satoshis every 2 seconds.
|
|
|
|
|
|
|
|
### Conclusion
|
|
|
|
|
2017-02-28 16:09:35 -08: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
|
2017-08-02 18:53:53 -07:00
|
|
|
resource](http://www.grpc.io/docs/tutorials/basic/python.html) provides more
|
|
|
|
details around how to drive `gRPC` from Python.
|