Atomic on-chain LTC to off-chain BTC swap

Jason Wong
3 min readJun 16, 2018

--

Bitcoin lightning has been enabled for some time and has more than 2000 nodes. A BTC user who want to use lightning apps such Satoshi’s Place need to be a participant of lightning network that requires the user to setup a BTC full node and run a lightning daemon. For non-BTC users, they have to convert their coins into BTC, setup a BTC full node and run a lightning daemon. It will be very convenient if there is a swap service that take their on-chain coins and pay their lightning invoice.

Submarine Swaps

Submarine Swaps is an atomic on-chain to off-chain swap service created by Alex Bosworth. It receives your on-chain BTC and pay for your off-chain BTC lightning invoice. It is an atomic swap which means if your invoice is not paid within a specified time period you can get back the on-chain BTC that you sent. You can try the on-chain tBTC to off-chain tBTC swap at https://submarineswaps.org/. The atomic swap rely on a script contract that is similar to the following:

// from chain_swap_script.md of submarine swaps repo
let controllingKey;
if (hash256(preimage) === paymentHash) {
controllingKey = alicePubKey;
} else if currentTime > refundTimeLock {
controllingKey = bobPubKey;
}
return validSignature(controllingKey)

The “paymentHash” is the hash of the preimage and is presented in the lightning invoice. The preimage will be revealed to the payer of the lightning invoice after payment. The swap service provider pays the invoice to get the preimage and use it as a proof of payment to claim the fund at swap address. If the invoice is not paid the swap service provider will not have the preimage and cannot move the fund at swap address. After a specified time, the user can move the fund at swap address back to the refund address which is controlled by the user.

Cross-chain submarine swap

Same chain swap is good but cross-chain swap is even better. I forked the submarine swaps project and make it able to swap on-chain LTC to off-chain BTC. Now users can pay an BTC lightning invoice with on-chain LTC! Here is how the cross-chain swap works:

  1. A user presents a valid BTC lightning invoice and on-chain LTC refund address (if not using “refund paper wallet”).
  2. Swap service provider creates the contract(as discussed above) and gives a P2SH LTC address(a.k.a. swap address) that encoded the hash of the contract script.
  3. The user pays the amount specified by swap service provide to the swap address.
  4. After fund transfer to the swap address is confirmed, swap service provider pays the LN invoice and then claim the fund at swap address.
  5. If the lightning invoice is not paid, the user moves the fund at swap address to the refund address that is controlled by the user.
on-chain to off-chain atomic swap

The source code is available at: https://github.com/Jasonhcwong/swaps-service/tree/ltc-support

You can try the cross-chain swap at: https://ltcswap.hcwong.me

Original submarine swaps repo: https://github.com/submarineswaps/swaps-service

Future plans

Currently the cross-chain swap supports only one on-chain currency, Alex Bosworth is working on making it support multiple on-chain currencies at the same time. Maybe one day it will support on-chain Ethereum to off-chain BTC swap!

--

--