Learn about P2TR (Pay To Taproot) to understand how to spend from a HTLC script.
Script architecture
Instead of contract functions, Bitcoin implements atomic swap logic through scripts that are executed when UTXOs are spent. Let’s explore the four core operations: We will use the following struct for passing swap info:Core operations
Initiate
Initiating an atomic swap on Bitcoin involves creating a transaction that locks funds in an HTLC script. Here’s what happens during initiation:1
Create transaction
The initiator creates a Bitcoin transaction that:
- Spends from their wallet (input).
- Creates an output that pays to the HTLC script address (derived from the HTLC parameters — pubkeys, secret hash, timelock, and destination data)
2
Set script conditions
The HTLC script ensures that the locked funds can only be spent if either:
- The redeemer provides the correct secret and their signature (redeem path)
- The initiator claims back after timelock expiry (refund path)
- Both parties agree to cancel (instant refund path)
Redeem
Redeeming an atomic swap means claiming the locked funds by providing the secret that matches the hash used during initiation. The redeem process involves:- Creating a transaction that spends from the HTLC script address.
- Providing the secret that matches the hash.
- Signing the transaction with the redeemer’s private key.
- Secret validation: Ensures the provided secret hashes to the expected value.
- Signature verification: Confirms the redeemer is authorized to claim.
Stack-based execution provides deterministic behavior and prevents many classes of smart contract vulnerabilities.
Stack visualization
Notes:
- The stack grows and shrinks from the top (top element is consumed first)
OP_SHA256
consumes the secret and produces its hash (proves knowledge of preimage)OP_EQUALVERIFY
is critical — it fails immediately if the hashes don’t match- Signature verification happens only if the secret is valid
Refund
The refund mechanism is a critical safety feature in atomic swaps that prevents funds from being locked forever if the swap fails to complete. Key concepts in refunding:- Timelock: A waiting period measured in blocks.
- Relative time: Measured from when the swap was initiated.
- Initiator authentication: Only the initiator can refund.
- Waiting for the timelock to expire
- Creating a transaction that spends from the HTLC script
- Providing proof of being the initiator (signature)
Stack visualization
Notes:
OP_CHECKSEQUENCEVERIFY
verifies that enough time has passed (relative to when UTXO was created).- CSV fails and prevents spending if the timelock hasn’t expired.
- CSV doesn’t consume the timelock value, so
OP_DROP
is needed to remove it. - The final signature check ensures only the original initiator can refund.
Relative timelock with CSV provides more flexibility than absolute block numbers, as it’s measured from when the UTXO was created rather than a specific block height.
- The counterparty never participates.
- Network issues prevent completion.
- The counterparty abandons the swap.
Instant refund
Instant refund is a cooperative cancellation mechanism that allows both parties to mutually agree to terminate the swap early, without waiting for the timelock to expire. Key concepts in instant refund:- Multi-signature: Requires both parties to agree.
- No timelock: Can be executed at any time.
- Mutual authentication: Both parties must provide valid signatures.
- Initiator signature validation: Ensures the initiator has agreed to cancel
- Redeemer signature validation: Ensures the redeemer has agreed to cancel
Stack visualization
Notes about
OP_CHECKSIGADD
:It consumes three stack elements:- A public key (redeemerPubkey)
- A numeric value to add to (result of first CHECKSIG)
- A signature to verify (redeemerSignature)
- Verifies if the signature is valid for the pubkey
- Adds 1 (if valid) or 0 (if invalid) to the numeric value
- Pushes the sum back onto the stack
- Sum = 2: Both signatures valid (1+1)
- Sum = 1: Only one signature valid (1+0 or 0+1)
- Sum = 0: Neither signature valid (0+0)
OP_NUMEQUAL
with 2 ensures both signatures must be validThe
OP_CHECKSIGADD
operation provides efficient multi-signature verification using Tapscript’s enhanced capabilities.Bitcoin-specific considerations
Transaction construction
- Input selection: Choose appropriate UTXOs to fund the HTLC script, considering transaction fees and optimal UTXO management.
- Script address generation: Generate the script address using Taproot’s address format for better privacy, efficiency and cost optimization.
- Fee calculation: Calculate appropriate fees based on current network conditions and transaction size.