A Quick Guide to Understanding Lightning Network Application Development in One Article

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 guide is highly  subjective . If you prefer O’Reilly-style authoritative texts, this article may not suit your appetite. However, if you want to understand the minimum development environment of the Lightning Network and its basic content, you might as well read it. My purpose of writing this article is to refine 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 basic knowledge points, you can write more complex and secure applications. However, everyone has to have a beginning. My personal feeling is that everything is difficult at the beginning. I think integrating these thoughts together can help some lost lambs alleviate their pain.

Before going into the details, please allow me to briefly introduce my background:

About a month ago, I quit my job as a stable data architect and started to study the ever-changing Bitcoin ecosystem. You will find this article indicating that you have taken this step (or are considering doing so).

Simply put, I think Bitcoin is the most interesting technological innovation I have ever seen. The more I understand Bitcoin, the more attracted to it. Bitcoin technology has the potential to change and even reshape the way of interaction today, making it reborn. I hope to contribute my own strength.

This is why I choose to develop on Bitcoin. I hope this article will benefit developers who are not familiar with the Lightning Network ecosystem and its potential, and encourage more developers to inject creativity into the Bitcoin field.

To be honest, I want to write too much about the Bitcoin ecology (among which, the concept of “energy-backed currency” ranks first), but I can only talk about it at a later date. Now let’s get to the topic:

A Quick Guide to Understanding Lightning Network Application Development in One Article

Lightning network architecture

A Quick Guide to Understanding Lightning Network Application Development in One Article

-Image source: lnrouter.app-

The basic design of Lightning Network is to use channels to realize one-to-one connections between nodes. Opening a channel requires depositing a certain amount of Bitcoin, and the transaction will be recorded on the Bitcoin blockchain. Then, users can use special off-chain contracts to transfer bitcoins in the channel between nodes without submitting new transactions to the blockchain.

The actual method 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 limit of about 100 satoshi (minus the transaction fee on the chain), which can be transferred directly to Alice or indirectly to other people through Alice. These transfers are almost instant, because we only need to adjust the balance of the channel based on each transaction. This adjustment is achieved by passing encrypted messages between the payer and the payee.

The Lightning application runs on the Lightning Network (the so-called Bitcoin Layer 2). Lightning apps also use Bitcoin as a payment method, but the settlement speed is faster and the transaction fee is lower.

Although I only summarized the Lightning Network protocol very briefly, knowing this is enough to get started. If you want to learn more about how the Lightning Network works, here are some good reading materials. Be careful not to indulge in the ocean of knowledge, sitting in front of the computer and reading late without typing a single 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.

I think that if you want to understand how the Lightning Network and its nodes work, a better way is to run a node yourself and figure out how to route payments through the node. Although this method requires more time and money, it is both interesting and knowledgeable. 1


The Lightning Network is an open protocol. This means that the Lightning Network has standard message communication and interpretation methods. Just as HTTPS and TCP/IP are not owned by anyone, so is the Lightning Network Protocol. Anyone can participate in the Lightning Network as long as they comply with the standards. Not only that, as developers continue to strengthen the protocol and add new features, this standard will continue to evolve.

There are currently four companies at the core of the development of the Lightning Network protocol: Lightning Labs, Blockstream, ACINQ and Square Crypto, and all have their own implementations:

Lightning Labs — lnd (Go)

Blockstream — c-lighting (C)

ACINQ – eclair (Scala)

Square Crypto — Rust Lightning (Rust)

All of the above implementations can communicate through the Lightning Network. The difference lies in the APIs that these implements. This article only focuses on the lnd of Lightning Labs, because this is the first one I contacted and understood the most. Although I finally chose Ind for accidental content, 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.

Please note that the discussion below will focus on Ind, but the basic concepts apply to all Lightning Network implementations.

Your development environment

No nonsense, just use Polar.

Okay, one more sentence. Whether you are a developer or a user, the biggest obstacle to building on the Lightning Network is how to create a backend. I’m not just talking about a wallet, because there are many simple and fast hosting solutions available. I’m referring to the real backend-the nodes that run Bitcoin and Lightning Network.

A Quick Guide to Understanding Lightning Network Application Development in One Article

The reason I say this is because your application needs to have certain permissions on the node to complete some important operations, such as creating invoices and monitoring payments through API/gRPC calls. If a node operator grants you access to the node, you can also complete these operations, but it is better to operate a node yourself.

