@zkim-platform/file-format 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +72 -0
- package/LICENSE +22 -0
- package/README.md +695 -0
- package/dist/index.cjs +12170 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1918 -0
- package/dist/index.d.ts +1918 -0
- package/dist/index.js +12126 -0
- package/dist/index.js.map +1 -0
- package/package.json +78 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,1918 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Singleton Pattern Base Classes for @zkim/file-format
|
|
3
|
+
* Lightweight singleton implementation without platform dependencies
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Base Singleton Class
|
|
7
|
+
* Use this for simple singletons that don't require initialization
|
|
8
|
+
*/
|
|
9
|
+
declare abstract class SingletonBase {
|
|
10
|
+
protected static instances: Map<new () => unknown, unknown>;
|
|
11
|
+
/**
|
|
12
|
+
* Protected constructor to prevent direct instantiation
|
|
13
|
+
*/
|
|
14
|
+
protected constructor();
|
|
15
|
+
/**
|
|
16
|
+
* Get or create the singleton instance
|
|
17
|
+
*/
|
|
18
|
+
static getInstance<T>(this: new () => T): T;
|
|
19
|
+
/**
|
|
20
|
+
* Clear all singleton instances (useful for testing)
|
|
21
|
+
*/
|
|
22
|
+
static clearInstances(): void;
|
|
23
|
+
/**
|
|
24
|
+
* Check if an instance exists
|
|
25
|
+
*/
|
|
26
|
+
static hasInstance<T>(this: new () => T): boolean;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Service Base Class
|
|
30
|
+
* Use this for services that require initialization and cleanup
|
|
31
|
+
*/
|
|
32
|
+
declare abstract class ServiceBase extends SingletonBase {
|
|
33
|
+
protected initialized: boolean;
|
|
34
|
+
protected initializing: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Public constructor for ServiceBase extensions
|
|
37
|
+
* Required because getServiceInstance() calls new this() internally
|
|
38
|
+
*/
|
|
39
|
+
constructor();
|
|
40
|
+
/**
|
|
41
|
+
* Initialize the service
|
|
42
|
+
* Must be async and return Promise<void>
|
|
43
|
+
*/
|
|
44
|
+
abstract initialize(): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Cleanup the service
|
|
47
|
+
* Must be async and return Promise<void>
|
|
48
|
+
*/
|
|
49
|
+
abstract cleanup(): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Check if the service is ready
|
|
52
|
+
*/
|
|
53
|
+
isReady(): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Get service status
|
|
56
|
+
*/
|
|
57
|
+
getStatus(): {
|
|
58
|
+
initialized: boolean;
|
|
59
|
+
[key: string]: unknown;
|
|
60
|
+
};
|
|
61
|
+
/**
|
|
62
|
+
* Ensure service is initialized before use
|
|
63
|
+
*/
|
|
64
|
+
protected ensureInitialized(): Promise<void>;
|
|
65
|
+
/**
|
|
66
|
+
* Get or create and initialize the singleton instance
|
|
67
|
+
* This is the ONLY way to get ServiceBase instances
|
|
68
|
+
*/
|
|
69
|
+
static getServiceInstance<T extends ServiceBase>(this: new () => T): Promise<T>;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Logger Interface and Console Implementation
|
|
74
|
+
* Lightweight logging adapter for @zkim/file-format package
|
|
75
|
+
*/
|
|
76
|
+
declare enum LogLevel {
|
|
77
|
+
DEBUG = "debug",
|
|
78
|
+
INFO = "info",
|
|
79
|
+
WARN = "warn",
|
|
80
|
+
ERROR = "error"
|
|
81
|
+
}
|
|
82
|
+
interface ILogger {
|
|
83
|
+
debug(message: string, context?: Record<string, unknown>): void;
|
|
84
|
+
info(message: string, context?: Record<string, unknown>): void;
|
|
85
|
+
warn(message: string, context?: Record<string, unknown>): void;
|
|
86
|
+
error(message: string, error?: unknown, context?: Record<string, unknown>): void;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Console Logger Implementation
|
|
90
|
+
* Simple console-based logger for standalone package usage
|
|
91
|
+
*/
|
|
92
|
+
declare class ConsoleLogger implements ILogger {
|
|
93
|
+
private logLevel;
|
|
94
|
+
constructor(logLevel?: LogLevel);
|
|
95
|
+
setLogLevel(level: LogLevel): void;
|
|
96
|
+
debug(message: string, context?: Record<string, unknown>): void;
|
|
97
|
+
info(message: string, context?: Record<string, unknown>): void;
|
|
98
|
+
warn(message: string, context?: Record<string, unknown>): void;
|
|
99
|
+
error(message: string, error?: unknown, context?: Record<string, unknown>): void;
|
|
100
|
+
private shouldLog;
|
|
101
|
+
private formatMessage;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Default logger instance
|
|
105
|
+
*/
|
|
106
|
+
declare const defaultLogger: ILogger;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* ZKIM File Format Types - Single Source of Truth
|
|
110
|
+
*
|
|
111
|
+
* This file contains all ZKIM file format-related types including core file format,
|
|
112
|
+
* archive format, message format, and service configuration types.
|
|
113
|
+
*/
|
|
114
|
+
/**
|
|
115
|
+
* ZKIM File Header Interface
|
|
116
|
+
*/
|
|
117
|
+
interface ZkimFileHeader {
|
|
118
|
+
magic: "ZKIM";
|
|
119
|
+
version: number;
|
|
120
|
+
flags: number;
|
|
121
|
+
platformKeyId: string;
|
|
122
|
+
userId: string;
|
|
123
|
+
fileId: string;
|
|
124
|
+
createdAt: number;
|
|
125
|
+
chunkCount: number;
|
|
126
|
+
totalSize: number;
|
|
127
|
+
compressionType: number;
|
|
128
|
+
encryptionType: number;
|
|
129
|
+
hashType: number;
|
|
130
|
+
signatureType: number;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* ZKIM File Chunk Interface
|
|
134
|
+
*/
|
|
135
|
+
interface ZkimFileChunk {
|
|
136
|
+
chunkIndex: number;
|
|
137
|
+
chunkSize: number;
|
|
138
|
+
compressedSize: number;
|
|
139
|
+
encryptedSize: number;
|
|
140
|
+
nonce: Uint8Array;
|
|
141
|
+
encryptedData: Uint8Array;
|
|
142
|
+
integrityHash: Uint8Array;
|
|
143
|
+
padding: Uint8Array;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Base File Metadata Interface
|
|
147
|
+
* Used as base for ZKIM file metadata
|
|
148
|
+
*/
|
|
149
|
+
interface FileMetadata {
|
|
150
|
+
id: string;
|
|
151
|
+
name: string;
|
|
152
|
+
size: number;
|
|
153
|
+
mimeType: string;
|
|
154
|
+
hash: string;
|
|
155
|
+
createdAt: number;
|
|
156
|
+
updatedAt: number;
|
|
157
|
+
tags?: string[];
|
|
158
|
+
metadata?: Record<string, unknown>;
|
|
159
|
+
accessControl?: {
|
|
160
|
+
readAccess: string[];
|
|
161
|
+
writeAccess: string[];
|
|
162
|
+
deleteAccess: string[];
|
|
163
|
+
};
|
|
164
|
+
retentionPolicy?: {
|
|
165
|
+
expiresAt?: number;
|
|
166
|
+
maxAccessCount?: number;
|
|
167
|
+
autoDelete?: boolean;
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* ZKIM File Metadata Interface
|
|
172
|
+
* Extends base FileMetadata with ZKIM-specific fields
|
|
173
|
+
*/
|
|
174
|
+
interface ZkimFileMetadata extends Omit<FileMetadata, "id" | "name" | "size" | "hash" | "updatedAt"> {
|
|
175
|
+
fileName: string;
|
|
176
|
+
customFields?: Record<string, unknown>;
|
|
177
|
+
userId?: string;
|
|
178
|
+
createdAt: number;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* ZKIM File Interface
|
|
182
|
+
*/
|
|
183
|
+
interface ZkimFile {
|
|
184
|
+
header: ZkimFileHeader;
|
|
185
|
+
chunks: ZkimFileChunk[];
|
|
186
|
+
metadata: ZkimFileMetadata;
|
|
187
|
+
platformSignature: Uint8Array;
|
|
188
|
+
userSignature: Uint8Array;
|
|
189
|
+
contentSignature: Uint8Array;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* ZKIM File Result Interface
|
|
193
|
+
*/
|
|
194
|
+
interface ZkimFileResult {
|
|
195
|
+
success: boolean;
|
|
196
|
+
file: ZkimFile;
|
|
197
|
+
zkimFile: ZkimFile;
|
|
198
|
+
objectId: string;
|
|
199
|
+
size: number;
|
|
200
|
+
chunks: number;
|
|
201
|
+
processingTime: number;
|
|
202
|
+
compressionRatio: number;
|
|
203
|
+
encryptionOverhead: number;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* ZKIM File Search Result Interface
|
|
207
|
+
*/
|
|
208
|
+
interface ZkimFileSearchResult {
|
|
209
|
+
fileId: string;
|
|
210
|
+
objectId: string;
|
|
211
|
+
relevance: number;
|
|
212
|
+
metadata: Partial<ZkimFileMetadata>;
|
|
213
|
+
accessLevel: "full" | "metadata" | "none";
|
|
214
|
+
lastAccessed: number;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* ZKIM File Service Configuration Interface
|
|
218
|
+
*/
|
|
219
|
+
interface ZKIMFileServiceConfig {
|
|
220
|
+
enableCompression: boolean;
|
|
221
|
+
enableDeduplication: boolean;
|
|
222
|
+
chunkSize: number;
|
|
223
|
+
compressionLevel: number;
|
|
224
|
+
compressionAlgorithm: "brotli" | "gzip";
|
|
225
|
+
enableSearchableEncryption: boolean;
|
|
226
|
+
enableIntegrityValidation: boolean;
|
|
227
|
+
enableMetadataIndexing: boolean;
|
|
228
|
+
maxFileSize: number;
|
|
229
|
+
enableStreaming: boolean;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* ZKIM Encryption Configuration Interface
|
|
233
|
+
*/
|
|
234
|
+
interface ZkimEncryptionConfig {
|
|
235
|
+
enableThreeLayerEncryption: boolean;
|
|
236
|
+
enableKeyRotation: boolean;
|
|
237
|
+
enablePerfectForwardSecrecy: boolean;
|
|
238
|
+
enableCompromiseDetection: boolean;
|
|
239
|
+
defaultAlgorithm: "xchacha20-poly1305" | "aes-256-gcm";
|
|
240
|
+
keySize: number;
|
|
241
|
+
nonceSize: number;
|
|
242
|
+
compressionEnabled: boolean;
|
|
243
|
+
compressionAlgorithm: "brotli" | "gzip";
|
|
244
|
+
compressionLevel: number;
|
|
245
|
+
algorithm?: string;
|
|
246
|
+
keyRotationInterval?: number;
|
|
247
|
+
maxKeyAge?: number;
|
|
248
|
+
enableKeyValidation?: boolean;
|
|
249
|
+
enablePerformanceOptimization?: boolean;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* ZKIM Integrity Configuration Interface
|
|
253
|
+
*/
|
|
254
|
+
interface ZkimIntegrityConfig {
|
|
255
|
+
enableHeaderValidation: boolean;
|
|
256
|
+
enableChunkValidation: boolean;
|
|
257
|
+
enableSignatureValidation: boolean;
|
|
258
|
+
enableMetadataValidation: boolean;
|
|
259
|
+
enableTamperDetection: boolean;
|
|
260
|
+
validationThreshold: number;
|
|
261
|
+
enableAuditLogging: boolean;
|
|
262
|
+
enablePerformanceMetrics: boolean;
|
|
263
|
+
hashAlgorithm: "blake3" | "sha256" | "sha512";
|
|
264
|
+
signatureAlgorithm: "ed25519" | "ecdsa" | "rsa";
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* Compression Configuration Interface
|
|
268
|
+
*/
|
|
269
|
+
interface CompressionConfig {
|
|
270
|
+
algorithm: "lz4" | "zstd" | "brotli" | "gzip";
|
|
271
|
+
level: number;
|
|
272
|
+
enableDictionary: boolean;
|
|
273
|
+
enableStreaming: boolean;
|
|
274
|
+
maxDictionarySize: number;
|
|
275
|
+
enableAdaptiveCompression: boolean;
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Compression Result Interface
|
|
279
|
+
*/
|
|
280
|
+
interface CompressionResult {
|
|
281
|
+
originalSize: number;
|
|
282
|
+
compressedSize: number;
|
|
283
|
+
compressedData: Uint8Array;
|
|
284
|
+
compressionRatio: number;
|
|
285
|
+
compressionTime: number;
|
|
286
|
+
decompressionTime: number;
|
|
287
|
+
algorithm: string;
|
|
288
|
+
level: number;
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Integrity Validation Result Interface
|
|
292
|
+
*/
|
|
293
|
+
interface IntegrityValidationResult {
|
|
294
|
+
isValid: boolean;
|
|
295
|
+
validationLevel: "none" | "basic" | "full";
|
|
296
|
+
headerValid: boolean;
|
|
297
|
+
chunksValid: boolean;
|
|
298
|
+
signaturesValid: boolean;
|
|
299
|
+
metadataValid: boolean;
|
|
300
|
+
errors: string[];
|
|
301
|
+
warnings: string[];
|
|
302
|
+
validationTime: number;
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Searchable Encryption Configuration Interface
|
|
306
|
+
*/
|
|
307
|
+
interface SearchableEncryptionConfig {
|
|
308
|
+
enableOPRF: boolean;
|
|
309
|
+
enableRateLimiting: boolean;
|
|
310
|
+
enableQueryBatching: boolean;
|
|
311
|
+
enableTrapdoorRotation: boolean;
|
|
312
|
+
epochDuration: number;
|
|
313
|
+
maxQueriesPerEpoch: number;
|
|
314
|
+
bucketSizes: number[];
|
|
315
|
+
minCoverage: number;
|
|
316
|
+
enablePrivacyEnhancement: boolean;
|
|
317
|
+
enableResultPadding: boolean;
|
|
318
|
+
enableQueryLogging: boolean;
|
|
319
|
+
}
|
|
320
|
+
/**
|
|
321
|
+
* Indexed File Interface
|
|
322
|
+
*/
|
|
323
|
+
interface IndexedFile {
|
|
324
|
+
fileId: string;
|
|
325
|
+
objectId: string;
|
|
326
|
+
userId: string;
|
|
327
|
+
metadata: ZkimFileMetadata;
|
|
328
|
+
trapdoors: string[];
|
|
329
|
+
indexedAt: number;
|
|
330
|
+
lastAccessed: number;
|
|
331
|
+
accessCount: number;
|
|
332
|
+
privacyLevel: "high" | "medium" | "low";
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Query History Entry Interface
|
|
336
|
+
*/
|
|
337
|
+
interface QueryHistoryEntry {
|
|
338
|
+
queryId: string;
|
|
339
|
+
query: string;
|
|
340
|
+
userId: string;
|
|
341
|
+
timestamp: number;
|
|
342
|
+
resultsCount: number;
|
|
343
|
+
processingTime: number;
|
|
344
|
+
privacyLevel: "high" | "medium" | "low";
|
|
345
|
+
trapdoorId?: string;
|
|
346
|
+
metadata?: Record<string, unknown>;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Search Query Interface
|
|
350
|
+
*/
|
|
351
|
+
interface SearchQuery {
|
|
352
|
+
queryId: string;
|
|
353
|
+
query: string;
|
|
354
|
+
userId: string;
|
|
355
|
+
timestamp: number;
|
|
356
|
+
priority: "high" | "medium" | "low";
|
|
357
|
+
metadata?: Record<string, unknown>;
|
|
358
|
+
}
|
|
359
|
+
/**
|
|
360
|
+
* Search Result Interface
|
|
361
|
+
*/
|
|
362
|
+
interface SearchResult {
|
|
363
|
+
queryId: string;
|
|
364
|
+
results: ZkimFileSearchResult[];
|
|
365
|
+
totalResults: number;
|
|
366
|
+
processingTime: number;
|
|
367
|
+
privacyLevel: "high" | "medium" | "low";
|
|
368
|
+
metadata?: Record<string, unknown>;
|
|
369
|
+
}
|
|
370
|
+
/**
|
|
371
|
+
* Trapdoor Interface
|
|
372
|
+
*/
|
|
373
|
+
interface Trapdoor {
|
|
374
|
+
trapdoorId: string;
|
|
375
|
+
userId: string;
|
|
376
|
+
query: string;
|
|
377
|
+
epoch: number;
|
|
378
|
+
expiresAt: number;
|
|
379
|
+
usageCount: number;
|
|
380
|
+
maxUsage: number;
|
|
381
|
+
isRevoked: boolean;
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Query Batch Configuration Interface
|
|
385
|
+
*/
|
|
386
|
+
interface QueryBatchConfig {
|
|
387
|
+
enableBatching: boolean;
|
|
388
|
+
batchSize: number;
|
|
389
|
+
batchTimeout: number;
|
|
390
|
+
maxConcurrentBatches: number;
|
|
391
|
+
enableLoadBalancing: boolean;
|
|
392
|
+
enableQueryOptimization: boolean;
|
|
393
|
+
}
|
|
394
|
+
/**
|
|
395
|
+
* Query Batch Interface
|
|
396
|
+
*/
|
|
397
|
+
interface QueryBatch {
|
|
398
|
+
batchId: string;
|
|
399
|
+
queries: SearchQuery[];
|
|
400
|
+
createdAt: number;
|
|
401
|
+
status: "pending" | "processing" | "completed" | "failed";
|
|
402
|
+
results: SearchResult[];
|
|
403
|
+
processingTime: number;
|
|
404
|
+
errorCount: number;
|
|
405
|
+
}
|
|
406
|
+
/**
|
|
407
|
+
* Trapdoor Rotation Configuration Interface
|
|
408
|
+
*/
|
|
409
|
+
interface TrapdoorRotationConfig {
|
|
410
|
+
enableRotation: boolean;
|
|
411
|
+
rotationInterval: number;
|
|
412
|
+
gracePeriod: number;
|
|
413
|
+
enableRevocation: boolean;
|
|
414
|
+
maxActiveTrapdoors: number;
|
|
415
|
+
enableUsageTracking: boolean;
|
|
416
|
+
}
|
|
417
|
+
/**
|
|
418
|
+
* Trapdoor Rotation Event Interface
|
|
419
|
+
*/
|
|
420
|
+
interface TrapdoorRotationEvent {
|
|
421
|
+
eventId: string;
|
|
422
|
+
userId: string;
|
|
423
|
+
trapdoorId: string;
|
|
424
|
+
eventType: "created" | "rotated" | "revoked" | "expired";
|
|
425
|
+
timestamp: number;
|
|
426
|
+
metadata?: Record<string, unknown>;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* Usage Pattern Interface
|
|
430
|
+
*/
|
|
431
|
+
interface UsagePattern {
|
|
432
|
+
userId: string;
|
|
433
|
+
queryPatterns: string[];
|
|
434
|
+
usageFrequency: number;
|
|
435
|
+
lastUsed: number;
|
|
436
|
+
totalUsage: number;
|
|
437
|
+
anomalyScore: number;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Anomaly Detector Class
|
|
441
|
+
*/
|
|
442
|
+
declare class AnomalyDetector {
|
|
443
|
+
detectAnomaly(usagePattern: UsagePattern, currentUsage: number): {
|
|
444
|
+
isAnomaly: boolean;
|
|
445
|
+
score: number;
|
|
446
|
+
reason: string;
|
|
447
|
+
};
|
|
448
|
+
}
|
|
449
|
+
/**
|
|
450
|
+
* Cached Result Interface
|
|
451
|
+
*/
|
|
452
|
+
interface CachedResult {
|
|
453
|
+
result: SearchResult;
|
|
454
|
+
timestamp: number;
|
|
455
|
+
accessCount: number;
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Query Batcher Performance Metrics Interface
|
|
459
|
+
*/
|
|
460
|
+
interface QueryBatcherPerformanceMetrics {
|
|
461
|
+
totalBatches: number;
|
|
462
|
+
totalQueries: number;
|
|
463
|
+
totalProcessingTime: number;
|
|
464
|
+
successfulQueries: number;
|
|
465
|
+
failedQueries: number;
|
|
466
|
+
averageProcessingTime: number;
|
|
467
|
+
successRate: number;
|
|
468
|
+
averageBatchTime: number;
|
|
469
|
+
averageQueryTime: number;
|
|
470
|
+
cacheHitRate: number;
|
|
471
|
+
loadBalancingEfficiency: number;
|
|
472
|
+
updateBatchMetrics(batchTime: number): void;
|
|
473
|
+
updateQueryMetrics(queryTime: number, success: boolean): void;
|
|
474
|
+
reset(): void;
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Metadata With Access Level Interface
|
|
478
|
+
*/
|
|
479
|
+
interface MetadataWithAccessLevel {
|
|
480
|
+
fileName?: string;
|
|
481
|
+
accessLevel?: string;
|
|
482
|
+
[key: string]: unknown;
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
/**
|
|
486
|
+
* Storage Interface for @zkim/file-format
|
|
487
|
+
* Abstract storage backend for different storage implementations
|
|
488
|
+
*/
|
|
489
|
+
/**
|
|
490
|
+
* Storage backend interface
|
|
491
|
+
* Implement this interface to provide custom storage backends
|
|
492
|
+
*/
|
|
493
|
+
interface IStorageBackend {
|
|
494
|
+
/**
|
|
495
|
+
* Store data with the given key
|
|
496
|
+
*/
|
|
497
|
+
set(key: string, value: Uint8Array): Promise<void>;
|
|
498
|
+
/**
|
|
499
|
+
* Retrieve data by key
|
|
500
|
+
*/
|
|
501
|
+
get(key: string): Promise<Uint8Array | null>;
|
|
502
|
+
/**
|
|
503
|
+
* Check if key exists
|
|
504
|
+
*/
|
|
505
|
+
has(key: string): Promise<boolean>;
|
|
506
|
+
/**
|
|
507
|
+
* Delete data by key
|
|
508
|
+
*/
|
|
509
|
+
delete(key: string): Promise<void>;
|
|
510
|
+
/**
|
|
511
|
+
* Clear all data
|
|
512
|
+
*/
|
|
513
|
+
clear(): Promise<void>;
|
|
514
|
+
/**
|
|
515
|
+
* Get all keys
|
|
516
|
+
*/
|
|
517
|
+
keys(): Promise<string[]>;
|
|
518
|
+
}
|
|
519
|
+
/**
|
|
520
|
+
* In-memory storage implementation
|
|
521
|
+
* Useful for testing or temporary storage
|
|
522
|
+
*/
|
|
523
|
+
declare class InMemoryStorage implements IStorageBackend {
|
|
524
|
+
private storage;
|
|
525
|
+
set(key: string, value: Uint8Array): Promise<void>;
|
|
526
|
+
get(key: string): Promise<Uint8Array | null>;
|
|
527
|
+
has(key: string): Promise<boolean>;
|
|
528
|
+
delete(key: string): Promise<void>;
|
|
529
|
+
clear(): Promise<void>;
|
|
530
|
+
keys(): Promise<string[]>;
|
|
531
|
+
}
|
|
532
|
+
/**
|
|
533
|
+
* LocalStorage-based storage implementation
|
|
534
|
+
* For browser environments
|
|
535
|
+
*/
|
|
536
|
+
declare class LocalStorageBackend implements IStorageBackend {
|
|
537
|
+
private prefix;
|
|
538
|
+
constructor(prefix?: string);
|
|
539
|
+
private getKey;
|
|
540
|
+
set(key: string, value: Uint8Array): Promise<void>;
|
|
541
|
+
get(key: string): Promise<Uint8Array | null>;
|
|
542
|
+
has(key: string): Promise<boolean>;
|
|
543
|
+
delete(key: string): Promise<void>;
|
|
544
|
+
clear(): Promise<void>;
|
|
545
|
+
keys(): Promise<string[]>;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
/**
|
|
549
|
+
* ZKIM File Service - Core File Format Implementation
|
|
550
|
+
* Handles ZKIM file creation, management, and operations
|
|
551
|
+
*
|
|
552
|
+
* Service Flow:
|
|
553
|
+
* 1. Create ZKIM files with three-layer encryption (Platform/User/Content)
|
|
554
|
+
* 2. Manage file lifecycle and metadata
|
|
555
|
+
* 3. Provide searchable encryption capabilities
|
|
556
|
+
* 4. Ensure integrity validation and tamper detection
|
|
557
|
+
*/
|
|
558
|
+
|
|
559
|
+
declare class ZKIMFileService extends ServiceBase {
|
|
560
|
+
private readonly defaultConfig;
|
|
561
|
+
private config;
|
|
562
|
+
private storageService;
|
|
563
|
+
private logger;
|
|
564
|
+
constructor(config?: Partial<ZKIMFileServiceConfig>, logger?: ILogger, storageService?: IStorageBackend);
|
|
565
|
+
initialize(): Promise<void>;
|
|
566
|
+
/**
|
|
567
|
+
* Create a new ZKIM file with three-layer encryption
|
|
568
|
+
* @param skipCasStorage - If true, skip storing to CAS (prevents circular dependency when persisting to filesystem)
|
|
569
|
+
*/
|
|
570
|
+
createZkimFile(data: Uint8Array | string, userId: string, platformKey: Uint8Array, userKey: Uint8Array, metadata?: Partial<ZkimFileMetadata>, skipCasStorage?: boolean): Promise<ZkimFileResult>;
|
|
571
|
+
/**
|
|
572
|
+
* Decrypt and retrieve a ZKIM file
|
|
573
|
+
*/
|
|
574
|
+
decryptZkimFile(zkimFile: ZkimFile, userId: string, userKey: Uint8Array): Promise<Uint8Array>;
|
|
575
|
+
/**
|
|
576
|
+
* Get a ZKIM file by object ID
|
|
577
|
+
*/
|
|
578
|
+
getZkimFile(objectId: string): Promise<{
|
|
579
|
+
success: boolean;
|
|
580
|
+
data?: ZkimFile;
|
|
581
|
+
error?: string;
|
|
582
|
+
}>;
|
|
583
|
+
/**
|
|
584
|
+
* Search through encrypted files using searchable encryption
|
|
585
|
+
*/
|
|
586
|
+
searchFiles(query: string, userId: string, limit?: number): Promise<SearchResult>;
|
|
587
|
+
/**
|
|
588
|
+
* Validate file integrity
|
|
589
|
+
*/
|
|
590
|
+
validateFileIntegrity(zkimFile: ZkimFile): Promise<IntegrityValidationResult>;
|
|
591
|
+
/**
|
|
592
|
+
* Update file metadata
|
|
593
|
+
*/
|
|
594
|
+
updateFileMetadata(zkimFile: ZkimFile, userId: string, updates: Partial<ZkimFileMetadata>): Promise<ZkimFile>;
|
|
595
|
+
/**
|
|
596
|
+
* Expand Ed25519 seed (32 bytes) to full private key (64 bytes)
|
|
597
|
+
* Ed25519 requires 64-byte private keys: seed (32) + public key (32)
|
|
598
|
+
*/
|
|
599
|
+
private expandEd25519Key;
|
|
600
|
+
/**
|
|
601
|
+
* Derive a 64-byte Ed25519 signing key from a 32-byte encryption key
|
|
602
|
+
* Used as fallback when signing keys are not provided in metadata
|
|
603
|
+
*/
|
|
604
|
+
private deriveSigningKeyFromEncryptionKey;
|
|
605
|
+
/**
|
|
606
|
+
* Decode base64 signing key with error handling
|
|
607
|
+
*/
|
|
608
|
+
private decodeBase64SigningKey;
|
|
609
|
+
/**
|
|
610
|
+
* Validate Ed25519 private key length
|
|
611
|
+
*/
|
|
612
|
+
private validateEd25519KeyLength;
|
|
613
|
+
/**
|
|
614
|
+
* Get or derive Ed25519 signing key from metadata or encryption key
|
|
615
|
+
* Handles base64 decoding, key expansion, and fallback derivation
|
|
616
|
+
*/
|
|
617
|
+
private getOrDeriveSigningKey;
|
|
618
|
+
private generateFileId;
|
|
619
|
+
private createFileHeader;
|
|
620
|
+
private processData;
|
|
621
|
+
/**
|
|
622
|
+
* Map 3-layer encrypted content to ZKIM chunks
|
|
623
|
+
* This method chunks the contentEncrypted data (already encrypted by 3-layer encryption)
|
|
624
|
+
*/
|
|
625
|
+
private mapEncryptedContentToChunks;
|
|
626
|
+
/**
|
|
627
|
+
* Generate cryptographically random nonce for chunk storage
|
|
628
|
+
*
|
|
629
|
+
* Security Critical: Nonces MUST be random and unique for each chunk.
|
|
630
|
+
* Deterministic nonce generation breaks XChaCha20-Poly1305 security guarantees.
|
|
631
|
+
*
|
|
632
|
+
* Note: Chunks are storage chunks of already-encrypted content data.
|
|
633
|
+
* These nonces are stored in wire format for metadata purposes.
|
|
634
|
+
* Even though chunks are not re-encrypted, we use random nonces for security best practices.
|
|
635
|
+
*/
|
|
636
|
+
private generateChunkNonce;
|
|
637
|
+
private generateIntegrityHash;
|
|
638
|
+
private getUserKey;
|
|
639
|
+
private generatePadding;
|
|
640
|
+
private createFileMetadata;
|
|
641
|
+
private generateSignatures;
|
|
642
|
+
private signData;
|
|
643
|
+
private verifyUserAccess;
|
|
644
|
+
private decompressData;
|
|
645
|
+
private reconstructData;
|
|
646
|
+
private generateQueryId;
|
|
647
|
+
/**
|
|
648
|
+
* Cleanup method required by ServiceBase
|
|
649
|
+
*/
|
|
650
|
+
cleanup(): Promise<void>;
|
|
651
|
+
/**
|
|
652
|
+
* Download a ZKIM file by object ID
|
|
653
|
+
*/
|
|
654
|
+
downloadFile(objectId: string, userId: string): Promise<{
|
|
655
|
+
success: boolean;
|
|
656
|
+
data?: Uint8Array;
|
|
657
|
+
error?: string;
|
|
658
|
+
}>;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
/**
|
|
662
|
+
* Encryption Service Interface
|
|
663
|
+
* Abstract interface for encryption operations used by wire format utilities
|
|
664
|
+
*/
|
|
665
|
+
interface IEncryptionService {
|
|
666
|
+
/**
|
|
667
|
+
* Decrypt user layer encryption
|
|
668
|
+
* Returns file metadata and content key
|
|
669
|
+
*/
|
|
670
|
+
decryptUserLayer(userEncrypted: Uint8Array, userKey: Uint8Array, nonce: Uint8Array): Promise<{
|
|
671
|
+
fileId: string;
|
|
672
|
+
contentKey: Uint8Array;
|
|
673
|
+
metadata: Record<string, unknown>;
|
|
674
|
+
}>;
|
|
675
|
+
/**
|
|
676
|
+
* Decrypt platform layer encryption (optional)
|
|
677
|
+
* Returns search metadata
|
|
678
|
+
*/
|
|
679
|
+
decryptPlatformLayer(platformEncrypted: Uint8Array, platformKey: Uint8Array, nonce: Uint8Array): Promise<Record<string, unknown>>;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
/**
|
|
683
|
+
* ZKIM Encryption Service - Three-Layer Encryption Implementation
|
|
684
|
+
* Handles encryption/decryption for Platform, User, and Content layers
|
|
685
|
+
*
|
|
686
|
+
* Service Flow:
|
|
687
|
+
* 1. Platform layer encryption (search-only, never decrypts)
|
|
688
|
+
* 2. User layer encryption (full decryption authority)
|
|
689
|
+
* 3. Content layer encryption (per-file random keys)
|
|
690
|
+
* 4. Key management and rotation
|
|
691
|
+
*/
|
|
692
|
+
|
|
693
|
+
declare class ZkimEncryption extends ServiceBase implements IEncryptionService {
|
|
694
|
+
private static readonly THREE_LAYER_COUNT;
|
|
695
|
+
private static readonly COMPRESSION_LEVEL_DEFAULT;
|
|
696
|
+
private readonly defaultConfig;
|
|
697
|
+
private config;
|
|
698
|
+
private keyStore;
|
|
699
|
+
private sessionKeys;
|
|
700
|
+
private logger;
|
|
701
|
+
constructor(config?: Partial<ZkimEncryptionConfig>, logger?: ILogger);
|
|
702
|
+
initialize(): Promise<void>;
|
|
703
|
+
/**
|
|
704
|
+
* Encrypt data with three-layer encryption
|
|
705
|
+
*
|
|
706
|
+
* Three-Layer Encryption Architecture:
|
|
707
|
+
* 1. Platform Layer: Search-only encryption (never decrypts user data)
|
|
708
|
+
* - Uses platform key for searchable encryption
|
|
709
|
+
* - Enables privacy-preserving search without data access
|
|
710
|
+
* - Encrypts searchable metadata and indexes
|
|
711
|
+
*
|
|
712
|
+
* 2. User Layer: Full decryption authority
|
|
713
|
+
* - Uses user-specific key for complete data access
|
|
714
|
+
* - Enables user to decrypt all their content
|
|
715
|
+
* - Provides user-level access control
|
|
716
|
+
*
|
|
717
|
+
* 3. Content Layer: Per-file random keys
|
|
718
|
+
* - Uses unique random key for each file
|
|
719
|
+
* - Provides perfect forward secrecy
|
|
720
|
+
* - Prevents cross-file key compromise
|
|
721
|
+
*
|
|
722
|
+
* Security Properties:
|
|
723
|
+
* - Authenticated encryption with associated data (AEAD)
|
|
724
|
+
* - 256-bit security level with XChaCha20-Poly1305
|
|
725
|
+
* - Nonce reuse protection with unique nonces per layer
|
|
726
|
+
* - Integrity validation with authentication tags
|
|
727
|
+
* - Perfect forward secrecy with per-file keys
|
|
728
|
+
*
|
|
729
|
+
* @param data - Plaintext data to encrypt
|
|
730
|
+
* @param platformKey - 32-byte platform encryption key
|
|
731
|
+
* @param userKey - 32-byte user-specific encryption key
|
|
732
|
+
* @param fileId - Unique file identifier for key derivation
|
|
733
|
+
* @param metadata - Optional metadata for encryption context
|
|
734
|
+
* @returns Object containing encrypted data for each layer, content key, and nonces
|
|
735
|
+
*/
|
|
736
|
+
encryptData(data: Uint8Array, platformKey: Uint8Array, userKey: Uint8Array, fileId: string, metadata?: Record<string, unknown>): Promise<{
|
|
737
|
+
platformEncrypted: Uint8Array;
|
|
738
|
+
userEncrypted: Uint8Array;
|
|
739
|
+
contentEncrypted: Uint8Array;
|
|
740
|
+
contentKey: Uint8Array;
|
|
741
|
+
nonces: Uint8Array[];
|
|
742
|
+
}>;
|
|
743
|
+
/**
|
|
744
|
+
* Decrypt chunk data
|
|
745
|
+
*/
|
|
746
|
+
decryptChunk(chunk: ZkimFileChunk, userKey: Uint8Array, fileId: string, chunkIndex: number): Promise<Uint8Array>;
|
|
747
|
+
/**
|
|
748
|
+
* Decrypt user layer to get content key
|
|
749
|
+
*/
|
|
750
|
+
decryptUserLayer(userEncrypted: Uint8Array, userKey: Uint8Array, nonce: Uint8Array): Promise<{
|
|
751
|
+
fileId: string;
|
|
752
|
+
contentKey: Uint8Array;
|
|
753
|
+
metadata: Record<string, unknown>;
|
|
754
|
+
}>;
|
|
755
|
+
/**
|
|
756
|
+
* Decrypt platform layer (search-only metadata)
|
|
757
|
+
*/
|
|
758
|
+
decryptPlatformLayer(platformEncrypted: Uint8Array, platformKey: Uint8Array, nonce: Uint8Array): Promise<Record<string, unknown>>;
|
|
759
|
+
/**
|
|
760
|
+
* Simple decrypt method for general use
|
|
761
|
+
*/
|
|
762
|
+
decrypt(encryptedData: Uint8Array, key: Uint8Array, nonce: Uint8Array): Promise<Uint8Array>;
|
|
763
|
+
/**
|
|
764
|
+
* Compress data using configured algorithm
|
|
765
|
+
*/
|
|
766
|
+
compressData(data: Uint8Array, config?: Partial<CompressionConfig>): Promise<CompressionResult>;
|
|
767
|
+
/**
|
|
768
|
+
* Decompress data using configured algorithm
|
|
769
|
+
*/
|
|
770
|
+
decompressData(data: Uint8Array, originalSize: number, config?: Partial<CompressionConfig>): Promise<Uint8Array>;
|
|
771
|
+
/**
|
|
772
|
+
* Generate session key for secure communication
|
|
773
|
+
*/
|
|
774
|
+
generateSessionKey(peerId: string, ephemeralKey: Uint8Array): Promise<Uint8Array>;
|
|
775
|
+
/**
|
|
776
|
+
* Rotate keys for enhanced security
|
|
777
|
+
*/
|
|
778
|
+
rotateKeys(fileId: string): Promise<Uint8Array>;
|
|
779
|
+
/**
|
|
780
|
+
* Check for compromised keys
|
|
781
|
+
*/
|
|
782
|
+
checkKeyCompromise(fileId: string): Promise<boolean>;
|
|
783
|
+
protected ensureInitialized(): Promise<void>;
|
|
784
|
+
private initializeKeyManagement;
|
|
785
|
+
/**
|
|
786
|
+
* Extract searchable text from content data
|
|
787
|
+
* Tokenizes content into searchable words for privacy-preserving search
|
|
788
|
+
*/
|
|
789
|
+
private extractSearchableText;
|
|
790
|
+
/**
|
|
791
|
+
* Generate cryptographically secure random nonces for encryption
|
|
792
|
+
*
|
|
793
|
+
* Security Critical: Nonces MUST be random and unique for each encryption operation.
|
|
794
|
+
* This method uses sodium.randombytes_buf() for cryptographically random nonce generation.
|
|
795
|
+
* Deterministic nonce generation (e.g., hash-based) breaks XChaCha20-Poly1305 security guarantees.
|
|
796
|
+
*
|
|
797
|
+
* Nonce Requirements:
|
|
798
|
+
* - Must be unique for each encryption operation
|
|
799
|
+
* - Must be cryptographically random (not deterministic)
|
|
800
|
+
* - Must be 24 bytes for XChaCha20-Poly1305
|
|
801
|
+
* - Never reuse nonces with the same key
|
|
802
|
+
*
|
|
803
|
+
* Implementation: Uses sodium.randombytes_buf() for true cryptographic randomness.
|
|
804
|
+
* The fileId parameter is used ONLY for logging/tracking, NOT for nonce generation.
|
|
805
|
+
*
|
|
806
|
+
* @param fileId - File identifier (used for logging only, NOT for nonce generation)
|
|
807
|
+
* @param count - Number of nonces to generate (typically 3 for three-layer encryption)
|
|
808
|
+
* @returns Array of cryptographically random nonces generated via sodium.randombytes_buf()
|
|
809
|
+
*/
|
|
810
|
+
private generateNonces;
|
|
811
|
+
/**
|
|
812
|
+
* Core encryption method using XChaCha20-Poly1305 AEAD
|
|
813
|
+
*
|
|
814
|
+
* Cryptographic Implementation:
|
|
815
|
+
* - Uses XChaCha20-Poly1305 for authenticated encryption with associated data
|
|
816
|
+
* - Provides 256-bit security level with 24-byte nonce and 32-byte key
|
|
817
|
+
* - Implements AEAD (Authenticated Encryption with Associated Data)
|
|
818
|
+
* - Returns separate encrypted data and authentication tag
|
|
819
|
+
*
|
|
820
|
+
* Security Properties:
|
|
821
|
+
* - Authenticated encryption prevents tampering
|
|
822
|
+
* - Nonce reuse protection (each encryption uses unique nonce)
|
|
823
|
+
* - Integrity validation through authentication tag
|
|
824
|
+
* - Constant-time operations to prevent timing attacks
|
|
825
|
+
* - Forward secrecy with unique keys per operation
|
|
826
|
+
*
|
|
827
|
+
* Performance Characteristics:
|
|
828
|
+
* - Fast encryption/decryption for large data
|
|
829
|
+
* - Minimal memory overhead
|
|
830
|
+
* - Optimized for streaming operations
|
|
831
|
+
* - Hardware acceleration support where available
|
|
832
|
+
*
|
|
833
|
+
* @param data - Plaintext data to encrypt
|
|
834
|
+
* @param key - 32-byte encryption key (must be cryptographically random)
|
|
835
|
+
* @param nonce - 24-byte nonce (must be unique for each encryption)
|
|
836
|
+
* @param layer - Layer identifier for logging (platform/user/content)
|
|
837
|
+
* @returns EncryptionResult with encrypted data, tag, and metadata
|
|
838
|
+
*/
|
|
839
|
+
private encryptLayer;
|
|
840
|
+
private decryptLayer;
|
|
841
|
+
/**
|
|
842
|
+
* Create no compression result for disabled compression
|
|
843
|
+
*/
|
|
844
|
+
private createNoCompressionResult;
|
|
845
|
+
/**
|
|
846
|
+
* Compress data using Brotli algorithm
|
|
847
|
+
* Uses browser's native CompressionStream API (backendless-compatible)
|
|
848
|
+
*/
|
|
849
|
+
private compressBrotli;
|
|
850
|
+
/**
|
|
851
|
+
* Decompress data using Brotli algorithm
|
|
852
|
+
* Uses browser's native DecompressionStream API (backendless-compatible)
|
|
853
|
+
*/
|
|
854
|
+
private decompressBrotli;
|
|
855
|
+
/**
|
|
856
|
+
* Compress data using GZIP algorithm
|
|
857
|
+
* Delegates to compression-utils for mockability in tests
|
|
858
|
+
*/
|
|
859
|
+
private compressGZIP;
|
|
860
|
+
/**
|
|
861
|
+
* Decompress data using GZIP algorithm
|
|
862
|
+
* Delegates to compression-utils for mockability in tests
|
|
863
|
+
*/
|
|
864
|
+
private decompressGZIP;
|
|
865
|
+
/**
|
|
866
|
+
* Clean up resources
|
|
867
|
+
*/
|
|
868
|
+
cleanup(): Promise<void>;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* ZKIM Integrity Service - File Validation and Tamper Detection
|
|
873
|
+
* Handles integrity validation using BLAKE3 + Ed25519 signatures
|
|
874
|
+
*
|
|
875
|
+
* Service Flow:
|
|
876
|
+
* 1. Validate file header integrity
|
|
877
|
+
* 2. Verify chunk integrity with BLAKE3 hashes
|
|
878
|
+
* 3. Validate signatures (Platform, User, Content)
|
|
879
|
+
* 4. Detect tampering and provide detailed validation results
|
|
880
|
+
*/
|
|
881
|
+
|
|
882
|
+
declare class ZkimIntegrity extends ServiceBase {
|
|
883
|
+
private readonly defaultConfig;
|
|
884
|
+
private config;
|
|
885
|
+
private isInitialized;
|
|
886
|
+
private validationCache;
|
|
887
|
+
private auditLog;
|
|
888
|
+
private logger;
|
|
889
|
+
constructor(config?: Partial<ZkimIntegrityConfig>, logger?: ILogger);
|
|
890
|
+
initialize(): Promise<void>;
|
|
891
|
+
/**
|
|
892
|
+
* Validate complete ZKIM file integrity
|
|
893
|
+
*
|
|
894
|
+
* @param zkimFile - The ZKIM file to validate
|
|
895
|
+
* @param platformKey - Optional platform encryption key (required for signature verification)
|
|
896
|
+
* @param userKey - Optional user encryption key (required for signature verification)
|
|
897
|
+
*/
|
|
898
|
+
validateFile(zkimFile: ZkimFile, platformKey?: Uint8Array, userKey?: Uint8Array): Promise<IntegrityValidationResult>;
|
|
899
|
+
/**
|
|
900
|
+
* Validate specific file header
|
|
901
|
+
*/
|
|
902
|
+
validateHeader(header: ZkimFileHeader): Promise<boolean>;
|
|
903
|
+
/**
|
|
904
|
+
* Validate file chunks integrity
|
|
905
|
+
*/
|
|
906
|
+
validateChunks(chunks: ZkimFileChunk[], header: ZkimFileHeader): Promise<boolean>;
|
|
907
|
+
/**
|
|
908
|
+
* Validate file signatures
|
|
909
|
+
*
|
|
910
|
+
* @param zkimFile - The ZKIM file to validate
|
|
911
|
+
* @param platformKey - Optional platform encryption key (used to derive public key for verification)
|
|
912
|
+
* @param userKey - Optional user encryption key (used to derive public key for verification)
|
|
913
|
+
* @returns true if signatures are valid or if keys are not provided (signature verification skipped)
|
|
914
|
+
*/
|
|
915
|
+
validateSignatures(zkimFile: ZkimFile, platformKey?: Uint8Array, userKey?: Uint8Array): Promise<boolean>;
|
|
916
|
+
/**
|
|
917
|
+
* Validate file metadata
|
|
918
|
+
*/
|
|
919
|
+
validateMetadata(metadata: ZkimFileMetadata): Promise<boolean>;
|
|
920
|
+
/**
|
|
921
|
+
* Detect tampering in file
|
|
922
|
+
*/
|
|
923
|
+
detectTampering(zkimFile: ZkimFile): Promise<{
|
|
924
|
+
isTampered: boolean;
|
|
925
|
+
tamperType: string[];
|
|
926
|
+
evidence: string[];
|
|
927
|
+
}>;
|
|
928
|
+
private initializeValidationSystems;
|
|
929
|
+
private isCacheValid;
|
|
930
|
+
private calculateValidationScore;
|
|
931
|
+
private determineValidationLevel;
|
|
932
|
+
private isValidAlgorithmId;
|
|
933
|
+
private verifySignature;
|
|
934
|
+
private logAuditEntry;
|
|
935
|
+
/**
|
|
936
|
+
* Get audit log entries
|
|
937
|
+
*/
|
|
938
|
+
getAuditLog(limit?: number): Array<{
|
|
939
|
+
timestamp: number;
|
|
940
|
+
fileId: string;
|
|
941
|
+
operation: string;
|
|
942
|
+
result: boolean;
|
|
943
|
+
details: string;
|
|
944
|
+
}>;
|
|
945
|
+
/**
|
|
946
|
+
* Clear validation cache
|
|
947
|
+
*/
|
|
948
|
+
clearCache(): void;
|
|
949
|
+
/**
|
|
950
|
+
* Clean up resources
|
|
951
|
+
*/
|
|
952
|
+
cleanup(): Promise<void>;
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
/**
|
|
956
|
+
* ZKIM Searchable Encryption Service - OPRF-based Privacy-Preserving Search
|
|
957
|
+
* Implements searchable encryption using OPRF on ristretto255 curve
|
|
958
|
+
*
|
|
959
|
+
* Service Flow:
|
|
960
|
+
* 1. Index files with OPRF-based searchable encryption
|
|
961
|
+
* 2. Process search queries with privacy enhancement
|
|
962
|
+
* 3. Return results with rate limiting and padding
|
|
963
|
+
* 4. Manage trapdoor rotation and revocation
|
|
964
|
+
*/
|
|
965
|
+
|
|
966
|
+
declare class SearchableEncryption extends ServiceBase {
|
|
967
|
+
private readonly defaultConfig;
|
|
968
|
+
static search(query: SearchQuery, limit?: number): Promise<SearchResult>;
|
|
969
|
+
private config;
|
|
970
|
+
private isInitialized;
|
|
971
|
+
private fileIndex;
|
|
972
|
+
private trapdoors;
|
|
973
|
+
private queryHistory;
|
|
974
|
+
private currentEpoch;
|
|
975
|
+
private epochStartTime;
|
|
976
|
+
private oprfSecretKey;
|
|
977
|
+
private zkimFileService;
|
|
978
|
+
private saveIndexTimer;
|
|
979
|
+
private epochTimer;
|
|
980
|
+
private logger;
|
|
981
|
+
private createIndexedFile;
|
|
982
|
+
private createQueryHistoryEntry;
|
|
983
|
+
constructor(config?: Partial<SearchableEncryptionConfig>, logger?: ILogger);
|
|
984
|
+
initialize(): Promise<void>;
|
|
985
|
+
/**
|
|
986
|
+
* Index a file for searchable encryption
|
|
987
|
+
*/
|
|
988
|
+
indexFile(zkimFile: ZkimFile, userId: string): Promise<void>;
|
|
989
|
+
/**
|
|
990
|
+
* Search through encrypted files
|
|
991
|
+
*/
|
|
992
|
+
search(query: SearchQuery, limit?: number): Promise<SearchResult>;
|
|
993
|
+
/**
|
|
994
|
+
* Update file index when metadata changes
|
|
995
|
+
*/
|
|
996
|
+
updateFileIndex(zkimFile: ZkimFile, userId: string): Promise<void>;
|
|
997
|
+
/**
|
|
998
|
+
* Remove file from search index
|
|
999
|
+
*/
|
|
1000
|
+
removeFileFromIndex(fileId: string): Promise<void>;
|
|
1001
|
+
/**
|
|
1002
|
+
* Rotate trapdoors for enhanced privacy
|
|
1003
|
+
*/
|
|
1004
|
+
rotateTrapdoors(): Promise<void>;
|
|
1005
|
+
/**
|
|
1006
|
+
* Generate OPRF token for a word (public API for indexing)
|
|
1007
|
+
* Used by message indexing service to generate privacy-preserving tokens
|
|
1008
|
+
*/
|
|
1009
|
+
generateOPRFToken(word: string): Promise<string>;
|
|
1010
|
+
/**
|
|
1011
|
+
* Get search statistics
|
|
1012
|
+
*/
|
|
1013
|
+
getSearchStats(): Promise<{
|
|
1014
|
+
totalIndexedFiles: number;
|
|
1015
|
+
totalTrapdoors: number;
|
|
1016
|
+
activeTrapdoors: number;
|
|
1017
|
+
queriesThisEpoch: number;
|
|
1018
|
+
averageQueryTime: number;
|
|
1019
|
+
privacyLevels: Record<string, number>;
|
|
1020
|
+
}>;
|
|
1021
|
+
protected ensureInitialized(): Promise<void>;
|
|
1022
|
+
private initializeOPRF;
|
|
1023
|
+
private startEpochManagement;
|
|
1024
|
+
private advanceEpoch;
|
|
1025
|
+
private generateObjectId;
|
|
1026
|
+
private generateSearchTokens;
|
|
1027
|
+
/**
|
|
1028
|
+
* Generate OPRF token using Ristretto255 curve
|
|
1029
|
+
* OPRF evaluation: F(k, x) = H(x) * k
|
|
1030
|
+
* 1. Hash input to uniform distribution (BLAKE3)
|
|
1031
|
+
* 2. Map hash to Ristretto255 point (hash as scalar × base point)
|
|
1032
|
+
* 3. Multiply by secret key scalar
|
|
1033
|
+
* 4. Return base64-encoded result
|
|
1034
|
+
*/
|
|
1035
|
+
private generateToken;
|
|
1036
|
+
/**
|
|
1037
|
+
* Convert bytes to scalar for Ristretto255
|
|
1038
|
+
* Clamps bytes to valid scalar range (mod curve order)
|
|
1039
|
+
*/
|
|
1040
|
+
private bytesToScalar;
|
|
1041
|
+
/**
|
|
1042
|
+
* Generate OPRF trapdoor for search query
|
|
1043
|
+
* Same OPRF evaluation as generateToken, returns raw bytes instead of base64
|
|
1044
|
+
*/
|
|
1045
|
+
private generateOPRFTrapdoor;
|
|
1046
|
+
private determineAccessLevel;
|
|
1047
|
+
private checkRateLimit;
|
|
1048
|
+
private generateTrapdoor;
|
|
1049
|
+
private generateTrapdoorId;
|
|
1050
|
+
private performOPRFSearch;
|
|
1051
|
+
/**
|
|
1052
|
+
* Match OPRF trapdoor against indexed trapdoors using constant-time comparison
|
|
1053
|
+
* Prevents timing attacks by using sodium.memcmp for all comparisons
|
|
1054
|
+
*/
|
|
1055
|
+
private matchesOPRFQuery;
|
|
1056
|
+
private calculateRelevance;
|
|
1057
|
+
private applyPrivacyEnhancement;
|
|
1058
|
+
private addResultPadding;
|
|
1059
|
+
private selectTargetBucket;
|
|
1060
|
+
private generatePaddingResults;
|
|
1061
|
+
private generateRandomFloat;
|
|
1062
|
+
private shuffleArray;
|
|
1063
|
+
private determinePrivacyLevel;
|
|
1064
|
+
private rotateTrapdoor;
|
|
1065
|
+
private cleanupExpiredTrapdoors;
|
|
1066
|
+
private logQueryHistory;
|
|
1067
|
+
private calculatePrivacyLevels;
|
|
1068
|
+
cleanup(): Promise<void>;
|
|
1069
|
+
/**
|
|
1070
|
+
* Load file index from persistence (browser: localStorage, Node.js: in-memory only)
|
|
1071
|
+
*/
|
|
1072
|
+
private loadFileIndex;
|
|
1073
|
+
/**
|
|
1074
|
+
* Save file index to persistence (browser: localStorage as ZKIM file, Node.js: in-memory only)
|
|
1075
|
+
*/
|
|
1076
|
+
private saveFileIndex;
|
|
1077
|
+
private startAutoSave;
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
/**
|
|
1081
|
+
* Error Types and Classes for @zkim/file-format
|
|
1082
|
+
* Lightweight error handling without platform dependencies
|
|
1083
|
+
*/
|
|
1084
|
+
interface ErrorContext {
|
|
1085
|
+
message: string;
|
|
1086
|
+
type: string;
|
|
1087
|
+
source: string;
|
|
1088
|
+
operation: string;
|
|
1089
|
+
timestamp: number;
|
|
1090
|
+
severity: "low" | "medium" | "high" | "critical";
|
|
1091
|
+
metadata?: Record<string, unknown>;
|
|
1092
|
+
service?: string;
|
|
1093
|
+
userId?: string;
|
|
1094
|
+
sessionId?: string;
|
|
1095
|
+
requestId?: string;
|
|
1096
|
+
[key: string]: unknown;
|
|
1097
|
+
}
|
|
1098
|
+
interface ServiceResult<T = unknown> {
|
|
1099
|
+
success: boolean;
|
|
1100
|
+
data?: T;
|
|
1101
|
+
error?: string;
|
|
1102
|
+
errorCode?: string;
|
|
1103
|
+
context?: ErrorContext;
|
|
1104
|
+
metadata?: Record<string, unknown>;
|
|
1105
|
+
}
|
|
1106
|
+
/**
|
|
1107
|
+
* Base Service Error Class
|
|
1108
|
+
*/
|
|
1109
|
+
declare class ServiceError extends Error {
|
|
1110
|
+
readonly code?: string;
|
|
1111
|
+
readonly details?: Record<string, unknown>;
|
|
1112
|
+
constructor(message: string, options?: {
|
|
1113
|
+
code?: string;
|
|
1114
|
+
details?: Record<string, unknown>;
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
/**
|
|
1118
|
+
* ZKIM File Format Specific Errors
|
|
1119
|
+
*/
|
|
1120
|
+
declare class ZKIMFileError extends ServiceError {
|
|
1121
|
+
constructor(message: string, options?: {
|
|
1122
|
+
code?: string;
|
|
1123
|
+
details?: Record<string, unknown>;
|
|
1124
|
+
});
|
|
1125
|
+
}
|
|
1126
|
+
declare class ZKIMEncryptionError extends ServiceError {
|
|
1127
|
+
constructor(message: string, options?: {
|
|
1128
|
+
code?: string;
|
|
1129
|
+
details?: Record<string, unknown>;
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
declare class ZKIMIntegrityError extends ServiceError {
|
|
1133
|
+
constructor(message: string, options?: {
|
|
1134
|
+
code?: string;
|
|
1135
|
+
details?: Record<string, unknown>;
|
|
1136
|
+
});
|
|
1137
|
+
}
|
|
1138
|
+
declare class ZKIMStorageError extends ServiceError {
|
|
1139
|
+
constructor(message: string, options?: {
|
|
1140
|
+
code?: string;
|
|
1141
|
+
details?: Record<string, unknown>;
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
|
|
1145
|
+
/**
|
|
1146
|
+
* ZKIM Trapdoor Rotator Service - Privacy Enhancement and Key Rotation
|
|
1147
|
+
* Handles trapdoor rotation, revocation, and usage tracking for enhanced privacy
|
|
1148
|
+
*
|
|
1149
|
+
* Service Flow:
|
|
1150
|
+
* 1. Manage trapdoor lifecycle and rotation schedules
|
|
1151
|
+
* 2. Implement automatic revocation and expiration
|
|
1152
|
+
* 3. Track usage patterns and detect anomalies
|
|
1153
|
+
* 4. Provide privacy metrics and audit logging
|
|
1154
|
+
*/
|
|
1155
|
+
|
|
1156
|
+
interface TrapdoorRotatorServiceConfig extends TrapdoorRotationConfig {
|
|
1157
|
+
enableAnomalyDetection: boolean;
|
|
1158
|
+
enableAuditLogging: boolean;
|
|
1159
|
+
rotationThreshold: number;
|
|
1160
|
+
revocationThreshold: number;
|
|
1161
|
+
}
|
|
1162
|
+
declare class TrapdoorRotator extends ServiceBase {
|
|
1163
|
+
private readonly defaultConfig;
|
|
1164
|
+
private config;
|
|
1165
|
+
private trapdoors;
|
|
1166
|
+
private rotationEvents;
|
|
1167
|
+
private usagePatterns;
|
|
1168
|
+
private rotationTimer?;
|
|
1169
|
+
private anomalyDetector;
|
|
1170
|
+
private logger;
|
|
1171
|
+
constructor(config?: Partial<TrapdoorRotatorServiceConfig>, logger?: ILogger);
|
|
1172
|
+
initialize(): Promise<void>;
|
|
1173
|
+
/**
|
|
1174
|
+
* Create a new trapdoor
|
|
1175
|
+
*/
|
|
1176
|
+
createTrapdoor(userId: string, query: string, maxUsage?: number): Promise<ServiceResult<Trapdoor>>;
|
|
1177
|
+
/**
|
|
1178
|
+
* Rotate an existing trapdoor
|
|
1179
|
+
*/
|
|
1180
|
+
rotateTrapdoor(trapdoorId: string): Promise<ServiceResult<Trapdoor>>;
|
|
1181
|
+
/**
|
|
1182
|
+
* Revoke a trapdoor
|
|
1183
|
+
*/
|
|
1184
|
+
revokeTrapdoor(trapdoorId: string, reason?: string): Promise<void>;
|
|
1185
|
+
/**
|
|
1186
|
+
* Update trapdoor usage
|
|
1187
|
+
*/
|
|
1188
|
+
updateTrapdoorUsage(trapdoorId: string): Promise<ServiceResult<{
|
|
1189
|
+
shouldRotate: boolean;
|
|
1190
|
+
shouldRevoke: boolean;
|
|
1191
|
+
anomalyDetected: boolean;
|
|
1192
|
+
}>>;
|
|
1193
|
+
/**
|
|
1194
|
+
* Get trapdoor information
|
|
1195
|
+
*/
|
|
1196
|
+
getTrapdoorInfo(trapdoorId: string): ServiceResult<Trapdoor | null>;
|
|
1197
|
+
/**
|
|
1198
|
+
* Get user's active trapdoors
|
|
1199
|
+
*/
|
|
1200
|
+
getUserTrapdoors(userId: string): ServiceResult<Trapdoor[]>;
|
|
1201
|
+
/**
|
|
1202
|
+
* Get rotation events
|
|
1203
|
+
*/
|
|
1204
|
+
getRotationEvents(limit?: number): ServiceResult<TrapdoorRotationEvent[]>;
|
|
1205
|
+
/**
|
|
1206
|
+
* Get usage statistics
|
|
1207
|
+
*/
|
|
1208
|
+
getUsageStats(): ServiceResult<{
|
|
1209
|
+
totalTrapdoors: number;
|
|
1210
|
+
activeTrapdoors: number;
|
|
1211
|
+
revokedTrapdoors: number;
|
|
1212
|
+
averageUsagePerTrapdoor: number;
|
|
1213
|
+
totalRotations: number;
|
|
1214
|
+
totalRevocations: number;
|
|
1215
|
+
anomalyCount: number;
|
|
1216
|
+
}>;
|
|
1217
|
+
private initializeRotationSystem;
|
|
1218
|
+
private startRotationTimer;
|
|
1219
|
+
private performScheduledRotations;
|
|
1220
|
+
private generateTrapdoorId;
|
|
1221
|
+
private getActiveTrapdoorCount;
|
|
1222
|
+
private updateUsagePattern;
|
|
1223
|
+
private logRotationEvent;
|
|
1224
|
+
/**
|
|
1225
|
+
* Clean up resources
|
|
1226
|
+
*/
|
|
1227
|
+
cleanup(): Promise<void>;
|
|
1228
|
+
}
|
|
1229
|
+
|
|
1230
|
+
/**
|
|
1231
|
+
* ZKIM Query Batcher Service - Query Optimization and Batching
|
|
1232
|
+
* Handles query batching, load balancing, and optimization for searchable encryption
|
|
1233
|
+
*
|
|
1234
|
+
* Service Flow:
|
|
1235
|
+
* 1. Batch multiple queries for efficient processing
|
|
1236
|
+
* 2. Implement load balancing across search nodes
|
|
1237
|
+
* 3. Optimize query execution and caching
|
|
1238
|
+
* 4. Provide query analytics and performance metrics
|
|
1239
|
+
*/
|
|
1240
|
+
|
|
1241
|
+
interface QueryBatcherServiceConfig extends QueryBatchConfig {
|
|
1242
|
+
maxResultsPerQuery: number;
|
|
1243
|
+
enableQueryCaching: boolean;
|
|
1244
|
+
cacheSize: number;
|
|
1245
|
+
cacheTTL: number;
|
|
1246
|
+
enablePerformanceMetrics: boolean;
|
|
1247
|
+
}
|
|
1248
|
+
declare class QueryBatcher extends ServiceBase {
|
|
1249
|
+
private readonly defaultConfig;
|
|
1250
|
+
private config;
|
|
1251
|
+
private isInitialized;
|
|
1252
|
+
private pendingQueries;
|
|
1253
|
+
private activeBatches;
|
|
1254
|
+
private queryCache;
|
|
1255
|
+
private batchTimer;
|
|
1256
|
+
private batchCleanupTimers;
|
|
1257
|
+
private logger;
|
|
1258
|
+
private performanceMetrics;
|
|
1259
|
+
private createPerformanceMetrics;
|
|
1260
|
+
private batchCounter;
|
|
1261
|
+
constructor(config?: Partial<QueryBatcherServiceConfig>, logger?: ILogger);
|
|
1262
|
+
initialize(): Promise<void>;
|
|
1263
|
+
/**
|
|
1264
|
+
* Add query to batch processing
|
|
1265
|
+
*/
|
|
1266
|
+
addQuery(query: SearchQuery): Promise<string>;
|
|
1267
|
+
/**
|
|
1268
|
+
* Process queries in batches
|
|
1269
|
+
*/
|
|
1270
|
+
processBatch(): Promise<void>;
|
|
1271
|
+
/**
|
|
1272
|
+
* Get batch status and results
|
|
1273
|
+
*/
|
|
1274
|
+
getBatchStatus(batchId: string): Promise<QueryBatch | null>;
|
|
1275
|
+
/**
|
|
1276
|
+
* Get query results from batch
|
|
1277
|
+
*/
|
|
1278
|
+
getQueryResults(queryId: string): Promise<SearchResult | null>;
|
|
1279
|
+
/**
|
|
1280
|
+
* Get performance metrics
|
|
1281
|
+
*/
|
|
1282
|
+
getPerformanceMetrics(): Promise<{
|
|
1283
|
+
totalBatches: number;
|
|
1284
|
+
totalQueries: number;
|
|
1285
|
+
averageBatchTime: number;
|
|
1286
|
+
averageQueryTime: number;
|
|
1287
|
+
cacheHitRate: number;
|
|
1288
|
+
loadBalancingEfficiency: number;
|
|
1289
|
+
pendingQueries: number;
|
|
1290
|
+
activeBatches: number;
|
|
1291
|
+
cacheSize: number;
|
|
1292
|
+
}>;
|
|
1293
|
+
/**
|
|
1294
|
+
* Clear cache and reset metrics
|
|
1295
|
+
*/
|
|
1296
|
+
reset(): Promise<void>;
|
|
1297
|
+
protected ensureInitialized(): Promise<void>;
|
|
1298
|
+
private initializeBatchingSystem;
|
|
1299
|
+
private startBatchProcessing;
|
|
1300
|
+
private shouldStartNewBatch;
|
|
1301
|
+
private generateBatchId;
|
|
1302
|
+
private processBatchAsync;
|
|
1303
|
+
private processQuery;
|
|
1304
|
+
private getCachedResult;
|
|
1305
|
+
/**
|
|
1306
|
+
* Update query performance metrics
|
|
1307
|
+
*/
|
|
1308
|
+
private updateQueryMetrics;
|
|
1309
|
+
/**
|
|
1310
|
+
* Calculate performance score based on processing time
|
|
1311
|
+
*/
|
|
1312
|
+
private calculatePerformanceScore;
|
|
1313
|
+
private cacheResult;
|
|
1314
|
+
private generateCacheKey;
|
|
1315
|
+
private isCacheValid;
|
|
1316
|
+
/**
|
|
1317
|
+
* Clean up resources
|
|
1318
|
+
*/
|
|
1319
|
+
cleanup(): Promise<void>;
|
|
1320
|
+
}
|
|
1321
|
+
|
|
1322
|
+
/**
|
|
1323
|
+
* ZKIM Error Recovery Service
|
|
1324
|
+
*
|
|
1325
|
+
* Comprehensive error recovery strategies for ZKIM operations
|
|
1326
|
+
* including corruption recovery, validation, and repair mechanisms.
|
|
1327
|
+
*
|
|
1328
|
+
* @fileoverview Error recovery and repair strategies
|
|
1329
|
+
*/
|
|
1330
|
+
|
|
1331
|
+
/**
|
|
1332
|
+
* Recovery result interface
|
|
1333
|
+
*/
|
|
1334
|
+
interface ZkimRecoveryResult {
|
|
1335
|
+
success: boolean;
|
|
1336
|
+
recoveredData?: Uint8Array;
|
|
1337
|
+
repairActions: string[];
|
|
1338
|
+
warnings: string[];
|
|
1339
|
+
errors: string[];
|
|
1340
|
+
metadata?: Record<string, unknown>;
|
|
1341
|
+
}
|
|
1342
|
+
/**
|
|
1343
|
+
* Corruption detection result
|
|
1344
|
+
*/
|
|
1345
|
+
interface ZkimCorruptionDetection {
|
|
1346
|
+
isCorrupted: boolean;
|
|
1347
|
+
corruptionType: "header" | "chunk" | "signature" | "metadata" | "unknown";
|
|
1348
|
+
severity: "low" | "medium" | "high" | "critical";
|
|
1349
|
+
affectedChunks: number[];
|
|
1350
|
+
description: string;
|
|
1351
|
+
}
|
|
1352
|
+
/**
|
|
1353
|
+
* Repair strategy
|
|
1354
|
+
*/
|
|
1355
|
+
interface ZkimRepairStrategy {
|
|
1356
|
+
strategy: "skip" | "reconstruct" | "recover" | "fail";
|
|
1357
|
+
confidence: number;
|
|
1358
|
+
description: string;
|
|
1359
|
+
actions: string[];
|
|
1360
|
+
}
|
|
1361
|
+
declare class ZkimErrorRecovery extends ServiceBase {
|
|
1362
|
+
private readonly context;
|
|
1363
|
+
private recoveryAttempts;
|
|
1364
|
+
private logger;
|
|
1365
|
+
constructor(logger?: ILogger);
|
|
1366
|
+
/**
|
|
1367
|
+
* Initialize error recovery service
|
|
1368
|
+
*/
|
|
1369
|
+
initialize(): Promise<void>;
|
|
1370
|
+
/**
|
|
1371
|
+
* Cleanup error recovery service
|
|
1372
|
+
*/
|
|
1373
|
+
cleanup(): Promise<void>;
|
|
1374
|
+
/**
|
|
1375
|
+
* Recover from file corruption
|
|
1376
|
+
*/
|
|
1377
|
+
recoverFromCorruption(corruptedData: Uint8Array, fileId: string, options?: {
|
|
1378
|
+
maxRepairAttempts?: number;
|
|
1379
|
+
enableReconstruction?: boolean;
|
|
1380
|
+
strictValidation?: boolean;
|
|
1381
|
+
}): Promise<ZkimRecoveryResult>;
|
|
1382
|
+
/**
|
|
1383
|
+
* Validate and repair file integrity
|
|
1384
|
+
*/
|
|
1385
|
+
validateAndRepair(data: Uint8Array, fileId: string, options?: {
|
|
1386
|
+
enableRepair?: boolean;
|
|
1387
|
+
strictMode?: boolean;
|
|
1388
|
+
}): Promise<ZkimRecoveryResult>;
|
|
1389
|
+
/**
|
|
1390
|
+
* Detect corruption in file data
|
|
1391
|
+
*/
|
|
1392
|
+
private detectCorruption;
|
|
1393
|
+
/**
|
|
1394
|
+
* Determine repair strategy based on corruption type
|
|
1395
|
+
*/
|
|
1396
|
+
private determineRepairStrategy;
|
|
1397
|
+
/**
|
|
1398
|
+
* Execute recovery based on strategy
|
|
1399
|
+
*/
|
|
1400
|
+
private executeRecovery;
|
|
1401
|
+
/**
|
|
1402
|
+
* Execute skip strategy
|
|
1403
|
+
*/
|
|
1404
|
+
private executeSkipStrategy;
|
|
1405
|
+
/**
|
|
1406
|
+
* Execute reconstruct strategy
|
|
1407
|
+
*/
|
|
1408
|
+
private executeReconstructStrategy;
|
|
1409
|
+
/**
|
|
1410
|
+
* Execute recover strategy
|
|
1411
|
+
*/
|
|
1412
|
+
private executeRecoverStrategy;
|
|
1413
|
+
/**
|
|
1414
|
+
* Validate file structure
|
|
1415
|
+
*/
|
|
1416
|
+
private validateFileStructure;
|
|
1417
|
+
/**
|
|
1418
|
+
* Repair file structure
|
|
1419
|
+
*/
|
|
1420
|
+
private repairFileStructure;
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
/**
|
|
1424
|
+
* ZKIM Performance Monitor
|
|
1425
|
+
*
|
|
1426
|
+
* Comprehensive performance monitoring and metrics collection for ZKIM operations
|
|
1427
|
+
* including encryption, decryption, serialization, and parsing performance.
|
|
1428
|
+
*
|
|
1429
|
+
* @fileoverview Performance monitoring and metrics collection
|
|
1430
|
+
*/
|
|
1431
|
+
|
|
1432
|
+
/**
|
|
1433
|
+
* Performance metrics interface
|
|
1434
|
+
*/
|
|
1435
|
+
interface ZkimPerformanceMetrics {
|
|
1436
|
+
operation: string;
|
|
1437
|
+
duration: number;
|
|
1438
|
+
dataSize: number;
|
|
1439
|
+
throughput: number;
|
|
1440
|
+
memoryUsage: number;
|
|
1441
|
+
timestamp: number;
|
|
1442
|
+
success: boolean;
|
|
1443
|
+
error?: string;
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
* Performance statistics
|
|
1447
|
+
*/
|
|
1448
|
+
interface ZkimPerformanceStats {
|
|
1449
|
+
totalOperations: number;
|
|
1450
|
+
successfulOperations: number;
|
|
1451
|
+
failedOperations: number;
|
|
1452
|
+
averageDuration: number;
|
|
1453
|
+
averageThroughput: number;
|
|
1454
|
+
averageMemoryUsage: number;
|
|
1455
|
+
successRate: number;
|
|
1456
|
+
p95Duration: number;
|
|
1457
|
+
p99Duration: number;
|
|
1458
|
+
maxDuration: number;
|
|
1459
|
+
minDuration: number;
|
|
1460
|
+
}
|
|
1461
|
+
/**
|
|
1462
|
+
* Performance thresholds
|
|
1463
|
+
*/
|
|
1464
|
+
interface ZkimPerformanceThresholds {
|
|
1465
|
+
maxDuration: number;
|
|
1466
|
+
minThroughput: number;
|
|
1467
|
+
maxMemoryUsage: number;
|
|
1468
|
+
minSuccessRate: number;
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* ZKIM Performance Monitor
|
|
1472
|
+
*/
|
|
1473
|
+
declare class ZkimPerformanceMonitor extends ServiceBase {
|
|
1474
|
+
private readonly context;
|
|
1475
|
+
private metrics;
|
|
1476
|
+
private thresholds;
|
|
1477
|
+
private monitoringInterval;
|
|
1478
|
+
private logger;
|
|
1479
|
+
constructor(thresholds?: Partial<ZkimPerformanceThresholds>, logger?: ILogger);
|
|
1480
|
+
/**
|
|
1481
|
+
* Initialize performance monitoring
|
|
1482
|
+
*/
|
|
1483
|
+
initialize(): Promise<void>;
|
|
1484
|
+
/**
|
|
1485
|
+
* Get memory usage from browser performance API
|
|
1486
|
+
* Falls back to 0 if performance.memory is not available
|
|
1487
|
+
*/
|
|
1488
|
+
private getMemoryUsage;
|
|
1489
|
+
/**
|
|
1490
|
+
* Record performance metrics for an operation
|
|
1491
|
+
*/
|
|
1492
|
+
recordOperation(operation: string, duration: number, dataSize: number, success: boolean, error?: string): void;
|
|
1493
|
+
/**
|
|
1494
|
+
* Get performance statistics
|
|
1495
|
+
*/
|
|
1496
|
+
getPerformanceStats(): ZkimPerformanceStats;
|
|
1497
|
+
/**
|
|
1498
|
+
* Get performance metrics for a specific operation
|
|
1499
|
+
*/
|
|
1500
|
+
getOperationMetrics(operation: string): ZkimPerformanceMetrics[];
|
|
1501
|
+
/**
|
|
1502
|
+
* Get recent performance metrics
|
|
1503
|
+
*/
|
|
1504
|
+
getRecentMetrics(limit?: number): ZkimPerformanceMetrics[];
|
|
1505
|
+
/**
|
|
1506
|
+
* Clear performance metrics
|
|
1507
|
+
*/
|
|
1508
|
+
clearMetrics(): void;
|
|
1509
|
+
/**
|
|
1510
|
+
* Update performance thresholds
|
|
1511
|
+
*/
|
|
1512
|
+
updateThresholds(thresholds: Partial<ZkimPerformanceThresholds>): void;
|
|
1513
|
+
/**
|
|
1514
|
+
* Check if a metric value violates thresholds
|
|
1515
|
+
*/
|
|
1516
|
+
private checkMetricThreshold;
|
|
1517
|
+
/**
|
|
1518
|
+
* Generate alert message for threshold violation
|
|
1519
|
+
*/
|
|
1520
|
+
private generateAlertMessage;
|
|
1521
|
+
/**
|
|
1522
|
+
* Get performance alerts based on statistics
|
|
1523
|
+
*/
|
|
1524
|
+
getPerformanceAlerts(): string[];
|
|
1525
|
+
/**
|
|
1526
|
+
* Start performance monitoring
|
|
1527
|
+
*/
|
|
1528
|
+
private startMonitoring;
|
|
1529
|
+
/**
|
|
1530
|
+
* Perform health check
|
|
1531
|
+
*/
|
|
1532
|
+
private performHealthCheck;
|
|
1533
|
+
/**
|
|
1534
|
+
* Check performance thresholds for a single metric
|
|
1535
|
+
*/
|
|
1536
|
+
private checkThresholds;
|
|
1537
|
+
/**
|
|
1538
|
+
* Stop performance monitoring
|
|
1539
|
+
*/
|
|
1540
|
+
stopMonitoring(): void;
|
|
1541
|
+
/**
|
|
1542
|
+
* Cleanup resources
|
|
1543
|
+
*/
|
|
1544
|
+
cleanup(): Promise<void>;
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
/**
|
|
1548
|
+
* ZKIM File Wire Format Utilities
|
|
1549
|
+
*
|
|
1550
|
+
* Contains all wire format I/O operations for ZKIM files.
|
|
1551
|
+
* Wire Format: MAGIC + VERSION + FLAGS + EH_PLATFORM + EH_USER + CHUNKS + MERKLE_ROOT + SIG
|
|
1552
|
+
*
|
|
1553
|
+
* This module exports pure functions (not class methods) for wire format operations.
|
|
1554
|
+
*/
|
|
1555
|
+
|
|
1556
|
+
/**
|
|
1557
|
+
* Write little-endian u16
|
|
1558
|
+
*/
|
|
1559
|
+
declare function writeU16(value: number): Uint8Array;
|
|
1560
|
+
/**
|
|
1561
|
+
* Read little-endian u16
|
|
1562
|
+
*/
|
|
1563
|
+
declare function readU16(buffer: Uint8Array, offset: number): number;
|
|
1564
|
+
/**
|
|
1565
|
+
* Format EH header: nonce24 || tag16 (40 bytes fixed)
|
|
1566
|
+
* Ciphertext is stored in chunks, not in EH header
|
|
1567
|
+
*/
|
|
1568
|
+
declare function formatEhHeader(nonce: Uint8Array, data: {
|
|
1569
|
+
ciphertext: Uint8Array;
|
|
1570
|
+
tag: Uint8Array;
|
|
1571
|
+
} | Uint8Array): Uint8Array;
|
|
1572
|
+
/**
|
|
1573
|
+
* Calculate Merkle root from chunk integrity hashes using BLAKE3
|
|
1574
|
+
*/
|
|
1575
|
+
declare function calculateMerkleRoot(chunks: ZkimFileChunk[]): Uint8Array;
|
|
1576
|
+
/**
|
|
1577
|
+
* Generate file signature: Ed25519(BLAKE3("zkim/root" || root || manifestHash || algSuiteId || version))
|
|
1578
|
+
* Derives 64-byte signing key from 32-byte userKey using BLAKE3
|
|
1579
|
+
*/
|
|
1580
|
+
declare function generateFileSignature(merkleRoot: Uint8Array, manifestHash: Uint8Array, algSuiteId: number, version: number, userKey: Uint8Array): Promise<Uint8Array>;
|
|
1581
|
+
/**
|
|
1582
|
+
* Calculate manifest hash (BLAKE3 of EH_USER header)
|
|
1583
|
+
*/
|
|
1584
|
+
declare function calculateManifestHash(ehUser: Uint8Array): Uint8Array;
|
|
1585
|
+
/**
|
|
1586
|
+
* Write ZKIM file in wire format according to spec
|
|
1587
|
+
* Wire Format: MAGIC + VERSION + FLAGS + EH_PLATFORM + EH_USER + CHUNKS + MERKLE_ROOT + SIG
|
|
1588
|
+
*/
|
|
1589
|
+
declare function writeWireFormat(header: ZkimFileHeader, ehPlatform: Uint8Array, ehUser: Uint8Array, chunks: ZkimFileChunk[], merkleRoot: Uint8Array, signature: Uint8Array, logger?: ILogger): Uint8Array;
|
|
1590
|
+
/**
|
|
1591
|
+
* Parse ZKIM file from bytes (wire format only)
|
|
1592
|
+
*/
|
|
1593
|
+
declare function parseZkimFile(data: Uint8Array, userKey: Uint8Array, platformKey: Uint8Array, encryptionService: IEncryptionService, logger?: ILogger): Promise<ZkimFile>;
|
|
1594
|
+
|
|
1595
|
+
/**
|
|
1596
|
+
* Error Handling Utilities for @zkim/file-format
|
|
1597
|
+
* Lightweight error handling without platform dependencies
|
|
1598
|
+
*/
|
|
1599
|
+
|
|
1600
|
+
/**
|
|
1601
|
+
* Error handling utilities for services
|
|
1602
|
+
*/
|
|
1603
|
+
declare class ErrorUtils {
|
|
1604
|
+
private static logger;
|
|
1605
|
+
/**
|
|
1606
|
+
* Set custom logger instance
|
|
1607
|
+
*/
|
|
1608
|
+
static setLogger(logger: ILogger): void;
|
|
1609
|
+
/**
|
|
1610
|
+
* Create error context for consistent error handling
|
|
1611
|
+
*/
|
|
1612
|
+
static createContext(service: string, operation: string, additionalContext?: Record<string, unknown>): ErrorContext;
|
|
1613
|
+
/**
|
|
1614
|
+
* Extract error message from unknown error type
|
|
1615
|
+
*/
|
|
1616
|
+
static getErrorMessage(error: unknown): string;
|
|
1617
|
+
/**
|
|
1618
|
+
* Create a successful service result
|
|
1619
|
+
*/
|
|
1620
|
+
static createSuccessResult<T>(data: T): ServiceResult<T>;
|
|
1621
|
+
/**
|
|
1622
|
+
* Create a failed service result
|
|
1623
|
+
*/
|
|
1624
|
+
static createErrorResult<T>(error: string | Error): ServiceResult<T>;
|
|
1625
|
+
/**
|
|
1626
|
+
* Wrap async operations with error handling
|
|
1627
|
+
*/
|
|
1628
|
+
static withErrorHandling<T>(operation: () => Promise<T>, context: ErrorContext, fallback?: T): Promise<ServiceResult<T>>;
|
|
1629
|
+
/**
|
|
1630
|
+
* Handle errors with context and logging
|
|
1631
|
+
*/
|
|
1632
|
+
static handleError(error: unknown, context: ErrorContext): void;
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
/**
|
|
1636
|
+
* Environment-Agnostic Crypto Utilities for @zkim/file-format
|
|
1637
|
+
* Provides crypto operations that work in both browser and Node.js environments
|
|
1638
|
+
*/
|
|
1639
|
+
/**
|
|
1640
|
+
* Generate random bytes using libsodium
|
|
1641
|
+
*/
|
|
1642
|
+
declare function generateRandomBytes(length: number): Promise<Uint8Array>;
|
|
1643
|
+
/**
|
|
1644
|
+
* Generate random hex string
|
|
1645
|
+
*/
|
|
1646
|
+
declare function generateRandomHex(length: number): Promise<string>;
|
|
1647
|
+
/**
|
|
1648
|
+
* Hash data using BLAKE3
|
|
1649
|
+
*/
|
|
1650
|
+
declare function hashData(data: Uint8Array, outputLength?: number): Uint8Array;
|
|
1651
|
+
/**
|
|
1652
|
+
* Hash data to hex string
|
|
1653
|
+
*/
|
|
1654
|
+
declare function hashDataToHex(data: Uint8Array, outputLength?: number): string;
|
|
1655
|
+
/**
|
|
1656
|
+
* Generate key pair using X25519 (for encryption)
|
|
1657
|
+
*/
|
|
1658
|
+
declare function generateKeyPair(): Promise<{
|
|
1659
|
+
publicKey: Uint8Array;
|
|
1660
|
+
privateKey: Uint8Array;
|
|
1661
|
+
}>;
|
|
1662
|
+
/**
|
|
1663
|
+
* Generate signing key pair using Ed25519
|
|
1664
|
+
*/
|
|
1665
|
+
declare function generateSigningKeyPair(): Promise<{
|
|
1666
|
+
publicKey: Uint8Array;
|
|
1667
|
+
privateKey: Uint8Array;
|
|
1668
|
+
}>;
|
|
1669
|
+
/**
|
|
1670
|
+
* Encrypt data using XChaCha20-Poly1305
|
|
1671
|
+
*/
|
|
1672
|
+
declare function encryptData(data: Uint8Array, key: Uint8Array, nonce?: Uint8Array): Promise<{
|
|
1673
|
+
ciphertext: Uint8Array;
|
|
1674
|
+
nonce: Uint8Array;
|
|
1675
|
+
}>;
|
|
1676
|
+
/**
|
|
1677
|
+
* Decrypt data using XChaCha20-Poly1305
|
|
1678
|
+
*/
|
|
1679
|
+
declare function decryptData(ciphertext: Uint8Array, key: Uint8Array, nonce: Uint8Array): Promise<Uint8Array>;
|
|
1680
|
+
/**
|
|
1681
|
+
* Convert Uint8Array to base64 string
|
|
1682
|
+
*/
|
|
1683
|
+
declare function toBase64(data: Uint8Array): Promise<string>;
|
|
1684
|
+
/**
|
|
1685
|
+
* Convert base64 string to Uint8Array
|
|
1686
|
+
*/
|
|
1687
|
+
declare function fromBase64(data: string): Promise<Uint8Array>;
|
|
1688
|
+
/**
|
|
1689
|
+
* Convert Uint8Array to hex string
|
|
1690
|
+
*/
|
|
1691
|
+
declare function toHex(data: Uint8Array): Promise<string>;
|
|
1692
|
+
/**
|
|
1693
|
+
* Convert hex string to Uint8Array
|
|
1694
|
+
*/
|
|
1695
|
+
declare function fromHex(data: string): Promise<Uint8Array>;
|
|
1696
|
+
|
|
1697
|
+
/**
|
|
1698
|
+
* ZKIM Compression Utilities
|
|
1699
|
+
*
|
|
1700
|
+
* Abstraction layer for compression operations
|
|
1701
|
+
* Uses pako for browser compatibility, with Node.js zlib fallback for Node.js environments
|
|
1702
|
+
*
|
|
1703
|
+
* Note: Node.js zlib is only used as a fallback in Node.js runtime environments (e.g., Jest tests).
|
|
1704
|
+
* In browser environments, pako is used exclusively. The dynamic import of zlib will fail
|
|
1705
|
+
* gracefully in browsers and fall back to pako.
|
|
1706
|
+
*/
|
|
1707
|
+
|
|
1708
|
+
/**
|
|
1709
|
+
* Compress data using GZIP algorithm
|
|
1710
|
+
* Uses pako for browser compatibility, with Node.js zlib fallback
|
|
1711
|
+
*/
|
|
1712
|
+
declare function compressGzip(data: Uint8Array, level?: number, logger?: ILogger): Promise<Uint8Array>;
|
|
1713
|
+
/**
|
|
1714
|
+
* Decompress data using GZIP algorithm
|
|
1715
|
+
* Uses pako for browser compatibility, with Node.js zlib fallback
|
|
1716
|
+
*/
|
|
1717
|
+
declare function decompressGzip(data: Uint8Array, originalSize?: number, logger?: ILogger): Promise<Uint8Array>;
|
|
1718
|
+
|
|
1719
|
+
/**
|
|
1720
|
+
* Constant-Time Security Utilities
|
|
1721
|
+
*
|
|
1722
|
+
* Provides constant-time implementations of common operations to prevent
|
|
1723
|
+
* timing attacks in cryptographic applications.
|
|
1724
|
+
*
|
|
1725
|
+
* Security Features:
|
|
1726
|
+
* - Constant-time string comparison
|
|
1727
|
+
* - Constant-time array operations
|
|
1728
|
+
* - Timing attack prevention
|
|
1729
|
+
* - Secure random delays
|
|
1730
|
+
* - Memory-safe operations
|
|
1731
|
+
*/
|
|
1732
|
+
declare class ConstantTimeSecurity {
|
|
1733
|
+
private static readonly MIN_OPERATION_TIME;
|
|
1734
|
+
private static readonly MAX_OPERATION_TIME;
|
|
1735
|
+
private static readonly RANDOM_DELAY_FACTOR;
|
|
1736
|
+
/**
|
|
1737
|
+
* Constant-time string comparison
|
|
1738
|
+
*
|
|
1739
|
+
* Prevents timing attacks by ensuring comparison always takes the same time
|
|
1740
|
+
* regardless of where the strings differ.
|
|
1741
|
+
*/
|
|
1742
|
+
static constantTimeStringCompare(a: string, b: string): boolean;
|
|
1743
|
+
/**
|
|
1744
|
+
* Constant-time byte array comparison
|
|
1745
|
+
*
|
|
1746
|
+
* Prevents timing attacks on binary data comparisons.
|
|
1747
|
+
*/
|
|
1748
|
+
static constantTimeByteCompare(a: Uint8Array, b: Uint8Array): boolean;
|
|
1749
|
+
/**
|
|
1750
|
+
* Constant-time array search
|
|
1751
|
+
*
|
|
1752
|
+
* Prevents timing attacks on array search operations by always
|
|
1753
|
+
* checking all elements regardless of early matches.
|
|
1754
|
+
*/
|
|
1755
|
+
static constantTimeArrayIncludes<T>(array: T[], target: T, compareFn?: (a: T, b: T) => boolean): boolean;
|
|
1756
|
+
/**
|
|
1757
|
+
* Constant-time string array search
|
|
1758
|
+
*
|
|
1759
|
+
* Specialized version for string arrays using constant-time comparison.
|
|
1760
|
+
*/
|
|
1761
|
+
static constantTimeStringArrayIncludes(array: string[], target: string): boolean;
|
|
1762
|
+
/**
|
|
1763
|
+
* Constant-time length validation
|
|
1764
|
+
*
|
|
1765
|
+
* Validates array/string length without revealing the actual length
|
|
1766
|
+
* through timing differences.
|
|
1767
|
+
*/
|
|
1768
|
+
static constantTimeLengthCheck(data: string | Uint8Array, expectedLength: number): boolean;
|
|
1769
|
+
/**
|
|
1770
|
+
* Secure random delay
|
|
1771
|
+
*
|
|
1772
|
+
* Adds a random delay to prevent timing analysis attacks.
|
|
1773
|
+
*/
|
|
1774
|
+
static addSecureDelay(baseTime?: number): Promise<void>;
|
|
1775
|
+
/**
|
|
1776
|
+
* Timing attack prevention wrapper
|
|
1777
|
+
*
|
|
1778
|
+
* Wraps any operation with timing attack prevention measures.
|
|
1779
|
+
*/
|
|
1780
|
+
static withTimingProtection<T>(operation: () => T | Promise<T>, minTime?: number, maxTime?: number): Promise<T>;
|
|
1781
|
+
/**
|
|
1782
|
+
* Constant-time magic number validation
|
|
1783
|
+
*
|
|
1784
|
+
* Validates magic numbers without timing leaks.
|
|
1785
|
+
*/
|
|
1786
|
+
static validateMagicNumber(actual: string, expected?: string): boolean;
|
|
1787
|
+
/**
|
|
1788
|
+
* Constant-time version validation
|
|
1789
|
+
*
|
|
1790
|
+
* Validates version numbers without timing leaks.
|
|
1791
|
+
*/
|
|
1792
|
+
static validateVersion(actual: number, expected: number, minVersion?: number, maxVersion?: number): boolean;
|
|
1793
|
+
/**
|
|
1794
|
+
* Constant-time size validation
|
|
1795
|
+
*
|
|
1796
|
+
* Validates data sizes without timing leaks.
|
|
1797
|
+
*/
|
|
1798
|
+
static validateSize(actualSize: number, expectedSize: number, tolerance?: number): boolean;
|
|
1799
|
+
/**
|
|
1800
|
+
* Secure comparison with logging
|
|
1801
|
+
*
|
|
1802
|
+
* Performs constant-time comparison with security logging.
|
|
1803
|
+
*/
|
|
1804
|
+
static secureCompare<T>(actual: T, expected: T, context: string, compareFn?: (a: T, b: T) => boolean): boolean;
|
|
1805
|
+
/**
|
|
1806
|
+
* Memory-safe string operations
|
|
1807
|
+
*
|
|
1808
|
+
* Performs string operations without memory leaks.
|
|
1809
|
+
*/
|
|
1810
|
+
static memorySafeStringOperation(operation: (str: string) => string): (str: string) => string;
|
|
1811
|
+
/**
|
|
1812
|
+
* Secure array operations
|
|
1813
|
+
*
|
|
1814
|
+
* Performs array operations with timing attack prevention.
|
|
1815
|
+
*/
|
|
1816
|
+
static secureArrayOperation<T>(array: T[], operation: (arr: T[]) => boolean): boolean;
|
|
1817
|
+
/**
|
|
1818
|
+
* Timing attack detection
|
|
1819
|
+
*
|
|
1820
|
+
* Detects potential timing attacks by monitoring operation times.
|
|
1821
|
+
*/
|
|
1822
|
+
static detectTimingAttack(operationTimes: number[], threshold?: number): boolean;
|
|
1823
|
+
}
|
|
1824
|
+
|
|
1825
|
+
/**
|
|
1826
|
+
* ZKIM File Format Constants
|
|
1827
|
+
* Single source of truth for all file format-related constants
|
|
1828
|
+
*/
|
|
1829
|
+
/**
|
|
1830
|
+
* ZKIM Encryption Constants
|
|
1831
|
+
*/
|
|
1832
|
+
declare const ZKIM_ENCRYPTION_CONSTANTS: {
|
|
1833
|
+
readonly DEFAULT_ALGORITHM: "xchacha20-poly1305";
|
|
1834
|
+
readonly KEY_SIZE: 32;
|
|
1835
|
+
readonly NONCE_SIZE: 24;
|
|
1836
|
+
readonly TAG_SIZE: 16;
|
|
1837
|
+
readonly SALT_LENGTH: 32;
|
|
1838
|
+
readonly ITERATIONS: 100000;
|
|
1839
|
+
readonly KEY_ROTATION_INTERVAL: number;
|
|
1840
|
+
readonly MAX_KEY_AGE: number;
|
|
1841
|
+
readonly ED25519_PRIVATE_KEY_SIZE: 64;
|
|
1842
|
+
readonly ED25519_PUBLIC_KEY_SIZE: 32;
|
|
1843
|
+
readonly EH_HEADER_SIZE: 40;
|
|
1844
|
+
readonly MAGIC_BYTES_SIZE: 4;
|
|
1845
|
+
readonly VERSION_BYTES_SIZE: 2;
|
|
1846
|
+
readonly FLAGS_BYTES_SIZE: 2;
|
|
1847
|
+
readonly MERKLE_ROOT_SIZE: 32;
|
|
1848
|
+
readonly SIGNATURE_SIZE: 64;
|
|
1849
|
+
readonly WORKER_BATCH_SIZE: 25;
|
|
1850
|
+
readonly WORKER_BATCH_INTERVAL_MS: 25;
|
|
1851
|
+
};
|
|
1852
|
+
/**
|
|
1853
|
+
* ZKIM File Service Constants
|
|
1854
|
+
*/
|
|
1855
|
+
declare const ZKIM_FILE_SERVICE_CONSTANTS: {
|
|
1856
|
+
readonly COMPRESSION_TYPE_MAP: {
|
|
1857
|
+
readonly brotli: 1;
|
|
1858
|
+
readonly gzip: 2;
|
|
1859
|
+
};
|
|
1860
|
+
readonly COMPRESSION_TYPE_REVERSE_MAP: {
|
|
1861
|
+
readonly 1: "brotli";
|
|
1862
|
+
readonly 2: "gzip";
|
|
1863
|
+
};
|
|
1864
|
+
readonly DEFAULT_MAGIC: "ZKIM";
|
|
1865
|
+
readonly DEFAULT_VERSION: 1;
|
|
1866
|
+
readonly NONCE_PREFIX: "zkim/nonce";
|
|
1867
|
+
readonly KEY_PREFIX: "zkim/key";
|
|
1868
|
+
readonly USER_KEY_PREFIX: "zkim/userkey";
|
|
1869
|
+
};
|
|
1870
|
+
/**
|
|
1871
|
+
* File Processing Constants
|
|
1872
|
+
*/
|
|
1873
|
+
declare const FILE_PROCESSING_CONSTANTS: {
|
|
1874
|
+
readonly DEFAULT_CHUNK_SIZE: number;
|
|
1875
|
+
readonly MAX_CHUNK_SIZE: number;
|
|
1876
|
+
readonly MIN_CHUNK_SIZE: 1024;
|
|
1877
|
+
readonly DEFAULT_MAX_FILE_SIZE: number;
|
|
1878
|
+
readonly COMPRESSION_LEVEL: 6;
|
|
1879
|
+
readonly DEFAULT_COMPRESSION_ALGORITHM: "gzip";
|
|
1880
|
+
readonly MAX_METADATA_SIZE: number;
|
|
1881
|
+
};
|
|
1882
|
+
/**
|
|
1883
|
+
* Service Lifecycle Constants
|
|
1884
|
+
*/
|
|
1885
|
+
declare const ZKIM_SERVICE_LIFECYCLE_CONSTANTS: {
|
|
1886
|
+
readonly INITIALIZATION_TIMEOUT: 30000;
|
|
1887
|
+
readonly CLEANUP_TIMEOUT: 10000;
|
|
1888
|
+
readonly MAX_INITIALIZATION_RETRIES: 3;
|
|
1889
|
+
readonly HEALTH_CHECK_INTERVAL: 60000;
|
|
1890
|
+
};
|
|
1891
|
+
/**
|
|
1892
|
+
* ZKIM Binary Format Constants
|
|
1893
|
+
* Magic bytes represent ASCII "ZKIM": Z=0x5a, K=0x4b, I=0x49, M=0x4d
|
|
1894
|
+
*/
|
|
1895
|
+
declare const ZKIM_BINARY_CONSTANTS: {
|
|
1896
|
+
readonly MAGIC_BYTE_Z: 90;
|
|
1897
|
+
readonly MAGIC_BYTE_K: 75;
|
|
1898
|
+
readonly MAGIC_BYTE_I: 73;
|
|
1899
|
+
readonly MAGIC_BYTE_M: 77;
|
|
1900
|
+
readonly MAGIC_BYTES: Uint8Array<ArrayBuffer>;
|
|
1901
|
+
readonly VERSION: 1;
|
|
1902
|
+
readonly HEADER_SIZE: 16;
|
|
1903
|
+
readonly MAX_FILE_SIZE: number;
|
|
1904
|
+
readonly CHUNK_SIZE: number;
|
|
1905
|
+
};
|
|
1906
|
+
|
|
1907
|
+
/**
|
|
1908
|
+
* @zkim/file-format - ZKIM Secure File Format
|
|
1909
|
+
*
|
|
1910
|
+
* A secure, encrypted file format with three-layer encryption,
|
|
1911
|
+
* integrity validation, and privacy-preserving search capabilities.
|
|
1912
|
+
*
|
|
1913
|
+
* @packageDocumentation
|
|
1914
|
+
*/
|
|
1915
|
+
|
|
1916
|
+
declare const VERSION = "1.0.0";
|
|
1917
|
+
|
|
1918
|
+
export { AnomalyDetector, type CachedResult, ConsoleLogger, ConstantTimeSecurity, type ErrorContext, ErrorUtils, FILE_PROCESSING_CONSTANTS, type IEncryptionService, type ILogger, type IStorageBackend, InMemoryStorage, type IndexedFile, type IntegrityValidationResult, LocalStorageBackend, type MetadataWithAccessLevel, type QueryBatch, type QueryBatchConfig, QueryBatcher, type QueryBatcherPerformanceMetrics, type QueryHistoryEntry, type SearchQuery, type SearchResult, SearchableEncryption, type SearchableEncryptionConfig, ServiceBase, ServiceError, type ServiceResult, SingletonBase, type Trapdoor, type TrapdoorRotationConfig, type TrapdoorRotationEvent, TrapdoorRotator, type UsagePattern, VERSION, ZKIMEncryptionError, ZKIMFileError, ZKIMFileService, type ZKIMFileServiceConfig, ZKIMIntegrityError, ZKIMStorageError, ZKIM_BINARY_CONSTANTS, ZKIM_ENCRYPTION_CONSTANTS, ZKIM_FILE_SERVICE_CONSTANTS, ZKIM_SERVICE_LIFECYCLE_CONSTANTS, type ZkimCorruptionDetection, ZkimEncryption, type ZkimEncryptionConfig, ZkimErrorRecovery, type ZkimFile, type ZkimFileChunk, type ZkimFileHeader, type ZkimFileMetadata, type ZkimFileResult, type ZkimFileSearchResult, ZkimIntegrity, type ZkimIntegrityConfig, type ZkimPerformanceMetrics, ZkimPerformanceMonitor, type ZkimPerformanceStats, type ZkimPerformanceThresholds, type ZkimRecoveryResult, type ZkimRepairStrategy, calculateManifestHash, calculateMerkleRoot, compressGzip, decompressGzip, decryptData, defaultLogger, encryptData, formatEhHeader, fromBase64, fromHex, generateFileSignature, generateKeyPair, generateRandomBytes, generateRandomHex, generateSigningKeyPair, hashData, hashDataToHex, parseZkimFile, readU16, toBase64, toHex, writeU16, writeWireFormat };
|