Skip to content

Commit edcddd7

Browse files
authored
Merge pull request #220 from shaaibu7/docs/backend-docs
Docs/backend docs
2 parents f810bed + ecfcf64 commit edcddd7

File tree

1 file changed

+205
-0
lines changed

1 file changed

+205
-0
lines changed

README.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,211 @@ remitwise-frontend/
124124
- **Insurance**: Integrate with `insurance` contract for policy management
125125
- **Family Wallets**: Connect to `family_wallet` contract for member management
126126

127+
## Backend / API
128+
129+
### Overview
130+
131+
This Next.js application uses API routes to handle backend functionality. API routes are located in the `app/api/` directory and follow Next.js 14 App Router conventions.
132+
133+
**Key API Routes:**
134+
- `/api/auth/*` - Authentication endpoints (wallet connect, nonce generation, signature verification)
135+
- `/api/transactions/*` - Transaction history and status
136+
- `/api/contracts/*` - Soroban smart contract interactions
137+
- `/api/health` - Health check endpoint
138+
139+
All API routes are serverless functions deployed alongside the frontend.
140+
141+
### Environment Variables
142+
143+
Create a `.env.local` file in the root directory with the following variables:
144+
145+
```bash
146+
# Stellar Network Configuration
147+
NEXT_PUBLIC_STELLAR_NETWORK=testnet # or 'mainnet'
148+
NEXT_PUBLIC_HORIZON_URL=https://horizon-testnet.stellar.org
149+
NEXT_PUBLIC_SOROBAN_RPC_URL=https://soroban-testnet.stellar.org
150+
151+
# Contract IDs (deployed Soroban contracts)
152+
NEXT_PUBLIC_REMITTANCE_SPLIT_CONTRACT_ID=
153+
NEXT_PUBLIC_SAVINGS_GOALS_CONTRACT_ID=
154+
NEXT_PUBLIC_BILL_PAYMENTS_CONTRACT_ID=
155+
NEXT_PUBLIC_INSURANCE_CONTRACT_ID=
156+
NEXT_PUBLIC_FAMILY_WALLET_CONTRACT_ID=
157+
158+
# Authentication
159+
AUTH_SECRET=your-secret-key-here # Generate with: openssl rand -base64 32
160+
SESSION_COOKIE_NAME=remitwise-session
161+
SESSION_MAX_AGE=86400 # 24 hours in seconds
162+
163+
# API Configuration
164+
API_RATE_LIMIT=100 # requests per minute
165+
API_TIMEOUT=30000 # milliseconds
166+
167+
# Optional: Anchor Platform
168+
ANCHOR_PLATFORM_URL=
169+
ANCHOR_PLATFORM_API_KEY=
170+
```
171+
172+
See `.env.example` for a complete list of configuration options.
173+
174+
### Running the Backend
175+
176+
The backend API routes run automatically with the Next.js development server:
177+
178+
```bash
179+
# Development mode (with hot reload)
180+
npm run dev
181+
182+
# Production build
183+
npm run build
184+
npm start
185+
```
186+
187+
The API will be available at `http://localhost:3000/api/*`
188+
189+
### Authentication Flow
190+
191+
RemitWise uses wallet-based authentication with the following flow:
192+
193+
1. **Wallet Connect**: User connects their Stellar wallet (Freighter, Albedo, etc.)
194+
- Frontend detects wallet extension
195+
- User authorizes connection
196+
- Public key is retrieved
197+
198+
2. **Nonce Generation**:
199+
- Client requests nonce from `/api/auth/nonce`
200+
- Server generates unique nonce and stores temporarily
201+
- Nonce returned to client
202+
203+
3. **Signature Verification**:
204+
- Client signs nonce with wallet private key
205+
- Signed message sent to `/api/auth/verify`
206+
- Server verifies signature matches public key
207+
- If valid, session token is created
208+
209+
4. **Session Management**:
210+
- JWT token stored in HTTP-only cookie
211+
- Token includes user's public key and expiration
212+
- Subsequent requests include token for authentication
213+
- Token validated on protected API routes
214+
215+
**Protected Routes**: All `/api/transactions/*`, `/api/contracts/*` endpoints require valid session.
216+
217+
### Contract IDs and Deployment
218+
219+
The application interacts with the following Soroban smart contracts on Stellar:
220+
221+
| Contract | Purpose | Environment Variable |
222+
|----------|---------|---------------------|
223+
| Remittance Split | Automatic money allocation | `NEXT_PUBLIC_REMITTANCE_SPLIT_CONTRACT_ID` |
224+
| Savings Goals | Goal-based savings management | `NEXT_PUBLIC_SAVINGS_GOALS_CONTRACT_ID` |
225+
| Bill Payments | Bill tracking and payments | `NEXT_PUBLIC_BILL_PAYMENTS_CONTRACT_ID` |
226+
| Insurance | Micro-insurance policies | `NEXT_PUBLIC_INSURANCE_CONTRACT_ID` |
227+
| Family Wallet | Family member management | `NEXT_PUBLIC_FAMILY_WALLET_CONTRACT_ID` |
228+
229+
**Deployment Notes:**
230+
- Contracts must be deployed to Stellar testnet/mainnet before use
231+
- Update contract IDs in `.env.local` after deployment
232+
- Verify contract addresses match network (testnet vs mainnet)
233+
- Contract ABIs are included in `lib/contracts/` directory
234+
235+
### Health and Monitoring
236+
237+
**Health Check Endpoint**: `GET /api/health`
238+
239+
Returns system status and connectivity:
240+
241+
```json
242+
{
243+
"status": "healthy",
244+
"timestamp": "2024-02-24T10:30:00Z",
245+
"services": {
246+
"stellar": "connected",
247+
"contracts": "available",
248+
"database": "connected"
249+
},
250+
"version": "0.1.0"
251+
}
252+
```
253+
254+
**Monitoring Recommendations:**
255+
- Set up uptime monitoring for `/api/health`
256+
- Monitor API response times and error rates
257+
- Track Stellar network connectivity
258+
- Log contract interaction failures
259+
- Set alerts for authentication failures
260+
261+
### Testing
262+
263+
**Unit Tests**
264+
265+
Test individual API route handlers and utility functions:
266+
267+
```bash
268+
# Run unit tests
269+
npm test
270+
271+
# Run with coverage
272+
npm run test:coverage
273+
274+
# Watch mode
275+
npm run test:watch
276+
```
277+
278+
Unit tests are located alongside source files with `.test.ts` extension.
279+
280+
**Integration Tests**
281+
282+
Test complete API flows including authentication and contract interactions:
283+
284+
```bash
285+
# Run integration tests
286+
npm run test:integration
287+
288+
# Test specific API route
289+
npm run test:integration -- auth
290+
```
291+
292+
Integration tests are in the `__tests__/integration/` directory.
293+
294+
**Testing Checklist:**
295+
- ✓ Authentication flow (nonce → sign → verify)
296+
- ✓ Contract interactions (all 5 contracts)
297+
- ✓ Error handling and validation
298+
- ✓ Rate limiting
299+
- ✓ Session management
300+
301+
### API Documentation
302+
303+
For detailed API specifications:
304+
305+
- **OpenAPI Spec**: See `openapi.yaml` for complete API documentation
306+
- **Interactive Docs**: Run `npm run docs` to view Swagger UI at `http://localhost:3000/api-docs`
307+
- **Postman Collection**: Import `postman_collection.json` for testing
308+
309+
**Quick API Reference:**
310+
311+
```bash
312+
# Authentication
313+
POST /api/auth/nonce # Get nonce for signing
314+
POST /api/auth/verify # Verify signature and create session
315+
POST /api/auth/logout # End session
316+
317+
# Transactions
318+
GET /api/transactions # List user transactions
319+
GET /api/transactions/:id # Get transaction details
320+
321+
# Contracts
322+
POST /api/contracts/split # Initialize/update split configuration
323+
POST /api/contracts/goals # Create/manage savings goals
324+
POST /api/contracts/bills # Manage bill payments
325+
POST /api/contracts/insurance # Manage insurance policies
326+
POST /api/contracts/family # Manage family wallet
327+
328+
# System
329+
GET /api/health # Health check
330+
```
331+
127332
## Design Notes
128333

129334
- All forms are currently disabled (placeholders)

0 commit comments

Comments
 (0)