ai-agent-bitcoin-escrow 0.1.0

A Rust library for AI agents to create, manage, and execute Bitcoin escrow contracts using multisig
Documentation
//! Basic escrow example demonstrating the creation and management of a Bitcoin escrow.
//!
//! Run with: cargo run --example basic_escrow

use ai_agent_bitcoin_escrow::{
    audit::AuditLogger,
    conditions::ConditionBuilder,
    escrow::{EscrowBuilder, EscrowManager},
    multisig::create_participant,
    types::{EscrowConfig, EscrowRole},
};
use bitcoin::Network;
use chrono::{Duration, Utc};

fn main() {
    println!("=== AI Agent Bitcoin Escrow Example ===\n");

    // Step 1: Create an audit logger
    println!("1. Setting up audit logging...");
    let audit = AuditLogger::new("/tmp/escrow_audit.log").expect("Failed to create audit logger");
    let mut manager = EscrowManager::new(audit);
    println!("   ✓ Audit logger initialized\n");

    // Step 2: Create participants (buyer, seller, arbiter)
    println!("2. Creating escrow participants...");
    let (buyer, buyer_key) = create_participant(EscrowRole::Buyer, "alice".to_string(), Network::Testnet)
        .expect("Failed to create buyer");
    let (seller, seller_key) = create_participant(EscrowRole::Seller, "bob".to_string(), Network::Testnet)
        .expect("Failed to create seller");
    let (arbiter, arbiter_key) = create_participant(EscrowRole::Arbiter, "charlie".to_string(), Network::Testnet)
        .expect("Failed to create arbiter");
    println!("   ✓ Buyer: {} (role: {:?})", buyer.id, buyer.role);
    println!("   ✓ Seller: {} (role: {:?})", seller.id, seller.role);
    println!("   ✓ Arbiter: {} (role: {:?})", arbiter.id, arbiter.role);
    println!();

    // Step 3: Create a contract with conditions
    println!("3. Building escrow contract...");
    let expiry_time = Utc::now() + Duration::days(30);
    
    let condition = ConditionBuilder::new()
        .timelock(expiry_time)
        .build_single()
        .expect("Failed to build condition");

    let contract = EscrowBuilder::new()
        .network(Network::Testnet)
        .threshold(2) // 2-of-3 multisig
        .participant(buyer)
        .participant(seller)
        .participant(arbiter)
        .description("AI Agent Bounty Payment - Testnet")
        .condition(condition)
        .build()
        .expect("Failed to build contract");

    println!("   ✓ Contract ID: {}", contract.id);
    println!("   ✓ Network: {:?}", contract.config.network);
    println!("   ✓ Threshold: {}-of-{} multisig", contract.config.threshold, contract.config.total_participants);
    println!("   ✓ Expires: {}", expiry_time.to_rfc3339());
    println!();

    // Step 4: Initialize multisig
    println!("4. Initializing multisig wallet...");
    let mut contract = contract;
    contract.initialize_multisig().expect("Failed to initialize multisig");
    let escrow_address = contract.escrow_address().expect("Failed to get escrow address");
    println!("   ✓ Escrow Address: {}", escrow_address);
    println!("   ✓ Descriptor: {}", contract.multisig.as_ref().unwrap().descriptor);
    println!();

    // Step 5: Display status
    println!("5. Contract Status:");
    println!("   Status: {:?}", contract.status);
    println!("   Participants: {}", contract.participants.len());
    println!("   Conditions: {}", contract.conditions.len());
    println!();

    // Step 6: Show keys (for demo purposes only - in production, secure these!)
    println!("6. Participant Keys (SAVE SECURELY!):");
    println!("   Buyer Private Key: {}", buyer_key.to_wif());
    println!("   Seller Private Key: {}", seller_key.to_wif());
    println!("   Arbiter Private Key: {}", arbiter_key.to_wif());
    println!();

    // Step 7: Verify audit log
    println!("7. Audit Log:");
    let entries = manager.audit().get_all_entries();
    println!("   Total entries: {}", entries.len());
    if let Some(last) = entries.last() {
        println!("   Last event: {:?}", last.event_type);
        println!("   Timestamp: {}", last.timestamp.to_rfc3339());
    }
    println!();

    println!("=== Setup Complete ===");
    println!("\nNext steps:");
    println!("1. Send testnet BTC to the escrow address above");
    println!("2. Once funded, conditions can be evaluated");
    println!("3. After conditions are satisfied, collect signatures for release");
    println!("4. Broadcast the release transaction");
}