ICP Canisters

Rust-based canisters for Internet Computer Protocol with ICRC-1 token integration

ICP Canisters

Rust-based canisters for Internet Computer integration

Hash Time Lock Canister

Core atomic swap canister

The main canister implementing hash time lock functionality for secure cross-chain swaps on the Internet Computer. Provides ICRC-1 token integration and comprehensive swap management capabilities.

Core Features
  • Hash time lock creation
  • Preimage verification
  • ICRC-1 token transfers
  • Time-based expiration
Security
  • SHA-256 hashing
  • Timelock validation
  • Principal verification
  • Double-spending prevention

Hash Time Lock Canister

Rust-based canister implementing atomic swap functionality with ICRC-1 token support.

Key Functions
new_contract

Creates a new hash time lock contract

claim

Claims funds using preimage

refund

Refunds after timelock expiration

get_contract

Retrieves contract details

Data Structures
#[derive(CandidType, Deserialize, Clone, Debug, Serialize)]
pub struct TimeLockContract {
    pub sender: String,
    pub receiver: String,
    pub amount: u64,
    pub hashlock: String,
    pub timelock: u64,
    pub preimage: Option,
    pub withdrawn: bool,
    pub refunded: bool,
    pub ledger_id: String,
}
Transfer Service
pub async fn transfer_icrc1(
    icrc1_canister: Principal, 
    amount: u64, 
    to_owner: Principal, 
    subaccount: Option
) -> CallResult<(TransferResult, )> {
    // ICRC-1 token transfer implementation
}
ICRC-1 Integration
  • Native ICP token support
  • Cross-canister calls
  • Principal-based security
  • Time-based operations
Query Functions
  • get_all_contracts()
  • get_contracts_by_sender()
  • get_contracts_by_receiver()
  • get_expired_contracts()

Deployment Guide

Deploy canisters to Internet Computer

Prerequisites
  • Rust and Cargo installed
  • DFX (Internet Computer SDK)
  • Node.js and npm
  • Internet Computer network access
Installation
cd icp-canisters
npm install
dfx start --background
# Start local IC network
Build
dfx build
# Build canisters
dfx build hashed-time-lock
# Build specific canister
Deploy
dfx deploy
# Deploy all canisters
dfx deploy hashed-time-lock
# Deploy specific canister
dfx deploy --network ic
# Deploy to mainnet
Testing
cd tests
npm test
# Run all tests
npm run test:watch
# Run in watch mode

Testing

Comprehensive test suite covering canister functionality and integration.

Ledger Integration
  • ICP ledger deployment
  • Token minting
  • Balance verification
  • Transfer testing
Canister Functions
  • Contract creation
  • Preimage verification
  • Swap lifecycle
  • Error handling

API Reference

Complete canister interface documentation

Update Methods (State-Changing)
Method Description Parameters
new_contract Create hash time lock contract NewContractRequest
claim Claim funds with preimage ClaimRequest
refund Refund after expiration RefundRequest
Query Methods (Read-Only)
Method Description Return Type
get_contract Get specific contract Option<TimeLockContract>
get_all_contracts Get all contracts Vec<(String, TimeLockContract)>
hash_preimage Hash a preimage String
get_version Get canister version String