In this note, we define and explain HTLC. After that, introduce atomic swap by using HTlC, and take a look at an example of atomic swap between chana A and chain B. Note: In all examples, Alice is the seller and Bob is the buyer. # HTLC : Hashed Timelock Contract (HTLC) is a framework used for payment transfers via platforms like the Lightning Network, allowing receivers to confirm the receipt of funds before time-out. The primary goal of HTLCs is to provide an extra level of security while transferring funds through a decentralized system. Hash time lock contracts can help to eliminate the need for third parties in contracts between two parties. Third parties that are often involved in contracts are lawyers, banks, etc. Lawyers are often required to draw up contracts, and banks are often required to help store money and then transfer it to the receiving party in the contract. With hash time lock contracts, two parties could hypothetically set up contracts and transfer money without the need for third parties. This is because the sending party could create the conditional payment, and then the receiving party could agree to it, receive it, and help validate the transaction in the process. HTLC works on two concepts – Hashlock and Timelock. The term Hashed Timelock Contract has been derived by combining these two words. These features make the release and transfer of funds easier. However, if a receiver fails to claim payment receipt before the stipulated period, they lose the right to receive the funds, and the amount gets refunded to the buyer. * **Hashlock** It refers to the restriction imposed on the transaction until the receiver provides the right key. In simple terms, the transaction is hashed or encrypted cryptographically to secure it. It acts as a Two-factor Authentication or a Secret Recovery Phrase for the parties. So, if the receiver wants to access the funds, they must provide the security key or pin to the sender. This key is like the private key but differs slightly in its function. * **Timelock** As the name suggests, the timelock feature limits receivers from confirming the payment details before a preset date and time. It typically sets aside certain Bitcoin or cryptocurrencies that get released upon confirmation. # **How do HTLC work?** The way that Hash Time Lock Contracts work is that the person who will be making the payment(Bob) sets up a specific hash, which represents the amount of money that will be paid. To receive the payment, the recipient(Alice) will have to create a cryptographic proof of payment, and she will have to do this within the specified amount of time. The amount of time that the recipient has to accept the payment can vary significantly from one Time Locked contract to the next. If the recipient meets the deadline, then the money will be theirs, if he or she fails to meet the deadline, it won’t. So, there is an often a lot at stake when it comes to meeting deadlines from hash Time Locked contracts, when Cryptocurrencies are being exchanged. The amount of time that the recipient has to accept the payment can vary significantly from one Time Locked contract to the next. If the recipient meets the deadline, then the money will be theirs, if he or she fails to meet the deadline, it won’t. So, there is an often a lot at stake when it comes to meeting deadlines from hash Time Locked contracts, when Cryptocurrencies are being exchanged. ![](https://hackmd.io/_uploads/HyZ1YtX22.png) # example of HTLC Bob and Alice have a Bitcoin transaction of 10 Bitcoin (BTC) to execute. Bob creates a crypto hash and sends it to Alice. Now, the receiver must crack the hash per the hashed block. Similarly, Bob can create a time lock that allows Alice to confirm the transaction before a block (50 blocks). As she completes the verification, she receives 10 BTC as her payment. But, if Alice fails to do so, she will not be able to receive any crypto money. Instead, one mistake on her side can cause her to lose the entire amount. # Implemention of HTLC in Bitcoin ``` OP_IF // hash lock branch OP_SHA256 <hash of secret> OP_EQUALVERIFY <pubKey of swap> OP_CHECKSIG OP_ELSE // time lock branch <locktime> OP_CHECKLOCKTIMEVERIFY OP_DROP <pubKey of refund> OP_CHECKSIG OP_ENDIF ``` # Cross-chain Atomic Swaps Atomic swaps enable peer-to-peer exchanges of crypto tokens across different blockchain networks that only execute if both parties each deposit a predetermined amount of tokens to the exchange contract. This enables any two users to swap digital tokens without relying on a third party to facilitate the transaction—thereby reducing counterparty risks. “Atomicity” is a software term that refers to database transactions that only execute in full or not at all. Named accordingly, atomic swaps either receive the necessary token deposits from each user and perform the swap or return all deposited tokens to their original owner. Now let's use HTLC to swap Alice’s BSV coins and Bob’s BTC coins, based on a ratio they both agree. * **basic** A bitcoin address is like a locked mailbox with a deposit slot. When Bob sends bitcoins to Alice, he puts them in the slot of mailbox A, with Alice’s “address”. Only Alice has the key to open the mailbox and retrieve the coins. * **Setup** Alice puts BSV into Bob’s mailbox with a hash lock and she tells Bob the hash. Bob then puts BTC into Alice’s mailbox with the same hash lock. These two hash locks share the same PIN, which Alice generates and keeps hidden from Bob for now. ![](https://hackmd.io/_uploads/HJhqtcX2n.png) * **swap** Alice uses her private key A and the secret PIN to open mailbox A to acquire BTC Bob deposits. Bob learns the PIN that Alice just revealed on the BTC blockchain. He can use the same PIN to open mailbox B, together with his private key B, and get BSV Alice deposits. They have swapped their coins without any third party. Opening one mailbox effectively gives the other party the ability to open the other mailbox. Alice cannot open her mailbox without Bob opening his. # What if Alice or Bob aborts? If Bob does not put BTC into Mailbox A after Alice deposits BSV, her BSV is stuck. Similarly, if Alice does not enter her PIN after setup, Bob’s BTC coins are stuck, so are Alice’s BSV. This is where time lock comes in. Each mailbox has a fall-safe time lock, so coins can be refunded if nobody opens the lockbox in a timely fashion. For example, Bob can unlock Mailbox A with his key B after a certain time. Note that Alice’s time lock on Mailbox B must be longer than Bob’s on Mailbox A. Otherwise Alice can wait till Mailbox B’s time lock expires, takes her BSV coins back and uses PIN to open Mailbox A to take Bob’s BTC deposit. # Atomic Swap Protocol Sequence In summary, an atomic swap protocol between BSV and BTC can be executed following these steps. 1. Alice generates a secure random number x and computes its hash: h = SHA256(x). Alice sends h to Bob. 2. Alice locks up coins in a HTLC on BSV, which can be unlocked in one of the two ways: 1) a value that hashes to h and Bob’s signature; 2) Alice’s signature after, say, 24 hours. Alice deploys the contract by broadcasting a transaction to the BSV network. 3. Bob locks up coins in a HTLC on BTC, which can be unlocked in one of the two ways: 1) a value that hashes to h and Alice’s signature; 2) Bob’s signature after, say, 48hours. Bob deploys the contract by broadcasting a transaction to the BTC network. 4. Upon confirming Bob’s transaction, Alice claims the BTC by providing x and her signature. 5. Bob, observing x on BTC, uses x and his signature to claim BSV. In case Step 3 or 4 does not happen, both can take back their coins after time lock expires. # References * [Cross-chain Atomic Swaps](https://xiaohuiliu.medium.com/cross-chain-atomic-swaps-f13e874fc) * [Hashed Timelock Contract](https://www.wallstreetmojo.com/hashed-timelock-contract/#:~:text=Hashed%20Timelock%20Contract%2C%20or%20HTLC,it%2C%20they%20receive%20the%20payment.) * [ What is HTLC?](https://swap.readthedocs.io/en/v0.2.3/htlc.html)