Skip to content

Manuel1234477/Stellar-Micro-Donation-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

415 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Stellar Micro-Donation API

A Node.js/Express API for managing micro-donations on the Stellar blockchain network. Supports one-time donations, recurring donation schedules, wallet management, and donation analytics.

πŸ“‹ Table of Contents

✨ Features

  • One-Time Donations: Create and verify donations on Stellar testnet/mainnet
  • Recurring Donations: Schedule automated recurring donations (daily, weekly, monthly)
  • Wallet Management: Track wallets and query transaction history
  • Analytics: Get donation statistics and summaries
  • API Key Rotation: Zero-downtime key rotation with versioning and graceful deprecation
  • Mock Mode: Development mode with simulated Stellar operations
  • Debug Mode: Configurable verbose logging for local development troubleshooting
  • Failure Simulation: Comprehensive network failure testing for robust error handling
  • Automated Scheduler: Background service for executing recurring donations
  • Rate Limiting: Protection against abuse with configurable request limits on donation endpoints
  • Idempotency: Prevent duplicate transactions with idempotency key support
  • Sensitive Data Masking: Automatic masking of secrets, API keys, and private values in all logs

πŸ—οΈ Architecture

High-Level Overview

                                     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                     β”‚   Clients   β”‚
                                     β”‚ (Web/Mobile)β”‚
                                     β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚ HTTP/HTTPS
                                            β–Ό
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚      Express.js API Layer       β”‚
                            β”‚  /donations  /wallets  /stream  β”‚
                            β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚
                                            β–Ό
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚       Service Layer             β”‚
                            β”‚  Stellar Service | Scheduler    β”‚
                            β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                                            β”‚
                                β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                                β–Ό              β–Ό            β–Ό
                            β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
                            β”‚ SQLite   β”‚   β”‚ Stellar  β”‚  β”‚ Horizon β”‚
                            β”‚ Database β”‚   β”‚ Network  β”‚  β”‚   API   β”‚
                            β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

For detailed architecture documentation, see:

Key Components

  • API Layer: Express.js routes handling HTTP requests
  • Service Layer: Business logic and Stellar blockchain integration
  • Data Layer: SQLite database for persistent storage
  • Scheduler: Background service for recurring donations (runs every 60s)

πŸš€ Getting Started

Prerequisites

  • Node.js (v14 or higher)
  • npm or yarn
  • SQLite3

Installation

  1. Clone the repository:
git clone https://github.com/yourusername/Stellar-Micro-Donation-API.git
cd Stellar-Micro-Donation-API
  1. Install dependencies:
npm install
  1. Initialize the database:
npm run init-db
  1. Start the server:
npm start

The API will be available at http://localhost:3000

Development Mode

For development with auto-reload:

npm run dev

Debug Mode

Enable verbose logging for troubleshooting:

# Add to .env file
DEBUG_MODE=true

# Start server with debug logging
npm start

See Debug Mode Documentation for details.

βš™οΈ Runtime Configuration

For comprehensive documentation of runtime environment assumptions, timeouts, retry logic, persistence requirements, background services, and operational procedures, see:

RUNTIME_ASSUMPTIONS.md - Complete runtime environment documentation

This document covers:

  • Network Configuration: Timeouts, retry logic, external dependencies
  • Persistence Layer: Database paths, storage requirements, backup strategies
  • Background Services: Scheduler intervals, service behavior, lifecycle management
  • Resource Requirements: Memory, disk, CPU, and network requirements by deployment scale
  • Configuration Reference: All environment variables with validation rules
  • Production Deployment: Warnings, readiness checklist, monitoring recommendations
  • Operational Procedures: Graceful shutdown, troubleshooting guide

πŸ“‘ API Endpoints

Quick Reference

For detailed request/response examples with error handling, see the Complete API Examples Documentation.

Donations

  • POST /donations - Create a new donation
  • GET /donations - List all donations
  • GET /donations/recent?limit=10 - Get recent donations
  • GET /donations/:id - Get specific donation
  • GET /donations/limits - Get donation amount limits
  • POST /donations/verify - Verify transaction on blockchain
  • PATCH /donations/:id/status - Update donation status

Wallets

  • POST /wallets - Create wallet metadata
  • GET /wallets - List all wallets
  • GET /wallets/:id - Get specific wallet
  • GET /wallets/:publicKey/transactions - Get all transactions for a wallet
  • PATCH /wallets/:id - Update wallet metadata

