docs: unify code and shell cmd blocks

This commit is contained in:
Oliver Gugger
2021-01-17 14:58:57 +01:00
parent b20afc0679
commit 2c634bfaf3
19 changed files with 260 additions and 257 deletions

View File

@@ -21,22 +21,22 @@ Create a new `.net core` console application called `lndclient` at your root dir
Create a folder `Grpc` in the root of your project and fetch the lnd proto files
```bash
mkdir Grpc
curl -o Grpc/rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
```shell
mkdir Grpc
curl -o Grpc/rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
```
Install `Grpc.Tools`, `Google.Protobuf`, `Grpc.Core` using NuGet or manually with `dotnet add`:
```bash
dotnet add package Grpc.Tools
dotnet add package Google.Protobuf
dotnet add package Grpc.Core
```shell
dotnet add package Grpc.Tools
dotnet add package Google.Protobuf
dotnet add package Grpc.Core
```
Add the `rpc.proto` file to the `.csproj` file in an ItemGroup. (In Visual Studio you can do this by unloading the project, editing the `.csproj` file and then reloading it)
```
```xml
<ItemGroup>
<Protobuf Include="Grpc\rpc.proto" GrpcServices="Client" />
</ItemGroup>
@@ -48,8 +48,7 @@ You're done! Build the project and verify that it works.
Use the code below to set up a channel and client to connect to your `lnd` node:
```c#
```cs
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
@@ -69,7 +68,6 @@ var cert = File.ReadAllText(<Tls_Cert_Location>);
var sslCreds = new SslCredentials(cert);
var channel = new Grpc.Core.Channel("localhost:10009", sslCreds);
var client = new Lnrpc.Lightning.LightningClient(channel);
```
### Examples
@@ -78,7 +76,7 @@ Let's walk through some examples of C# `gRPC` clients. These examples assume tha
#### Simple RPC
```c#
```cs
// Retrieve and display the wallet balance
// Use "WalletBalanceAsync" if in async context
var response = client.WalletBalance(new WalletBalanceRequest());
@@ -87,7 +85,7 @@ Console.WriteLine(response);
#### Response-streaming RPC
```c#
```cs
var request = new InvoiceSubscription();
using (var call = client.SubscribeInvoices(request))
{
@@ -100,20 +98,20 @@ using (var call = client.SubscribeInvoices(request))
```
Now, create an invoice for your node at `localhost:10009` and send a payment to it from another node.
```bash
$ lncli addinvoice --amt=100
```shell
lncli addinvoice --amt=100
{
"r_hash": <R_HASH>,
"pay_req": <PAY_REQ>
}
$ lncli sendpayment --pay_req=<PAY_REQ>
lncli sendpayment --pay_req=<PAY_REQ>
```
Your console should now display the details of the recently satisfied invoice.
#### Bidirectional-streaming RPC
```c#
```cs
using (var call = client.SendPayment())
{
var responseReaderTask = Task.Run(async () =>
@@ -155,7 +153,7 @@ This example will send a payment of 100 satoshis every 2 seconds.
To authenticate using macaroons you need to include the macaroon in the metadata of the request.
```c#
```cs
// Lnd admin macaroon is at <LND_DIR>/data/chain/bitcoin/simnet/admin.macaroon on Windows
// ~/.lnd/data/chain/bitcoin/simnet/admin.macaroon on Linux and ~/Library/Application Support/Lnd/data/chain/bitcoin/simnet/admin.macaroon on Mac
byte[] macaroonBytes = File.ReadAllBytes("<LND_DIR>/data/chain/bitcoin/simnet/admin.macaroon");
@@ -164,13 +162,13 @@ var macaroon = BitConverter.ToString(macaroonBytes).Replace("-", ""); // hex for
The simplest approach to use the macaroon is to include the metadata in each request as shown below.
```c#
```cs
client.GetInfo(new GetInfoRequest(), new Metadata() { new Metadata.Entry("macaroon", macaroon) });
```
However, this can get tiresome to do for each request, so to avoid explicitly including the macaroon we can update the credentials to include it automatically.
```c#
```cs
// build ssl credentials using the cert the same as before
var sslCreds = new SslCredentials(cert);

View File

