
Original Title: "Dry Goods | A Quick Guide to Lightning Network Application Development" by Michael Rhee
If you want to get started with Lightning Network programming, and don't care about the so-called correct and best practices, this article is for you. In other words, this guideline is highly subjective. If you prefer authoritative texts in the O'Reilly style, this article may not be to your liking. However, if you want to understand the minimal development environment of Lightning Network and its basic content, it is worth reading. The purpose of my writing this article is to extract the basic knowledge points for creating lightning network applications, so that novices can quickly build and experiment.
I believe that as long as you master the basics, you can write more complex and secure applications. However, everyone has to start somewhere, and my personal feeling is that everything is difficult at the beginning, and I think bringing these thoughts together can help some lost lambs to ease their pain.
Before diving into the details, allow me to give a little background on myself:
About a month ago, I quit my stable job as a data architect and started to study the ever-changing Bitcoin ecosystem. You'll find this article showing that you've taken that step (or are thinking about it).
Simply put, I think Bitcoin is the most interesting technological innovation I have ever seen. The more I learned about Bitcoin, the more drawn I was to it. Bitcoin technology promises to change and even reshape the way we interact today, making it completely new. I hope to contribute my part.
That's why I choose to develop on Bitcoin. I hope this article will benefit developers unfamiliar with the Lightning Network ecosystem and its potential, and encourage more developers to inject creativity into the Bitcoin space.
Honestly, there's so much I want to write about the Bitcoin ecosystem (the concept of an "energy-backed currency" is number one), but I'll have to wait for another day. Now let's get down to business:
image description
-Source: lnrouter.app-
The basic design of the Lightning Network is to use channels to realize one-to-one connections between nodes. To open a channel, you need to deposit a certain amount of Bitcoin, and this transaction will be recorded on the Bitcoin blockchain. Users can then use special off-chain contracts to transfer bitcoins in channels between nodes without submitting new transactions to the blockchain.
The practical approach is to track the balance changes in the channel in real time. For example, if you deposit 100 satoshi in the channel opened with Alice, you have a transfer quota of about 100 satoshi (minus on-chain transaction fees), which can be transferred directly to Alice or indirectly through Alice. These transfers are almost instant since we only need to adjust the balance of the channel with each transaction. This adjustment is achieved by passing encrypted messages between the sender and receiver.
Lightning apps run on top of the Lightning Network (the so-called Bitcoin layer 2). Lightning Apps also use Bitcoin as a payment method, but with faster settlement and lower transaction fees.
Although I only gave a very brief overview of the Lightning Network protocol, it is enough to get started. If you want to understand how the Lightning Network works in more detail, here is some good reading. Be careful not to indulge in the ocean of knowledge, sitting in front of the computer and reading late, but not typing a line of code. It's not that this is necessarily bad, but our purpose is still to practice. In addition, it is also important to ensure adequate sleep.
accomplish
accomplish
The Lightning Network is an open protocol. This means that the Lightning Network has a standard way of communicating and interpreting messages. Just like HTTPS and TCP/IP are not owned by anyone, neither is the Lightning Network protocol. Anyone can participate in the Lightning Network as long as they adhere to the standards. Not only that, but the standard will continue to evolve as developers continue to harden the protocol and add new features.
Four companies are currently at the heart of Lightning protocol development: Lightning Labs, Blockstream, ACINQ, and Square Crypto, all with their own implementations:
Lightning Labs — lnd (Go)
Blockstream — c-lighting (C)
ACINQ — eclair (Scala)
Square Crypto — Rust Lightning (Rust)
All of the above implementations are able to communicate over the Lightning Network. The difference is the API of these implementations. This article only focuses on Lightning Labs' lnd, because that's the first I've come into contact with and the one I know the most about. Although I finally chose Ind because of accidental factors, I have to boast that Lightning Labs' Ind documentation is well written, and developers can get a lot of help and support through the official Slack channel.
Note that the discussion below will revolve around Ind, but the basic concepts apply to all Lightning Network implementations.
your development environment
No bullshit, just use Polar.
Well, just a few more words. Whether you are a developer or a user, the biggest hurdle to building on the Lightning Network is how to create the backend. I'm not just talking about a wallet, because there are many simple and fast custody solutions available. I mean the actual backend — the nodes that run Bitcoin and the Lightning Network.
I say this because your app needs to have certain permissions on the node to do some important things like create invoices and monitor payments via API/gRPC calls. You can also do this if a node operator has granted you access to the node, but it is better to run a node yourself.
A node that can be used for lightning network development must run two processes: (1) a bitcoin client that is synchronized to the latest block of the bitcoin blockchain; (2) a lightning network client that has opened a channel (can send and receive payments).
If you are using ordinary home broadband, it will take at least a few days just to synchronize the blockchain. You could try Neutrino (a lightweight bitcoin wallet), but I've heard that using it to develop on the Lightning Network can cause some issues. I haven't tried it myself. Anyway, if you're new to these kinds of tools, I think it's best to remove variables that might have a negative impact. So, if you can afford it, I recommend just syncing the full blockchain.
If we sort the layers of Bitcoin, the Lightning Network layer is the second layer of Bitcoin. As I mentioned above, we need to open channels on the Lightning Network to conduct transactions. While opening a channel on the Lightning Network is not very difficult, you do need some planning and coordination, as well as paying bitcoins. You can of course stake bitcoins to open channels and start trading. However, you don't have to take this financial risk. 2
image description
-look! Can't wait to sync! (Polar interface) -
The great thing about Polar is that when you're ready to move your app to a testnet or mainnet backend, you only need to update a few configurations. I can already easily switch between local, testnet, and mainnet backends by adding or commenting out a few lines of code in the app. (I'll end up with .env files. Of course, profiling tools are not the purpose of this article.)
Polar also supports lnd, c-lighting and eclair nodes. This means you have the flexibility to experiment with other implementations.
Once again: just use Polar. This is a quick and easy way to get started. Also, what if you don't like developing on the Lightning Network at all? Finding this out early is better than wasting too much time and money on a system you won't necessarily use. Having said that, if you find yourself addicted to Stephan Livera's podcast, which you can't get out of listening to every night, you can set up a more serious development environment.
text
Click "Create Network" to easily create a simulated Lightning Network environment in Polar. You can name the network whatever you want (I personally think "test" is a smart choice) and add two lnd nodes and a Bitcoin Core node. In fact, for those just getting started, these are just the minimum configuration, and more nodes can be added later. Once you have created your network you can start it. It may take a little longer to start the network for the first time because you have to download all the Docker images. By the way, you need to run Docker on your own device. Like I said before, you still have to figure it out on your own here.
Finally, you will see a Polar interface similar to the one below:
In the interface shown in the figure above, we can see the Ind nodes of Alice and Bob, and both nodes are connected to the Bitcoin Core backend process. Also note that the block height at this time is 1. This is our genesis block! Unlike live blockchains, Polar only produces blocks when transactions are submitted during development and testing.
The next step is to create a channel between Alice and Bob for lightning payments. Clicking on Alice's node, the control panel on the right shows the different ways we can interact with that node. Clicking on Actions, we see everything we need to fund Alice's wallet with simulated bitcoins and open a channel between Alice and Bob. Next, let's recharge some funds to Alice's wallet, and then open the channel between Alice and Bob.
Click "Deposit" and then deposit 1 million satoshi into Alice's wallet.
If everything went according to plan, you should see the block height and Alice's wallet balance change. If not, please ask for help. this is not your fault. 3
Now we can open a channel between Alice and Bob. Now that Alice has money, we can let her open an "Outgoing" channel with Bob. The so-called transfer channel means that Alice can transfer a certain amount of bitcoins to Bob through this channel. Multiple transfer transactions can be made between Alice and Bob, as long as there is a balance in the channel. Note, however, that when the channel is first opened, only Alice can transfer money to Bob, since the available balance is with her. Of course, after Alice transfers money to Bob, Bob can transfer money to Alice.
This method of fund management in the channel is to ensure that both parties have the ability to trade, which is what we call the liquidity of the Lightning Network. In fact, users can only use the balance in the channel. Going a little deeper, a payment can only be successful if there is a sufficient balance in the channel between the sender and the payee (and the funds are flowing in the correct direction). Otherwise the payment will fail. How to manage the limited liquidity in the Lightning Network and how to find workarounds is a topic worth exploring in itself.
To be honest, I have had some problems opening channels with Polar. I suspect it's a state discrepancy between the UI and the backend, but I'm not sure. Sometimes, shutting down and restarting a node (or the entire network) helps. Quitting completely and restarting Polar will also work. Plus, when you're in a situation like this, going outside for 10 minutes can be a great way to relax your mind and spirit.
Whatever the reason, I've found that the best way to do these node operations is through the CLI provided by Polar. While the UI doesn't necessarily show the change, we can query the database directly (so to speak 4) to determine if the UI is faithfully reflecting the change. Polar makes it easy. Let's do it now.
First select Alice’s Ind node, enter the “Actions” interface, click “Launch” under “Terminal”, and then a command prompt will appear, as shown in the following figure:
Now we can use the Ind node's lncli tool to open channels, create invoices and make payments. First, run the following command to get an overview:
lncli --help
We can run the following command to open a channel with a balance of 100,000 satoshi between Alice and Bob:
lncli openchannel --node_key
You'll see a response with "funding_txid", which corresponds to the funding transaction that Alice and Bob broadcast to our simulated Bitcoin blockchain. Run the following command to see our newly created channel:
lncli listchannels
If the command returns an empty list, try mining a few blocks with a Bitcoin node (under the “Actions” screen). Doing so should help confirm the transaction and open the channel.
Now, we can see through the listchannels response that the channel between Alice and Bob has been successfully opened, and the channel balance is about 100,000 satoshi (minus the transaction fee). The less than 100,000 satoshi is the available balance that Alice can pay to Bob through the Lightning Network.
payment process
By far the easiest (and I believe most common) payment process on the Lightning Network is via invoice. An invoice is essentially a set of payment instructions with an "amount" and a "payee". There are other parameters and variants that help payment instructions unlock more interesting possibilities (e.g. hodl invoices and BOLT12 proposals), but this article only focuses on the most basic patterns.
Moving on to setting up our Polar, let's create an invoice first, but this time using Bob's node. So we're going to fire up Bob's terminal and run the following command:
lncli addinvoice --amt 100
The above command creates an invoice worth 100 satoshi (Actually, I got a connection error while following these steps. If you have the same problem, stop and restart Bob's node). We can see the information of this invoice through the returned response:
{ "r_hash": "7d91cafaba85b6086924142dfd890f350eb53b17b80e2993d0a2ce5ccc7252f1", "payment_request": "lnbcrt1u1ps3lu04pp50kgu4746skmqs6fyzsklmzg0x58t2wchhq8zny7s5t89enrj2tcsdqqcqzpgsp55rtlzlf5rt0z5zg34nc2rlcm9mw6nd77x45r85z6zp07qumphr7q9qyyssqzrvxdlsluaeu7esscvv8skcmaly4794j7pg9ytapmn50uukezf4xpqma9758s39wpn4pwk475dztezg4tff8xpylksl4mww57q8hj7cq7s7222", "add_index": "1", "payment_addr": "a0d7f17d341ade2a0911acf0a1ff1b2edda9b7de356833d05a105fe07361b8fc"}
For now, we only focus on the "payment_request" part, because this part of the data contains everything Alice needs to make a payment to Bob, namely, the payment amount and the payee's address5.
If we switch back to Alice's node terminal, we can get the payment request and pass it as an argument to the following command:
- lncli sendpayment --pay_req
The result is:
success.
text
At this point, you should have enough basic knowledge and tools to start building applications. The diagram above outlines a simple example application using the same API calls we use in Polar to create and pay invoices. That's all you need to build a basic application architecture. Of course, this is just one example, we will encounter many other problems during the development process (it is only the beginning), but you will slowly figure out what you need and how to solve them.
Some additional suggestions and references:
There are many libraries that can help developers write less boilerplate code and get straight to the point. Personally, learning how to use these libraries has brought me more frustration than productivity. The problem is mostly with me. Abstractions are great, but only if you have a basic understanding of what is being abstracted. I didn't realize this when I started out. I find Ind's API documentation the easiest to learn. When I followed this guide to write a gRPC client in Javascript, I was on the right track.
If you'd like to see a more concrete application example, take a look at the tutorials in the Lightning Labs Builder's Guide. If you are familiar with the tools express, mobx and React used in the tutorial, then I highly recommend this tutorial. If you're not familiar with these tools, you probably won't get much out of this tutorial, but you'll still be able to learn something. One of the things I like about this tutorial is that it shows some of the interesting things you can do with apps built using the Lightning Network (and cryptographic proofs).
Finally, if you think anything in this article is unclear or inaccurate, please feel free to send me feedback or questions.
Thanks for reading.
footnote
footnote
If you want to run node, Umbrel is great for beginners. I've heard MyNode, RaspiBolt, and RaspiBlitz are also good. If you like tinkering with software systems (or SimCity) then running nodes is at worst a fun pastime, at best a costly and endless game of optimization (requires real money the silver kind).
You can also run experiments on the testnet and earn some experimental bitcoins through a bitcoin faucet. These coins are worthless, but being a miser can be fun. If you're not interested in configuring and managing nodes yourself, you can use a service like Voltage. Voltage is a pay-as-you-go cloud service that supports testnet and mainnet nodes.
Well, we can't be 100% sure. For caution, check out the developer channel on the lnd Slack. I've met many developers here who build and maintain these tools. You may see me! Someone may have experienced the problem you are facing. If not, great - everyone can learn from your question.
The blockchain is a public database, and everyone has root access. For more information, check out: https://balajis.com/yes-you-may-need-a-blockchain/
For a detailed explanation of other fields in the invoice, you can view this summary. To learn more about the underlying contract, please read this article.