HashLock
Represents a hash lock used in cross-chain atomic swaps. Supports both single-fill and multiple-fill orders using Merkle trees.
Constructor
new HashLock(val: string)
32-byte hex-encoded hash value
Use static factory methods (forSingleFill, forMultipleFills) instead of calling the constructor directly.
Static Properties
Returns “bytes32” - the Solidity type for hash locks
Static Methods
forSingleFill
Create a HashLock for a single-fill order by hashing a secret.
HashLock.forSingleFill(secret: string): HashLock
32-byte hex-encoded secret value
Returns: HashLock containing keccak256(secret)
Example:
const secret = "0x1234..." // 32 bytes hex
const hashLock = HashLock.forSingleFill(secret)
Create a HashLock for multiple-fill orders using a Merkle tree.
HashLock.forMultipleFills(leaves: MerkleLeaf[]): HashLock
Array of Merkle leaves (must be greater than 2)
Returns: HashLock containing Merkle root with embedded leaf count
The leaves array must contain more than 2 elements. For single or dual fills, use forSingleFill instead.
Example:
const secrets = ["0x1234...", "0x5678...", "0x9abc..."]
const leaves = HashLock.getMerkleLeaves(secrets)
const hashLock = HashLock.forMultipleFills(leaves)
hashSecret
Hash a secret using keccak256.
HashLock.hashSecret(secret: string): string
32-byte hex-encoded secret value
Returns: Keccak256 hash of the secret
getMerkleLeaves
Convert secrets to Merkle leaves by hashing and indexing them.
HashLock.getMerkleLeaves(secrets: string[]): MerkleLeaf[]
Array of 32-byte hex-encoded secrets
Returns: Array of Merkle leaves, each computed as keccak256(index, hashSecret(secret))
getMerkleLeavesFromSecretHashes
Convert pre-hashed secrets to Merkle leaves.
HashLock.getMerkleLeavesFromSecretHashes(secretHashes: string[]): MerkleLeaf[]
Array of already-hashed secrets
Returns: Array of Merkle leaves, each computed as keccak256(index, secretHash)
getProof
Generate a Merkle proof for a specific leaf index.
HashLock.getProof(leaves: string[], idx: number): MerkleLeaf[]
Index of the leaf to prove
Returns: Merkle proof path for the specified leaf
Example:
const leaves = HashLock.getMerkleLeaves(secrets)
const proof = HashLock.getProof(leaves, 0) // Proof for first secret
fromString
Create a HashLock from a hex string.
HashLock.fromString(value: string): HashLock
32-byte hex-encoded hash value
fromBuffer
Create a HashLock from a Buffer.
HashLock.fromBuffer(value: Buffer): HashLock
Buffer containing 32 bytes of hash data
Instance Methods
getPartsCount
Get the number of parts (fills) for a multiple-fill order.
Returns: Number of fills encoded in the hash lock
Only use this method for multiple-fill orders. For single-fill orders, this will return garbage data.
toString
Convert hash lock to hex string.
toBuffer
Convert hash lock to Buffer.
Check if this hash lock equals another.
eq(other: HashLock): boolean
MerkleLeaf
Type alias for Merkle tree leaves.
type MerkleLeaf = string & {_tag: 'MerkleLeaf'}
A branded string type representing a leaf in a Merkle tree.