Recurring Donations (Stream)

  • POST /stream/create - Create recurring donation schedule
  • GET /stream/schedules - List all schedules
  • GET /stream/schedules/:id - Get specific schedule
  • DELETE /stream/schedules/:id - Cancel schedule

Statistics

  • GET /stats/daily - Get daily donation statistics
  • GET /stats/weekly - Get weekly donation statistics
  • GET /stats/summary - Get summary analytics
  • GET /stats/donors - Get donor statistics
  • GET /stats/recipients - Get recipient statistics
  • GET /stats/analytics-fees - Get analytics fee summary
  • GET /stats/wallet/:walletAddress/analytics - Get wallet analytics

Transactions

  • GET /transactions - Get paginated transactions
  • POST /transactions/sync - Sync wallet transactions from Stellar network

Health Check

  • GET /health - API health status

πŸ—„οΈ Database Schema

Users Table

CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    publicKey TEXT NOT NULL UNIQUE,
    createdAt DATETIME DEFAULT CURRENT_TIMESTAMP
);

Transactions Table

CREATE TABLE transactions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    senderId INTEGER NOT NULL,
    receiverId INTEGER NOT NULL,
    amount REAL NOT NULL,
    memo TEXT,
    timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (senderId) REFERENCES users(id),
    FOREIGN KEY (receiverId) REFERENCES users(id)
);

Recurring Donations Table

CREATE TABLE recurring_donations (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    donorId INTEGER NOT NULL,
    recipientId INTEGER NOT NULL,
    amount REAL NOT NULL,
    frequency TEXT NOT NULL,
    nextExecutionDate DATETIME NOT NULL,
    status TEXT DEFAULT 'active',
    executionCount INTEGER DEFAULT 0,
    FOREIGN KEY (donorId) REFERENCES users(id),
    FOREIGN KEY (recipientId) REFERENCES users(id)
);

πŸ› οΈ Development

Project Structure

Stellar-Micro-Donation-API/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ config/           # Configuration files
β”‚   β”œβ”€β”€ middleware/       # Express middleware
β”‚   β”œβ”€β”€ routes/           # API route handlers
β”‚   β”‚   β”œβ”€β”€ app.js
β”‚   β”‚   β”œβ”€β”€ donation.js
β”‚   β”‚   β”œβ”€β”€ wallet.js
β”‚   β”‚   β”œβ”€β”€ stream.js
β”‚   β”‚   β”œβ”€β”€ transaction.js
β”‚   β”‚   └── stats.js
β”‚   β”œβ”€β”€ services/         # Business logic services
β”‚   β”‚   β”œβ”€β”€ StellarService.js
β”‚   β”‚   β”œβ”€β”€ MockStellarService.js
β”‚   β”‚   └── RecurringDonationScheduler.js
β”‚   β”œβ”€β”€ scripts/          # Database scripts
β”‚   β”‚   └── initDB.js
β”‚   └── utils/            # Utility functions
β”‚       β”œβ”€β”€ database.js
β”‚       └── permissions.js
β”œβ”€β”€ data/                 # SQLite database files
β”œβ”€β”€ docs/                 # Documentation
β”œβ”€β”€ tests/                # Test files
└── package.json

API Key Permissions

The API uses role-based access control (RBAC) with three roles:

Role Permissions Use Case
admin All permissions (*) System administration, API key management
user donations:, wallets:, stream:, stats:read, transactions: Standard API operations
guest donations:read, stats:read Read-only public access

For detailed permission audit and security hardening, see API Key Permissions Audit.

Environment Variables

Create a .env file in the project root:

STELLAR_NETWORK=testnet
HORIZON_URL=https://horizon-testnet.stellar.org
PORT=3000

For API key authentication, use the new database-backed system (recommended):

npm run keys:create -- --name "My Key" --role user --expires 365

Or use legacy environment-based keys (deprecated):

API_KEYS=your-api-key-here

Required at startup:

  • API_KEYS (legacy method, or use database-backed keys)
  • ENCRYPTION_KEY (required only when NODE_ENV=production)

Validated at startup (if provided):

  • PORT must be an integer from 1 to 65535
  • STELLAR_NETWORK must be one of testnet, mainnet, futurenet
  • MOCK_STELLAR must be true or false
  • HORIZON_URL must be a valid URL

API Key Management

The API supports zero-downtime key rotation. See:

Quick commands:

npm run keys:create -- --name "My Key" --role user --expires 365
npm run keys:list
npm run keys -- deprecate --id 1
npm run keys -- revoke --id 2

