Documentation Index
Fetch the complete documentation index at: https://mintlify.com/1inch/cross-chain-sdk/llms.txt
Use this file to discover all available pages before exploring further.
What is a Hash Lock?
A hash lock is a cryptographic commitment scheme used to secure atomic swaps. It works by:- Generating a random secret (32 bytes)
- Computing the hash of that secret using keccak256
- Locking funds that can only be unlocked by revealing the original secret
HashLock class:
Hash-Lock Cryptography
Secret Hashing
Secrets must be exactly 32 bytes and are hashed using keccak256:Why keccak256? This cryptographic hash function is:
- One-way: Cannot derive the secret from the hash
- Deterministic: Same secret always produces same hash
- Collision-resistant: Nearly impossible to find two secrets with same hash
Security Properties
The hash lock provides:- Commitment: Maker commits to a secret without revealing it
- Authorization: Only someone with the secret can unlock funds
- Atomicity: Same secret unlocks both source and destination escrows
Single-Fill vs Multi-Fill Orders
The SDK supports two types of orders:Single-Fill Orders
For orders filled in one transaction, use a simple hash of the secret:Multi-Fill Orders
For orders filled in multiple parts (partial fills), use a Merkle tree:- Each partial fill uses a different secret
- All secrets commit to a single Merkle root
- Proofs verify individual secrets belong to the set
Multi-fill orders enable better liquidity by allowing multiple resolvers to fill portions of large orders.
Merkle Tree Implementation
Generating Merkle Leaves
Each secret is combined with its index to create a unique leaf:Creating Merkle Proofs
When filling an order, the resolver needs a proof that their secret is valid:Multi-Fill Interaction
The escrow factory creates interactions with Merkle proofs:Parts Count Encoding
For multi-fill orders, the number of parts is encoded in the hash lock:Complete Example: Multi-Fill Order
HashLock API Reference
Static Methods
| Method | Description |
|---|---|
forSingleFill(secret) | Create hash lock for single-fill order |
forMultipleFills(leaves) | Create hash lock for multi-fill order |
hashSecret(secret) | Hash a secret with keccak256 |
getMerkleLeaves(secrets) | Generate Merkle leaves from secrets |
getProof(leaves, idx) | Get Merkle proof for specific index |
fromString(value) | Deserialize from hex string |
fromBuffer(value) | Deserialize from Buffer |
Instance Methods
| Method | Description |
|---|---|
toString() | Serialize to hex string |
toBuffer() | Serialize to Buffer |
getPartsCount() | Get fill count (multi-fill only) |
eq(other) | Check equality |
Best Practices
-
Generate cryptographically random secrets
-
Store secrets securely until reveal time
- Never log secrets to console in production
- Use secure storage (encrypted, memory-only, etc.)
- Clear secrets from memory after use
-
Verify order parameters before revealing secrets
- Check escrow addresses match expectations
- Verify token amounts and addresses
- Confirm time-locks are acceptable
-
Choose appropriate fill strategy
- Single-fill: Simpler, lower gas costs
- Multi-fill: Better for large orders, enables partial fills
Related Concepts
- Atomic Swaps - Overall swap mechanism
- Secrets Management - Safe secret handling
- Escrows - How hash locks protect escrow funds