Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 | 1x 13x 13x 13x 13x 11x 11x 9x 2x 2x 2x 2x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 2x 2x 2x 3x 1x 2x 2x 1x 1x 1x 1x 1x 1x 3x 3x 2x 4x 2x 2x 2x 3x | import { Injectable } from '@angular/core'; import { IDisotService, DisotEntry, DisotEntryType, DisotFilter } from '../domain/interfaces/disot.interface'; import { ContentHash } from '../domain/interfaces/content.interface'; import { CasService } from './cas.service'; import { SignatureService } from './signature.service'; import { HashService } from './hash.service'; @Injectable({ providedIn: 'root' }) export class DisotService implements IDisotService { private entries = new Map<string, DisotEntry>(); constructor( private casService: CasService, private signatureService: SignatureService, private hashService: HashService ) {} async createEntry( content: ContentHash | any, type: DisotEntryType, privateKey: string, additionalMetadata?: Record<string, any> ): Promise<DisotEntry> { const timestamp = new Date(); // Handle both ContentHash and direct content (for metadata) let contentHash: ContentHash; let metadata: any; if (this.isContentHash(content)) { contentHash = content; } else { // For metadata and other direct content, hash it const contentData = new TextEncoder().encode(JSON.stringify(content)); const hashValue = await this.hashService.hash(contentData); contentHash = { algorithm: 'sha256', value: hashValue }; metadata = content; } // Merge additional metadata if provided Iif (additionalMetadata) { metadata = { ...metadata, ...additionalMetadata }; } // Create entry data to sign const entryData = { contentHash, type, timestamp: timestamp.toISOString(), ...(metadata && { metadata }) }; const dataToSign = new TextEncoder().encode(JSON.stringify(entryData)); const signature = await this.signatureService.sign(dataToSign, privateKey); const entry: DisotEntry = { id: '', // Will be set after storing contentHash, signature, timestamp, type, ...(metadata && { metadata }) }; // Store the entry in CAS const entryContent = new TextEncoder().encode(JSON.stringify(entry)); const entryHash = await this.casService.store({ data: entryContent }); entry.id = entryHash.value; // Store in memory for quick access this.entries.set(entry.id, entry); return entry; } private isContentHash(content: any): content is ContentHash { return content && typeof content.algorithm === 'string' && typeof content.value === 'string'; } async verifyEntry(entry: DisotEntry): Promise<boolean> { const entryData = { contentHash: entry.contentHash, type: entry.type, timestamp: entry.timestamp.toISOString(), ...(entry.metadata && { metadata: entry.metadata }) }; const dataToVerify = new TextEncoder().encode(JSON.stringify(entryData)); return this.signatureService.verify(dataToVerify, entry.signature); } async getEntry(id: string): Promise<DisotEntry> { // Check memory cache first if (this.entries.has(id)) { return this.entries.get(id)!; } // Try to retrieve from CAS try { const content = await this.casService.retrieve({ algorithm: 'sha256', value: id }); const entryJson = new TextDecoder().decode(content.data); const entry = JSON.parse(entryJson) as DisotEntry; entry.timestamp = new Date(entry.timestamp); // Cache for future access this.entries.set(id, entry); return entry; } catch (error) { throw new Error('DISOT entry not found'); } } async listEntries(filter?: DisotFilter): Promise<DisotEntry[]> { let entries = Array.from(this.entries.values()); if (filter) { if (filter.type) { entries = entries.filter(e => e.type === filter.type); } Iif (filter.publicKey) { entries = entries.filter(e => e.signature.publicKey === filter.publicKey); } Iif (filter.fromDate) { entries = entries.filter(e => e.timestamp >= filter.fromDate!); } Iif (filter.toDate) { entries = entries.filter(e => e.timestamp <= filter.toDate!); } } // Sort by timestamp descending return entries.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime()); } } |