πŸ§ͺ Testing

Run Tests

npm test

Test Isolation

All tests are fully isolated and can run independently in any order:

# Run tests in random order to verify isolation
npm test -- --randomize

# Run with specific seed to reproduce order
npm test -- --randomize --seed=123456

For detailed information about test isolation, see Test Isolation Guide.

Run Tests with Coverage

npm run test:coverage

This generates:

  • Terminal coverage summary
  • HTML report at coverage/lcov-report/index.html
  • LCOV report for CI/CD integration
  • JSON summary for programmatic access

Check Coverage Thresholds

npm run check-coverage

Validates that coverage meets minimum thresholds:

  • Branches: 30%
  • Functions: 30%
  • Lines: 30%
  • Statements: 30%

View Coverage Report

After running coverage, open the HTML report:

# macOS
open coverage/lcov-report/index.html

# Windows
start coverage/lcov-report/index.html

# Linux
xdg-open coverage/lcov-report/index.html

Coverage Enforcement

Coverage is automatically enforced in CI/CD:

  • βœ… PRs must meet minimum 30% coverage thresholds
  • ❌ Builds fail if coverage drops below thresholds
  • πŸ“Š Coverage reports uploaded as artifacts (30-day retention)

For detailed coverage documentation, see Coverage Guide.

Run Specific Tests

npm test -- tests/integration.test.js

Run Integration Tests for Donation Routes

Comprehensive end-to-end tests for all donation endpoints:

npm test tests/donation-routes-integration.test.js

Coverage: 60+ test cases covering:

  • All 7 donation endpoints
  • Success and failure scenarios
  • Validation, authentication, idempotency
  • Rate limiting and error handling
  • No live Stellar network required (uses MockStellarService)

For detailed information, see Donation Routes Integration Tests.

Test Recurring Donations

node test-recurring-donations.js

Test Failure Scenarios

The project includes comprehensive failure simulation for testing network errors and retry logic:

# Run failure simulation tests
npm test tests/stellar-network-failures.test.js

# Run retry logic tests
npm test tests/stellar-retry-logic.test.js

Failure Types Tested:

  • Timeouts and network errors
  • Service unavailability
  • Transaction failures (bad sequence, insufficient fee)
  • Rate limiting
  • Partial responses

For detailed information, see Stellar Failure Simulation Guide.

�️ Troubleshooting

Having issues? We've got you covered!

Quick Fixes

  • Server won't start? Check environment: npm run validate-env
  • Tests failing? Clear cache: npx jest --clearCache
  • Port in use? Kill process: kill -9 $(lsof -ti:3000)
  • Dependencies broken? Fresh install: rm -rf node_modules package-lock.json && npm install

Common Issues

  • Missing .env file β†’ cp .env.example .env
  • API keys required β†’ Add API_KEYS=dev_key_123 to .env
  • Use mock mode for development β†’ MOCK_STELLAR=true

Get Help

Debug Mode

Enable detailed logging for troubleshooting:

DEBUG_MODE=true LOG_VERBOSE=true npm start

πŸ”§ Configuration

Stellar Network

The API can work with both Stellar testnet and mainnet. Configure via environment variables:

  • Testnet (default): For development and testing
  • Mainnet: For production use

Recurring Donation Scheduler

The scheduler runs automatically when the server starts and checks for due donations every 60 seconds. It can be configured in src/services/RecurringDonationScheduler.js.

πŸ“š Documentation

Getting Started

Technical Documentation

Development & Deployment

🀝 Contributing

New to the project? Check out our Onboarding Checklist for a step-by-step guide to getting started!

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes and add tests
  4. Run tests locally (npm test)
  5. Check coverage (npm run test:coverage)
  6. Ensure coverage thresholds are met (npm run check-coverage)
  7. Commit your changes (git commit -m 'Add amazing feature')
  8. Push to the branch (git push origin feature/amazing-feature)
  9. Open a Pull Request

Note: All CI checks must pass before merge, including:

  • βœ… All tests passing
  • βœ… Coverage thresholds met (30% minimum)
  • βœ… Linting checks passed
  • βœ… Security checks passed

See Branch Protection and Coverage Guide for details.

πŸ“ License

This project is licensed under the MIT License.

πŸ™ Acknowledgments

πŸ“ž Support

For issues and questions:


Built with ❀️ using Node.js and Stellar

πŸ“š Documentation

For comprehensive documentation, see the Documentation Index.

Key Documentation

About

No description, website, or topics provided.

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages