Routing & HTLCs Deep Dive
You do not need a direct channel with every person you want to pay. Lightning finds a path through the network — like GPS finding a route through a road network. Hash Time-Locked Contracts ensure every hop is trustless.
Lightning routing enables payments to travel across multiple channels, creating a network effect that scales Bitcoin transactions globally. The secret sauce is Hash Time Locked Contracts (HTLCs).
How routing works:
1. Source Routing: Sender finds path to receiver 2. HTLC Creation: Each hop creates conditional payment 3. Secret Sharing: Payment secret flows back along path 4. Settlement: Each hop settles when secret is revealed 5. Cleanup: Failed routes are automatically cleaned up
HTLC components:
- •Hash Lock: Payment conditional on revealing preimage
- •Time Lock: Expiry mechanism to prevent stuck payments
- •Atomic Execution: Either entire payment succeeds or fails
- •Fee Structure: Each hop charges small routing fee
Route finding algorithms:
- •Dijkstra's Algorithm: Find shortest path by fee
- •Multi-path Payments: Split payment across routes
- •Liquidity Probing: Discover available channel capacity
# HTLC Output Script
OP_DUP OP_HASH160 <H> OP_EQUALVERIFY OP_CHECKSIG
OP_IF
<recipient_pubkey>
OP_ELSE
<timeout> OP_CHECKSEQUENCEVERIFY OP_DROP
<sender_pubkey>
OP_ENDIF
# Payment flow
def create_htlc(amount, hash_lock, timeout, recipient, sender):
return ConditionalPayment(
amount=amount,
condition=hash_lock,
timeout=timeout,
recipient=recipient,
fallback=sender
)HTLCs guarantee that a payment either completes fully across every hop or fails completely with no one losing funds. This is the cryptographic mechanism that makes Lightning trustless.
Test Your Knowledge
2 questions · Passing score: 85%
Enjoying these lessons?
Get a free Bitcoin lesson in your inbox every week. Join thousands of learners.
Free forever. No spam. Unsubscribe anytime.