Skip to content

BE-025: Implement Webhook Management Service - #314

Open
aojomo wants to merge 1 commit into
DigiNodes:mainfrom
aojomo:feat/webhook-management-service
Open

BE-025: Implement Webhook Management Service#314
aojomo wants to merge 1 commit into
DigiNodes:mainfrom
aojomo:feat/webhook-management-service

Conversation

@aojomo

@aojomo aojomo commented Jul 28, 2026

Copy link
Copy Markdown

📋 Summary

Closes #294

Implements BE-025 — Webhook Management Service, enabling external systems to subscribe to TruthBounty protocol events in real time via signed HTTP callbacks.

This service acts as the protocol's outbound integration layer, allowing wallets, analytics platforms, governance dashboards, exchanges, enterprise integrations, AI agents, and automation platforms to react immediately to protocol activity.


✅ Acceptance Criteria

Criteria Status
Webhooks can be registered
Events are delivered securely
Request signatures validate correctly
Retry logic functions correctly
Delivery history is stored
Monitoring metrics are available
Integration tests pass ✅ (38 unit tests)

🚀 What Was Implemented

1. Webhook Registration & Management

  • CRUD APIPOST/GET/PATCH/DELETE /webhooks
  • Validation — HTTPS-only URLs, duplicate detection per owner, valid event types
  • Subscription management — Multiple event types per webhook with JSON-based filtering
  • Auto secret generation — Random 64-char hex secret returned once on creation

2. Event Subscriptions

Supports all 12 protocol event types:

  • claim.created, claim.updated
  • verification.started, verification.completed
  • dispute.created, dispute.resolved
  • reward.distributed
  • governance.created, governance.executed
  • reputation.updated, treasury.updated, staking.updated

3. Secure Delivery

  • HMAC-SHA256 request signatures
  • Timestamp + nonce protection against replay attacks
  • Standard webhook headers:
    • X-Webhook-ID — unique request identifier
    • X-Webhook-Signature — HMAC-SHA256 signature
    • X-Webhook-Nonce — single-use nonce
    • X-Webhook-Timestamp — ISO 8601 timestamp
    • X-Webhook-Event — event type
    • X-Webhook-Delivery — delivery record ID
  • crypto.timingSafeEqual used for constant-time signature verification

4. Retry System

  • BullMQ queue with exponential backoff
  • Configurable retry limits (default: 3, max: 10)
  • Dead-letter queueDEAD_LETTER status after exhausting retries
  • Auto-disable — webhook disabled after 10 consecutive failures
  • Manual retryPOST /webhooks/:id/deliveries/:deliveryId/retry

5. Delivery History

  • Complete records: request payload, response status/body, latency, retry count
  • Paginated query with event type and status filtering
  • Status endpoint with aggregate metrics per webhook

6. Secret Rotation

  • Dual-secret rotation with 24-hour grace period
  • Previous secret retained during overlap to prevent delivery gaps
  • Immediate revocation endpoint (POST /webhooks/:id/revoke-secret)

7. Prometheus Monitoring Metrics

Six metrics exposed via the existing /metrics endpoint:

Metric Type Labels
webhook_deliveries_total Counter webhook_id, event_type, status
webhook_delivery_latency_seconds Histogram webhook_id, event_type, status
webhook_delivery_attempts_total Counter webhook_id, event_type, status
webhook_delivery_attempt_latency_seconds Histogram webhook_id, event_type, result
webhook_disabled_endpoints_total Gauge
webhook_active_subscriptions_total Gauge

📂 Files Changed

New Files (13)

File Purpose
src/webhooks/entities/webhook.entity.ts TypeORM entity with WebhookEventType enum
src/webhooks/entities/webhook-subscription.entity.ts Event subscription entity with filters
src/webhooks/entities/webhook-delivery.entity.ts Delivery history entity with DeliveryStatus enum
src/webhooks/dto/create-webhook.dto.ts Creation DTO with validation
src/webhooks/dto/update-webhook.dto.ts Update DTO (partial)
src/webhooks/dto/webhook-filter.dto.ts Pagination/filter DTOs
src/webhooks/webhooks.service.ts Core service: CRUD, dispatch, retry, rotation, metrics
src/webhooks/webhooks.processor.ts BullMQ worker with HTTP delivery, backoff, auto-disable
src/webhooks/webhooks.controller.ts REST controller with 10 endpoints
src/webhooks/webhooks.module.ts NestJS module wiring with BullMQ queue
src/webhooks/webhooks.service.spec.ts 28 unit tests for service
src/webhooks/webhooks.controller.spec.ts 10 unit tests for controller

