Skip to content

Production-quality adapter service between Official Registry & Chain - REST API with JWT auth, blockchain integration, and comprehensive documentation

License

Notifications You must be signed in to change notification settings

ShantanuVr/registry-adapter-api

Repository files navigation

Registry Adapter API

License: MIT Node.js Version TypeScript Docker

A production-quality adapter service that serves as the boundary enforcer between the Official Registry and on-chain contracts. It provides separate API namespaces for credit and token domains, enforces deterministic mapping, and ensures proper domain separation.

πŸš€ Quick Start

# Clone the repository
git clone https://github.com/ShantanuVr/registry-adapter-api.git
cd registry-adapter-api

# Install dependencies
pnpm install

# Start with Docker Compose
docker compose up -d

# Run database migrations and seeds
pnpm db:migrate
pnpm db:seed

# Start development server
pnpm dev

πŸ“‹ Features

  • Domain Separation: Separate /v1/credit/* and /v1/token/* API namespaces
  • Boundary Enforcement: Clear separation between Registry (credit) and Chain (token) domains
  • Deterministic Mapping: Maintains classId -> {chainId, contract, tokenId} mapping table
  • Role-Based Access: Token operations restricted to ADMIN role only
  • Idempotency: Guarantees idempotent operations with strong audit trails
  • Security: JWT verification, RBAC, rate limiting, and request validation
  • Observability: Structured logging, Prometheus metrics, and health checks
  • Documentation: Complete OpenAPI 3.1 specification with interactive docs

πŸ”— API Endpoints

πŸ—οΈ Architecture

Domain Boundaries

The adapter enforces strict domain boundaries between credit and token operations:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    REGISTRY ADAPTER API                         β”‚
β”‚                     (Boundary Enforcer)                         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  CREDIT DOMAIN (Registry-facing)    β”‚  TOKEN DOMAIN (Chain-facing) β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚  β”‚ /v1/credit/issuance/finalize   β”‚ β”‚  β”‚ /v1/token/mint          β”‚ β”‚
β”‚  β”‚ /v1/credit/retire              β”‚ β”‚  β”‚ /v1/token/burn          β”‚ β”‚
β”‚  β”‚ /v1/classes/map                β”‚ β”‚  β”‚ /v1/token/bridge        β”‚ β”‚
β”‚  β”‚ /v1/classes/resolve            β”‚ β”‚  β”‚                         β”‚ β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚  domain: "credit"                    β”‚  domain: "token"              β”‚
β”‚  sourceOfRecord: "registry"          β”‚  sourceOfRecord: "chain"      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚                    DETERMINISTIC MAPPING                        β”‚
β”‚              classId -> {chainId, contract, tokenId}            β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

API Flow Diagram

Registry (Credit Domain)          Adapter API              Blockchain (Token Domain)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                     β”‚         β”‚                 β”‚         β”‚                     β”‚
β”‚  Issuance Finalize  β”‚ ──────► β”‚ /v1/credit/     β”‚ ──────► β”‚ Mint Tokens         β”‚
β”‚  (Webhook)          β”‚         β”‚ issuance/        β”‚         β”‚ (Admin CLI only)    β”‚
β”‚                     β”‚         β”‚ finalize        β”‚         β”‚                     β”‚
β”‚                     β”‚         β”‚                 β”‚         β”‚                     β”‚
β”‚  Credit Retirement  β”‚ ──────► β”‚ /v1/credit/     β”‚ ──────► β”‚ Burn Tokens         β”‚
β”‚  (Webhook)          β”‚         β”‚ retire          β”‚         β”‚ (Admin CLI only)    β”‚
β”‚                     β”‚         β”‚                 β”‚         β”‚                     β”‚
β”‚  Class Mapping      β”‚ ──────► β”‚ /v1/classes/    β”‚ ──────► β”‚ Contract Address    β”‚
β”‚  (Admin CLI)        β”‚         β”‚ map             β”‚         β”‚ Token ID            β”‚
β”‚                     β”‚         β”‚                 β”‚         β”‚                     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Core Modules

  • Auth: JWT verification and role-based authorization
  • Classes: Project-to-class mapping and deterministic token mapping
  • Issuance: Credit issuance finalization and token minting
  • Retire: Credit retirement and token burning
  • Anchor: Evidence anchoring on-chain
  • Receipts: Transaction receipt management
  • Audit: Append-only audit logging
  • Idempotency: Request deduplication

Data Model

  • Receipt: Transaction records with on-chain details
  • ClassMap: Project/vintage to class ID mappings
  • TokenMap: Deterministic classId -> {chainId, contract, tokenId} mappings
  • Idempotency: Request deduplication tracking
  • AuditEvent: Immutable audit trail

πŸ”’ Security & Access Control

Role-Based Access

  • ADMIN: Full access to all endpoints including token operations
  • VERIFIER: Read-only access to credit domain endpoints
  • ISSUER: Access to credit issuance endpoints
  • BURNER: Access to credit retirement endpoints
  • EVIDENCE: Access to evidence anchoring endpoints
  • VIEWER: Read-only access to all endpoints

Domain Restrictions

  • Credit Domain (/v1/credit/*): Accessible to Registry webhooks and authorized roles
  • Token Domain (/v1/token/*): Restricted to ADMIN role only
  • Mint Operations: Only accessible via Registry webhooks or Admin CLI
  • Mapping Operations: Only accessible via Admin CLI

πŸ“– API Documentation

Credit Domain Endpoints

Issuance Finalization

POST /v1/credit/issuance/finalize
Content-Type: application/json
Authorization: Bearer <jwt-token>
Idempotency-Key: <uuid>

{
  "issuanceId": "issuance_123",
  "projectId": "project_456",
  "vintageStart": "2023-01-01T00:00:00Z",
  "vintageEnd": "2023-12-31T23:59:59Z",
  "quantity": 1000,
  "factorRef": "factor_789",
  "evidenceHashes": ["0xabc123..."]
}

Response:

{
  "domain": "credit",
  "sourceOfRecord": "registry",
  "adapterTxId": "c1234567890abcdef",
  "classId": "class_123",
  "quantity": 1000,
  "txHash": "0xdef456...",
  "blockNumber": 12345,
  "onchainHash": "0xghi789...",
  "receiptUrl": "/v1/receipts/c1234567890abcdef"
}

Credit Retirement

POST /v1/credit/retire
Content-Type: application/json
Authorization: Bearer <jwt-token>
Idempotency-Key: <uuid>

{
  "classId": "class_123",
  "holder": "0x1234567890123456789012345678901234567890",
  "quantity": 100,
  "purposeHash": "0xabc123...",
  "beneficiaryHash": "0xdef456..."
}

Token Domain Endpoints (Admin Only)

Token Minting

POST /v1/token/mint
Content-Type: application/json
Authorization: Bearer <admin-jwt-token>
Idempotency-Key: <uuid>

{
  "classId": "class_123",
  "to": "0x1234567890123456789012345678901234567890",
  "quantity": 1000,
  "chainId": "137",
  "contractAddress": "0xabcdef1234567890123456789012345678901234",
  "tokenId": "token_123"
}

Response:

{
  "domain": "token",
  "sourceOfRecord": "chain",
  "adapterTxId": "c1234567890abcdef",
  "classId": "class_123",
  "quantity": 1000,
  "txHash": "0xdef456...",
  "blockNumber": 12345,
  "onchainHash": "0xghi789...",
  "receiptUrl": "/v1/receipts/c1234567890abcdef"
}

Class Mapping Management

Create Mapping

POST /v1/classes/map
Content-Type: application/json
Authorization: Bearer <admin-jwt-token>
Idempotency-Key: <uuid>

{
  "classId": "class_123",
  "chainId": "137",
  "contractAddress": "0xabcdef1234567890123456789012345678901234",
  "tokenId": "token_123"
}

Get Mapping

GET /v1/classes/map/class_123
Authorization: Bearer <jwt-token>

πŸ› οΈ Development

pnpm dev          # Start development server
pnpm build        # Build for production
pnpm start        # Start production server
pnpm db:migrate   # Run database migrations
pnpm db:seed      # Seed database with demo data
pnpm test         # Run unit tests
pnpm test:e2e     # Run end-to-end tests
pnpm lint         # Run ESLint

πŸ“– Documentation

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support


Built with ❀️ by the Carbon Credit Registry Team

About

Production-quality adapter service between Official Registry & Chain - REST API with JWT auth, blockchain integration, and comprehensive documentation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published