This documentation section provides an introduction to atomic swaps, hash time locked contracts (HTLCs) and how these concepts are used in COMIT. Atomic Swaps and HTLCs are core concepts of the COMIT protocol.
This slide shows an overview of how atomics swaps using HTLCs work in COMIT, more details can be found in the sections below.
An atomic swap is an exchange of two digital assets between two parties in which both parties are guaranteed to either receive the other asset OR retain their original asset i.e., an atomic swap guarantees that neither party is able to take both assets.
Hash Time Locked Contract (HTLC)
HTLC stands for hash time locked contract. An HTLC is a script or smart contract that locks a digital asset over time. HTLCs only work on ledgers with time locks.
Both parties are required to have the same knowledge of all parameters for the HTLCs on both ledgers before they can start. Both parties then have the same functions available within the HTLCs and can decide to move forward or wait to take their money back. No party has access to both assets at the same time within the time boundaries of the atomic swap.
HTLCs typically define three functions:
- Fund: Locking up the asset in the HTLC.
- Redeem: Until the expiry time-lock is reached the other party has the chance to take the locked-up assets with the secret.
- Refund: After the expiry condition has been met, the party that locked up the asset can take it back.
HTLCs are secured by a time-lock and hash-lock. The hash-lock requires the generation of a secret prior to funding. The order of the actions is then tied to the cryptographic roles of Alice and Bob. Alice is the role that comes up with the secret. She shares the hash of that secret with Bob enabling him to fund on his side. The party in the cryptographic role of Alice has to fund and redeem first, because she has the actual secret.
Atomic Swaps using HTLCs in COMIT
In COMIT we distinguish between 2 different phases when performing an Atomic Swap: 1) The Negotiation phase where the two parties agree on the ledgers, assets and amounts to be swapped. 2) The Execution phase where the two parties follow the actual atomic swap protocol.
Below we describe what actions Alice and Bob would need to perform if they were about to swap ETH on Ethereum for BTC on Bitcoin.
- Alice generates a secret and hashes it. She sends to Bob:
- Hash of the secret.
- Her refund address on Ethereum.
- Her redeem address on Bitcoin.
- Proposed expiry times for the HTLCs.
- Bob receives the swap proposed by Alice and sends back:
- His refund address on Bitcoin.
- His redeem address on Ethereum.
- Alice has to fund first, she deploys an Ethereum HTLC transferring the agreed amount of ETH into the contract.
- Bob monitors the Ethereum ledger and notices that Alice has deployed the HTLC funding the ETH. As soon as this transaction has enough confirmations, Bob sends a Bitcoin transaction that is guarded by the script-hash of the Bitcoin HTLC (Bitcoin script).
- Alice monitors the Bitcoin ledger and notices that Bob has sent the funding transaction. As soon as this transaction has enough confirmation, Alice can now redeem the Bitcoin HTLC. By doing so she reveals the secret.
- Bob is monitoring the Bitcoin ledger and notices that Alice has spent from the funding transaction by revealing the secret. He extracts the actual secret from Alice's redeem transaction and uses it to redeem on Ethereum.
The COMIT protocol acts as an enabler for atomic swaps in applications. The COMIT network daemon (cnd) mainly does two things:
- Monitor the ledgers by processing blocks supplied by blockchain nodes.
- Define the transaction details required for a swap and hand them over to an application built on top of COMIT. The application is responsible for sending the actual signed transactions into the respective blockchain network.
HTLCs in COMIT
In order to create HTLCs for the execution of an atomic swap, the two parties have to exchange the following parameters:
- Ledgers, assets and amounts to be swapped.
- Redeem and refund identity (address) of the respective parties.
- Expiry times to be used for the HTLCs.
- Hash of the secret. This hash is sent from the party in the cryptographic role of Alice to the party in the cryptographic role of Bob.
HTLCs on different Ledgers
On Ethereum this can be achieved by deploying an Ethereum Smart Contract. Note that an Ethereum HTLC does not necessarily have to be written in Solidity. Given that HTLCs are really simple smart contracts, they can also be written in EVM assembly as defined in the COMIT Ether HTLC specification and COMIT ERC20 HTLC specification.
The expiries of the HTLCs have to be defined so that Alice's expiry time is further in the future than Bob's. This is because Alice is protected by the secret, and if she chooses not to reveal the secret, Bob must have the chance to refund early.
Choosing the right initial expiries, and defining if it is still safe for a party to move forward during swap execution is not trivial. The presentation slides below show some scenarios to demonstrate the motivation of both parties to act according to the protocol during swap execution.