Onchain interaction with your node.
The Lightning Network relies on the Bitcoin protocol to operate, so it is important to understand how your Lightning Node interacts with it. The Bitcoin network is often called “Layer one” and the Lightning Network, “Layer Two.” Your Lightning Node has an on-chain wallet built into it. This wallet allows you to send and receive Bitcoin as you normally would.
This on-chain wallet funds the channels that you will inevitably open. When your channels close, this wallet is also where the funds on your side of the closed channel will appear after confirmation. This wallet is also a hot wallet, meaning the keys are stored on an online accessible device. Take your security seriously if you are running your node at home. Have passwords, configurations, etc.. set up and stored safely.
For LND, there is no coin control for your on-chain wallet. You cannot pick and choose which UTXOs are combined when opening channels. For privacy-conscious users, you must be very careful to avoid combining UTXOs during channel opens. If opening from coinjoin, it may be wise to send the exact amount of funds to the Lightning node on-chain wallet with which you plan to open a channel rather than a large sum to avoid combining UTXOs. If there is a change output after a channel opens, consider sending it to an address you control that allows coin control before opening a new channel.
Your node’s seed phrase that you wrote down during your node’s initial setup will help you recover these on-chain funds should your node fail. The seed phrase provided to you from your LND node cannot be recovered on any other wallet such as Electrum or Blockstream Green. Any failed LND node funds must be recovered on another LND instance.
What happens on-chain when a channel is opened or closed
The Bitcoin Lightning Network is often described as a layer two technology, with the traditional Bitcoin layer below it. Interaction with the base Bitcoin network is required to use the Lightning Network in a sovereign way (running your own node). Your Lightning node has a built-in regular (layer one) Bitcoin wallet. This wallet is used to allocate funds to opening channels. And anytime you close a channel, the funds on your local/outbound side of the closed channel are then moved to your layer one Bitcoin wallet built into your node.
The Lightning Network uses the base Bitcoin layer for security and authentication. Each Lightning Channel uses what is known as a multi-sig transaction with a time lock to secure your funds and allow payments through the channel. Remember that your on-chain UTXOs cannot be chosen, so if privacy is a concern, please be careful with combining UTXOs when opening channels. When funds move through a lightning network channel, both parties update and sign an on-chain transaction. Each peer now has access to a broadcastable transaction with updated balances. This ensures that the correct amount of funds will be returned to each party on-chain if the channel closes.
Cooperative vs. Unilateral Channel Closure
Your lightning channel is a 2 of 2 multi-sig smart contracts. Each peer can close a channel at any time because both parties have access to the fully signed but unbroadcasted close transaction at all times. To close a channel means to allocate the funds in a channel back to the Bitcoin base layer. These funds you own (or would be considered your ‘outbound’ or ‘local’ funds) will then be deposited into your LND on-chain wallet.
Every time the balance of a channel changes, each peer signs an on-chain transaction updating the contract. Each peer then stores the signed 2 of 2 transactions if either peer requests a closure.
In the event of a cooperative close, one would broadcast the signed transaction, also known as a commitment transaction, to the Bitcoin network. They would choose the fee rate and the channel would close, and each peer would receive what they had on their side of the channel on-chain.
In the event of a unilateral close, this transaction enters a time lock state, allowing the other peer to contest the close if it represents a channel state with a timestamp before the timestamp of the other peer. An uncooperative close also allocates more funds for the on-chain fee to be sure the transaction will confirm on-chain. An uncooperative close is usually initiated by a node runner whose peer has been offline for a long time and is unlikely to return. If a node attempts to broadcast a channel state that is not the most current one, and the other peer comes back online, they will detect the attempted cheat and the network will allow the cheated peer to have the total amount of funds in the channel.
Another way to protect your node should your node go offline is to have another node on the network watch your node. These are called watchtower nodes and are completely invisible on the network. Because of this, node runners are incentivized not to cheat their peers because the punishment is grave and there is no way of knowing if their peer has a watchtower active.