veil/demo.sh
2026-06-22 23:56:08 +03:00

147 lines
7.5 KiB
Bash
Executable File

#!/bin/bash
set -e
echo "๐Ÿš€ Starting Privacy Pool Demo..."
NETWORK=testnet # testnet, local
TOKEN_ADDRESS=CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC # XLM token address on testnet
# Clean up old test files
echo "๐Ÿงน Cleaning up old test files..."
rm -f demo_coin.json demo_state.json demo_association.json vk_hex.txt proof_hex.txt public_hex.txt withdrawal_input.json circuits/witness.wtns circuits/proof.json circuits/public.json 2>/dev/null || true
# Check prerequisites
echo "๐Ÿ” Checking prerequisites..."
command -v jq >/dev/null 2>&1 || { echo "โŒ Error: jq is required but not installed. Please install jq first."; exit 1; }
command -v stellar >/dev/null 2>&1 || { echo "โŒ Error: stellar CLI is required but not installed."; exit 1; }
# Fund demo_user account if needed
echo "๐Ÿฆ Ensuring demo_user account is funded..."
if ! stellar keys ls 2>/dev/null | grep -q "^demo_user$"; then
echo " Generating demo_user key..."
stellar keys generate demo_user > /dev/null 2>&1
fi
echo " Funding demo_user account..."
stellar keys fund demo_user --network $NETWORK 2>&1 | grep -q "funded" && echo " โœ… Account funded" || echo "โš ๏ธ demo_user may already be funded"
# Step 1: Deploy contract
echo "๐Ÿ“ฆ Deploying contract..."
cargo build --target wasm32v1-none --release -p privacy-pools || { echo "โŒ Error: Failed to build contract"; exit 1; }
stellar contract optimize --wasm target/wasm32v1-none/release/privacy_pools.wasm --wasm-out target/wasm32v1-none/release/privacy_pools.optimized.wasm || { echo "โŒ Error: Failed to optimize WASM"; exit 1; }
# Convert verification key to hex format and extract it
echo "๐Ÿ”‘ Converting verification key..."
cargo run --bin stellar-circom2soroban vk circuits/output/main_verification_key.json > vk_hex.txt || { echo "โŒ Error: Failed to convert verification key"; exit 1; }
VK_HEX=$(cat vk_hex.txt | grep -o '[0-9a-f]*$')
if [ -z "$VK_HEX" ]; then
echo "โŒ Error: Failed to extract verification key hex"
exit 1
fi
echo "๐Ÿš€ Deploying contract to $NETWORK..."
DEPLOY_OUTPUT=$(stellar contract deploy --wasm target/wasm32v1-none/release/privacy_pools.optimized.wasm --source demo_user --network $NETWORK -- --vk_bytes $VK_HEX --token_address $TOKEN_ADDRESS --admin demo_user 2>&1)
if [ $? -ne 0 ]; then
echo "โŒ Error: Contract deployment failed"
echo "$DEPLOY_OUTPUT"
exit 1
fi
CONTRACT_ID=$(echo "$DEPLOY_OUTPUT" | grep -oE 'C[A-Z0-9]{55}' | tail -1)
if [ -z "$CONTRACT_ID" ]; then
echo "โŒ Error: Failed to extract contract ID from deployment"
echo "Deployment output:"
echo "$DEPLOY_OUTPUT"
exit 1
fi
echo "โœ… Contract deployed with ID: $CONTRACT_ID"
# Check who the admin is
echo "๐Ÿ‘ค Checking contract admin..."
stellar contract invoke --id $CONTRACT_ID --source demo_user --network $NETWORK -- get_admin || { echo "โŒ Error: Failed to get admin"; exit 1; }
# Step 2: Generate coin
echo "๐Ÿช™ Generating coin..."
cargo run --bin stellar-coinutils generate demo_pool -o demo_coin.json || { echo "โŒ Error: Failed to generate coin"; exit 1; }
COMMITMENT_HEX=$(cat demo_coin.json | jq -r '.commitment_hex' | sed 's/^0x//')
if [ -z "$COMMITMENT_HEX" ]; then
echo "โŒ Error: Failed to extract commitment hex"
exit 1
fi
echo "Generated coin with commitment: $COMMITMENT_HEX"
# Step 3: Deposit
echo "๐Ÿ’ฐ Depositing coin..."
stellar contract invoke --id $CONTRACT_ID --source demo_user --network $NETWORK -- deposit --from demo_user --commitment $COMMITMENT_HEX || { echo "โŒ Error: Failed to deposit coin"; exit 1; }
echo "Deposit successful!"
# Step 4: Check balance
echo "๐Ÿ“Š Checking balance..."
stellar contract invoke --id $CONTRACT_ID --source demo_user --network $NETWORK -- get_balance || { echo "โŒ Error: Failed to get balance"; exit 1; }
# Step 5: Create state file and association set
echo "๐Ÿ“‹ Creating state file..."
COMMITMENT=$(cat demo_coin.json | jq -r '.coin.commitment')
echo "{
\"commitments\": [
\"$COMMITMENT\"
],
\"scope\": \"demo_pool\"
}" > demo_state.json
echo "๐Ÿท๏ธ Creating association set..."
LABEL=$(cat demo_coin.json | jq -r '.coin.label')
cargo run --bin stellar-coinutils update-association demo_association.json "$LABEL" || { echo "โŒ Error: Failed to create association set"; exit 1; }
# Extract association root from the association set and set it in the contract
echo "๐Ÿ”— Setting association root in contract..."
ASSOCIATION_ROOT_DECIMAL=$(cat demo_association.json | jq -r '.root')
if [ -z "$ASSOCIATION_ROOT_DECIMAL" ] || [ "$ASSOCIATION_ROOT_DECIMAL" = "null" ]; then
echo "โŒ Error: Failed to extract association root from association set"
exit 1
fi
# Convert decimal string to hex using Python (since it handles big integers well)
ASSOCIATION_ROOT_HEX=$(python3 -c "
import sys
decimal_str = sys.argv[1]
# Convert decimal to hex
hex_str = hex(int(decimal_str))[2:] # Remove '0x' prefix
# Pad to 64 hex characters (32 bytes)
padded_hex = hex_str.zfill(64)
print(padded_hex)
" "$ASSOCIATION_ROOT_DECIMAL")
if [ -z "$ASSOCIATION_ROOT_HEX" ]; then
echo "โŒ Error: Failed to convert association root to hex format"
exit 1
fi
echo "๐Ÿ” Debug: Association root decimal: $ASSOCIATION_ROOT_DECIMAL"
echo "๐Ÿ” Debug: Association root hex: $ASSOCIATION_ROOT_HEX"
# Note: Only the admin (contract deployer) can set association root
stellar contract invoke --id $CONTRACT_ID --source demo_user --network $NETWORK -- set_association_root --caller demo_user --association_root $ASSOCIATION_ROOT_HEX || { echo "โŒ Error: Failed to set association root"; exit 1; }
echo "โœ… Association root set successfully"
echo "๐Ÿ” Creating withdrawal proof..."
cargo run --bin stellar-coinutils withdraw demo_coin.json demo_state.json demo_association.json -o withdrawal_input.json || { echo "โŒ Error: Failed to create withdrawal input"; exit 1; }
echo "๐Ÿ“ Generating witness and proof..."
cd circuits
node build/main_js/generate_witness.js build/main_js/main.wasm ../withdrawal_input.json witness.wtns || { echo "โŒ Error: Failed to generate witness"; exit 1; }
snarkjs groth16 prove output/main_final.zkey witness.wtns proof.json public.json || { echo "โŒ Error: Failed to generate proof"; exit 1; }
cd ..
echo "๐Ÿ”„ Converting proof for Soroban..."
cargo run --bin stellar-circom2soroban proof circuits/proof.json > proof_hex.txt || { echo "โŒ Error: Failed to convert proof"; exit 1; }
cargo run --bin stellar-circom2soroban public circuits/public.json > public_hex.txt || { echo "โŒ Error: Failed to convert public signals"; exit 1; }
PROOF_HEX=$(sed -n '/^Proof Hex encoding:/{n;p;}' proof_hex.txt | tr -d '[:space:]' | sed -E 's/^0x//i')
PUBLIC_HEX=$(sed -n '/^Public signals Hex encoding:/{n;p;}' public_hex.txt | tr -d '[:space:]' | sed -E 's/^0x//i')
if [ -z "$PROOF_HEX" ] || [ -z "$PUBLIC_HEX" ]; then
echo "โŒ Error: Failed to extract proof or public signals"
exit 1
fi
echo "๐Ÿ” Debug: Proof hex length: ${#PROOF_HEX}"
echo "๐Ÿ” Debug: Public hex length: ${#PUBLIC_HEX}"
# Step 6: Withdraw
echo "๐Ÿ’ธ Withdrawing coin..."
stellar contract invoke --id $CONTRACT_ID --source demo_user --network $NETWORK -- withdraw --to demo_user --proof_bytes "$PROOF_HEX" --pub_signals_bytes "$PUBLIC_HEX" || { echo "โŒ Error: Failed to withdraw coin"; exit 1; }
echo "Withdrawal successful!"
# Step 7: Verify
echo "โœ… Verifying withdrawal..."
stellar contract invoke --id $CONTRACT_ID --source demo_user --network $NETWORK -- get_nullifiers || { echo "โŒ Error: Failed to get nullifiers"; exit 1; }
stellar contract invoke --id $CONTRACT_ID --source demo_user --network $NETWORK -- get_balance || { echo "โŒ Error: Failed to get final balance"; exit 1; }
echo "๐ŸŽ‰ Demo completed successfully!"