This implementation adds a Sealed Provenance Vault (SPV) middleware that intercepts media uploads flagged for encryption and routes them through the KMS service before they reach external storage.
-
backend/src/middlewares/spv.middleware.ts
- Main middleware that intercepts upload requests
- Checks
req.body.isSealedflag - Routes file buffer to KMS service for encryption
- Attaches encrypted data to request for downstream processing
-
backend/src/services/spv.service.ts
- Business logic for SPV operations
- File encryption using AES-256-GCM
- SPV record management (create, read, update)
- File decryption functionality
- Integration with KMS key management
-
backend/src/controllers/spv.controller.ts
- HTTP request/response handling
- Input validation
- Error handling with appropriate status codes
- Asset and SPV record creation
- CRUD operations for SPV records
-
backend/src/routes/spv.routes.ts
- API endpoint definitions
- Multer configuration for file uploads
- Middleware chain setup
- Route handlers
-
backend/src/middlewares/README.md
- Comprehensive documentation
- API endpoint specifications
- Security features
- Integration guide
-
backend/src/middlewares/spv.middleware.test.md
- Testing guide with Postman examples
- cURL command examples
- Error scenario testing
- Database verification steps
-
backend/package.json
- Added
multerdependency for file uploads - Added
@types/multerdev dependency
- Added
-
backend/src/index.ts
- Imported SPV routes
- Registered
/api/v1/spvendpoint
✅ Strict Layered Architecture: Implements Controller → Service → Model pattern
✅ Data Source: All data retrieved from MongoDB database (no mock objects)
✅ API Versioning: All endpoints use /api/v1/ prefix
✅ Production Ready:
- Robust error handling with specific error messages
- Strong TypeScript typings throughout
- Input validation for all endpoints
- Secure encryption using AES-256-GCM
- Environment variable configuration
Upload and encrypt a file for SPV protection.
Request:
- Method: POST
- Content-Type: multipart/form-data
- Body:
file: File to uploadisSealed: boolean (true to encrypt)userId: MongoDB ObjectIdaccessType: 'private' | 'public_with_conditions' | 'nft_holders_only' | 'specific_users'allowedUsers: Array of user IDs (optional)nftContractAddress: Stellar contract address (optional)
Response (201):
{
"status": "success",
"message": "Encrypted asset uploaded and SPV record created successfully",
"data": {
"assetId": "...",
"spvRecordId": "...",
"fileName": "...",
"isEncrypted": true,
"encryptionKeyVersion": "v1",
"isSealed": true,
"accessType": "private",
"createdAt": "..."
}
}Retrieve SPV record by asset ID.
Retrieve all SPV records for a specific user.
Update the sealed status of an SPV record.
- AES-256-GCM Encryption: Industry-standard authenticated encryption
- Key Versioning: Tracks which KMS key version encrypted each file
- Master Key Protection: Symmetric keys encrypted with master key
- Access Control Policies: Multiple access types supported
- Input Validation: All inputs validated before processing
- Error Sanitization: Sensitive details hidden in production mode
Client Request
↓
Multer (file upload handling)
↓
SPV Middleware (checks isSealed flag)
↓
├─→ isSealed = false → Pass through (no encryption)
│
└─→ isSealed = true → Encrypt file buffer
↓
Fetch active KMS key
↓
Decrypt symmetric key
↓
Encrypt file with AES-256-GCM
↓
Attach encrypted data to request
↓
Controller (create Asset & SPV records)
↓
Service (business logic)
↓
Model (database operations)
↓
Response to Client
- Links to encrypted file storage
- Tracks encryption status and key version
- Stores access policy
- Links asset to KMS key
- Defines access control policies
- Tracks sealed status
- Supports multiple access types
- Stores encrypted symmetric keys
- Tracks key versions
- Manages key lifecycle (active/inactive)
- MongoDB running on localhost:27017
- Environment variables configured in
.env - Dependencies installed:
npm install - KMS data seeded:
npm run seed:kms
- Start server:
npm run dev - POST to
/api/v1/spv/uploadwith:- File attachment
isSealed=true- Valid
userIdfrom seed data
- Verify 201 response with encrypted asset details
- Check MongoDB for Asset and SPVRecord documents
curl -X POST http://localhost:4000/api/v1/spv/upload \
-F "file=@./test-image.jpg" \
-F "isSealed=true" \
-F "userId=507f1f77bcf86cd799439011" \
-F "accessType=private"The implementation handles all error scenarios:
- 400 Bad Request: Missing/invalid parameters
- 404 Not Found: No active KMS key or record not found
- 500 Internal Server Error: Encryption or database errors
All errors return consistent JSON format:
{
"status": "error",
"message": "Human-readable error message",
"error": "Detailed error (development only)"
}- Actual storage integration (IPFS/S3/GridFS)
- Decryption endpoint with access control validation
- NFT ownership verification for gated access
- Audit logging for all encryption/decryption operations
- Streaming support for large files
- Rate limiting on upload endpoints
- File type validation and sanitization
- Virus scanning integration
- ✅ Strict Layered Architecture (Controller → Service → Model)
- ✅ Data from database (no inline mocks)
- ✅ Environment variables for configuration
- ✅ API versioning (/api/v1/...)
- ✅ Production-ready code quality
- ✅ Robust error handling
- ✅ Strong TypeScript typings
- ✅ No TypeScript compilation errors
- ✅ Comprehensive documentation
- ✅ Testing guide provided
# Navigate to backend directory
cd backend
# Install dependencies
npm install
# Configure environment
cp .env.example .env
# Edit .env with your MongoDB URI and MASTER_KEY
# Seed KMS data
npm run seed:kms
# Start development server
npm run devmulter@^1.4.5-lts.1: File upload handling@types/multer@^1.4.12: TypeScript types for multer
- The middleware is designed to be non-blocking for non-SPV uploads
- Encryption is performed in-memory before storage
- The implementation uses the existing KMS infrastructure
- All database operations use Mongoose models
- Error messages are sanitized in production mode
- The middleware can be easily extended for additional storage providers
- Branch name:
spv-interceptor - Base branch:
main - Implementation directory:
backend/src/middlewares/
- ✅ Code follows CONTRIBUTING.md guidelines
- ✅ No inline mock objects or hardcoded values
- ✅ API is versioned
- ✅ Production-ready code quality
- ✅ Comprehensive error handling
- ✅ TypeScript compilation successful
- ✅ Documentation provided
- ✅ Testing guide included
- ⏳ Screenshot of working API (to be added during testing)
- ⏳ PR includes "Closes #[issue_id]" (to be added when creating PR)