Skip to content

Latest commit

 

History

History
256 lines (194 loc) · 7.83 KB

File metadata and controls

256 lines (194 loc) · 7.83 KB

Stellar Campaign Wallet Architecture - Implementation Summary

Overview

This implementation provides a complete on-chain wallet architecture for CrowdPay campaigns, with secure key management, comprehensive API endpoints, and full documentation for developers and operators.

What Was Implemented

1. Secure Key Management (backend/src/services/walletService.js)

  • AES-256-GCM encryption for campaign wallet private keys
  • Encryption key stored in environment variable WALLET_ENCRYPTION_KEY
  • Each encrypted secret includes IV, auth tag, and ciphertext for maximum security
  • Functions: encryptSecret(), decryptSecret()

2. Database Schema Updates

Migration: backend/db/migrations/001_add_campaign_wallet_secrets.sql

  • Added wallet_secret_encrypted column to campaigns table
  • Added index on wallet_public_key for efficient lookups

Updated Schema: backend/db/schema.sql

  • Campaigns table now stores encrypted wallet secrets
  • Maintains backward compatibility with existing campaigns

3. Enhanced Stellar Service (backend/src/services/stellarService.js)

Added three new functions:

  • recoverWalletFromSecret(secret): Reconstruct keypair from encrypted secret
  • getWalletTransactionHistory(publicKey, limit): Retrieve transaction history for audit
  • getWalletPayments(publicKey, limit): Get detailed payment operations

4. Wallet Management API (backend/src/routes/wallets.js)

New endpoints for campaign wallet management:

  • GET /api/wallets/:campaignId/config - View multisig configuration
  • GET /api/wallets/:campaignId/transactions - Transaction history
  • GET /api/wallets/:campaignId/payments - Payment audit trail
  • POST /api/wallets/:campaignId/recover - Recover wallet from encrypted secret

All endpoints require authentication and verify campaign creator ownership.

5. Updated Campaign Creation (backend/src/routes/campaigns.js)

  • Campaign creation now encrypts and stores wallet secrets
  • Secrets stored securely in database for recovery purposes
  • Maintains existing multisig and trustline setup

6. Comprehensive Documentation

WALLET_ARCHITECTURE.md (399 lines):

  • Complete architecture overview
  • Wallet lifecycle phases (creation → contribution → withdrawal → recovery)
  • Security considerations and best practices
  • API reference with examples
  • Troubleshooting guide
  • Future enhancement roadmap

OPERATOR_GUIDE.md (556 lines):

  • Step-by-step setup instructions
  • Daily operations procedures
  • Maintenance tasks (weekly, monthly, quarterly)
  • Troubleshooting common issues
  • Security and incident response procedures
  • Monitoring and alerting setup
  • Emergency procedures

API.md (updated):

  • Added wallet management endpoint documentation
  • Request/response examples
  • Error codes and handling

Key Features

Security

AES-256-GCM encryption for wallet secrets
Multisig control (creator + platform signatures required)
Disabled master keys after wallet setup
Access control on all wallet endpoints
Audit trail for all wallet operations

Auditability

Transaction history retrieval
Payment operations tracking
On-chain verification via Stellar Horizon
Database records for all contributions and withdrawals

Developer Experience

Clear API documentation with examples
Step-by-step guides for common operations
Troubleshooting procedures for common issues
Testing instructions for testnet and mainnet

Operator Experience

Setup procedures for initial deployment
Daily operations checklists
Maintenance schedules (weekly, monthly, quarterly)
Emergency procedures for incident response
Monitoring recommendations with key metrics

File Changes

New Files Created

crowdpay/backend/src/services/walletService.js
crowdpay/backend/src/routes/wallets.js
crowdpay/backend/db/migrations/001_add_campaign_wallet_secrets.sql
crowdpay/WALLET_ARCHITECTURE.md
crowdpay/OPERATOR_GUIDE.md

Modified Files

crowdpay/backend/db/schema.sql
crowdpay/backend/src/services/stellarService.js
crowdpay/backend/src/routes/campaigns.js
crowdpay/backend/src/index.js
crowdpay/backend/.env.example
crowdpay/backend/API.md

Quick Start

For Developers

  1. Read the architecture:

    cat WALLET_ARCHITECTURE.md
  2. Setup environment:

    # Generate encryption key
    node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"
    
    # Add to .env
    echo "WALLET_ENCRYPTION_KEY=<generated_key>" >> backend/.env
  3. Run migrations:

    psql $DATABASE_URL -f backend/db/migrations/001_add_campaign_wallet_secrets.sql
  4. Test wallet creation:

    curl -X POST http://localhost:3001/api/campaigns \
      -H "Authorization: Bearer $TOKEN" \
      -H "Content-Type: application/json" \
      -d '{"title":"Test","target_amount":"100","asset_type":"USDC"}'

For Operators

  1. Read the operator guide:

    cat OPERATOR_GUIDE.md
  2. Setup platform account:

    cd contracts/stellar
    node campaignWallet.js --setup-platform
  3. Configure monitoring:

    • Set up alerts for platform balance < 50 XLM
    • Monitor ledger monitor lag
    • Track withdrawal processing time
  4. Backup encryption key:

    • Store WALLET_ENCRYPTION_KEY in secure vault
    • Create offline backup
    • Document recovery procedures

Acceptance Criteria Status

Each new campaign triggers a unique Stellar account setup

  • Implemented in createCampaignWallet() function
  • Automatic wallet creation on campaign POST
  • Encrypted secret storage in database

Developers have step-by-step documentation for wallet creation

  • WALLET_ARCHITECTURE.md covers complete lifecycle
  • API.md provides endpoint documentation
  • Code comments explain each step

Fund flows and account controls are fully auditable

  • Transaction history endpoint
  • Payment operations endpoint
  • On-chain verification via Stellar Horizon
  • Database audit trail

Security Recommendations

Development

  • Use testnet for all development and testing
  • Never commit encryption keys to version control
  • Test recovery procedures regularly

Production

  • Use AWS KMS, Google Cloud KMS, or Azure Key Vault for encryption keys
  • Implement key rotation every 90 days
  • Enable comprehensive audit logging
  • Set up monitoring and alerting
  • Restrict wallet recovery endpoint to admin users only
  • Use separate platform accounts for testnet and mainnet

Testing Checklist

  • Test campaign creation with wallet encryption
  • Test wallet configuration retrieval
  • Test transaction history endpoint
  • Test payment history endpoint
  • Test wallet recovery endpoint
  • Test encryption/decryption with sample keys
  • Test multisig withdrawal flow
  • Verify on-chain wallet configuration
  • Test error handling for missing encryption key
  • Test access control (non-creator cannot access wallet)

Next Steps

  1. Run the test suite to verify all functionality
  2. Deploy to testnet and create test campaigns
  3. Verify wallet creation on Stellar testnet
  4. Test complete contribution flow with encrypted wallets
  5. Test withdrawal flow with multisig
  6. Review security with security team
  7. Setup monitoring and alerting
  8. Train operators on procedures
  9. Document mainnet deployment plan
  10. Plan key rotation schedule

Support

  • Architecture Questions: See WALLET_ARCHITECTURE.md
  • Operational Issues: See OPERATOR_GUIDE.md
  • API Usage: See backend/API.md
  • Stellar Network: https://developers.stellar.org/

Version

  • Implementation Date: 2026-04-24
  • Version: 1.0
  • Status: Ready for testing

All acceptance criteria met. Implementation complete and documented.