sBridge
HTLC
Hash Time Locked Contracts: trustless atomic swaps without an intermediary
HTLC is a trustless atomic swap primitive built directly into the SBChain protocol. It enables cross-chain asset exchanges without a trusted intermediary, using cryptographic hash locks and time locks.
How HTLC Works
An HTLC locks funds on-chain under two conditions:
- Hash lock: funds are released to the recipient if they provide the correct preimage (secret) that hashes to a pre-agreed hash
- Time lock: if the preimage is not revealed before the expiry, funds are automatically returned to the sender
Atomic Cross-Chain Swap Flow
Alice wants to swap SBChain tokens with Bob who holds Bitcoin:
- Alice generates a secret preimage and computes its hash
- Alice creates an HTLC on SBChain locking her tokens for Bob, using the hash and a time lock (
htlc_create_operation) - Bob sees the HTLC on-chain and creates a corresponding HTLC on Bitcoin locking his BTC for Alice, using the same hash with a shorter time lock
- Alice redeems Bob's Bitcoin HTLC by revealing the preimage (
htlc_redeem_operation), which reveals the secret on Bitcoin's chain - Bob uses the now-public preimage to redeem Alice's SBChain HTLC
- Both transfers complete atomically — if either party fails to act, both HTLCs expire and funds are returned
HTLC Operations
| Operation | Description | Fee |
|---|---|---|
| htlc_create_operation | Lock funds under a hash lock with a time expiry | 1 × precision + fee_per_day |
| htlc_redeem_operation | Claim locked funds by providing the correct preimage | 1 × precision + fee_per_kb |
| htlc_extend_operation | Extend the time lock expiry on an existing HTLC | 1 × precision + fee_per_day |
| htlc_redeemed_operation (virtual) | Emitted when an HTLC is successfully redeemed | None |
| htlc_refund_operation (virtual) | Emitted when an HTLC expires and funds are returned | None |
HTLC Fields
| Field | Description |
|---|---|
| from | Account locking the funds |
| to | Account that can claim the funds with the correct preimage |
| amount | Asset and amount locked |
| preimage_hash | Hash of the secret (typed: RIPEMD160, SHA1, SHA256, or Hash160) |
| preimage_size | Expected size of the preimage in bytes |
| claim_period_seconds | Time until the HTLC expires and funds are returned |
Supported Hash Algorithms
RIPEMD160SHA1SHA256Hash160
HTLC vs Gateway
| Gateway (IOU) | HTLC | |
|---|---|---|
| Trust required | Yes, trust the gateway | No, cryptographic guarantee |
| User experience | Simple deposit/withdraw flow | Requires coordination between both parties |
| Speed | Depends on gateway processing | Depends on both chains' confirmation times |
| Use case | Ongoing liquidity, trading | One-time atomic swaps between counterparties |
| Counterparty risk | Gateway custodial risk | None — protocol enforced |