Skip to content
buybitcoinsmart

Glossary / Lightning & layer 2

Submarine swap

Definition
A submarine swap trades on-chain bitcoin for Lightning bitcoin in one atomic step, using the same payment hash on both sides so neither party can take the money and run.

The two sides of the trade live on different layers, which is the whole difficulty. A swap solves it by making one secret unlock both legs: the on-chain contract and the Lightning payment are tied to a single hash, so either both settle or both refund. Alex Bosworth described the technique in 2018.

How it works

A submarine swap is one secret spending two contracts that sit on different layers.

Say you hold on-chain coins and want a Lightning balance. The swap provider generates a random preimage, hashes it, and issues you a Lightning invoice committed to that hash. You pay a normal on-chain transaction into a script that says: the provider can claim this by revealing a preimage matching the hash, or you can claim it back after a timelock expires. The provider sees your funds confirm, reveals the preimage on chain to claim them, and that same preimage is what settles the Lightning invoice into your wallet. One value, two settlements, no window where one side holds both.

The timelocks are what make it safe rather than merely convenient. The on-chain refund path has to expire later than the Lightning leg, so that if the provider stalls, your Lightning payment has already failed and unwound before your on-chain refund becomes claimable. Get that ordering backwards and the provider could sit on the preimage until your refund is spent, then settle the Lightning side.

Run the same construction in reverse and it does the opposite job. In a reverse swap, you pay a Lightning invoice and the provider pays you on chain, which converts channel balance into cold storage without closing a channel. Lightning Labs packaged both directions as Loop, released as an alpha on March 20, 2019, with Loop Out being the reverse direction and the one people use to create room to receive.

What the swap does not remove is counterparty exposure of a milder kind. Your money cannot be stolen, because every path is enforced by script or expires. It can be delayed: a provider that goes offline mid-swap leaves you waiting for a timelock, which is minutes on the Lightning leg and can be hours on the on-chain one.

Where you see it

Swaps are the plumbing behind features that never mention them.

Mobile wallets that let you "top up from your on-chain balance" without opening a channel are usually running a submarine swap under the hood. Wallets that offer a one tap move from Lightning back to a hardware wallet address are running the reverse. Node operators use them the way a shop uses a night deposit, draining full channels to on-chain storage and restoring the ability to be paid. Boltz and Loop are the two names you will meet most often in tooling and documentation.

Two costs are worth knowing before you use one. First, the provider charges a service fee on top of the on-chain mining fee, and on small amounts the mining fee dominates: swapping 50,000 satoshis when the chain wants 20 sat/vB is a bad trade no matter how good the provider's rate is. Second, one leg is an on-chain transaction, so the whole operation waits on confirmations even though the Lightning half is instant.

Submarine swap vs atomic swap

A submarine swap trades bitcoin for bitcoin across two layers; an atomic swap trades two different assets across two different chains. Both use the same hash and timelock trick, which is why the names get mixed up. The difference is what changes hands: after a submarine swap you hold the same asset in a different place, on chain instead of in a channel, whereas after an atomic swap you hold something else entirely, and you have taken on the price risk of that asset. Submarine swaps are routine plumbing used daily. Cross-chain atomic swaps remain rare in practice, because they need both chains to support compatible contracts and both parties to stay online.

Not to be confused with

Frequently asked questions

Can a swap provider steal my coins?

No. Every path in a submarine swap is enforced by script: the provider can only claim your on-chain funds by revealing the preimage that also pays your Lightning invoice, and otherwise the timelock returns the money to you.

Why is a small submarine swap so expensive?

Because one leg is an ordinary on-chain transaction, so it pays the going mining fee regardless of the amount. Swapping tens of thousands of satoshis during a busy fee period can cost a large share of the amount being swapped.

Related terms

More in Lightning & layer 2