@@ -11,7 +11,7 @@ with lnd in Java. We'll be using Maven as our build tool.
### Setup and Installation
#### Project Structure
```
```text
.
├── pom.xml
└── src
@@ -34,13 +34,13 @@ Note the ***proto*** folder, where all the proto files are kept.
- [http.proto](https://github.com/grpc-ecosystem/grpc-gateway/blob/master/third_party/googleapis/google/api/http.proto)
#### pom.xml
```
```xml
<properties>
<grpc.version>1.8.0</grpc.version>
</properties>
```
The following dependencies are required.
```
```xml
<dependencies>
<dependency>
<groupId>io.grpc</groupId>
@@ -70,7 +70,7 @@ The following dependencies are required.
</dependencies>
```
In the build section, we'll need to configure the following things :
```
```xml
<build>
<extensions>
<extension>
@@ -184,11 +184,11 @@ public class Main {
```
#### Running the example
Execute the following command in the directory where the **pom.xml** file is located.
```
mvn compile exec:java -Dexec.mainClass="Main" -Dexec.cleanupDaemonThreads=false
```shell
mvn compile exec:java -Dexec.mainClass="Main" -Dexec.cleanupDaemonThreads=false
```
##### Sample output
```
```text
[INFO] Scanning for projects...
[INFO] ------------------------------------------------------------------------
[INFO] Detecting the operating system and CPU architecture

View File

@@ -121,7 +121,7 @@ invoice.
This example has a few dependencies:
```shell
npm install --save async lodash bytebuffer
npm install --save async lodash bytebuffer
```
You can run the following in your shell or put it in a program and run it like

View File

@@ -10,32 +10,32 @@ 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.
1. Create a virtual environment for your project
```
$ virtualenv lnd
```shell
virtualenv lnd
```
2. Activate the virtual environment
```
$ source lnd/bin/activate
```shell
source lnd/bin/activate
```
3. 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
```shell
lnd pip install grpcio grpcio-tools googleapis-common-protos
```
4. 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
```shell
lnd git clone https://github.com/googleapis/googleapis.git
```
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))
or just download it
```
(lnd)$ curl -o rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
```shell
lnd curl -o rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
```
6. Compile the proto file
```
(lnd)$ python -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. rpc.proto
```shell
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
@@ -52,9 +52,9 @@ For example, if you want to generate the RPC modules for the `Router` subserver
extra steps (after completing all 6 step described above) to get the
`router_pb2.py` and `router_pb2_grpc.py`:
```
(lnd)$ curl -o router.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/routerrpc/router.proto
(lnd)$ python -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. router.proto
```shell
lnd curl -o router.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/routerrpc/router.proto
lnd python -m grpc_tools.protoc --proto_path=googleapis:. --python_out=. --grpc_python_out=. router.proto
```
### Imports and Client
@@ -105,13 +105,13 @@ for invoice in stub.SubscribeInvoices(request):
Now, create an invoice for your node at `localhost:10009`and send a payment to
it from another node.
```bash
$ lncli addinvoice --amt=100
```shell
lnd ⛰ lncli addinvoice --amt=100
{
"r_hash": <R_HASH>,
"pay_req": <PAY_REQ>
}
$ lncli sendpayment --pay_req=<PAY_REQ>
lnd ⛰ lncli sendpayment --pay_req=<PAY_REQ>
```
Your Python console should now display the details of the recently satisfied

View File

@@ -14,27 +14,27 @@ the `lnd` proto file in Ruby before you can use it to communicate with `lnd`.
Install gRPC rubygems:
```
$ gem install grpc
$ gem install grpc-tools
```shell
gem install grpc
gem install grpc-tools
```
Clone the Google APIs repository:
```
$ git clone https://github.com/googleapis/googleapis.git
```shell
git clone https://github.com/googleapis/googleapis.git
```
Fetch the `rpc.proto` file (or copy it from your local source directory):
```
$ curl -o rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
```shell
curl -o rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
```
Compile the proto file:
```
$ grpc_tools_ruby_protoc --proto_path googleapis:. --ruby_out=. --grpc_out=. rpc.proto
```shell
grpc_tools_ruby_protoc --proto_path googleapis:. --ruby_out=. --grpc_out=. rpc.proto
```
Two files will be generated in the current directory:
@@ -98,8 +98,8 @@ end
Now, create an invoice on your node:
```bash
$ lncli addinvoice --amt=590
```shell
lncli addinvoice --amt=590
{
"r_hash": <R_HASH>,
"pay_req": <PAY_REQ>
@@ -108,8 +108,8 @@ $ lncli addinvoice --amt=590
Next send a payment to it from another node:
```
$ lncli sendpayment --pay_req=<PAY_REQ>
```shell
lncli sendpayment --pay_req=<PAY_REQ>
```
You should now see the details of the settled invoice appear.
@@ -182,4 +182,4 @@ stub = Lnrpc::Lightning::Stub.new(
credentials,
channel_args: {"grpc.max_receive_message_length" => 1024 * 1024 * 50}
)
```
```