Skip to content

onihani/kudifi-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

21 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Kudifi Backend

A comprehensive backend service for Kudifi - Ghana's first USSD-based crypto wallet platform. Built with Elysia and Bun, it provides seamless blockchain wallet management, token transfers, and mobile money integration through simple USSD flows accessible on any mobile phone.

🌐 Production URL: https://kudifi-backend.fly.dev/

🌟 Key Features

  • 🏦 Smart Wallet Management: Auto-created wallets with account abstraction via Thirdweb
  • β›½ Gas-Free Transactions: Sponsored gas fees for seamless user experience
  • πŸ“± USSD Interface: Complete crypto wallet functionality via USSD codes
  • πŸ” PIN Security: 4-digit PIN protection with retry limits
  • πŸ’ APE Chain Exclusive: Built exclusively on APE Chain for optimal performance
  • πŸ’Έ Token Support: APE, USDT, USDC with real-time pricing
  • πŸ‡¬πŸ‡­ Ghana-Focused: Ghanaian phone number validation and GHS pricing
  • πŸ’³ Mobile Money Integration: Built-in support for withdrawal to MoMo (coming soon)

πŸ“± USSD Flow Overview

Users interact with Kudifi through intuitive USSD menus:

New User Journey

*XXX# β†’ Welcome to Kudifi! πŸš€
       β†’ 1. Create wallet
       β†’ βœ… Wallet created
       β†’ Set 4-digit PIN
       β†’ βœ… Ready to use!

Main Menu

Welcome to Kudifi
1. Send tokens     β†’ Choose token β†’ Enter phone β†’ Confirm β†’ Amount β†’ PIN β†’ βœ… Sent
2. Check balance   β†’ Choose token β†’ πŸ’° Balance shown with GHS value
3. Buy tokens      β†’ Choose token β†’ Enter GHS amount β†’ βœ… Purchase initiated
4. View Rewards    β†’ 🎁 Coming soon!
5. View Wallet     β†’ πŸ‘› Smart wallet address displayed
6. Withdraw MoMo   β†’ πŸ“± Coming soon!
7. Donate to Team  β†’ Choose token β†’ Amount β†’ PIN β†’ ❀️ Thank you!

Send Tokens Flow

Send APE Tokens
└─ Enter phone (054xxxxxxxx)
   └─ Confirm recipient
      └─ Enter amount
         └─ Confirm transaction  
            └─ Enter PIN
               └─ βœ… Transaction successful!

πŸ—οΈ Backend Architecture

Framework & Runtime

  • Elysia: Modern web framework for Bun
  • Bun: Fast JavaScript runtime and package manager
  • TypeScript: Full type safety throughout the codebase

Blockchain Integration

  • Thirdweb: Smart wallet creation with account abstraction
  • APE Chain Exclusive: Built exclusively on APE Chain for optimal performance and ecosystem benefits
  • Account Abstraction: Users get smart wallets without gas complexity
  • Gas Sponsorship: All transactions sponsored for seamless UX
  • Multi-token Support: APE, USDT, USDC on APE Chain

Database & Caching

  • PostgreSQL: Primary database with Prisma ORM
  • Prisma Accelerate: Enhanced performance and connection pooling
  • Upstash Redis: Caching for prices and session management

External Services

  • Pyth Network: Real-time cryptocurrency price feeds
  • Currency API: Fiat exchange rates for GHS conversion
  • Africa's Talking: USSD gateway integration (planned)

πŸ“‘ API Architecture

USSD Endpoint

The core endpoint processes all USSD interactions:

POST /
Content-Type: application/json

{
  "sessionId": "ATUid_session_id",
  "serviceCode": "*123#", 
  "phoneNumber": "+233541234567",
  "text": "1*2*0541234567*1*100"
}

Response Format:

  • CON: Continue session with menu
  • END: End session with final message

Flow Processing

The backend intelligently routes requests based on input patterns:

// Examples of routing logic
"1" β†’ Send tokens menu
"1*2" β†’ Send USDT 
"1*2*0541234567" β†’ Confirm recipient for USDT
"1*2*0541234567*1*100*1234" β†’ Execute transfer with PIN

πŸ” Security Features

PIN Protection

  • 4-digit PIN required for all transactions
  • bcrypt hashing for secure storage
  • Retry limits with Redis-based lockout
  • PIN validation on every sensitive operation

