| Crates.io | zcash-htlc-builder |
| lib.rs | zcash-htlc-builder |
| version | 0.1.5 |
| created_at | 2025-11-30 01:08:38.801+00 |
| updated_at | 2025-11-30 22:31:09.685742+00 |
| description | Production-ready Zcash HTLC builder for atomic swaps using transparent transactions |
| homepage | https://github.com/Mist-Labs/zcash-htlc-builder |
| repository | https://github.com/Mist-Labs/zcash-htlc-builder |
| max_upload_size | |
| id | 1957721 |
| size | 184,091 |
A production-ready Rust library for creating and managing Hash Time-Locked Contracts (HTLCs) on Zcash's transparent transaction layer. Built for atomic swaps and cross-chain bridges.
Add to your Cargo.toml:
[dependencies]
zcash-htlc-builder = "0.1.5"
tokio = { version = "1", features = ["full"] }
Create zcash-config.toml in your project root:
network = "testnet" # or "mainnet"
rpc_url = "http://localhost:18232"
rpc_user = "your-rpc-user"
rpc_password = "your-rpc-password"
database_url = "postgresql://user:password@localhost/zcash_htlc"
database_max_connections = 10
explorer_api = "https://explorer.testnet.z.cash/api"
# Optional: Relayer Configuration (for automated HTLC management)
[relayer]
hot_wallet_privkey = "your-private-key"
hot_wallet_address = "your-zcash-address"
max_tx_per_batch = 10
poll_interval_secs = 10
max_retry_attempts = 3
min_confirmations = 1
network_fee_zec = "0.0001"
โ ๏ธ Security Warning: Never commit
zcash-config.tomlwith real credentials to version control. Add it to.gitignoreand usezcash-config.toml.exampleas a template.
# Create PostgreSQL database
createdb zcash_htlc
# Migrations run automatically when you first use the library
use zcash_htlc_builder::{
ZcashHTLCClient, ZcashConfig, HTLCParams, UTXO,
database::Database,
};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load configuration from file
let config = ZcashConfig::from_toml_file("zcash-config.toml")?;
// Initialize database
let database = Arc::new(Database::new(
&config.database_url,
config.database_max_connections,
)?);
// Create client
let client = ZcashHTLCClient::new(config, database);
// Generate keys
let recipient_privkey = client.generate_privkey();
let recipient_pubkey = client.derive_pubkey(&recipient_privkey)?;
let refund_privkey = client.generate_privkey();
let refund_pubkey = client.derive_pubkey(&refund_privkey)?;
// Generate secret and hash lock
let secret = hex::encode(rand::random::<[u8; 32]>());
let hash_lock = client.generate_hash_lock(&secret);
// Create HTLC parameters
let params = HTLCParams {
recipient_pubkey,
refund_pubkey,
hash_lock: hash_lock.clone(),
timelock: 500000, // Block height
amount: "0.01".to_string(),
};
// Prepare funding (replace with your actual UTXOs)
let funding_utxos = vec![
UTXO {
txid: "your-txid".to_string(),
vout: 0,
script_pubkey: "script-hex".to_string(),
amount: "0.02".to_string(),
confirmations: 6,
}
];
// Create HTLC
let result = client.create_htlc(
params,
funding_utxos,
"your-change-address",
vec!["your-funding-privkey"],
).await?;
println!("โ
HTLC Created!");
println!("๐ HTLC ID: {}", result.htlc_id);
println!("๐ TXID: {}", result.txid);
println!("๐ P2SH Address: {}", result.p2sh_address);
println!("๐๏ธ Secret: {}", secret);
// Later, redeem the HTLC
let redeem_txid = client.redeem_htlc(
&result.htlc_id,
&secret,
"recipient-address",
&recipient_privkey,
).await?;
println!("โ
HTLC Redeemed: {}", redeem_txid);
Ok(())
}
You can also use JSON format:
{
"network": "testnet",
"rpc_url": "http://localhost:18232",
"rpc_user": "user",
"rpc_password": "password",
"database_url": "postgresql://localhost/zcash_htlc",
"database_max_connections": 10,
"explorer_api": "https://explorer.testnet.z.cash/api"
}
Load with:
let config = ZcashConfig::from_json_file("zcash-config.json")?;
The library includes a command-line tool for testing and operations.
cargo install zcash-htlc-builder
All CLI commands use the config file from your project root or via custom path.
zcash-htlc-cli keygen
# Or with custom config:
zcash-htlc-cli keygen ./my-config.toml
zcash-htlc-cli hashlock "my-secret-phrase"
Output:
๐ Hash Lock:
Secret: my-secret-phrase
Hash Lock: 6e9f78c1c24acdee688a360f1212c9b9989e7469d6a6e39e4ed7ca279f0c7846
zcash-htlc-cli create
zcash-htlc-cli redeem <htlc_id> <secret> <recipient_address> <privkey>
zcash-htlc-cli refund <htlc_id> <refund_address> <privkey>
zcash-htlc-cli broadcast <hex-encoded-tx>
You can set ZCASH_CONFIG environment variable to specify config file location:
export ZCASH_CONFIG=./production-config.toml
zcash-htlc-cli keygen
Check the examples/ directory for complete working examples:
# Run the full HTLC flow example
cargo run --example test_htlc_flow
Output:
๐งช Testing HTLC Flow
๐ Step 1: Generating Keys
๐ค Recipient Private Key: 489175b18cd8f36e...
๐ค Recipient Public Key: 02f6b9fc88bf40a5c...
๐ฆ Relayer Private Key: c980fd0225a51ec8...
๐ฆ Relayer Public Key: 03bb93b3c358ba56e...
๐ Step 2: Generating Secret and Hash Lock
๐๏ธ Secret: 8f2c59d64b11f329...
๐ Hash Lock: 346a7d2128ff4d1b...
...
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ ZcashHTLCClient (Main API) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โข create_htlc() โ
โ โข redeem_htlc() โ
โ โข refund_htlc() โ
โ โข generate_privkey() โ
โ โข derive_pubkey() โ
โ โข generate_hash_lock() โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ โ โ
โผ โผ โผ
โโโโโโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
โ Builder โ โ Signer โ โ Database โ
โ โ โ โ โ โ
โ โข HTLC TX โ โ โข Sign โ โ โข HTLCs โ
โ โข Redeem TX โ โ โข Verify โ โ โข Operations โ
โ โข Refund TX โ โ โข Keys โ โ โข UTXOs โ
โโโโโโโโโโโโโโโโ โโโโโโโโโโโโ โโโโโโโโโโโโโโโโ
| Table | Description |
|---|---|
| zcash_htlcs | HTLC state and metadata |
| htlc_operations | Transaction operations (create/redeem/refund) |
| relayer_utxos | UTXOs managed by relayer's hot wallet |
| indexer_checkpoints | Blockchain sync state |
| Field | Type | Required | Description |
|---|---|---|---|
network |
string | โ Yes | "testnet" or "mainnet" |
rpc_url |
string | โ Yes | Zcash RPC endpoint |
rpc_user |
string | โ No | RPC username or API key |
rpc_password |
string | โ No | RPC password |
database_url |
string | โ Yes | PostgreSQL connection string |
database_max_connections |
number | โ No | Max DB connections (default: 10) |
explorer_api |
string | โ No | Block explorer API URL |
| Field | Type | Required | Description |
|---|---|---|---|
hot_wallet_privkey |
string | โ ๏ธ Yes* | Private key for funding |
hot_wallet_address |
string | โ ๏ธ Yes* | Address for funding |
max_tx_per_batch |
number | โ No | Max transactions per batch (default: 10) |
poll_interval_secs |
number | โ No | Polling interval in seconds (default: 10) |
network_fee_zec |
string | โ No | Network fee in ZEC (default: "0.0001") |
*Required only if running automated relayer
zcash-config.toml with real keys to version control| Property | Value |
|---|---|
| RPC Port | 18232 |
| Faucet | testnet.zecfaucet.com |
| Explorer | blockexplorer.one/zcash/testnet |
| Property | Value |
|---|---|
| RPC Port | 8232 |
| Explorer | blockexplorer.one/zcash/mainnet |
# Run all tests
cargo test
# Run specific test
cargo test test_build_htlc_script
# With logging
RUST_LOG=debug cargo test
# Run examples
cargo run --example test_htlc_flow
| Crate | Version | Purpose |
|---|---|---|
| bitcoin | 0.29 | Transaction building (Zcash compatible) |
| secp256k1 | 0.24 | Cryptographic signatures |
| diesel | 2.1 | PostgreSQL ORM |
| tokio | 1.0 | Async runtime |
| reqwest | 0.11 | HTTP client for RPC |
| serde | 1.0 | Serialization/deserialization |
| toml | 0.8 | TOML configuration parsing |
zcash-config.toml exists in project rootpg_isreadycreatedb zcash_htlcContributions welcome! Please:
git checkout -b feature/amazing-feature)cargo test and cargo clippy pass# Clone repository
git clone https://github.com/Mist-Labs/zcash-htlc-builder.git
cd zcash-htlc-builder
# Install dependencies
cargo build
# Run tests
cargo test
# Run clippy
cargo clippy -- -D warnings
# Format code
cargo fmt
See CHANGELOG.md for version history.
This project is licensed under the Apache-2.0 License - see the LICENSE file for details.
Built with โค๏ธ for the Zcash ecosystem by Mist Labs
โญ If you find this library useful, please star the repository!