This PR implements a comprehensive PDF lease agreement generation system with IPFS anchoring, bridging the gap between decentralized smart contracts and legally binding, real-world rental agreements.
Closes #90
- PDF Generation Service: Professional lease agreements using pdfmake with complete lease data mapping
- IPFS Integration: Multi-provider support (Pinata, Web3.Storage, Local IPFS) with automatic fallback
- Asynchronous Processing: BullMQ worker for non-blocking PDF generation with retry logic
- Blockchain Anchoring: Embeds Soroban transaction hash in PDF footer for cryptographic verification
GET /api/v1/leases/:id/contract- Stream PDF directly from IPFSGET /api/v1/leases/:id/contract/status- Check generation statusPOST /api/v1/leases/:id/contract/generate- Manual generation triggerGET /api/v1/leases/contracts/queue/stats- Queue monitoringPOST /api/v1/leases/contracts/cleanup- Maintenance endpoint
lease_pdf_recordstable for PDF metadata and IPFS CIDspdf_generation_jobstable for job tracking and monitoring- Proper indexing for performance optimization
src/services/leasePdfService.js- PDF generation with professional templatessrc/services/ipfsService.js- Multi-provider IPFS upload/retrieval
src/jobs/leasePdfGenerationJob.js- BullMQ async processing worker
src/controllers/LeaseContractController.js- REST API endpointssrc/routes/leaseContractRoutes.js- Route definitions with OpenAPI docs
migrations/014_add_lease_pdf_records.sql- Database schema updates- Updated
package.jsonwith pdfmake dependency - Updated
.env.examplewith IPFS configuration
tests/leasePdfService.test.js- PDF generation teststests/ipfsService.test.js- IPFS service teststests/leasePdfGenerationJob.test.js- Job processing teststests/leaseContractController.test.js- API integration tests
docs/PDF_LEASE_GENERATION.md- Complete feature documentation
- ✅ Acceptance 1: Users receive compliant, professional PDF rental agreements backing their crypto transactions
- ✅ Acceptance 2: The IPFS integration provides an unbreakable cryptographic link between the legal document and the blockchain
- ✅ Acceptance 3: The generation process scales efficiently, separating heavy rendering tasks from the core REST API
- Unit Tests: Complete coverage for all services and utilities
- Integration Tests: Full API endpoint testing with mocked dependencies
- Error Handling: Comprehensive error scenario testing
- Performance: Async processing verification and queue management
# IPFS Provider (pinata, web3storage, local)
IPFS_PROVIDER=pinata
# Pinata Configuration
PINATA_API_KEY=your_pinata_api_key
PINATA_SECRET_KEY=your_pinata_secret_key
# Web3.Storage Configuration
WEB3_STORAGE_TOKEN=your_web3_storage_token
# Redis Configuration (for BullMQ)
REDIS_HOST=localhost
REDIS_PORT=6379
# PDF Generation
PDF_GENERATION_ENABLED=true┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
│ API Endpoint │───▶│ BullMQ Queue │───▶│ PDF Generator │
│ (Controller) │ │ (Worker) │ │ (Service) │
└─────────────────┘ └──────────────────┘ └─────────────────┘
│ │
▼ ▼
┌──────────────────┐ ┌─────────────────┐
│ Job Tracking │ │ IPFS Upload │
│ (Database) │ │ (Service) │
└──────────────────┘ └─────────────────┘
│
▼
┌─────────────────┐
│ IPFS Storage │
│ (Pinata/Web3) │
└─────────────────┘
- Cryptographic Linking: Transaction hash embedding ensures document authenticity
- Immutable Storage: IPFS provides permanent, tamper-proof storage
- Access Control: API endpoints verify lease ownership
- Error Handling: Comprehensive retry logic and fallback mechanisms
- Audit Trail: Complete logging of all PDF generation events
- Asynchronous Processing: Non-blocking PDF generation
- Queue Management: BullMQ provides job prioritization and scaling
- Memory Efficiency: Streaming PDF generation without disk storage
- Caching: IPFS CID caching for instant retrieval
- Install dependencies:
npm install - Configure IPFS provider credentials in
.env - Run database migration
- Ensure Redis is running for BullMQ
- Start the application
// Request PDF generation
const response = await fetch('/api/v1/leases/lease-123/contract');
if (response.status === 202) {
// PDF generation in progress
const { jobId } = await response.json();
// Poll status endpoint for completion
}// After lease initialization on blockchain
await fetch(`/api/v1/leases/${leaseId}/contract/generate`, {
method: 'POST',
body: JSON.stringify({ priority: 'high' })
});- pdfmake: PDF generation library
- bullmq: Job queue management
- ipfs-http-client: IPFS client library
- axios: HTTP client for IPFS providers
- All tests passing
- Documentation updated
- Environment variables documented
- Database migration included
- API endpoints documented with OpenAPI
- Error handling implemented
- Security considerations addressed
- Performance optimizations implemented
Please review the following areas:
- Security: IPFS provider credentials and access control
- Performance: Queue configuration and concurrency settings
- Documentation: API clarity and integration examples
- Testing: Coverage of edge cases and error scenarios
Run the database migration to add the new tables:
sqlite3 data/leaseflow-protocol.sqlite < migrations/014_add_lease_pdf_records.sqlThis implementation provides a complete, production-ready solution for PDF lease agreement generation with IPFS anchoring, fully addressing the requirements of issue #90.