docs+python: fix headings, make numbered list
This commit is contained in:
parent
20a776d703
commit
0bb449c8fe
@ -3,46 +3,46 @@
|
|||||||
This section enumerates what you need to do to write a client that communicates
|
This section enumerates what you need to do to write a client that communicates
|
||||||
with `lnd` in Python.
|
with `lnd` in Python.
|
||||||
|
|
||||||
### Setup and Installation
|
## Setup and Installation
|
||||||
|
|
||||||
Lnd uses the gRPC protocol for communication with clients like lncli. gRPC is
|
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
|
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.
|
file in Python before you can use it to communicate with lnd.
|
||||||
|
|
||||||
* Create a virtual environment for your project
|
1. Create a virtual environment for your project
|
||||||
```
|
```
|
||||||
$ virtualenv lnd
|
$ virtualenv lnd
|
||||||
```
|
```
|
||||||
* Activate the virtual environment
|
2. Activate the virtual environment
|
||||||
```
|
```
|
||||||
$ source lnd/bin/activate
|
$ source lnd/bin/activate
|
||||||
```
|
```
|
||||||
* Install dependencies (googleapis-common-protos is required due to the use of
|
3. Install dependencies (googleapis-common-protos is required due to the use of
|
||||||
google/api/annotations.proto)
|
google/api/annotations.proto)
|
||||||
```
|
```
|
||||||
(lnd)$ pip install grpcio grpcio-tools googleapis-common-protos
|
(lnd)$ pip install grpcio grpcio-tools googleapis-common-protos
|
||||||
```
|
```
|
||||||
* Clone the google api's repository (required due to the use of
|
4. Clone the google api's repository (required due to the use of
|
||||||
google/api/annotations.proto)
|
google/api/annotations.proto)
|
||||||
```
|
```
|
||||||
(lnd)$ git clone https://github.com/googleapis/googleapis.git
|
(lnd)$ git clone https://github.com/googleapis/googleapis.git
|
||||||
```
|
```
|
||||||
* Copy the lnd rpc.proto file (you'll find this at
|
5. Copy the lnd rpc.proto file (you'll find this at
|
||||||
[lnrpc/rpc.proto](https://github.com/lightningnetwork/lnd/blob/master/lnrpc/rpc.proto))
|
[lnrpc/rpc.proto](https://github.com/lightningnetwork/lnd/blob/master/lnrpc/rpc.proto))
|
||||||
or just download it
|
or just download it
|
||||||
```
|
```
|
||||||
(lnd)$ curl -o rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
|
(lnd)$ curl -o rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
|
||||||
```
|
```
|
||||||
* Compile the proto file
|
6. Compile the proto file
|
||||||
```
|
```
|
||||||
(lnd)$ python -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. rpc.proto
|
(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
|
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 anytime you use
|
be generated. These files will be imported in your project anytime you use
|
||||||
Python gRPC.
|
Python gRPC.
|
||||||
|
|
||||||
#### Imports and Client
|
### Imports and Client
|
||||||
|
|
||||||
Every time you use Python gRPC, you will have to import the generated rpc modules
|
Every time you use Python gRPC, you will have to import the generated rpc modules
|
||||||
and set up a channel and stub to your connect to your `lnd` node:
|
and set up a channel and stub to your connect to your `lnd` node:
|
||||||
@ -66,13 +66,13 @@ channel = grpc.secure_channel('localhost:10009', creds)
|
|||||||
stub = lnrpc.LightningStub(channel)
|
stub = lnrpc.LightningStub(channel)
|
||||||
```
|
```
|
||||||
|
|
||||||
### Examples
|
## Examples
|
||||||
|
|
||||||
Let's walk through some examples of Python gRPC clients. These examples assume
|
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
|
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.
|
is at the default `localhost:10009`, with an open channel between the two nodes.
|
||||||
|
|
||||||
#### Simple RPC
|
### Simple RPC
|
||||||
|
|
||||||
```python
|
```python
|
||||||
# Retrieve and display the wallet balance
|
# Retrieve and display the wallet balance
|
||||||
@ -80,7 +80,7 @@ response = stub.WalletBalance(ln.WalletBalanceRequest())
|
|||||||
print(response.total_balance)
|
print(response.total_balance)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Response-streaming RPC
|
### Response-streaming RPC
|
||||||
|
|
||||||
```python
|
```python
|
||||||
request = ln.InvoiceSubscription()
|
request = ln.InvoiceSubscription()
|
||||||
@ -102,7 +102,7 @@ $ lncli sendpayment --pay_req=<PAY_REQ>
|
|||||||
Your Python console should now display the details of the recently satisfied
|
Your Python console should now display the details of the recently satisfied
|
||||||
invoice.
|
invoice.
|
||||||
|
|
||||||
#### Bidirectional-streaming RPC
|
### Bidirectional-streaming RPC
|
||||||
|
|
||||||
```python
|
```python
|
||||||
from time import sleep
|
from time import sleep
|
||||||
@ -133,7 +133,7 @@ for payment in stub.SendPayment(request_iterable):
|
|||||||
```
|
```
|
||||||
This example will send a payment of 100 satoshis every 2 seconds.
|
This example will send a payment of 100 satoshis every 2 seconds.
|
||||||
|
|
||||||
#### Using Macaroons
|
### Using Macaroons
|
||||||
|
|
||||||
To authenticate using macaroons you need to include the macaroon in the metadata of the request.
|
To authenticate using macaroons you need to include the macaroon in the metadata of the request.
|
||||||
|
|
||||||
@ -180,7 +180,7 @@ stub.GetInfo(ln.GetInfoRequest())
|
|||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Conclusion
|
## Conclusion
|
||||||
|
|
||||||
With the above, you should have all the `lnd` related `gRPC` dependencies
|
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
|
installed locally into your virtual environment. In order to get up to speed
|
||||||
|
Loading…
Reference in New Issue
Block a user