A node that can be used for Lightning Network development must run two processes: (1) a Bitcoin client that synchronizes 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 to synchronize the blockchain alone. You can try Neutrino (a lightweight Bitcoin wallet), but I heard that using it to develop on the Lightning Network may cause some problems. I haven’t tried it myself. Anyway, if you are new to this type of tool, I think it’s best to remove variables that may have a negative impact. Therefore, if you can afford it, I suggest you synchronize the complete blockchain just fine.

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 a channel on the Lightning Network to conduct transactions. Although it is not difficult to open a channel on the Lightning Network, you need some planning and coordination, and you also need to pay Bitcoin. Of course you can stake Bitcoin to open the channel and start trading. However, you do not need to bear this financial risk. 2

Leave it alone for now. Polar is a great tool that allows you to simulate running these processes in a Docker container on your laptop. In addition, it also provides a beautiful UI interface to show you the network visualization effect.

A Quick Guide to Understanding Lightning Network Application Development in One Article

-look! Can’t wait to sync! (Polar interface)-

The advantage of Polar is that when you are ready to transfer the application to the testnet or mainnet backend, you only need to update some configurations. I can already switch easily between the local, testnet and mainnet backends by adding or commenting out a few lines of code in the application. (I will get the .env file at the end. Of course, the evaluation tool is not the purpose of this article.)

Polar also supports lnd, c-lighting and eclair nodes. This means you have the flexibility to try other implementations.

To repeat: just use Polar. This is a quick and easy way to get started. Besides, what if you don’t like developing on the Lightning Network at all? It’s better to discover this early than to waste too much time and money on a system that you don’t necessarily know how to use. Having said that, if you find yourself addicted to the podcasts of Stephan Livera (a well-known Bitcoin anchor), you can set up a more serious development environment by listening to it every night.

Set up Polar

A Quick Guide to Understanding Lightning Network Application Development in One Article

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 one Bitcoin Core node. In fact, for those who are just getting started, these are only the minimum configuration, and more nodes can be added later. After you create the network, you can start it. It may take longer to start the network for the first time because you have to download all Docker images. By the way, you need to run Docker on your own device. As I said before, I still have to explore it on my own.

Finally, you will see a Polar interface similar to the picture below:

A Quick Guide to Understanding Lightning Network Application Development in One Article

In the interface shown in the figure above, we can see the Ind nodes of Alice and Bob, and these two nodes are connected to the Bitcoin Core backend process. Also note that the block height at this time is 1. This is our creation block! Unlike real-time blockchains, Polar will only generate blocks when transactions are submitted during development and testing.

The next step is to create a channel between Alice and Bob to make lightning payments. Click on Alice’s node, and the control panel on the right will show the different ways we can interact with the node. Click “Actions” and we will see all the tools needed to recharge Alice’s wallet to simulate Bitcoin 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.

A Quick Guide to Understanding Lightning Network Application Development in One Article

If everything goes according to plan, you will 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 ask her to open an “Outgoing” channel with Bob. The so-called transfer channel is that Alice can transfer a certain amount of Bitcoin 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. However, please note that when the channel is first opened, only Alice can transfer money to Bob, because the available balance is with her. Of course, after Alice transfers to Bob, Bob can transfer to Alice.

This method of fund management in the channel is to ensure that both parties have the ability to trade. This is what we call the liquidity of the Lightning Network  . In fact, users can only use the balance in the channel. Going deeper, the payment can only be successful when there is sufficient balance in the channel between the payer and the payee (and the flow of funds is correct). Otherwise, the payment will fail. How to manage the limited liquidity in the Lightning Network and how to find workarounds is itself a topic worth exploring.

Frankly speaking, I have encountered some problems when opening channels with Polar. I suspect this is caused by the state difference between the UI and the backend, but I am not sure. Sometimes, shutting down and restarting the node (or the entire network) helps. Exiting completely and restarting Polar is also effective. In addition, when you encounter this kind of situation, going out for 10 minutes can relax your mind and spirit very well.

Whatever the reason, I found that the best way to perform these node operations is through the CLI provided by Polar. Although the UI does not necessarily show changes, we can directly query the database (so to speak 4 ) to determine whether the UI truly reflects the changes. Polar makes it easy. Let’s get started 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:

