# Cross-Chain Atomic Swap Two parties, Alice (who has BTC) and Bob (who has LTC), wish to swap coins trust-lessly via HTLCs on each chain. Parameters ``` Assembly Secret preimage x such that H = RIPEMD160(SHA256(x)) Timeouts: Chain A (BTC) locktime T₁ (e.g. block 700_000) Chain B (LTC) locktime T₂ < T₁ (e.g. block 150_000) ``` ## 1. Alice Funds HTLC₁ on BTC= Alice constructs HTLC₁ script: ``` Assembly OP_IF # Bob redeems by revealing x OP_HASH160 <H> OP_EQUALVERIFY <Bob_BTC_pubkey> OP_CHECKSIG OP_ELSE # Alice refunds after T₁ <T₁> OP_CHECKLOCKTIMEVERIFY OP_DROP <Alice_BTC_pubkey> OP_CHECKSIG OP_ENDIF ``` Alice creates and broadcasts the Funding₁ TX: ```Assembly vin: Alice’s UTXO vout: 1 × P2WSH(sha256(HTLC₁_script)) amount = Amount₁ Network confirms → UTXO₁ locked under HTLC₁_script. ``` ## 2. Bob Watches & Funds HTLC₂ on LTC Bob verifies Funding₁ is safely in the chain. Bob constructs HTLC₂ with same hash H, but timeout T₂: ```Assembly OP_IF OP_HASH160 <H> OP_EQUALVERIFY <Alice_LTC_pubkey> OP_CHECKSIG OP_ELSE <T₂> OP_CHECKLOCKTIMEVERIFY OP_DROP <Bob_LTC_pubkey> OP_CHECKSIG OP_ENDIF ``` Bob broadcasts Funding₂ TX on Litecoin: ``` Assembly vin: Bob’s LTC UTXO vout: 1 × P2WSH(sha256(HTLC₂_script)) amount = Amount₂ Network confirms → UTXO₂ locked under HTLC₂_script. ``` ## 3. Alice Redeems BTC on Chain B Alice constructs Redeem₂ TX on LTC, spending UTXO₂: ```Assembly nLockTime < now (no need CLTV branch) witness stack: <Alice_LTC_signature> <x> OP_TRUE # choose OP_IF branch Script path: OP_IF → hash-lock branch OP_HASH160 hashes x, equals <H> → OK OP_CHECKSIG verifies Alice’s sig → OK ``` Network confirms, Bob learns x from the on-chain witness. ## 4. Bob Redeems BTC on Chain A Bob now knows x. He builds Redeem₁ TX on BTC: ```Assembly witness stack: <Bob_BTC_signature> <x> OP_TRUE ``` Script path same as above → funds → Bob’s wallet. ## 5. Time-Out Refunds (Fail-Safes) If Bob never funds HTLC₂ by watching Funding₁, Alice waits until block ≥ T₁, then broadcasts Refund₁ TX: ```Assembly <Alice_BTC_signature> OP_FALSE # choose OP_ELSE branch ``` If Alice never redeems HTLC₂ (and thus never reveals x), Bob waits until block ≥ T₂, then broadcasts Refund₂ TX similarly. # Main Idea Alice generates a secret and creates an HTLC using the hash of that secret. Bob then locks his asset on Chain B with the same hash. When Alice claims the asset on Chain B, she must reveal the secret. Bob can then use that revealed secret to claim the asset locked in the HTLC on Chain A.