Skip to content

trustflow-protocol/trustflow-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

71 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ”§ TrustFlow Core β€” Backend API

License: MIT

The off-chain backbone of the TrustFlow Protocol.

TrustFlow Core is the backend API service that powers off-chain logic for the TrustFlow gig-economy platform. Built with NestJS and TypeScript, it bridges the Stellar/Soroban blockchain with real-world application features β€” handling authentication, escrow state management, webhook delivery, and Prometheus-grade observability.


✨ Core Features

  • πŸ” JWT Authentication with Wallet Signatures: Secure wallet-based auth using Stellar signature verification. Users authenticate by signing a cryptographic challenge with their Freighter wallet, proving ownership without exposing private keys.
  • πŸ’Ό Escrow Management: Full CRUD API for escrow entities β€” creation, funding, milestone tracking.
  • 🌐 Stellar Integration: Native Horizon and Soroban RPC helpers for on-chain reads and writes.
  • πŸ”” Webhook Engine: Event-driven webhook dispatch with automatic retry logic.
  • πŸ“Š Monitoring & Metrics: Built-in Prometheus metrics, health checks, and alerting helpers.

πŸ—‚οΈ Project Structure

backend/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ auth/               # JWT auth β€” controller, guard, service, strategy, DTOs
β”‚   β”‚   β”œβ”€β”€ dto/            # Request/response DTOs for validation
β”‚   β”‚   └── auth.module.ts # Auth module configuration
β”‚   β”œβ”€β”€ escrow/             # Escrow API β€” controller, service, DTOs, entity
β”‚   β”œβ”€β”€ stellar/            # Stellar helpers β€” Horizon, Soroban, config, service
β”‚   β”œβ”€β”€ webhook/            # Webhook dispatch β€” controller, service, retry helper
β”‚   β”œβ”€β”€ monitoring/         # Health checks, metrics, Prometheus helpers
β”‚   └── main.ts             # App entry point

πŸš€ Getting Started

Prerequisites

  • Node.js >= 20
  • A Stellar RPC endpoint (testnet or mainnet)
  • Freighter wallet (for client-side wallet signature testing)

Installation

npm install

Environment Setup

Copy the example env file and fill in your values:

cp .env.example .env

Key variables:

JWT_SECRET=your-secret
STELLAR_RPC_URL=https://soroban-testnet.stellar.org
HORIZON_URL=https://horizon-testnet.stellar.org

Running

# Development
cd backend
npm run dev

# Production
npm run build && npm start

# Run tests
npm test

# Run CI checks locally
./scripts/ci-check.sh

See Backend Setup Instructions for detailed development workflow.


πŸ“– API Modules

πŸ“š Interactive API Documentation

Swagger UI: http://localhost:3001/api/docs
OpenAPI JSON: http://localhost:3001/api/docs-json

Full guide: API Documentation

Auth (/auth)

  • GET /auth/challenge β€” Get authentication challenge for wallet signing
  • POST /auth/verify β€” Verify wallet signature, returns JWT
  • JWT Guard protects all downstream routes.

Authentication Flow

  1. Request Challenge: Client requests a cryptographic challenge for their wallet address
  2. Sign Challenge: User signs the challenge with their Freighter wallet
  3. Verify & Get Token: Client sends the signature to verify and receive a JWT token
  4. Use Token: Include JWT in Authorization header for authenticated requests

Example Usage

# 1. Get challenge
curl "http://localhost:3001/auth/challenge?address=GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"

# 2. Sign challenge with Freighter wallet (client-side)

# 3. Verify signature and get JWT
curl -X POST http://localhost:3001/auth/verify \
  -H "Content-Type: application/json" \
  -d '{"address":"G...","signature":"..."}'

# 4. Use JWT for authenticated requests
curl http://localhost:3001/escrows \
  -H "Authorization: Bearer <jwt-token>"

Escrow (/escrow)

  • POST /escrow β€” Create a new escrow vault.
  • GET /escrow/:id β€” Fetch escrow state and milestone details.
  • GET /escrow/depositor/:address β€” Get all escrows by depositor
  • POST /escrow/:id/release β€” Approve a milestone tranche.
  • POST /escrow/:id/dispute β€” Raise a dispute (triggers Discord notification).

Webhooks (/webhook)

  • POST /webhook β€” Register a webhook endpoint.
  • DELETE /webhook/:id β€” Unregister a webhook.
  • Automatic retry logic handles delivery failures gracefully.
  • Discord Integration: Automatically notifies a Discord channel when disputes need jurors. Setup Guide

Monitoring (/health, /metrics)

  • GET /health β€” Liveness and readiness probe.
  • GET /metrics β€” Prometheus-compatible metrics endpoint.

πŸ›‘οΈ Security

  • Wallet Signature Verification: Uses @stellar/stellar-sdk for cryptographic signature verification
  • Challenge Expiration: Challenges expire after 5 minutes to prevent replay attacks
  • One-Time Use: Each challenge can only be used once
  • JWT Expiration: Tokens expire after 24 hours
  • Address Validation: Validates Stellar public key format (G-prefixed, 56 characters)
  • Input Validation: Uses class-validator DTOs on all endpoints
  • Guard Middleware: All protected routes require valid JWT via JwtAuthGuard
  • Environment Secrets: Never logged or exposed in responses

For detailed authentication implementation documentation, see AUTH_IMPLEMENTATION.md.


πŸ”„ CI/CD

The project uses GitHub Actions for continuous integration:

  • βœ… Automated Testing: Runs on every PR affecting backend code
  • βœ… Multi-Version Testing: Tests against Node.js 18.x and 20.x
  • βœ… Code Quality: ESLint and Prettier checks
  • βœ… Type Safety: TypeScript compilation and type checking
  • βœ… Fast Builds: Target runtime under 3 minutes
  • βœ… Branch Protection: PRs blocked on failing tests

See CI/CD Documentation for details.

Local CI Check:

cd backend && ./scripts/ci-check.sh

πŸ—ΊοΈ Roadmap

  • JWT Authentication with Wallet Signatures: Implemented Stellar signature verification
  • GraphQL Layer: Optional GraphQL gateway over REST endpoints.
  • Rate Limiting: Per-wallet throttling on auth and escrow routes.
  • Event Sourcing: Full audit log for all escrow state transitions.
  • Multi-network Support: Seamless mainnet/testnet switching via config.
  • Token Refresh: Implement refresh token mechanism for better UX
  • Redis Integration: Use Redis for distributed challenge storage

🀝 Community & Support


Securing the future of work, one transaction at a time.


πŸ“œ License

MIT License. Copyright (c) 2026 TrustFlow Protocol.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors