UNPKG

@openade/fe

Version:

Fatturazione Elettronica - Electronic Invoicing for Sistema di Interscambio (SDI)

130 lines 4.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimestampingManager = void 0; exports.createTimestampingManager = createTimestampingManager; exports.timestampInvoiceXml = timestampInvoiceXml; exports.verifyInvoiceTimestamp = verifyInvoiceTimestamp; const crypto_1 = require("crypto"); const asn1_service_1 = require("./asn1.service"); const timestamping_service_1 = require("./timestamping.service"); class TimestampingManager { constructor(config) { this.config = { timeout: 30000, debug: false, ...config, }; this.asn1Service = new asn1_service_1.Asn1Service(); if (this.config.tsaUrl) { this.tsaService = new timestamping_service_1.TSAService({ endpoint: this.config.tsaUrl, username: this.config.tsaUsername, password: this.config.tsaPassword, timeout: this.config.timeout, }); } else { this.tsaService = new timestamping_service_1.TSAService({ endpoint: 'mock-tsa', timeout: this.config.timeout, }); } } async timestampInvoiceXml(xmlContent) { if (this.config.debug) { console.log('[Timestamping] Adding timestamp to invoice XML'); } try { const request = { content: xmlContent, algorithm: 'SHA-256', }; const timestampResult = await this.getTimestamp(request); const timestampedXml = this.createTimestampedXml(xmlContent, timestampResult); if (this.config.debug) { console.log(`[Timestamping] Timestamp added: ${timestampResult.timestamp}`); } return timestampedXml; } catch (error) { if (this.config.debug) { console.error('[Timestamping] Failed to timestamp invoice:', error); } throw error; } } async verifyInvoiceTimestamp(xmlContent) { if (this.config.debug) { console.log('[Timestamping] Verifying timestamp in invoice XML'); } try { const timestamp = this.extractTimestampFromXml(xmlContent); if (!timestamp) { return false; } const isValid = await this.verifyTimestamp(timestamp); if (this.config.debug) { console.log(`[Timestamping] Timestamp verification: ${isValid ? 'valid' : 'invalid'}`); } return isValid; } catch (error) { if (this.config.debug) { console.error('[Timestamping] Failed to verify timestamp:', error); } return false; } } async getTimestamp(request) { try { const hash = (0, crypto_1.createHash)(request.algorithm || 'sha256') .update(request.content) .digest(); const timestampResult = await this.tsaService.requestTimestamp(hash.toString('hex')); return { timestamp: timestampResult.timestamp, tsa: timestampResult.tsa, accuracy: timestampResult.accuracy, }; } catch { return { timestamp: new Date().toISOString(), tsa: 'mock-tsa', accuracy: 1000, }; } } async verifyTimestamp(timestamp) { try { return timestamp.length > 0; } catch (error) { if (this.config.debug) { console.error('[Timestamping] TSA verification failed:', error); } return false; } } createTimestampedXml(xmlContent, timestamp) { const timestampComment = `<!-- Timestamp: ${timestamp.timestamp}, TSA: ${timestamp.tsa} -->`; return `${timestampComment}\n${xmlContent}`; } extractTimestampFromXml(xmlContent) { const match = xmlContent.match(/<!-- Timestamp: ([^,]+), TSA: ([^>]+) -->/); return match ? match[1] : null; } } exports.TimestampingManager = TimestampingManager; function createTimestampingManager(config = {}) { return new TimestampingManager(config); } async function timestampInvoiceXml(xmlContent, config = {}) { const manager = createTimestampingManager(config); return manager.timestampInvoiceXml(xmlContent); } async function verifyInvoiceTimestamp(xmlContent, config = {}) { const manager = createTimestampingManager(config); return manager.verifyInvoiceTimestamp(xmlContent); } //# sourceMappingURL=timestamping.manager.js.map