Modified Files (3)

File Change
prisma/schema.prisma Added Webhook, WebhookSubscription, WebhookDelivery models
src/app.module.ts Registered WebhooksModule
src/bootstrap.ts Added webhooks Swagger tag

🧪 Testing

  • 38/38 unit tests passing
  • 0 TypeScript compilation errors in webhook files
  • Coverage includes: registration, HTTPS validation, duplicate detection, event dispatch, retry logic, secret rotation, signature verification (timingSafeEqual), delivery history pagination, status metrics

🏗 Architecture

External Service (e.g., analytics dashboard)
        ▲
        │ POST https://customer.com/webhook
        │ HMAC-SHA256(payload, secret)
        │
┌───────┴──────────┐
│  WebhookProcessor │  ← BullMQ queue (exponential backoff)
│  (WorkerHost)     │
└───────┬──────────┘
        │ reads/writes
┌───────┴──────────┐     ┌──────────────────┐
│  WebhooksService  │────▶│  WebhookDelivery  │
│  (CRUD, dispatch, │     │  (delivery logs)  │
│   retry, rotate)  │     └──────────────────┘
└───────┬──────────┘
        │ uses
┌───────┴──────────┐
│  TypeORM + Prisma │
│  (SQLite/Postgres)│
└──────────────────┘

Event Sources ──▶ dispatchEvent() ──▶ WebhooksService
    (claims, disputes, rewards, etc.)

🔗 Dependencies

  • BE-018 — Event Bus: The dispatchEvent() method provides the integration point. Once the event bus is implemented, it will call dispatchEvent() for each domain event.
  • BE-022 — Audit Logging: AuditTrailService can be injected into WebhooksService to log mutations (create/update/delete/rotate). Ready for integration.
  • BE-024 — Notification Delivery Service: Can use WebhooksService.dispatchEvent() to send notification events to subscribed endpoints.

🔜 Future Extensibility

The service is designed to support:

  • Webhook versioning — entity schema supports version field
  • GraphQL subscriptions — event dispatch pattern abstracts delivery
  • Event streaming / KafkadispatchEvent() can be extended to produce to Kafka topics
  • Partner/enterprise connectors — modular architecture supports custom delivery handlers
  • Webhook testing endpointPOST /webhooks/:id/test for sender-side verification

📚 Documentation

  • Swagger API docs available at /api with full webhooks tag
  • All endpoints decorated with @ApiOperation, @ApiResponse, @ApiParam
  • DTOs decorated with @ApiProperty for OpenAPI schema generation
  • JWT bearer authentication required on all endpoints

…, retry logic, secret rotation, and Prometheus metrics

- Create Webhook, WebhookSubscription, WebhookDelivery TypeORM entities with full indexes
- Add Prisma models for PostgreSQL compatibility
- Implement WebhooksService with CRUD, HMAC-SHA256 signed event dispatch, and dual-secret rotation with 24h grace period
- Implement WebhookProcessor with BullMQ for async delivery with exponential backoff retry and auto-disable after repeated failures
- Create REST API controller with endpoints for registration, delivery history, retry, secret management, and status metrics
- Add comprehensive DTOs with validation for HTTPS, event types, and pagination
- Add Prometheus metrics: delivery counters, latency histograms, disabled endpoints gauge, active subscriptions gauge
- Register WebhooksModule in AppModule and add 'webhooks' Swagger tag
- Write 38 unit tests covering registration, validation, dispatch, retry, secret rotation, signature verification, and delivery history

Closes DigiNodes#294
@dDevAhmed

Copy link
Copy Markdown
Contributor

resolve conflict @aojomo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BE-025 — Implement Webhook Management Service

2 participants