Skip to content

SCAI-Foundation/irys_backend

Repository files navigation

Irys Server

A standalone Express.js server for managing file uploads, downloads, and searches on the Irys decentralized storage network with encryption support for private files and group-based permissions.

Features

  • User Management: Registration, authentication, and profile management
  • Group Management: Create public/private groups with member management
  • File Operations: Upload, download, and manage files on Irys
  • Encryption: AES-256-GCM encryption for private files
  • Search: Advanced search capabilities with filters and suggestions
  • Permissions: User and group-level access control
  • Security: JWT authentication, rate limiting, input validation

Architecture

Core Components

  1. Authentication Service (middleware/auth.js)

    • JWT-based authentication
    • User and group management
    • Access control middleware
  2. Encryption Service (utils/encryption.js)

    • AES-256-GCM symmetric encryption
    • RSA asymmetric encryption
    • Password hashing with PBKDF2
  3. Irys Service (utils/irys.js)

    • Irys network integration
    • File upload/download operations
    • Transaction metadata management
  4. API Routes

    • /api/auth - Authentication endpoints
    • /api/files - File management endpoints
    • /api/groups - Group management endpoints
    • /api/search - Search and discovery endpoints

Installation

  1. Clone the repository and navigate to the irys_server directory:
cd irys_server
  1. Install dependencies:
npm install
  1. Copy environment configuration:
cp .env.example .env
  1. Configure environment variables in .env:
PORT=7711
IRYS_PRIVATE_KEY=your_irys_private_key_here
JWT_SECRET=your_super_secret_jwt_key_here
MASTER_ENCRYPTION_KEY=your_32_byte_master_encryption_key_here

Usage

Development

Start the development server with auto-reload:

npm run dev

Production

Start the production server:

npm start

Testing

Run the test suite:

npm test

Run tests in watch mode:

npm run test:watch

API Documentation

Authentication

Register User

POST /api/auth/register
Content-Type: application/json

{
  "username": "johndoe",
  "email": "john@example.com",
  "password": "SecurePassword123!"
}

Login

POST /api/auth/login
Content-Type: application/json

{
  "username": "johndoe",
  "password": "SecurePassword123!"
}

Get Profile

GET /api/auth/profile
Authorization: Bearer <jwt_token>

File Management

Upload File

POST /api/files/upload
Authorization: Bearer <jwt_token>
Content-Type: multipart/form-data

file: <file_data>
fileName: "document.pdf"
description: "Important document"
isPrivate: "true"
groupId: "group_123"
tags: ["document", "important"]

Download File

GET /api/files/download/:txId
Authorization: Bearer <jwt_token>

List User Files

GET /api/files/my?limit=20&offset=0&groupId=group_123&isPrivate=true
Authorization: Bearer <jwt_token>

Group Management

Create Group

POST /api/groups
Authorization: Bearer <jwt_token>
Content-Type: application/json

{
  "name": "Research Team",
  "description": "Collaborative research group",
  "isPrivate": false
}

Join Group

POST /api/groups/:groupId/join
Authorization: Bearer <jwt_token>

List Public Groups

GET /api/groups/public
Authorization: Bearer <jwt_token>

Search

Basic Search

GET /api/search?query=research&tags=science&limit=20&offset=0
Authorization: Bearer <jwt_token>

Advanced Search

POST /api/search/advanced
Authorization: Bearer <jwt_token>
Content-Type: application/json

{
  "query": "machine learning",
  "filters": {
    "contentType": "application/pdf",
    "isPrivate": false,
    "groupId": "group_123"
  },
  "dateRange": {
    "from": "2023-01-01T00:00:00Z",
    "to": "2023-12-31T23:59:59Z"
  },
  "sortBy": "uploadedAt",
  "sortOrder": "desc",
  "limit": 20,
  "offset": 0
}

Security Features

Encryption

  • Private Files: Encrypted using AES-256-GCM with user/group-specific keys
  • Key Management: RSA key pairs for secure key exchange
  • Password Security: PBKDF2 hashing with salt

Access Control

  • Authentication: JWT tokens with configurable expiration
  • Authorization: Role-based access (user, group member, group admin)
  • Rate Limiting: Configurable request limits per IP
  • Input Validation: Joi schema validation for all inputs

Data Protection

  • HTTPS: Enforced in production
  • CORS: Configurable cross-origin policies
  • Helmet: Security headers middleware
  • File Validation: Type and size restrictions

Configuration

Environment Variables

Variable Description Default
PORT Server port 7711
NODE_ENV Environment mode development
IRYS_NODE_URL Irys node URL https://node2.irys.xyz
IRYS_PRIVATE_KEY Irys wallet private key Required
IRYS_CURRENCY Irys currency ethereum
JWT_SECRET JWT signing secret Required
JWT_EXPIRES_IN JWT expiration time 24h
MASTER_ENCRYPTION_KEY Master encryption key (32 bytes hex) Required
MAX_FILE_SIZE Maximum file size 50MB
RATE_LIMIT_WINDOW_MS Rate limit window 15 minutes
RATE_LIMIT_MAX_REQUESTS Max requests per window 100

File Upload Limits

  • Maximum file size: 50MB (configurable)
  • Maximum files per request: 10
  • Supported file types: Images, PDFs, text files, archives, videos, audio

Error Handling

The server implements comprehensive error handling:

  • Validation Errors: 400 Bad Request with detailed messages
  • Authentication Errors: 401 Unauthorized
  • Authorization Errors: 403 Forbidden
  • Not Found Errors: 404 Not Found
  • Server Errors: 500 Internal Server Error

All errors include:

  • Error type and message
  • Timestamp
  • Request context (in development mode)

Logging

Structured logging with configurable levels:

  • Error: Critical errors and exceptions
  • Warn: Warning conditions
  • Info: General information
  • Debug: Detailed debugging information

Logs are written to:

  • Console (with colors)
  • Files (logs/error.log, logs/all.log)

Testing

Comprehensive test suite covering:

  • Authentication flows
  • File operations
  • Group management
  • Search functionality
  • Encryption/decryption
  • Error scenarios
  • Security features

Test coverage includes unit tests and integration tests with mocked Irys service.

Deployment

Docker (Recommended)

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 7711
CMD ["npm", "start"]

Environment Setup

  1. Set up Irys wallet with sufficient balance
  2. Configure secure JWT secret (32+ characters)
  3. Generate master encryption key (32 bytes hex)
  4. Set up reverse proxy (nginx/Apache) for HTTPS
  5. Configure firewall rules
  6. Set up monitoring and logging

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Write tests for new functionality
  4. Ensure all tests pass
  5. Submit a pull request

License

MIT License - see LICENSE file for details.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published