TeachMeBitcoin

Atomic Swap Scripts - Cross-Chain Pattern

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

17. Atomic Swap Scripts — Cross-Chain Pattern

Overview

Atomic swaps allow two parties to exchange cryptocurrencies across different blockchains trustlessly, without a centralized exchange. The mechanism ensures that either both transfers happen or neither does — the "atomic" guarantee. This uses Hash Time-Locked Contracts (HTLCs) with a shared secret pre-image as the coordination primitive.

HTLC Script for Atomic Swaps

Alice's HTLC on Bitcoin chain (locks BTC for Bob):

OP_IF
    OP_SHA256 <payment_hash> OP_EQUALVERIFY
    <bob_btc_pubkey> OP_CHECKSIG
OP_ELSE
    <alice_timelock> OP_CHECKLOCKTIMEVERIFY OP_DROP
    <alice_btc_pubkey> OP_CHECKSIG
OP_ENDIF

Bob's HTLC on Litecoin chain (locks LTC for Alice):

OP_IF
    OP_SHA256 <payment_hash> OP_EQUALVERIFY
    <alice_ltc_pubkey> OP_CHECKSIG
OP_ELSE
    <bob_timelock> OP_CHECKLOCKTIMEVERIFY OP_DROP
    <bob_ltc_pubkey> OP_CHECKSIG
OP_ENDIF

Both HTLCs use the same payment_hash = SHA256(secret).

Atomic Swap Protocol

Setup:
  Alice generates: secret (32 bytes)
  payment_hash = SHA256(secret)
  Alice shares: payment_hash (NOT the secret)

Step 1: Alice locks BTC

Alice creates HTLC on Bitcoin with:
    - payment_hash = H(secret)
    - success path: Bob can claim with secret + signature
    - refund path: Alice reclaims after T1 blocks

Step 2: Bob locks LTC

Bob sees Alice's HTLC, creates HTLC on Litecoin with:
    - Same payment_hash = H(secret)
    - success path: Alice can claim with secret + signature
    - refund path: Bob reclaims after T2 blocks (T2 < T1)

Step 3: Alice claims LTC

Alice spends Bob's Litecoin HTLC using:
    scriptSig: <alice_sig> <secret> OP_1
  This reveals `secret` on the Litecoin blockchain

Step 4: Bob claims BTC

Bob sees `secret` in Alice's LTC claim transaction
  Bob spends Alice's Bitcoin HTLC using:
    scriptSig: <bob_sig> <secret> OP_1

Timelock Relationship Requirement

Critical timing constraint:
  T1 (Alice's BTC refund) >> T2 (Bob's LTC refund)

Reason:
  If Bob's LTC refund expires first, he could:
    - Wait for Alice to claim LTC (revealing secret)
    - Immediately claim BTC too
    → Bob gets both coins!

Safe design:
  T2 = 24 hours
  T1 = 48 hours
  Bob must claim BTC before T1 after seeing secret

On-Chain Taproot Atomic Swaps

With Taproot, atomic swaps can use point-time-locked contracts (PTLCs) instead of HTLCs, eliminating the shared hash pre-image that links the two chains:

PTLC uses a scalar (adaptor signature) instead of hash pre-image:
  - No shared hash visible across chains
  - Breaks cross-chain transaction correlation
  - Requires Schnorr/Taproot on both chains
  - LN's move to Taproot channels enables PTLCs
☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!