A beginner’s guide to Submarine Swaps

5 min readMar 14, 2023

Overview

As bitcoin continues to grow, layers or protocols built upon it continually look for better ways to improve the scale, adoption, and utility of bitcoin. A major problem between these layers is transaction barriers because it’s not a straightforward step to simply send bitcoin from a lightning channel to an on-chain address without having to close and open channels. Submarine swaps work on the same principles as atomic swaps and provide a solution to this problem.

What is a Submarine Swap

A submarine swap bridges the gap between the lightning network and on-chain bitcoin blockchain funds by providing a way to trustlessly exchange your coins from lightning to on-chain addresses or in the reverse without counterparty risk. It allows you to basically do two things:

  1. Pay an on-chain address using an off-chain balance but without closing your channel, and
  2. Pay an off-chain invoice with an on-chain address without opening a channel

Submarine swaps are facilitated by a special type of bitcoin contract called HTLCs (Hash Time-Locked Contracts) which provides a way of locking the funds of a contract with a time lock and a hash lock. When provided, the required preimage to the hash which satisfies the hash lock can give access to spending the funds or refund the creator of the contract if the time limit elapses.

Due to the design of HTLC which makes it includes both on-chain of off-chain transactions, it can be leveraged between an on-chain sender and off-chain receiver and that is the basis of submarine swaps.

What problems do submarine swaps solve?

  1. Increasing lightning channel liquidity:
    One major problem of running a lightning channel is increasing channel liquidity, this is because with the current implementation of setting up a lightning channel, the channel capacity or liquidity is usually determined at the opening of the channel.
    To fund a channel, an on-chain transaction is made, and a specific amount of bitcoin is sent to that lightning channel. When that supply runs out, there is no method to refill the channel without having to open a new channel. Submarine swaps solve the problem by providing a way to seamlessly increase the lightning channel capacity through an on-chain payment.
  2. Bridging the gap between layers:
    There is no simple way to send lightning payments to someone who doesn’t use lightning or bitcoin transactions to someone who doesn’t use bitcoin. Submarine swaps solve this problem by bridging the gap between both layers, facilitating payments between on-chain bitcoin transactions and off-chain lightning payments thus bridging the gap and solving the transaction barrier between these two layers.

Steps to Achieving a Submarine Swap

To explain this we will be referencing lightning engineering docs, to explain how an on-chain transaction will be made to an off-chain lightning network.

To achieve these trustless properties and create a smart contract, the on-chain transaction makes use of hash time-locked contracts (HTLC) currently being used in the Lightning Network.
Before on-chain bitcoin is transferred, the recipient of the Lightning payment generates a preimage and this preimage is hashed and used to construct the HTLCs for both on-chain and off-chain payments.

The steps are outlined as follows;

1. Generate a bitcoin smart contract:
The sender of the on-chain payment knows the hash of this preimage after an exchange of public keys with the receiver. The sender can generate a Bitcoin address which is derived from the script with the following properties:
That, for a limited time specified in the Hash Time-Lock Contract, the funds held in that contract can be spent knowing the preimage and a valid signature from the receiver. After that, in a situation when the funds are not claimed by the receiver, and the time limit has expired they will be refunded back to the sender who can claim the funds using the signature.

Both the sender of the on-chain transaction and the recipient of the off-chain payment are able to generate and verify this script. The sender can now safely move BTC to this contract, knowing they will either receive satoshis off-chain or, if it fails, be able to claim their bitcoins back through a refund.

for more information on how this script works, you can get more information from swaps-service.

2. Generate and pay a Lightning invoice:
The next step is for the sender of the on-chain funds to generate a lightning invoice using the same preimage from the smart contract in step 1. The amount of the invoice to be paid will either be equal to the bitcoin sent or it might include/deduct a fee from the total. This depends on the arrangement of the two parties who want to swap coins and should be agreed on in advance.

After funds have been confirmed to be settled in the smart contract, it is safe to pay the Lightning invoice. To claim this Lightning payment, the sender of the on-chain funds has to publicize the preimage, which the recipient can now use to claim the funds from the bitcoin smart contract.

3. Claim bitcoin from the smart contract:
At this point, the off-chain funds are now in the Lightning wallet of the sender of the on-chain payments, while the on-chain funds are still held in the smart contract. Using their signature and the preimage obtained from the Lightning payment, the on-chain funds can be claimed. They will need to be confirmed before the HTLC expires, or else there is the risk the sender can claim their funds back as well through a refund transaction.

Conclusion

Submarine swaps play a fundamental part in scaling bitcoin by helping to bridge the gap between layers in bitcoin and abstracting away the complexity needed to facilitate the transfer of value between on-chain and off-chain assets.

References

  1. https://docs.lightning.engineering/the-lightning-network/multihop-payments/understanding-submarine-swaps#what-are-submarine-swaps-good-for
  2. https://medium.com/coinmonks/hashed-timelock-contracts-htlcs-b3f0ba1eee02
  3. https://thebitcoinmanual.com/articles/btc-submarine-swaps/
  4. https://blog.muun.com/a-closer-look-at-submarine-swaps-in-the-lightning-network/

--

--

Solomon Eze
Solomon Eze

No responses yet