Lightning network C#/.Net Node client setup
Let’s set up a node client with LND and C#. For lightning network Lapp development in C# and .Net!
You can clone my repo at: https://github.com/Greased-Lightning-Tutorials/LNDNodeClient
Tech/Tools needed
Visual studio 2019 (Any year should work fine),
Git bash(Or any other LINUX based terminal),
Lightning Polar and Docker
Step 1
Download Lightning Polar : https://lightningpolar.com/
Polar needs docker on your computer so download and install docker desktop: https://www.docker.com/products/docker-desktop
Step2
Setup a local Lightning network
Open Polar and create a local lightning network so that we do not have to deal with setting up a node and using real Bitcoins.
Add a name, 2 LND nodes. The number of nodes does not really matter for what we are doing. But we need a minimum of 2 nodes, and we will be using LND code to talk to our node so at least one node needs to be LND.
Click start in the top right Corner.
Then Quick mine in the top right corner to simulate mining a few blocks.
I’m going to use Bob as “My node” that I will have admin access to.
Alice is representing all other nodes on the lightning network.
Right click on Alice (one of the nodes that is not “your node”) and select outgoing channel.
Select “Your node” as destination. (If you have issues here, and get error messages, restarting Polar usually does the trick)
Repeat the process for all the other nodes if you have more and want to play around, create channels across random paths to simulate a lightning network. But you do not need any more channels right now for what we are doing. You can always add more later.
This is now what you should see and we are finished with the Polar for now!
Step 3
Create project and get all packages. (you could do any type of project but I’m doing a Console project for simplicity)
Open Visual studio and create a new project
Select a new Console App.
Name it and click create
Now it’s time to install the LND libraries that are needed, If you want additional documentation check out LND’s Github: https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/c%23.md
If you do not have Git bash download it here:
https://git-scm.com/downloads
Open up Git bash and navigate to your project, for me I will write:
cd “C:\Users\mrpin\source\repos\LNDNodeClient/LNDNodeClient”
You can use the command dir to make sure you are in the correct folder. You should be in your project main folder where your .csproj file is.
Then create a directory by typing mkdir Grpc
Then download the rpc.proto file by typing
curl -o Grpc/rpc.proto -s https://raw.githubusercontent.com/lightningnetwork/lnd/master/lnrpc/rpc.proto
Open up the Nuget package manager in Visual studio.
We need some libraries to talk to our Lightning node:
click Browse then search and install:
Grpc.Core
Grpc.Tools
Google.protobuf
If you click on the tab “Installed” now and make sure there is nothing in the search bar you should see this:
Open up your .csproj file by double clicking it in the top right corner then add this item group:
And now you should see the GRPC folder and the .proto file in your hierarchy.
PRESS CRL+B to build
You are now ready to start to talk to your LND node through C#. Everything is in place for your Node client and I will show you how to talk to your node next.
Step 4
Setup connection to your Lightning polar lightning node
Add a new folder to the hierarchy. I’m calling it LightningHelpers
Add a new Class, I’m calling it Helpers.cs
Add this code to the class:
Go to Lightning Polar, right click on your node and select “Connect” on the right hand side, Then copy and paste the path do the admin macaroon and path to the certificate to the PathToMacaroon and PathToSslCertificate variables, also copy the GRPC Host and paste it in the “GRPCHost” variable
Now you have full control over you node from your C# code. To test that everything works you can update your Program.cs file with the code below. You will now get a payment request in the console window, you can pay it from another node and we are done!
In order for SendLightning() to work you’ll need a payment request / Invoice from another node in the network, You can create one by selecting a node in Lightnin polar, right click and select new invoice, Copy the string into the method.
You can now expand on this to use all the functionality in a LND node. Build Lapps of all kinds! Check out the documentation at: https://github.com/lightningnetwork/lnd/blob/master/docs/grpc/c%23.md
Or check out some of my other tutorials at:
I might have added some more functions to the Git hub repo so check that out if you like: https://github.com/Greased-Lightning-Tutorials/LNDNodeClient