Phone Number Validation

// Valid Ghanaian prefixes
const GHANA_MOBILE_PREFIXES = [
  "020", "023", "024", "025", "026", "027", "028", "029",
  "050", "053", "054", "055", "057", "059", 
  "070", "071", "077"
];

Amount Validation

  • Range checks (0 < amount ≀ 1,000,000)
  • Balance verification before transactions
  • Decimal precision handling

πŸ› οΈ Services Architecture

ThirdwebService

// Core blockchain operations
- createWallet()           // Auto-create smart wallets
- getTokenBalance()        // Check token balances  
- sendToken()             // Execute transfers with gas sponsorship

PriceService

// Real-time pricing
- getTokenPriceInGHS()    // Live crypto β†’ GHS rates
- getCachedRate()         // Redis-cached exchange rates

User Management

// Database operations via Prisma
- User creation with wallet linking
- PIN hash storage and verification
- Transaction history tracking
- Purchase record management

🌍 Ghana-Specific Features

Localized Experience

  • Phone Format: 054 XXX XXXX validation and formatting
  • Currency: Real-time GHS pricing for all tokens
  • Mobile Money: Built-in MoMo withdrawal support (coming soon)
  • Language: English with Ghana-friendly terminology

Supported Networks

  • APE Chain Exclusive: Kudifi operates exclusively on APE Chain
  • Optimized Performance: Built specifically for APE Chain's ecosystem
  • Gas Sponsorship: All transaction fees covered by Kudifi
  • Smart Wallet Benefits: Account abstraction removes technical complexity

πŸš€ Getting Started

Prerequisites

  • Bun runtime
  • PostgreSQL database
  • Redis instance
  • Thirdweb API keys

Installation

  1. Clone and install dependencies:
git clone <repository-url>
cd kudifi-backend
bun install
  1. Environment setup:
cp .env.example .env
# Fill in your environment variables
  1. Database setup:
bunx prisma generate
bunx prisma db push
  1. Start development server:
bun run dev

The service will be available at:

πŸ“Š Environment Variables

Required Configuration

# Database
DATABASE_URL="postgresql://user:password@localhost:5432/database_name?schema=public"

# Thirdweb (APE Chain Integration)
THIRDWEB_CLIENT_ID="your_client_id_here"
THIRDWEB_SECRET_KEY="your_secret_key_here"
THIRDWEB_VAULT_ADMIN_KEY="your_vault_admin_key_here"
THIRDWEB_VAULT_ACCESS_TOKEN="your_vault_access_token_here"

# Redis (Upstash)
UPSTASH_REDIS_REST_URL="https://your-redis-url.upstash.io"
UPSTASH_REDIS_REST_TOKEN="your_redis_token_here"

Getting Your Keys

Thirdweb Setup:

  1. Visit Thirdweb Dashboard
  2. Create a new project or select existing
  3. Navigate to Settings β†’ API Keys
  4. Generate your Client ID and Secret Key
  5. For Vault keys, go to Vault section in dashboard

Upstash Redis:

  1. Sign up at Upstash
  2. Create a new Redis database
  3. Copy the REST URL and Token from database details

Database:

  • Use PostgreSQL 14+ for optimal performance
  • Ensure connection string includes schema parameter

πŸ”„ USSD Integration

Africa's Talking Integration

The backend is designed for seamless integration with Africa's Talking USSD gateway:

// Expected webhook payload
{
  "sessionId": "unique_session_id",
  "serviceCode": "*your_code#",
  "phoneNumber": "+233XXXXXXXXX", 
  "text": "user_input_chain"
}

Response Handling

  • CON responses: Continue the USSD session
  • END responses: Terminate with final message
  • Error handling: Graceful fallbacks for all edge cases

πŸ“ˆ Future Enhancements

  • Mobile Money Withdrawals: Direct USDT/USDC β†’ MoMo conversion
  • Rewards System: Cashback and loyalty programs
  • Multi-language Support: Twi, Ga, Hausa support
  • Advanced Trading: Limit orders and DCA features
  • Merchant Integration: Business payment solutions

Kudifi - Making cryptocurrency accessible to everyone in Ghana through simple USSD technology. πŸ‡¬πŸ‡­πŸš€

About

Backend for kudifi wallet

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors