B++ Logo

Atomic Swaps

Atomic swaps enable trustless peer-to-peer exchange of cryptocurrencies across different blockchains without requiring a trusted third party or centralized exchange.

Atomic swaps use hash time-locked contracts (HTLCs) to ensure that either both parties receive their funds or neither does:

Atomic Swap:
- Alice wants to trade BTC for LTC
- Bob wants to trade LTC for BTC
- Either both succeed or both fail
- No trusted intermediary needed

How Atomic Swaps Work

Process

1. Alice creates HTLC on Bitcoin chain
   - Locks BTC with hash lock
   - Time lock for refund

2. Bob creates HTLC on Litecoin chain
   - Locks LTC with same hash
   - Shorter time lock

3. Alice reveals secret (preimage)
   - Claims LTC from Bob's HTLC
   - Reveals hash to Bob

4. Bob uses secret to claim BTC
   - Claims BTC from Alice's HTLC
   - Swap complete

Safety

If Alice doesn't reveal:
- Bob's HTLC expires
- Bob gets LTC back
- Alice's HTLC expires
- Alice gets BTC back

If Bob doesn't create HTLC:
- Alice's HTLC expires
- Alice gets BTC back
- No loss for Alice

Code Examples

Creating HTLC


Lightning Network Swaps

Atomic swaps can also work on Lightning Network:

Lightning Atomic Swap:
- Swap between Lightning channels
- Instant execution
- Lower fees
- Requires channel liquidity

Limitations

Requirements

  1. Compatible blockchains: Both must support HTLCs
  2. Hash function compatibility: Same hash function
  3. Timing coordination: Time locks must be coordinated
  4. Liquidity: Both parties need funds available

Challenges

  • Complexity: Requires technical knowledge
  • Timing: Time locks must be carefully set
  • Liquidity: Both chains need funds
  • Adoption: Limited user-friendly tools


Resources