diff --git a/backend/src/queue/document.processor.ts b/backend/src/queue/document.processor.ts index 394cfd0..8f26a69 100644 --- a/backend/src/queue/document.processor.ts +++ b/backend/src/queue/document.processor.ts @@ -116,6 +116,22 @@ export class DocumentProcessor implements OnModuleDestroy { const { txHash, ledger } = await this.stellarService.anchorHash( document.fileHash, ); + + const isAnchored = await this.stellarService.verifyHash(document.fileHash); + if (!isAnchored) { + this.logger.error( + `Document ${documentId} hash not found on Stellar after anchoring`, + ); + await this.verificationService.create({ + documentId, + stellarTxHash: txHash, + stellarLedger: ledger, + anchoredAt: new Date(), + status: VerificationStatus.FAILED, + }); + return; + } + await this.verificationService.create({ documentId, stellarTxHash: txHash, diff --git a/backend/src/verification/verification.controller.ts b/backend/src/verification/verification.controller.ts index 4fff01e..6cee54e 100644 --- a/backend/src/verification/verification.controller.ts +++ b/backend/src/verification/verification.controller.ts @@ -3,6 +3,7 @@ import { Throttle } from '@nestjs/throttler'; import { DocumentsService } from '../documents/documents.service'; import { VerificationService } from './verification.service'; +import { VerificationCacheService } from './verification-cache.service'; // Stricter rate limiting for public endpoint (10 requests per minute) @Throttle({ default: { ttl: 60000, limit: 10 } }) @@ -11,6 +12,7 @@ export class VerificationController { constructor( private readonly documentsService: DocumentsService, private readonly verificationService: VerificationService, + private readonly verificationCache: VerificationCacheService, ) {} @Get(':hash') @@ -22,13 +24,22 @@ export class VerificationController { ); } + // Check cache first + const cached = this.verificationCache.get(hash); + if (cached !== undefined) { + return cached; + } + // Look up document by file hash const document = await this.documentsService.findByFileHash(hash); if (!document) { - return { + const response = { verified: false, message: 'Document not found', + documentStatus: null, }; + this.verificationCache.set(hash, response); + return response; } // Get the latest verification record @@ -36,19 +47,24 @@ export class VerificationController { document.id, ); + let response; if (!record) { - return { + response = { verified: false, message: 'Document has not been verified on Stellar', + documentStatus: document.status, + }; + } else { + response = { + verified: true, + stellarTxHash: record.stellarTxHash, + stellarLedger: record.stellarLedger, + anchoredAt: record.anchoredAt, + documentStatus: document.status, }; } - // Return only verification status - no document metadata - return { - verified: true, - stellarTxHash: record.stellarTxHash, - stellarLedger: record.stellarLedger, - anchoredAt: record.anchoredAt, - }; + this.verificationCache.set(hash, response); + return response; } }