A Quick Guide to Understanding Lightning Network Application Development in One Article

Now, we can use the lncli tool of the Ind node to open channels, create invoices and make payments. First, run the following command to understand the general situation:

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 <bob's public node key> --local_amt 100000

You will see a response with “funding_txid”, which corresponds to the recharge transaction broadcast by Alice and Bob to our simulated Bitcoin blockchain. Run the following command to view our newly created channel:

lncli listchannels

If the list returned by this command is empty, please try to mine a few blocks using the Bitcoin node (under the “Actions” interface). Doing so should help confirm transactions and open channels.

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 transaction fees). The satoshi of less than 100,000 is the available balance that Alice can pay to Bob through the Lightning Network.

Payment process

To date, the simplest (and I believe the most common) payment process on the Lightning Network is through invoice. An invoice is essentially a set of payment instructions with “amount” and “receiver”. There are other parameters and variants that can help payment instructions unlock more interesting possibilities (for example, hodl invoices and BOLT12 proposals), but this article only focuses on the most basic modes.

To continue setting up our Polar, we first create an invoice, but this time we will use Bob’s node. Therefore, we need to start Bob’s terminal and run the following command:

lncli addinvoice - office 100

The above command created an invoice worth 100 satoshi (actually, I encountered a connection error while performing these steps. If you also encounter the same problem, please 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"}

Now, we are only concerned with “payment_request” section, because this part of the data contains all the information required for Alice to Bob payment, that is, the payment amount and payee address 5 .

If we switch back to Alice’s node terminal, we can get the payment request and pass it as a parameter to the following command:

lncli sendpayment –pay_req <payment_request> The result is:

A Quick Guide to Understanding Lightning Network Application Development in One Article


Start your adventure

A Quick Guide to Understanding Lightning Network Application Development in One Article

At this point, you should already have enough basic knowledge and tools to start building applications. The figure above outlines a simple sample application that uses the API calls we used to create and pay invoices in Polar. This is all you need to build a most basic application architecture. Of course, this is just an example, we will encounter many other problems in the development process (currently only the beginning), but you will slowly figure out what you need and how to solve these problems.

Some additional suggestions and references:

There are many libraries to help developers write less boilerplate code and go straight to the topic. As far as I am concerned, learning how to use these libraries has brought me more frustration than greater efficiency. The problem is mainly with me. Abstraction is really great, but only if you have a basic understanding of what is being abstracted. I didn’t realize this when I started. I think Ind’s API documentation is the easiest to learn. When I followed this guide to write a gRPC client using Javascript, I was already on the right track.

If you want to see a more specific application example, take a look at the tutorial 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 are not familiar with these tools, you may not get much help from this tutorial, but you can still learn something. One of the reasons I like this tutorial is that it shows some interesting features that can be achieved by building applications using the Lightning Network (and cryptographic proofs).


  1. If you want to run a node, Umbrel is suitable for beginners. I heard that MyNode, RaspiBolt and RaspiBlitz are also good. If you like to patch the software system (or SimCity (simulation city game)), running a node is a fun pastime if it is broken, or better it is a high-cost and endless optimization game (requires real money) The kind of silver).
  2. You can also conduct experiments on the testnet and get some experimental bitcoins through the bitcoin faucet. These coins are worthless, but the experience of being a miser can be fun. If you are not interested in configuring and managing nodes by yourself, you can use services such as Voltage. Voltage is a pay-as-you-go cloud service that supports testnet and mainnet nodes.
  3. Well, we can’t be 100% sure. To be cautious, please check lnd Slack’s developer channel. I have met many developers here who build and maintain these tools. You might see me! Someone may have experienced the problem you are facing. If not, that’s great-everyone can learn from your questions.
  4. The blockchain is a public database, and everyone has root privileges. For more information, please check: https://balajis.com/yes-you-may-need-a-blockchain/
  5. 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.


Posted by:CoinYuppie,Reprinted with attribution to:https://coinyuppie.com/a-quick-guide-to-understanding-lightning-network-application-development-in-one-article/
Coinyuppie is an open information publishing platform, all information provided is not related to the views and positions of coinyuppie, and does not constitute any investment and financial advice. Users are expected to carefully screen and prevent risks.

Like (0)
Donate Buy me a coffee Buy me a coffee
Previous 2021-09-12 12:21
Next 2021-09-12 12:22

Related articles