@digitaldefiance/ecies-lib 4.10.6 → 4.10.8
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/README.md +577 -17
- package/package.json +1 -1
- package/src/examples/typed-configuration-usage.d.ts +12 -0
- package/src/examples/typed-configuration-usage.d.ts.map +1 -0
- package/src/examples/typed-configuration-usage.js +98 -0
- package/src/examples/typed-configuration-usage.js.map +1 -0
- package/src/index.d.ts +1 -0
- package/src/index.d.ts.map +1 -1
- package/src/index.js +1 -0
- package/src/index.js.map +1 -1
- package/src/typed-configuration.d.ts +162 -0
- package/src/typed-configuration.d.ts.map +1 -0
- package/src/typed-configuration.js +247 -0
- package/src/typed-configuration.js.map +1 -0
package/README.md
CHANGED
|
@@ -8,9 +8,9 @@ Production-ready, browser-compatible ECIES (Elliptic Curve Integrated Encryption
|
|
|
8
8
|
|
|
9
9
|
Part of [Express Suite](https://github.com/Digital-Defiance/express-suite)
|
|
10
10
|
|
|
11
|
-
**Current Version: v4.7
|
|
11
|
+
**Current Version: v4.10.7**
|
|
12
12
|
|
|
13
|
-
This library implements a modern, enterprise-grade ECIES protocol (v4.0) featuring HKDF key derivation, AAD binding, and optimized multi-recipient encryption. It includes a pluggable ID provider system, memory-efficient streaming encryption, and
|
|
13
|
+
This library implements a modern, enterprise-grade ECIES protocol (v4.0) featuring HKDF key derivation, AAD binding, and optimized multi-recipient encryption. It includes a pluggable ID provider system with PlatformID support, memory-efficient streaming encryption, comprehensive internationalization, and a complete cryptographic voting system with 15+ voting methods.
|
|
14
14
|
|
|
15
15
|
## Features
|
|
16
16
|
|
|
@@ -30,12 +30,24 @@ This library implements a modern, enterprise-grade ECIES protocol (v4.0) featuri
|
|
|
30
30
|
- **Single**: Includes data length prefix.
|
|
31
31
|
- **Multiple**: Efficient encryption for up to 65,535 recipients.
|
|
32
32
|
|
|
33
|
-
###
|
|
33
|
+
### 🗳️ Cryptographic Voting System
|
|
34
|
+
|
|
35
|
+
- **15+ Voting Methods**: Plurality, Approval, Weighted, Borda Count, Score, Ranked Choice (IRV), STAR, STV, Yes/No, Supermajority, and more
|
|
36
|
+
- **Government-Grade Security**: Homomorphic encryption, verifiable receipts, immutable audit logs, public bulletin board
|
|
37
|
+
- **Role Separation**: Poll aggregators cannot decrypt votes until closure (separate PollTallier)
|
|
38
|
+
- **Multi-Round Support**: True IRV, STAR voting, STV with intermediate decryption
|
|
39
|
+
- **Hierarchical Aggregation**: Precinct → County → State → National vote aggregation
|
|
40
|
+
- **Event Logging**: Comprehensive audit trail with microsecond timestamps
|
|
41
|
+
- **Browser Compatible**: Works in Node.js and modern browsers
|
|
42
|
+
|
|
43
|
+
### 🆔 Enhanced Identity & Management
|
|
34
44
|
|
|
35
45
|
- **Pluggable ID Providers**:
|
|
36
|
-
- **Flexible IDs**: Support for `ObjectId` (12 bytes), `GUID`/`UUID` (16 bytes), or custom formats (1-255 bytes)
|
|
37
|
-
- **
|
|
38
|
-
- **
|
|
46
|
+
- **Flexible IDs**: Support for `ObjectId` (12 bytes), `GUID`/`UUID` (16 bytes), or custom formats (1-255 bytes)
|
|
47
|
+
- **PlatformID Type**: Generic type system supporting `Uint8Array | GuidV4 | ObjectId | string`
|
|
48
|
+
- **Auto-Sync**: Configuration automatically adapts all cryptographic constants to the selected ID provider
|
|
49
|
+
- **Member System**: User abstraction with cryptographic operations, fully integrated with the configured ID provider
|
|
50
|
+
- **Strong Typing**: New typed configuration system provides compile-time type safety for ID operations
|
|
39
51
|
- **Key Management**:
|
|
40
52
|
- **BIP39**: Mnemonic phrase generation (12-24 words).
|
|
41
53
|
- **HD Wallets**: BIP32/BIP44 hierarchical deterministic derivation.
|
|
@@ -43,10 +55,11 @@ This library implements a modern, enterprise-grade ECIES protocol (v4.0) featuri
|
|
|
43
55
|
|
|
44
56
|
### 🚀 Advanced Capabilities
|
|
45
57
|
|
|
46
|
-
- **Streaming Encryption**: Memory-efficient processing for large files (<10MB RAM usage for any file size)
|
|
47
|
-
- **Internationalization (i18n)**: Automatic error translation in 8 languages (en-US, en-GB, fr, es, de, zh-CN, ja, uk)
|
|
48
|
-
- **Runtime Configuration**: Injectable configuration profiles via `ConstantsRegistry` for dependency injection and testing
|
|
49
|
-
- **Cross-Platform**: Fully compatible with Node.js 18+ and modern browsers (Chrome, Edge, Firefox, Safari)
|
|
58
|
+
- **Streaming Encryption**: Memory-efficient processing for large files (<10MB RAM usage for any file size)
|
|
59
|
+
- **Internationalization (i18n)**: Automatic error translation in 8 languages (en-US, en-GB, fr, es, de, zh-CN, ja, uk)
|
|
60
|
+
- **Runtime Configuration**: Injectable configuration profiles via `ConstantsRegistry` for dependency injection and testing
|
|
61
|
+
- **Cross-Platform**: Fully compatible with Node.js 18+ and modern browsers (Chrome, Edge, Firefox, Safari)
|
|
62
|
+
- **Voting System**: Complete cryptographic voting implementation with government-grade security requirements
|
|
50
63
|
|
|
51
64
|
## Installation
|
|
52
65
|
|
|
@@ -160,18 +173,25 @@ The library implements a robust ECIES variant designed for security and efficien
|
|
|
160
173
|
|
|
161
174
|
### ID Provider System
|
|
162
175
|
|
|
163
|
-
The library is agnostic to the format of unique identifiers. The `IdProvider` system drives the entire configuration:
|
|
176
|
+
The library is agnostic to the format of unique identifiers. The `IdProvider` system drives the entire configuration and now supports the `PlatformID` type for enhanced cross-platform compatibility:
|
|
164
177
|
|
|
165
178
|
- **ObjectIdProvider** (Default): 12-byte MongoDB-style IDs.
|
|
166
179
|
- **GuidV4Provider**: 16-byte raw GUIDs.
|
|
167
180
|
- **UuidProvider**: 16-byte UUIDs (string representation handles dashes).
|
|
168
181
|
- **CustomIdProvider**: Define your own size (1-255 bytes).
|
|
169
182
|
|
|
183
|
+
The `PlatformID` type supports multiple ID formats:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
export type PlatformID = Uint8Array | GuidV4 | ObjectId | string;
|
|
187
|
+
```
|
|
188
|
+
|
|
170
189
|
When you configure an ID provider, the library automatically:
|
|
171
190
|
|
|
172
191
|
- Updates `MEMBER_ID_LENGTH`.
|
|
173
192
|
- Updates `ECIES.MULTIPLE.RECIPIENT_ID_SIZE`.
|
|
174
193
|
- Validates that all internal constants are consistent.
|
|
194
|
+
- Provides seamless integration with the voting system through generic type parameters.
|
|
175
195
|
|
|
176
196
|
## Quick Start
|
|
177
197
|
|
|
@@ -182,7 +202,8 @@ import {
|
|
|
182
202
|
ECIESService,
|
|
183
203
|
getEciesI18nEngine,
|
|
184
204
|
createRuntimeConfiguration,
|
|
185
|
-
ObjectIdProvider
|
|
205
|
+
ObjectIdProvider,
|
|
206
|
+
getEnhancedIdProvider
|
|
186
207
|
} from '@digitaldefiance/ecies-lib';
|
|
187
208
|
|
|
188
209
|
// 1. Initialize i18n (required once)
|
|
@@ -208,9 +229,228 @@ const encrypted = await ecies.encryptSimpleOrSingle(false, publicKey, message);
|
|
|
208
229
|
const decrypted = await ecies.decryptSimpleOrSingleWithHeader(false, privateKey, encrypted);
|
|
209
230
|
|
|
210
231
|
console.log(new TextDecoder().decode(decrypted)); // "Hello, Secure World!"
|
|
232
|
+
|
|
233
|
+
// 6. Strong Typing for ID Operations (NEW!)
|
|
234
|
+
const idProvider = getEnhancedIdProvider<ObjectId>();
|
|
235
|
+
const objectId = idProvider.generateTyped(); // Returns ObjectId - strongly typed!
|
|
236
|
+
const serialized = idProvider.serializeTyped(objectId); // Accepts ObjectId directly
|
|
237
|
+
const deserialized = idProvider.deserializeTyped(serialized); // Returns ObjectId
|
|
211
238
|
```
|
|
212
239
|
|
|
213
|
-
|
|
240
|
+
## Cryptographic Voting System
|
|
241
|
+
|
|
242
|
+
The library includes a complete cryptographic voting system with government-grade security features, supporting 15+ voting methods from simple plurality to complex ranked choice voting.
|
|
243
|
+
|
|
244
|
+
### Quick Start - Voting
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
import {
|
|
248
|
+
ECIESService,
|
|
249
|
+
Member,
|
|
250
|
+
MemberType,
|
|
251
|
+
EmailString
|
|
252
|
+
} from '@digitaldefiance/ecies-lib';
|
|
253
|
+
import {
|
|
254
|
+
PollFactory,
|
|
255
|
+
VoteEncoder,
|
|
256
|
+
PollTallier,
|
|
257
|
+
VotingMethod
|
|
258
|
+
} from '@digitaldefiance/ecies-lib/voting';
|
|
259
|
+
|
|
260
|
+
// 1. Create authority with voting keys
|
|
261
|
+
const ecies = new ECIESService();
|
|
262
|
+
const { member: authority } = Member.newMember(
|
|
263
|
+
ecies,
|
|
264
|
+
MemberType.System,
|
|
265
|
+
'Election Authority',
|
|
266
|
+
new EmailString('authority@example.com')
|
|
267
|
+
);
|
|
268
|
+
await authority.deriveVotingKeys();
|
|
269
|
+
|
|
270
|
+
// 2. Create poll
|
|
271
|
+
const poll = PollFactory.createPlurality(
|
|
272
|
+
['Alice', 'Bob', 'Charlie'],
|
|
273
|
+
authority
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
// 3. Create voter and cast vote
|
|
277
|
+
const { member: voter } = Member.newMember(
|
|
278
|
+
ecies,
|
|
279
|
+
MemberType.User,
|
|
280
|
+
'Voter',
|
|
281
|
+
new EmailString('voter@example.com')
|
|
282
|
+
);
|
|
283
|
+
await voter.deriveVotingKeys();
|
|
284
|
+
|
|
285
|
+
const encoder = new VoteEncoder(authority.votingPublicKey!);
|
|
286
|
+
const vote = encoder.encodePlurality(0, 3); // Vote for Alice
|
|
287
|
+
const receipt = poll.vote(voter, vote);
|
|
288
|
+
|
|
289
|
+
// 4. Close and tally
|
|
290
|
+
poll.close();
|
|
291
|
+
const tallier = new PollTallier(
|
|
292
|
+
authority,
|
|
293
|
+
authority.votingPrivateKey!,
|
|
294
|
+
authority.votingPublicKey!
|
|
295
|
+
);
|
|
296
|
+
const results = tallier.tally(poll);
|
|
297
|
+
|
|
298
|
+
console.log('Winner:', results.choices[results.winner!]);
|
|
299
|
+
console.log('Tallies:', results.tallies);
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Supported Voting Methods
|
|
303
|
+
|
|
304
|
+
The system supports 15+ voting methods classified by security level:
|
|
305
|
+
|
|
306
|
+
#### ✅ Fully Secure (Single-round, Privacy-preserving)
|
|
307
|
+
- **Plurality** - First-past-the-post (most common) ✅ **Fully Implemented**
|
|
308
|
+
- **Approval** - Vote for multiple candidates ✅ **Fully Implemented**
|
|
309
|
+
- **Weighted** - Stakeholder voting with configurable limits ✅ **Fully Implemented**
|
|
310
|
+
- **Borda Count** - Ranked voting with point allocation ✅ **Fully Implemented**
|
|
311
|
+
- **Score Voting** - Rate candidates 0-10 ✅ **Fully Implemented**
|
|
312
|
+
- **Yes/No** - Referendums and ballot measures ✅ **Fully Implemented**
|
|
313
|
+
- **Yes/No/Abstain** - With abstention option ✅ **Fully Implemented**
|
|
314
|
+
- **Supermajority** - Requires 2/3 or 3/4 threshold ✅ **Fully Implemented**
|
|
315
|
+
|
|
316
|
+
#### ⚠️ Multi-Round (Requires intermediate decryption)
|
|
317
|
+
- **Ranked Choice (IRV)** - Instant runoff with elimination ✅ **Fully Implemented**
|
|
318
|
+
- **Two-Round** - Top 2 runoff election ✅ **Fully Implemented**
|
|
319
|
+
- **STAR** - Score Then Automatic Runoff ✅ **Fully Implemented**
|
|
320
|
+
- **STV** - Single Transferable Vote (proportional representation) ✅ **Fully Implemented**
|
|
321
|
+
|
|
322
|
+
#### ❌ Insecure (No privacy - for special cases only)
|
|
323
|
+
- **Quadratic** - Quadratic voting (requires non-homomorphic operations) ✅ **Fully Implemented**
|
|
324
|
+
- **Consensus** - Requires 95%+ agreement ✅ **Fully Implemented**
|
|
325
|
+
- **Consent-Based** - Sociocracy-style (no strong objections) ✅ **Fully Implemented**
|
|
326
|
+
|
|
327
|
+
### Voting System Architecture
|
|
328
|
+
|
|
329
|
+
```
|
|
330
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
331
|
+
│ SECURE ARCHITECTURE │
|
|
332
|
+
├─────────────────────────────────────────────────────────────┤
|
|
333
|
+
│ │
|
|
334
|
+
│ Poll (Vote Aggregator) │
|
|
335
|
+
│ ├─ Paillier PUBLIC key only ← encrypts & aggregates │
|
|
336
|
+
│ ├─ Authority's EC keys ← signs receipts │
|
|
337
|
+
│ └─ Cannot decrypt votes │
|
|
338
|
+
│ │
|
|
339
|
+
│ PollTallier (Separate Entity) │
|
|
340
|
+
│ ├─ Paillier PRIVATE key ← decrypts ONLY after close │
|
|
341
|
+
│ └─ Computes results │
|
|
342
|
+
│ │
|
|
343
|
+
│ Voter (Member) │
|
|
344
|
+
│ ├─ EC keypair ← verifies receipts │
|
|
345
|
+
│ └─ Voting public key ← encrypts votes │
|
|
346
|
+
│ │
|
|
347
|
+
└─────────────────────────────────────────────────────────────┘
|
|
348
|
+
```
|
|
349
|
+
|
|
350
|
+
### Government Requirements
|
|
351
|
+
|
|
352
|
+
The voting system meets government-grade requirements:
|
|
353
|
+
|
|
354
|
+
- **✅ Immutable Audit Log** - Cryptographic hash chain for all operations
|
|
355
|
+
- **✅ Public Bulletin Board** - Transparent, append-only vote publication with Merkle tree integrity
|
|
356
|
+
- **✅ Event Logger** - Comprehensive event tracking with microsecond timestamps
|
|
357
|
+
- **✅ Verifiable Receipts** - Cryptographically signed confirmations
|
|
358
|
+
- **✅ Role Separation** - Poll aggregator cannot decrypt votes
|
|
359
|
+
- **✅ Homomorphic Encryption** - Votes remain encrypted until tally
|
|
360
|
+
|
|
361
|
+
### Example: Ranked Choice Voting
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
import { PollFactory, VoteEncoder, PollTallier } from '@digitaldefiance/ecies-lib/voting';
|
|
365
|
+
|
|
366
|
+
// Create ranked choice poll
|
|
367
|
+
const poll = PollFactory.createRankedChoice(
|
|
368
|
+
['Alice', 'Bob', 'Charlie', 'Diana'],
|
|
369
|
+
authority
|
|
370
|
+
);
|
|
371
|
+
|
|
372
|
+
const encoder = new VoteEncoder(authority.votingPublicKey!);
|
|
373
|
+
|
|
374
|
+
// Voter ranks: Alice > Bob > Charlie (Diana not ranked)
|
|
375
|
+
const vote = encoder.encodeRankedChoice([0, 1, 2], 4);
|
|
376
|
+
const receipt = poll.vote(voter, vote);
|
|
377
|
+
|
|
378
|
+
// Verify receipt
|
|
379
|
+
const isValid = poll.verifyReceipt(voter, receipt);
|
|
380
|
+
|
|
381
|
+
// Close and tally with IRV elimination
|
|
382
|
+
poll.close();
|
|
383
|
+
const results = tallier.tally(poll);
|
|
384
|
+
|
|
385
|
+
console.log('Winner:', results.choices[results.winner!]);
|
|
386
|
+
console.log('Elimination rounds:', results.rounds);
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
### Security Validation
|
|
390
|
+
|
|
391
|
+
```typescript
|
|
392
|
+
import { VotingSecurityValidator, SecurityLevel } from '@digitaldefiance/ecies-lib/voting';
|
|
393
|
+
|
|
394
|
+
// Check security level
|
|
395
|
+
const level = VotingSecurityValidator.getSecurityLevel(VotingMethod.Plurality);
|
|
396
|
+
console.log(level); // SecurityLevel.FullyHomomorphic
|
|
397
|
+
|
|
398
|
+
// Validate before use (throws if insecure)
|
|
399
|
+
VotingSecurityValidator.validate(VotingMethod.Quadratic); // Throws error
|
|
400
|
+
|
|
401
|
+
// Allow insecure methods explicitly
|
|
402
|
+
VotingSecurityValidator.validate(VotingMethod.Quadratic, { allowInsecure: true });
|
|
403
|
+
```
|
|
404
|
+
|
|
405
|
+
### 2. Strong Typing for ID Providers (NEW!)
|
|
406
|
+
|
|
407
|
+
The library now provides strongly-typed alternatives to the weak typing pattern `Constants.idProvider.generate()`:
|
|
408
|
+
|
|
409
|
+
```typescript
|
|
410
|
+
import {
|
|
411
|
+
getRuntimeConfiguration,
|
|
412
|
+
getEnhancedIdProvider,
|
|
413
|
+
getTypedIdProvider,
|
|
414
|
+
createObjectIdConfiguration
|
|
415
|
+
} from '@digitaldefiance/ecies-lib';
|
|
416
|
+
import { ObjectId } from 'bson';
|
|
417
|
+
|
|
418
|
+
// BEFORE: Weak typing (still works for compatibility)
|
|
419
|
+
const Constants = getRuntimeConfiguration();
|
|
420
|
+
const rawBytes = Constants.idProvider.generate(); // Returns Uint8Array, no strong typing
|
|
421
|
+
const nativeId = Constants.idProvider.fromBytes(rawBytes); // Returns unknown, requires casting
|
|
422
|
+
|
|
423
|
+
// AFTER: Strong typing - Option 1 (Enhanced Provider - Recommended)
|
|
424
|
+
const enhancedProvider = getEnhancedIdProvider<ObjectId>();
|
|
425
|
+
|
|
426
|
+
// Original methods still work exactly the same
|
|
427
|
+
const rawBytes2 = enhancedProvider.generate(); // Uint8Array (same as before)
|
|
428
|
+
const isValid = enhancedProvider.validate(rawBytes2); // boolean (same as before)
|
|
429
|
+
|
|
430
|
+
// Plus new strongly-typed methods
|
|
431
|
+
const objectId = enhancedProvider.generateTyped(); // ObjectId - strongly typed!
|
|
432
|
+
const validTyped = enhancedProvider.validateTyped(objectId); // boolean, accepts ObjectId
|
|
433
|
+
const serialized = enhancedProvider.serializeTyped(objectId); // string, accepts ObjectId
|
|
434
|
+
const deserialized = enhancedProvider.deserializeTyped(serialized); // ObjectId
|
|
435
|
+
|
|
436
|
+
// AFTER: Strong typing - Option 2 (Simple Typed Provider)
|
|
437
|
+
const typedProvider = getTypedIdProvider<ObjectId>();
|
|
438
|
+
const bytes = typedProvider.generate();
|
|
439
|
+
const typedId = typedProvider.fromBytes(bytes); // Returns ObjectId, not unknown!
|
|
440
|
+
|
|
441
|
+
// AFTER: Strong typing - Option 3 (Configuration Wrapper)
|
|
442
|
+
const config = createObjectIdConfiguration();
|
|
443
|
+
const configId = config.generateId(); // ObjectId directly!
|
|
444
|
+
const configValid = config.validateId(configId); // boolean, accepts ObjectId
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
**Benefits:**
|
|
448
|
+
- ✅ **Full IntelliSense** - Autocomplete for native ID methods (`objectId.toHexString()`)
|
|
449
|
+
- ✅ **Compile-time checking** - Prevents type mismatches at build time
|
|
450
|
+
- ✅ **Zero breaking changes** - All existing code continues to work
|
|
451
|
+
- ✅ **Multiple migration paths** - Choose the approach that fits your use case
|
|
452
|
+
|
|
453
|
+
### 3. Using Custom ID Providers (e.g., GUID)
|
|
214
454
|
|
|
215
455
|
```typescript
|
|
216
456
|
import {
|
|
@@ -229,7 +469,7 @@ const ecies = new ECIESService(config);
|
|
|
229
469
|
const id = config.idProvider.generate(); // Returns 16-byte Uint8Array
|
|
230
470
|
```
|
|
231
471
|
|
|
232
|
-
###
|
|
472
|
+
### 4. Streaming Encryption (Large Files)
|
|
233
473
|
|
|
234
474
|
Encrypt gigabytes of data with minimal memory footprint (<10MB).
|
|
235
475
|
|
|
@@ -253,7 +493,7 @@ async function processFile(fileStream: ReadableStream, publicKey: Uint8Array) {
|
|
|
253
493
|
}
|
|
254
494
|
```
|
|
255
495
|
|
|
256
|
-
###
|
|
496
|
+
### 5. Member System
|
|
257
497
|
|
|
258
498
|
The `Member` class provides a high-level user abstraction that integrates keys, IDs, and encryption.
|
|
259
499
|
|
|
@@ -713,6 +953,19 @@ class MemberService {
|
|
|
713
953
|
- **`EciesFileService`**: Helper for chunked file encryption.
|
|
714
954
|
- **`PasswordLoginService`**: Secure authentication using PBKDF2 and encrypted key bundles.
|
|
715
955
|
|
|
956
|
+
### Voting System Services
|
|
957
|
+
|
|
958
|
+
- **`Poll`**: Core poll with vote aggregation and receipt generation
|
|
959
|
+
- **`PollTallier`**: Decrypts and tallies votes (separate from Poll for security)
|
|
960
|
+
- **`VoteEncoder`**: Encrypts votes using Paillier homomorphic encryption
|
|
961
|
+
- **`PollFactory`**: Convenient poll creation with method-specific configurations
|
|
962
|
+
- **`VotingSecurityValidator`**: Security level validation and enforcement
|
|
963
|
+
- **`ImmutableAuditLog`**: Hash-chained audit trail for government compliance
|
|
964
|
+
- **`PublicBulletinBoard`**: Append-only vote publication with Merkle tree integrity
|
|
965
|
+
- **`PollEventLogger`**: Event tracking with microsecond timestamps
|
|
966
|
+
- **Hierarchical Aggregators**: `PrecinctAggregator`, `CountyAggregator`, `StateAggregator`, `NationalAggregator`
|
|
967
|
+
- **`BatchVoteProcessor`**: Batch processing and checkpoint management
|
|
968
|
+
|
|
716
969
|
### ID Providers
|
|
717
970
|
|
|
718
971
|
- **`IIdProvider`**: Interface that all ID providers implement
|
|
@@ -737,6 +990,9 @@ class MemberService {
|
|
|
737
990
|
- `id`: Unique identifier (format determined by ID provider)
|
|
738
991
|
- `publicKey`: Member's public key
|
|
739
992
|
- `privateKey`: Member's private key (optional, can be loaded/unloaded)
|
|
993
|
+
- `votingPublicKey`: Paillier public key for voting (optional)
|
|
994
|
+
- `votingPrivateKey`: Paillier private key for voting (optional)
|
|
995
|
+
- `deriveVotingKeys()`: Generate Paillier keypair for voting
|
|
740
996
|
- `encryptData(data, recipientPublicKey?)`: Encrypt data
|
|
741
997
|
- `decryptData(encryptedData)`: Decrypt data
|
|
742
998
|
- `sign(data)`: Sign data with private key
|
|
@@ -746,6 +1002,25 @@ class MemberService {
|
|
|
746
1002
|
- `newMember(...)`: Static factory method
|
|
747
1003
|
- `fromMnemonic(...)`: Create from BIP39 mnemonic
|
|
748
1004
|
|
|
1005
|
+
### Voting System Types & Enumerations
|
|
1006
|
+
|
|
1007
|
+
- **`VotingMethod`**: Enum with 15+ voting methods (Plurality, Approval, Weighted, Borda, Score, RankedChoice, STAR, STV, etc.)
|
|
1008
|
+
- **`SecurityLevel`**: Enum classifying voting methods (FullyHomomorphic, MultiRound, Insecure)
|
|
1009
|
+
- **`EventType`**: Enum for event logging (PollCreated, VoteCast, PollClosed, etc.)
|
|
1010
|
+
- **`AuditEventType`**: Enum for audit events
|
|
1011
|
+
- **`JurisdictionalLevel`**: Enum for hierarchical aggregation (Precinct, County, State, National)
|
|
1012
|
+
|
|
1013
|
+
### Voting System Interfaces
|
|
1014
|
+
|
|
1015
|
+
- **`EncryptedVote<TID extends PlatformID>`**: Encrypted vote structure with generic ID support
|
|
1016
|
+
- **`PollResults<TID extends PlatformID>`**: Tally results with winner(s) and generic ID support
|
|
1017
|
+
- **`VoteReceipt`**: Cryptographic vote receipt with signature verification
|
|
1018
|
+
- **`PollConfiguration`**: Poll setup parameters
|
|
1019
|
+
- **`SupermajorityConfig`**: Threshold configuration for supermajority voting
|
|
1020
|
+
- **`AuditEntry`**: Immutable audit log entry
|
|
1021
|
+
- **`BulletinBoardEntry`**: Public bulletin board entry
|
|
1022
|
+
- **`EventLogEntry`**: Event log entry with timestamps
|
|
1023
|
+
|
|
749
1024
|
### Configuration & Registry
|
|
750
1025
|
|
|
751
1026
|
- **`Constants`**: The default, immutable configuration object.
|
|
@@ -754,6 +1029,27 @@ class MemberService {
|
|
|
754
1029
|
- `get(key)`: Retrieve a configuration.
|
|
755
1030
|
- **`createRuntimeConfiguration(overrides)`**: Creates a validated configuration object with your overrides.
|
|
756
1031
|
|
|
1032
|
+
### Strong Typing System (NEW!)
|
|
1033
|
+
|
|
1034
|
+
- **`getEnhancedIdProvider<T>(key?)`**: Get a strongly-typed ID provider with enhanced convenience methods
|
|
1035
|
+
- Drop-in replacement for `Constants.idProvider` with both original and typed methods
|
|
1036
|
+
- `generateTyped()`: Generate ID with native type (e.g., `ObjectId`)
|
|
1037
|
+
- `validateTyped(id)`: Validate ID with native type
|
|
1038
|
+
- `serializeTyped(id)`: Serialize ID with native type
|
|
1039
|
+
- `deserializeTyped(str)`: Deserialize to native type
|
|
1040
|
+
- **`getTypedIdProvider<T>(key?)`**: Get a simple strongly-typed ID provider
|
|
1041
|
+
- Minimal API surface with type-safe conversions
|
|
1042
|
+
- `fromBytes(bytes)`: Returns native type instead of `unknown`
|
|
1043
|
+
- **`createObjectIdConfiguration(overrides?)`**: Create ObjectId-typed configuration
|
|
1044
|
+
- **`createGuidV4Configuration(overrides?)`**: Create GuidV4-typed configuration
|
|
1045
|
+
- **`createUint8ArrayConfiguration(overrides?)`**: Create Uint8Array-typed configuration
|
|
1046
|
+
- **`createUuidConfiguration(overrides?)`**: Create UUID string-typed configuration
|
|
1047
|
+
- **`TypedConfiguration<T>`**: Configuration wrapper with strongly-typed ID operations
|
|
1048
|
+
- `generateId()`: Generate ID with native type
|
|
1049
|
+
- `validateId(id)`: Validate ID with native type
|
|
1050
|
+
- `serializeId(id)`: Serialize ID with native type
|
|
1051
|
+
- `deserializeId(str)`: Deserialize to native type
|
|
1052
|
+
|
|
757
1053
|
### Secure Primitives
|
|
758
1054
|
|
|
759
1055
|
- **`SecureString` / `SecureBuffer`**:
|
|
@@ -962,6 +1258,188 @@ The library maintains **100% test coverage** with over 1,200 tests, including:
|
|
|
962
1258
|
|
|
963
1259
|
## ChangeLog
|
|
964
1260
|
|
|
1261
|
+
### v4.10.7 - Strong Typing for ID Providers
|
|
1262
|
+
|
|
1263
|
+
**Major Features:**
|
|
1264
|
+
- **Strong Typing System**: Added comprehensive strong typing solution for ID provider operations
|
|
1265
|
+
- `getEnhancedIdProvider<T>()`: Drop-in replacement for `Constants.idProvider` with typed methods
|
|
1266
|
+
- `getTypedIdProvider<T>()`: Simple typed provider for minimal API surface
|
|
1267
|
+
- `createObjectIdConfiguration()`: ObjectId-typed configuration factory
|
|
1268
|
+
- `TypedConfiguration<T>`: Configuration wrapper with strongly-typed ID operations
|
|
1269
|
+
- **Enhanced Developer Experience**:
|
|
1270
|
+
- Full IntelliSense support for native ID types (`ObjectId`, `GuidV4`, `string`, etc.)
|
|
1271
|
+
- Compile-time type checking prevents runtime type errors
|
|
1272
|
+
- Multiple migration paths to choose from based on use case
|
|
1273
|
+
- **Zero Breaking Changes**: All existing code continues to work unchanged
|
|
1274
|
+
- Original `Constants.idProvider` pattern still supported
|
|
1275
|
+
- Enhanced providers include all original methods plus typed alternatives
|
|
1276
|
+
- Backward compatibility maintained for all existing APIs
|
|
1277
|
+
|
|
1278
|
+
**New APIs:**
|
|
1279
|
+
- `getEnhancedIdProvider<T>(key?)`: Enhanced provider with typed convenience methods
|
|
1280
|
+
- `getTypedIdProvider<T>(key?)`: Simple typed provider
|
|
1281
|
+
- `createObjectIdConfiguration(overrides?)`: ObjectId-typed configuration
|
|
1282
|
+
- `createGuidV4Configuration(overrides?)`: GuidV4-typed configuration
|
|
1283
|
+
- `createUint8ArrayConfiguration(overrides?)`: Uint8Array-typed configuration
|
|
1284
|
+
- `createUuidConfiguration(overrides?)`: UUID string-typed configuration
|
|
1285
|
+
- `TypedIdProviderWrapper<T>`: Enhanced wrapper with typed methods
|
|
1286
|
+
|
|
1287
|
+
**Migration Examples:**
|
|
1288
|
+
```typescript
|
|
1289
|
+
// BEFORE: Weak typing
|
|
1290
|
+
const Constants = getRuntimeConfiguration();
|
|
1291
|
+
const id = Constants.idProvider.generate(); // Uint8Array, no strong typing
|
|
1292
|
+
|
|
1293
|
+
// AFTER: Strong typing (multiple options)
|
|
1294
|
+
const enhancedProvider = getEnhancedIdProvider<ObjectId>();
|
|
1295
|
+
const objectId = enhancedProvider.generateTyped(); // ObjectId - strongly typed!
|
|
1296
|
+
|
|
1297
|
+
const typedProvider = getTypedIdProvider<ObjectId>();
|
|
1298
|
+
const typedId = typedProvider.fromBytes(bytes); // ObjectId, not unknown!
|
|
1299
|
+
|
|
1300
|
+
const config = createObjectIdConfiguration();
|
|
1301
|
+
const configId = config.generateId(); // ObjectId directly!
|
|
1302
|
+
```
|
|
1303
|
+
|
|
1304
|
+
**Documentation:**
|
|
1305
|
+
- Added comprehensive migration guide (`src/migration-guide.md`)
|
|
1306
|
+
- Updated README with strong typing examples
|
|
1307
|
+
- Added usage examples and real-world migration patterns
|
|
1308
|
+
|
|
1309
|
+
**Testing:**
|
|
1310
|
+
- 14 new tests covering all strong typing scenarios
|
|
1311
|
+
- Property-based tests for type safety validation
|
|
1312
|
+
- Migration pattern tests for backward compatibility
|
|
1313
|
+
|
|
1314
|
+
### v4.10.6 - Voting System & PlatformID Integration
|
|
1315
|
+
|
|
1316
|
+
**Major Features:**
|
|
1317
|
+
- **Complete Cryptographic Voting System**: Added comprehensive voting system with 15+ methods
|
|
1318
|
+
- Fully secure methods: Plurality, Approval, Weighted, Borda, Score, Yes/No, Supermajority
|
|
1319
|
+
- Multi-round methods: Ranked Choice (IRV), STAR, STV, Two-Round
|
|
1320
|
+
- Government-grade security: Immutable audit logs, public bulletin board, event logging
|
|
1321
|
+
- Role separation: Poll aggregators cannot decrypt votes until closure
|
|
1322
|
+
- **PlatformID Type System**: Enhanced ID provider system with generic type support
|
|
1323
|
+
- `PlatformID = Uint8Array | GuidV4 | ObjectId | string`
|
|
1324
|
+
- Generic interfaces: `EncryptedVote<TID extends PlatformID>`, `PollResults<TID extends PlatformID>`
|
|
1325
|
+
- Seamless integration between voting system and ID providers
|
|
1326
|
+
- **Enhanced Member System**: Added voting key derivation and management
|
|
1327
|
+
- `deriveVotingKeys()`: Generate Paillier keypairs for homomorphic encryption
|
|
1328
|
+
- `votingPublicKey` and `votingPrivateKey` properties for voting operations
|
|
1329
|
+
- Full integration with voting system interfaces
|
|
1330
|
+
|
|
1331
|
+
**Voting System Components:**
|
|
1332
|
+
- `Poll`: Core vote aggregation with receipt generation
|
|
1333
|
+
- `PollTallier`: Secure vote decryption and tallying (separate entity)
|
|
1334
|
+
- `VoteEncoder`: Paillier homomorphic encryption for all voting methods
|
|
1335
|
+
- `PollFactory`: Convenient poll creation with method-specific configurations
|
|
1336
|
+
- `VotingSecurityValidator`: Security level validation and enforcement
|
|
1337
|
+
- `ImmutableAuditLog`: Cryptographic hash chain for audit compliance
|
|
1338
|
+
- `PublicBulletinBoard`: Transparent vote publication with Merkle tree integrity
|
|
1339
|
+
- `PollEventLogger`: Comprehensive event tracking with microsecond timestamps
|
|
1340
|
+
- Hierarchical aggregators: Precinct → County → State → National
|
|
1341
|
+
|
|
1342
|
+
**Breaking Changes:**
|
|
1343
|
+
- Voting interfaces now use generic `PlatformID` types
|
|
1344
|
+
- Member interface extended with voting key properties
|
|
1345
|
+
- New voting system exports in main package
|
|
1346
|
+
|
|
1347
|
+
**Compatibility:**
|
|
1348
|
+
- Fully backward compatible for existing ECIES operations
|
|
1349
|
+
- New voting system is opt-in and doesn't affect existing functionality
|
|
1350
|
+
- Cross-platform compatible with `@digitaldefiance/node-ecies-lib`
|
|
1351
|
+
|
|
1352
|
+
### v4.10.5 - Voting System Enhancements
|
|
1353
|
+
|
|
1354
|
+
**Improvements:**
|
|
1355
|
+
- Enhanced voting system test coverage
|
|
1356
|
+
- Updated showcase application with improved voting demos
|
|
1357
|
+
- Bug fixes and stability improvements
|
|
1358
|
+
|
|
1359
|
+
### v4.10.0 - Complete Voting System Implementation
|
|
1360
|
+
|
|
1361
|
+
**Major Features:**
|
|
1362
|
+
- **All 15 Voting Methods Fully Implemented**: Complete implementation of all voting methods with both encoding and showcase demos
|
|
1363
|
+
- **Interactive Showcase Application**: React-based demos for all voting methods
|
|
1364
|
+
- Plurality, Approval, Weighted, Borda Count demos
|
|
1365
|
+
- Score Voting, Yes/No, Yes/No/Abstain, Supermajority demos
|
|
1366
|
+
- Ranked Choice (IRV), Two-Round, STAR, STV demos
|
|
1367
|
+
- Quadratic, Consensus, Consent-Based demos (marked as insecure)
|
|
1368
|
+
- **Enhanced Vote Encoding**: Generic `encode()` method supports all voting methods
|
|
1369
|
+
- **Comprehensive Testing**: Full test coverage for all voting methods and security levels
|
|
1370
|
+
|
|
1371
|
+
### v4.9.1 - Voting System Refinements
|
|
1372
|
+
|
|
1373
|
+
**Improvements:**
|
|
1374
|
+
- Enhanced voting system test suite
|
|
1375
|
+
- Improved showcase application stability
|
|
1376
|
+
- Bug fixes in voting method implementations
|
|
1377
|
+
|
|
1378
|
+
### v4.9.0 - Voting System Core Implementation
|
|
1379
|
+
|
|
1380
|
+
**Major Features:**
|
|
1381
|
+
- **Core Voting Infrastructure**: Implemented foundational voting system components
|
|
1382
|
+
- **Security Classifications**: Proper security level validation for all voting methods
|
|
1383
|
+
- **Homomorphic Encryption**: Paillier encryption for privacy-preserving vote aggregation
|
|
1384
|
+
- **Government Compliance**: Audit logging, bulletin board, and event tracking systems
|
|
1385
|
+
|
|
1386
|
+
### v4.8.7 - Showcase Application Development
|
|
1387
|
+
|
|
1388
|
+
**Improvements:**
|
|
1389
|
+
- Continued development of interactive voting demos
|
|
1390
|
+
- Enhanced user interface for voting demonstrations
|
|
1391
|
+
- Improved cryptographic visualization components
|
|
1392
|
+
|
|
1393
|
+
### v4.8.6 - Voting System Testing & Refinements
|
|
1394
|
+
|
|
1395
|
+
**Improvements:**
|
|
1396
|
+
- Enhanced test coverage for voting system components
|
|
1397
|
+
- Bug fixes in voting method implementations
|
|
1398
|
+
- Improved error handling and validation
|
|
1399
|
+
|
|
1400
|
+
### v4.8.5 - Voting System Expansion
|
|
1401
|
+
|
|
1402
|
+
**Features:**
|
|
1403
|
+
- Additional voting method implementations
|
|
1404
|
+
- Enhanced showcase application with more interactive demos
|
|
1405
|
+
- Improved voting system documentation
|
|
1406
|
+
|
|
1407
|
+
### v4.8.3 - Voting System Development
|
|
1408
|
+
|
|
1409
|
+
**Features:**
|
|
1410
|
+
- Continued voting system implementation
|
|
1411
|
+
- Enhanced cryptographic voting components
|
|
1412
|
+
- Improved test coverage
|
|
1413
|
+
|
|
1414
|
+
### v4.8.2 - Voting System Foundation
|
|
1415
|
+
|
|
1416
|
+
**Features:**
|
|
1417
|
+
- Initial voting system architecture
|
|
1418
|
+
- Core voting method implementations
|
|
1419
|
+
- Basic showcase application structure
|
|
1420
|
+
|
|
1421
|
+
### v4.8.1 - Voting System Initialization
|
|
1422
|
+
|
|
1423
|
+
**Features:**
|
|
1424
|
+
- Foundation for cryptographic voting system
|
|
1425
|
+
- Initial voting method definitions
|
|
1426
|
+
- Enhanced Member system for voting key management
|
|
1427
|
+
|
|
1428
|
+
### v4.8.0 - Voting System Introduction
|
|
1429
|
+
|
|
1430
|
+
**Major Features:**
|
|
1431
|
+
- **Initial Voting System**: Introduced cryptographic voting system architecture
|
|
1432
|
+
- **Voting Method Enumerations**: Defined all 15+ voting methods with security classifications
|
|
1433
|
+
- **Enhanced Member System**: Added voting key derivation capabilities
|
|
1434
|
+
- **Showcase Application**: Started development of interactive voting demos
|
|
1435
|
+
|
|
1436
|
+
### v4.7.14 - Pre-Voting System Enhancements
|
|
1437
|
+
|
|
1438
|
+
**Improvements:**
|
|
1439
|
+
- Enhanced core ECIES functionality
|
|
1440
|
+
- Improved ID provider system
|
|
1441
|
+
- Bug fixes and stability improvements
|
|
1442
|
+
|
|
965
1443
|
### v4.7.12
|
|
966
1444
|
|
|
967
1445
|
**Bug Fix: idProvider Configuration Now Respected by Member.newMember()**
|
|
@@ -1367,7 +1845,18 @@ tests/
|
|
|
1367
1845
|
├── e2e/ # End-to-end encryption/decryption tests
|
|
1368
1846
|
├── property/ # Property-based tests for cryptographic properties
|
|
1369
1847
|
├── compatibility/ # Cross-platform compatibility tests
|
|
1370
|
-
|
|
1848
|
+
├── vectors/ # Test vectors for protocol validation
|
|
1849
|
+
└── voting/ # Voting system tests
|
|
1850
|
+
├── voting.spec.ts # Core voting functionality (900+ tests)
|
|
1851
|
+
├── voting-stress.spec.ts # Stress tests with large datasets
|
|
1852
|
+
├── poll-core.spec.ts # Poll core functionality
|
|
1853
|
+
├── poll-audit.spec.ts # Audit log integration
|
|
1854
|
+
├── factory.spec.ts # Poll factory methods
|
|
1855
|
+
├── encoder.spec.ts # Vote encoding for all methods
|
|
1856
|
+
├── security.spec.ts # Security validation
|
|
1857
|
+
├── audit.spec.ts # Immutable audit log
|
|
1858
|
+
├── bulletin-board.spec.ts # Public bulletin board
|
|
1859
|
+
└── event-logger.spec.ts # Event logging system
|
|
1371
1860
|
```
|
|
1372
1861
|
|
|
1373
1862
|
### Running Tests
|
|
@@ -1382,6 +1871,11 @@ npm test -- --coverage
|
|
|
1382
1871
|
# Run specific test suite
|
|
1383
1872
|
npm test -- ecies-service.spec.ts
|
|
1384
1873
|
|
|
1874
|
+
# Run voting system tests
|
|
1875
|
+
npm test -- voting.spec.ts
|
|
1876
|
+
npm test -- voting-stress.spec.ts
|
|
1877
|
+
npm test -- poll-core.spec.ts
|
|
1878
|
+
|
|
1385
1879
|
# Run compatibility tests
|
|
1386
1880
|
npm test -- cross-platform-compatibility.spec.ts
|
|
1387
1881
|
|
|
@@ -1459,6 +1953,68 @@ describe('Multi-Recipient Encryption', () => {
|
|
|
1459
1953
|
});
|
|
1460
1954
|
```
|
|
1461
1955
|
|
|
1956
|
+
#### Testing Voting System
|
|
1957
|
+
|
|
1958
|
+
```typescript
|
|
1959
|
+
import { ECIESService, Member, MemberType, EmailString } from '@digitaldefiance/ecies-lib';
|
|
1960
|
+
import { PollFactory, VoteEncoder, PollTallier, VotingMethod } from '@digitaldefiance/ecies-lib/voting';
|
|
1961
|
+
|
|
1962
|
+
describe('Voting System', () => {
|
|
1963
|
+
it('should conduct a complete ranked choice election', async () => {
|
|
1964
|
+
const ecies = new ECIESService();
|
|
1965
|
+
|
|
1966
|
+
// Create authority
|
|
1967
|
+
const { member: authority } = Member.newMember(
|
|
1968
|
+
ecies,
|
|
1969
|
+
MemberType.System,
|
|
1970
|
+
'Authority',
|
|
1971
|
+
new EmailString('authority@example.com')
|
|
1972
|
+
);
|
|
1973
|
+
await authority.deriveVotingKeys();
|
|
1974
|
+
|
|
1975
|
+
// Create poll
|
|
1976
|
+
const poll = PollFactory.createRankedChoice(
|
|
1977
|
+
['Alice', 'Bob', 'Charlie'],
|
|
1978
|
+
authority
|
|
1979
|
+
);
|
|
1980
|
+
|
|
1981
|
+
// Create voters and cast votes
|
|
1982
|
+
const { member: voter1 } = Member.newMember(ecies, MemberType.User, 'Voter1', new EmailString('v1@example.com'));
|
|
1983
|
+
await voter1.deriveVotingKeys();
|
|
1984
|
+
|
|
1985
|
+
const encoder = new VoteEncoder(authority.votingPublicKey!);
|
|
1986
|
+
const vote = encoder.encodeRankedChoice([0, 1, 2], 3); // Alice > Bob > Charlie
|
|
1987
|
+
const receipt = poll.vote(voter1, vote);
|
|
1988
|
+
|
|
1989
|
+
// Verify receipt
|
|
1990
|
+
expect(poll.verifyReceipt(voter1, receipt)).toBe(true);
|
|
1991
|
+
|
|
1992
|
+
// Close and tally
|
|
1993
|
+
poll.close();
|
|
1994
|
+
const tallier = new PollTallier(
|
|
1995
|
+
authority,
|
|
1996
|
+
authority.votingPrivateKey!,
|
|
1997
|
+
authority.votingPublicKey!
|
|
1998
|
+
);
|
|
1999
|
+
const results = tallier.tally(poll);
|
|
2000
|
+
|
|
2001
|
+
expect(results.winner).toBeDefined();
|
|
2002
|
+
expect(results.choices[results.winner!]).toBe('Alice');
|
|
2003
|
+
});
|
|
2004
|
+
|
|
2005
|
+
it('should prevent double voting', async () => {
|
|
2006
|
+
const poll = PollFactory.createPlurality(['A', 'B'], authority);
|
|
2007
|
+
const encoder = new VoteEncoder(authority.votingPublicKey!);
|
|
2008
|
+
|
|
2009
|
+
poll.vote(voter, encoder.encodePlurality(0, 2));
|
|
2010
|
+
|
|
2011
|
+
expect(() => {
|
|
2012
|
+
poll.vote(voter, encoder.encodePlurality(1, 2));
|
|
2013
|
+
}).toThrow('Already voted');
|
|
2014
|
+
});
|
|
2015
|
+
});
|
|
2016
|
+
```
|
|
2017
|
+
|
|
1462
2018
|
#### Testing ID Providers
|
|
1463
2019
|
|
|
1464
2020
|
```typescript
|
|
@@ -1520,6 +2076,10 @@ describe('Cryptographic Properties', () => {
|
|
|
1520
2076
|
4. **Use property-based tests** for cryptographic invariants
|
|
1521
2077
|
5. **Test error conditions** like invalid keys, corrupted data, and wrong recipients
|
|
1522
2078
|
6. **Verify binary compatibility** with node-ecies-lib
|
|
2079
|
+
7. **Test all voting methods** across security levels (fully secure, multi-round, insecure)
|
|
2080
|
+
8. **Verify voting security** (double-vote prevention, receipt verification, role separation)
|
|
2081
|
+
9. **Test government requirements** (audit logs, bulletin board, event logging)
|
|
2082
|
+
10. **Stress test large elections** (1000+ voters, complex elimination rounds)
|
|
1523
2083
|
|
|
1524
2084
|
### Cross-Platform Testing
|
|
1525
2085
|
|
|
@@ -1555,5 +2115,5 @@ MIT © Digital Defiance
|
|
|
1555
2115
|
|
|
1556
2116
|
- **Repository:** <https://github.com/Digital-Defiance/ecies-lib>
|
|
1557
2117
|
- **npm:** <https://www.npmjs.com/package/@digitaldefiance/ecies-lib>
|
|
1558
|
-
- **Companion:** @digitaldefiance/node-ecies-lib (binary compatible)
|
|
2118
|
+
- **Companion:** @digitaldefiance/node-ecies-lib (binary compatible, extends this library with Buffer support and Node.js-specific features)
|
|
1559
2119
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Examples showing how to use the typed configuration system for strong ID provider typing.
|
|
3
|
+
* This demonstrates the solution to the original problem where Constants.idProvider.generate()
|
|
4
|
+
* had no strong typing.
|
|
5
|
+
*/
|
|
6
|
+
declare function beforeExample(): void;
|
|
7
|
+
declare function afterExample(): void;
|
|
8
|
+
declare function realWorldUsage(): void;
|
|
9
|
+
declare function serviceIntegration(): void;
|
|
10
|
+
declare function typeSafetyDemo(): void;
|
|
11
|
+
export { beforeExample, afterExample, realWorldUsage, serviceIntegration, typeSafetyDemo, };
|
|
12
|
+
//# sourceMappingURL=typed-configuration-usage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-configuration-usage.d.ts","sourceRoot":"","sources":["../../../../../packages/digitaldefiance-ecies-lib/src/examples/typed-configuration-usage.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAiBH,iBAAS,aAAa,SAKrB;AAMD,iBAAS,YAAY,SAsBpB;AAMD,iBAAS,cAAc,SAsBtB;AAMD,iBAAS,kBAAkB,SAU1B;AAMD,iBAAS,cAAc,SAgBtB;AAED,OAAO,EACL,aAAa,EACb,YAAY,EACZ,cAAc,EACd,kBAAkB,EAClB,cAAc,GACf,CAAC"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Examples showing how to use the typed configuration system for strong ID provider typing.
|
|
4
|
+
* This demonstrates the solution to the original problem where Constants.idProvider.generate()
|
|
5
|
+
* had no strong typing.
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.beforeExample = beforeExample;
|
|
9
|
+
exports.afterExample = afterExample;
|
|
10
|
+
exports.realWorldUsage = realWorldUsage;
|
|
11
|
+
exports.serviceIntegration = serviceIntegration;
|
|
12
|
+
exports.typeSafetyDemo = typeSafetyDemo;
|
|
13
|
+
const constants_1 = require("../constants");
|
|
14
|
+
const guidv4_provider_1 = require("../lib/id-providers/guidv4-provider");
|
|
15
|
+
const typed_configuration_1 = require("../typed-configuration");
|
|
16
|
+
// ============================================================================
|
|
17
|
+
// BEFORE: Weak typing with getRuntimeConfiguration()
|
|
18
|
+
// ============================================================================
|
|
19
|
+
function beforeExample() {
|
|
20
|
+
// This was the original problem - no strong typing
|
|
21
|
+
const Constants = (0, constants_1.getRuntimeConfiguration)();
|
|
22
|
+
const id = Constants.idProvider.generate(); // Returns Uint8Array, loses type info
|
|
23
|
+
const _nativeId = Constants.idProvider.fromBytes(id); // Returns unknown, no type safety
|
|
24
|
+
}
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// AFTER: Strong typing solutions
|
|
27
|
+
// ============================================================================
|
|
28
|
+
function afterExample() {
|
|
29
|
+
// Solution 1: Direct replacement for Constants.idProvider
|
|
30
|
+
const enhancedProvider = (0, typed_configuration_1.getEnhancedIdProvider)();
|
|
31
|
+
// Original methods still work exactly the same
|
|
32
|
+
const rawBytes = enhancedProvider.generate(); // Uint8Array (same as before)
|
|
33
|
+
const _isValid = enhancedProvider.validate(rawBytes); // boolean (same as before)
|
|
34
|
+
// But now you also have strongly-typed methods
|
|
35
|
+
const objectId = enhancedProvider.generateTyped(); // ObjectId - strongly typed!
|
|
36
|
+
const _validTyped = enhancedProvider.validateTyped(objectId); // boolean, accepts ObjectId
|
|
37
|
+
const serialized = enhancedProvider.serializeTyped(objectId); // string, accepts ObjectId
|
|
38
|
+
const _deserialized = enhancedProvider.deserializeTyped(serialized); // ObjectId
|
|
39
|
+
// Solution 2: Simple typed provider
|
|
40
|
+
const typedProvider = (0, typed_configuration_1.getTypedIdProvider)();
|
|
41
|
+
const bytes = typedProvider.generate();
|
|
42
|
+
const _typedId = typedProvider.fromBytes(bytes); // Returns ObjectId, not unknown!
|
|
43
|
+
// Solution 3: Configuration approach (for more complex scenarios)
|
|
44
|
+
const config = (0, typed_configuration_1.createObjectIdConfiguration)();
|
|
45
|
+
const _configId = config.generateId(); // Returns ObjectId - strongly typed!
|
|
46
|
+
}
|
|
47
|
+
// ============================================================================
|
|
48
|
+
// Real-world usage patterns
|
|
49
|
+
// ============================================================================
|
|
50
|
+
function realWorldUsage() {
|
|
51
|
+
// Pattern 1: Service initialization with strong typing
|
|
52
|
+
const config = (0, typed_configuration_1.createObjectIdConfiguration)();
|
|
53
|
+
// Now you can use config.idProvider with full type safety
|
|
54
|
+
const newId = config.generateId(); // ObjectId
|
|
55
|
+
const _isValid = config.validateId(newId); // boolean
|
|
56
|
+
const _idString = config.serializeId(newId); // string
|
|
57
|
+
// Pattern 2: Custom provider with type inference
|
|
58
|
+
const customProvider = new guidv4_provider_1.GuidV4Provider();
|
|
59
|
+
const typedConfig = (0, typed_configuration_1.createTypedConfiguration)({
|
|
60
|
+
idProvider: customProvider,
|
|
61
|
+
// Other overrides...
|
|
62
|
+
BcryptRounds: 12,
|
|
63
|
+
});
|
|
64
|
+
const _guidId = typedConfig.generateId(); // GuidV4 - automatically inferred!
|
|
65
|
+
// Pattern 3: Backward compatibility - access underlying constants
|
|
66
|
+
const _underlyingConstants = typedConfig.constants;
|
|
67
|
+
// This still works for existing code that expects IConstants
|
|
68
|
+
}
|
|
69
|
+
// ============================================================================
|
|
70
|
+
// Integration with ECIESService
|
|
71
|
+
// ============================================================================
|
|
72
|
+
function serviceIntegration() {
|
|
73
|
+
// The ECIESService can now use TypedConfiguration for better typing
|
|
74
|
+
const config = (0, typed_configuration_1.createObjectIdConfiguration)();
|
|
75
|
+
// Pass the underlying constants to existing services
|
|
76
|
+
// const service = new ECIESService<ObjectId>(config.constants);
|
|
77
|
+
// Or use the typed config directly for ID operations
|
|
78
|
+
const memberId = config.generateId(); // ObjectId
|
|
79
|
+
const _memberIdString = config.serializeId(memberId); // string
|
|
80
|
+
}
|
|
81
|
+
// ============================================================================
|
|
82
|
+
// Type safety demonstrations
|
|
83
|
+
// ============================================================================
|
|
84
|
+
function typeSafetyDemo() {
|
|
85
|
+
const objectIdConfig = (0, typed_configuration_1.createObjectIdConfiguration)();
|
|
86
|
+
const guidConfig = (0, typed_configuration_1.createGuidV4Configuration)({
|
|
87
|
+
idProvider: new guidv4_provider_1.GuidV4Provider(),
|
|
88
|
+
});
|
|
89
|
+
const objectId = objectIdConfig.generateId(); // ObjectId
|
|
90
|
+
const guid = guidConfig.generateId(); // GuidV4
|
|
91
|
+
// These operations are now type-safe:
|
|
92
|
+
objectIdConfig.serializeId(objectId); // ✅ Accepts ObjectId
|
|
93
|
+
guidConfig.serializeId(guid); // ✅ Accepts GuidV4
|
|
94
|
+
// These would be compile-time errors:
|
|
95
|
+
// objectIdConfig.serializeId(guid); // ❌ Type error: GuidV4 not assignable to ObjectId
|
|
96
|
+
// guidConfig.serializeId(objectId); // ❌ Type error: ObjectId not assignable to GuidV4
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=typed-configuration-usage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-configuration-usage.js","sourceRoot":"","sources":["../../../../../packages/digitaldefiance-ecies-lib/src/examples/typed-configuration-usage.ts"],"names":[],"mappings":";AAAA;;;;GAIG;;AAuHD,sCAAa;AACb,oCAAY;AACZ,wCAAc;AACd,gDAAkB;AAClB,wCAAc;AAxHhB,4CAAuD;AACvD,yEAAqE;AACrE,gEAMgC;AAEhC,+EAA+E;AAC/E,qDAAqD;AACrD,+EAA+E;AAE/E,SAAS,aAAa;IACpB,mDAAmD;IACnD,MAAM,SAAS,GAAG,IAAA,mCAAuB,GAAE,CAAC;IAC5C,MAAM,EAAE,GAAG,SAAS,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAC,sCAAsC;IAClF,MAAM,SAAS,GAAG,SAAS,CAAC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,CAAC,CAAC,SAAS;IAE/C,sCAAsC;IACtC,cAAc,CAAC,WAAW,CAAC,QAAQ,CAAC,CAAC,CAAC,qBAAqB;IAC3D,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC,CAAC,mBAAmB;IAEjD,sCAAsC;IACtC,uFAAuF;IACvF,uFAAuF;AACzF,CAAC"}
|
package/src/index.d.ts
CHANGED
|
@@ -15,6 +15,7 @@ export * from './lib/index';
|
|
|
15
15
|
export { EciesComponentId, EciesI18nEngineKey, getEciesI18nEngine, getEciesTranslation, resetEciesI18nEngine, safeEciesTranslation, } from './i18n-setup';
|
|
16
16
|
export { encryptionTypeEnumToType, encryptionTypeToString, ensureEciesEncryptionTypeEnum, validateEciesEncryptionTypeEnum, } from './utils/encryption-type-utils';
|
|
17
17
|
export * from './constants';
|
|
18
|
+
export * from './typed-configuration';
|
|
18
19
|
export * from './email-string';
|
|
19
20
|
export * from './enumerations';
|
|
20
21
|
export * from './errors';
|
package/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../packages/digitaldefiance-ecies-lib/src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,uBAAuB,CAAC;AAG/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAG5B,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,cAAc,CAAC;AAItB,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACtB,6BAA6B,EAC7B,+BAA+B,GAChC,MAAM,+BAA+B,CAAC;AASvC,cAAc,aAAa,CAAC;AAC5B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,8BAA8B,CAAC;AAC7C,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AACzE,YAAY,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACtE,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AAGxB,cAAc,cAAc,CAAC;AAG7B,YAAY,EACV,UAAU,EACV,SAAS,EACT,OAAO,IAAI,eAAe,GAC3B,MAAM,iBAAiB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../packages/digitaldefiance-ecies-lib/src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,uBAAuB,CAAC;AAG/B,cAAc,kBAAkB,CAAC;AACjC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAG5B,OAAO,EACL,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,oBAAoB,EACpB,oBAAoB,GACrB,MAAM,cAAc,CAAC;AAItB,OAAO,EACL,wBAAwB,EACxB,sBAAsB,EACtB,6BAA6B,EAC7B,+BAA+B,GAChC,MAAM,+BAA+B,CAAC;AASvC,cAAc,aAAa,CAAC;AAC5B,cAAc,uBAAuB,CAAC;AACtC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,8BAA8B,CAAC;AAC7C,OAAO,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AACzE,YAAY,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAC;AACtE,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,UAAU,CAAC;AACzB,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,iBAAiB,CAAC;AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AAGxB,cAAc,cAAc,CAAC;AAG7B,YAAY,EACV,UAAU,EACV,SAAS,EACT,OAAO,IAAI,eAAe,GAC3B,MAAM,iBAAiB,CAAC"}
|
package/src/index.js
CHANGED
|
@@ -40,6 +40,7 @@ Object.defineProperty(exports, "validateEciesEncryptionTypeEnum", { enumerable:
|
|
|
40
40
|
// Note: Existing services will be re-exported once migrated to v2
|
|
41
41
|
// For now, import from main index.ts for backward compatibility
|
|
42
42
|
tslib_1.__exportStar(require("./constants"), exports);
|
|
43
|
+
tslib_1.__exportStar(require("./typed-configuration"), exports);
|
|
43
44
|
tslib_1.__exportStar(require("./email-string"), exports);
|
|
44
45
|
tslib_1.__exportStar(require("./enumerations"), exports);
|
|
45
46
|
tslib_1.__exportStar(require("./errors"), exports);
|
package/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/digitaldefiance-ecies-lib/src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;;AAEH,mEAAmE;AACnE,wEAAwE;AACxE,iCAA+B;AAE/B,kBAAkB;AAClB,2DAAiC;AACjC,uDAA6B;AAC7B,sDAA4B;AAE5B,UAAU;AACV,2CAOsB;AANpB,8GAAA,gBAAgB,OAAA;AAChB,gHAAA,kBAAkB,OAAA;AAClB,gHAAA,kBAAkB,OAAA;AAClB,iHAAA,mBAAmB,OAAA;AACnB,kHAAA,oBAAoB,OAAA;AACpB,kHAAA,oBAAoB,OAAA;AAGtB,mFAAmF;AACnF,mEAAmE;AACnE,uEAKuC;AAJrC,iIAAA,wBAAwB,OAAA;AACxB,+HAAA,sBAAsB,OAAA;AACtB,sIAAA,6BAA6B,OAAA;AAC7B,wIAAA,+BAA+B,OAAA;AAGjC,2DAA2D;AAC3D,oEAAoE;AACpE,8DAA8D;AAE9D,kEAAkE;AAClE,gEAAgE;AAEhE,sDAA4B;AAC5B,yDAA+B;AAC/B,yDAA+B;AAC/B,mDAAyB;AACzB,uDAA6B;AAC7B,uEAA6C;AAC7C,kEAAyE;AAAhE,4HAAA,wBAAwB,OAAA;AAEjC,qEAA2C;AAC3C,qEAA2C;AAC3C,uEAA6C;AAC7C,6DAAmC;AACnC,4DAAkC;AAClC,mDAAyB;AACzB,4DAAkC;AAClC,yDAA+B;AAC/B,0DAAgC;AAChC,0DAAgC;AAChC,qDAA2B;AAC3B,kDAAwB;AACxB,kDAAwB;AAExB,qEAAqE;AACrE,uDAA6B"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/digitaldefiance-ecies-lib/src/index.ts"],"names":[],"mappings":";AAAA;;;;;;;;;GASG;;;;AAEH,mEAAmE;AACnE,wEAAwE;AACxE,iCAA+B;AAE/B,kBAAkB;AAClB,2DAAiC;AACjC,uDAA6B;AAC7B,sDAA4B;AAE5B,UAAU;AACV,2CAOsB;AANpB,8GAAA,gBAAgB,OAAA;AAChB,gHAAA,kBAAkB,OAAA;AAClB,gHAAA,kBAAkB,OAAA;AAClB,iHAAA,mBAAmB,OAAA;AACnB,kHAAA,oBAAoB,OAAA;AACpB,kHAAA,oBAAoB,OAAA;AAGtB,mFAAmF;AACnF,mEAAmE;AACnE,uEAKuC;AAJrC,iIAAA,wBAAwB,OAAA;AACxB,+HAAA,sBAAsB,OAAA;AACtB,sIAAA,6BAA6B,OAAA;AAC7B,wIAAA,+BAA+B,OAAA;AAGjC,2DAA2D;AAC3D,oEAAoE;AACpE,8DAA8D;AAE9D,kEAAkE;AAClE,gEAAgE;AAEhE,sDAA4B;AAC5B,gEAAsC;AACtC,yDAA+B;AAC/B,yDAA+B;AAC/B,mDAAyB;AACzB,uDAA6B;AAC7B,uEAA6C;AAC7C,kEAAyE;AAAhE,4HAAA,wBAAwB,OAAA;AAEjC,qEAA2C;AAC3C,qEAA2C;AAC3C,uEAA6C;AAC7C,6DAAmC;AACnC,4DAAkC;AAClC,mDAAyB;AACzB,4DAAkC;AAClC,yDAA+B;AAC/B,0DAAgC;AAChC,0DAAgC;AAChC,qDAA2B;AAC3B,kDAAwB;AACxB,kDAAwB;AAExB,qEAAqE;AACrE,uDAA6B"}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { ObjectId } from 'bson';
|
|
2
|
+
import { type ConfigurationKey } from './constants';
|
|
3
|
+
import type { IConstants } from './interfaces/constants';
|
|
4
|
+
import type { IIdProvider } from './interfaces/id-provider';
|
|
5
|
+
import type { GuidV4 } from './lib/guid';
|
|
6
|
+
import type { DeepPartial } from './types/deep-partial';
|
|
7
|
+
/**
|
|
8
|
+
* Typed configuration wrapper that preserves ID provider type information.
|
|
9
|
+
* This provides strong typing for idProvider operations without making the entire
|
|
10
|
+
* constants system generic.
|
|
11
|
+
*/
|
|
12
|
+
export declare class TypedConfiguration<TID> {
|
|
13
|
+
private readonly _constants;
|
|
14
|
+
private readonly _idProvider;
|
|
15
|
+
constructor(constants: IConstants);
|
|
16
|
+
/**
|
|
17
|
+
* Get the strongly-typed ID provider.
|
|
18
|
+
* This returns IIdProvider<TID> with full type safety.
|
|
19
|
+
*/
|
|
20
|
+
get idProvider(): IIdProvider<TID>;
|
|
21
|
+
/**
|
|
22
|
+
* Get the underlying constants (for compatibility with existing code).
|
|
23
|
+
*/
|
|
24
|
+
get constants(): IConstants;
|
|
25
|
+
/**
|
|
26
|
+
* Generate a new ID with strong typing.
|
|
27
|
+
*/
|
|
28
|
+
generateId(): TID;
|
|
29
|
+
/**
|
|
30
|
+
* Validate an ID with strong typing.
|
|
31
|
+
*/
|
|
32
|
+
validateId(id: TID): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Serialize an ID to string with strong typing.
|
|
35
|
+
*/
|
|
36
|
+
serializeId(id: TID): string;
|
|
37
|
+
/**
|
|
38
|
+
* Deserialize a string to ID with strong typing.
|
|
39
|
+
*/
|
|
40
|
+
deserializeId(str: string): TID;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Configuration factory functions that infer and preserve ID provider types.
|
|
44
|
+
*/
|
|
45
|
+
/**
|
|
46
|
+
* Create a typed configuration with ObjectId provider.
|
|
47
|
+
* This is the default configuration with strong ObjectId typing.
|
|
48
|
+
*/
|
|
49
|
+
export declare function createObjectIdConfiguration(overrides?: DeepPartial<IConstants>): TypedConfiguration<ObjectId>;
|
|
50
|
+
/**
|
|
51
|
+
* Create a typed configuration with GuidV4 provider.
|
|
52
|
+
*/
|
|
53
|
+
export declare function createGuidV4Configuration(overrides?: DeepPartial<IConstants>): TypedConfiguration<GuidV4>;
|
|
54
|
+
/**
|
|
55
|
+
* Create a typed configuration with Uint8Array provider (generic bytes).
|
|
56
|
+
*/
|
|
57
|
+
export declare function createUint8ArrayConfiguration(overrides?: DeepPartial<IConstants>): TypedConfiguration<Uint8Array>;
|
|
58
|
+
/**
|
|
59
|
+
* Create a typed configuration with UUID string provider.
|
|
60
|
+
*/
|
|
61
|
+
export declare function createUuidConfiguration(overrides?: DeepPartial<IConstants>): TypedConfiguration<string>;
|
|
62
|
+
/**
|
|
63
|
+
* Get a strongly-typed ID provider from the default configuration.
|
|
64
|
+
* This is the direct equivalent of `getRuntimeConfiguration().idProvider` with strong typing.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```typescript
|
|
68
|
+
* // BEFORE: Weak typing
|
|
69
|
+
* const Constants = getRuntimeConfiguration();
|
|
70
|
+
* const id = Constants.idProvider.generate(); // Returns Uint8Array, no strong typing
|
|
71
|
+
*
|
|
72
|
+
* // AFTER: Strong typing
|
|
73
|
+
* const idProvider = getTypedIdProvider<ObjectId>();
|
|
74
|
+
* const id = idProvider.generateTyped(); // Returns ObjectId - strongly typed!
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
export declare function getTypedIdProvider<TID>(key?: ConfigurationKey): IIdProvider<TID>;
|
|
78
|
+
/**
|
|
79
|
+
* Enhanced ID provider wrapper that adds strongly-typed convenience methods
|
|
80
|
+
* while preserving all original functionality.
|
|
81
|
+
*/
|
|
82
|
+
export declare class TypedIdProviderWrapper<TID> implements IIdProvider<TID> {
|
|
83
|
+
private readonly _provider;
|
|
84
|
+
constructor(provider: IIdProvider<TID>);
|
|
85
|
+
get byteLength(): number;
|
|
86
|
+
get name(): string;
|
|
87
|
+
generate(): Uint8Array;
|
|
88
|
+
validate(id: Uint8Array): boolean;
|
|
89
|
+
serialize(id: Uint8Array): string;
|
|
90
|
+
deserialize(str: string): Uint8Array;
|
|
91
|
+
toBytes(id: TID): Uint8Array;
|
|
92
|
+
fromBytes(bytes: Uint8Array): TID;
|
|
93
|
+
equals(a: TID, b: TID): boolean;
|
|
94
|
+
clone(id: TID): TID;
|
|
95
|
+
idToString(id: TID): string;
|
|
96
|
+
idFromString(str: string): TID;
|
|
97
|
+
/**
|
|
98
|
+
* Generate a new ID with strong typing.
|
|
99
|
+
* This is equivalent to generate() + fromBytes() but returns the native type directly.
|
|
100
|
+
*/
|
|
101
|
+
generateTyped(): TID;
|
|
102
|
+
/**
|
|
103
|
+
* Validate an ID with strong typing.
|
|
104
|
+
* Accepts the native ID type and validates it.
|
|
105
|
+
*/
|
|
106
|
+
validateTyped(id: TID): boolean;
|
|
107
|
+
/**
|
|
108
|
+
* Serialize an ID to string with strong typing.
|
|
109
|
+
* Accepts the native ID type directly.
|
|
110
|
+
*/
|
|
111
|
+
serializeTyped(id: TID): string;
|
|
112
|
+
/**
|
|
113
|
+
* Deserialize a string to ID with strong typing.
|
|
114
|
+
* Returns the native ID type directly.
|
|
115
|
+
*/
|
|
116
|
+
deserializeTyped(str: string): TID;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Get a strongly-typed ID provider with enhanced convenience methods.
|
|
120
|
+
* This provides the most ergonomic API for strongly-typed ID operations.
|
|
121
|
+
*
|
|
122
|
+
* @example
|
|
123
|
+
* ```typescript
|
|
124
|
+
* // Direct replacement for Constants.idProvider with strong typing
|
|
125
|
+
* const idProvider = getEnhancedIdProvider<ObjectId>();
|
|
126
|
+
*
|
|
127
|
+
* // All original methods work the same
|
|
128
|
+
* const bytes = idProvider.generate(); // Uint8Array
|
|
129
|
+
* const isValid = idProvider.validate(bytes); // boolean
|
|
130
|
+
*
|
|
131
|
+
* // Plus new strongly-typed methods
|
|
132
|
+
* const objectId = idProvider.generateTyped(); // ObjectId - strongly typed!
|
|
133
|
+
* const valid = idProvider.validateTyped(objectId); // boolean
|
|
134
|
+
* const serialized = idProvider.serializeTyped(objectId); // string
|
|
135
|
+
* const deserialized = idProvider.deserializeTyped(serialized); // ObjectId
|
|
136
|
+
* ```
|
|
137
|
+
*/
|
|
138
|
+
export declare function getEnhancedIdProvider<TID>(key?: ConfigurationKey): TypedIdProviderWrapper<TID>;
|
|
139
|
+
/**
|
|
140
|
+
* Get a typed configuration from the registry.
|
|
141
|
+
* The type parameter must match the actual ID provider type.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
* ```typescript
|
|
145
|
+
* // For ObjectId configurations
|
|
146
|
+
* const config = getTypedConfiguration<ObjectId>();
|
|
147
|
+
* const id = config.generateId(); // Returns ObjectId
|
|
148
|
+
*
|
|
149
|
+
* // For GUID configurations
|
|
150
|
+
* const guidConfig = getTypedConfiguration<GuidV4>('my-guid-config');
|
|
151
|
+
* const guid = guidConfig.generateId(); // Returns GuidV4
|
|
152
|
+
* ```
|
|
153
|
+
*/
|
|
154
|
+
export declare function getTypedConfiguration<TID>(key?: ConfigurationKey): TypedConfiguration<TID>;
|
|
155
|
+
/**
|
|
156
|
+
* Type-safe configuration builder that infers the ID provider type.
|
|
157
|
+
* This provides the strongest typing by inferring TID from the idProvider.
|
|
158
|
+
*/
|
|
159
|
+
export declare function createTypedConfiguration<TProvider extends IIdProvider<unknown>>(config: DeepPartial<IConstants> & {
|
|
160
|
+
idProvider: TProvider;
|
|
161
|
+
}): TypedConfiguration<TProvider extends IIdProvider<infer TID> ? TID : never>;
|
|
162
|
+
//# sourceMappingURL=typed-configuration.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-configuration.d.ts","sourceRoot":"","sources":["../../../../packages/digitaldefiance-ecies-lib/src/typed-configuration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,MAAM,CAAC;AACrC,OAAO,EAGL,KAAK,gBAAgB,EACtB,MAAM,aAAa,CAAC;AACrB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AACzD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AACzC,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAExD;;;;GAIG;AACH,qBAAa,kBAAkB,CAAC,GAAG;IACjC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAa;IACxC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAmB;gBAEnC,SAAS,EAAE,UAAU;IAKjC;;;OAGG;IACH,IAAI,UAAU,IAAI,WAAW,CAAC,GAAG,CAAC,CAEjC;IAED;;OAEG;IACH,IAAI,SAAS,IAAI,UAAU,CAE1B;IAED;;OAEG;IACH,UAAU,IAAI,GAAG;IAKjB;;OAEG;IACH,UAAU,CAAC,EAAE,EAAE,GAAG,GAAG,OAAO;IAK5B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,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,GAAG;IAGjC,MAAM,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,GAAG,OAAO;IAG/B,KAAK,CAAC,EAAE,EAAE,GAAG,GAAG,GAAG;IAGnB,UAAU,CAAC,EAAE,EAAE,GAAG,GAAG,MAAM;IAG3B,YAAY,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG;IAK9B;;;OAGG;IACH,aAAa,IAAI,GAAG;IAKpB;;;OAGG;IACH,aAAa,CAAC,EAAE,EAAE,GAAG,GAAG,OAAO;IAK/B;;;OAGG;IACH,cAAc,CAAC,EAAE,EAAE,GAAG,GAAG,MAAM;IAI/B;;;OAGG;IACH,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG;CAGnC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EACvC,GAAG,CAAC,EAAE,gBAAgB,GACrB,sBAAsB,CAAC,GAAG,CAAC,CAG7B;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,qBAAqB,CAAC,GAAG,EACvC,GAAG,CAAC,EAAE,gBAAgB,GACrB,kBAAkB,CAAC,GAAG,CAAC,CAGzB;AAED;;;GAGG;AACH,wBAAgB,wBAAwB,CACtC,SAAS,SAAS,WAAW,CAAC,OAAO,CAAC,EAEtC,MAAM,EAAE,WAAW,CAAC,UAAU,CAAC,GAAG;IAAE,UAAU,EAAE,SAAS,CAAA;CAAE,GAC1D,kBAAkB,CAAC,SAAS,SAAS,WAAW,CAAC,MAAM,GAAG,CAAC,GAAG,GAAG,GAAG,KAAK,CAAC,CAK5E"}
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypedIdProviderWrapper = exports.TypedConfiguration = void 0;
|
|
4
|
+
exports.createObjectIdConfiguration = createObjectIdConfiguration;
|
|
5
|
+
exports.createGuidV4Configuration = createGuidV4Configuration;
|
|
6
|
+
exports.createUint8ArrayConfiguration = createUint8ArrayConfiguration;
|
|
7
|
+
exports.createUuidConfiguration = createUuidConfiguration;
|
|
8
|
+
exports.getTypedIdProvider = getTypedIdProvider;
|
|
9
|
+
exports.getEnhancedIdProvider = getEnhancedIdProvider;
|
|
10
|
+
exports.getTypedConfiguration = getTypedConfiguration;
|
|
11
|
+
exports.createTypedConfiguration = createTypedConfiguration;
|
|
12
|
+
const constants_1 = require("./constants");
|
|
13
|
+
/**
|
|
14
|
+
* Typed configuration wrapper that preserves ID provider type information.
|
|
15
|
+
* This provides strong typing for idProvider operations without making the entire
|
|
16
|
+
* constants system generic.
|
|
17
|
+
*/
|
|
18
|
+
class TypedConfiguration {
|
|
19
|
+
_constants;
|
|
20
|
+
_idProvider;
|
|
21
|
+
constructor(constants) {
|
|
22
|
+
this._constants = constants;
|
|
23
|
+
this._idProvider = constants.idProvider;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Get the strongly-typed ID provider.
|
|
27
|
+
* This returns IIdProvider<TID> with full type safety.
|
|
28
|
+
*/
|
|
29
|
+
get idProvider() {
|
|
30
|
+
return this._idProvider;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Get the underlying constants (for compatibility with existing code).
|
|
34
|
+
*/
|
|
35
|
+
get constants() {
|
|
36
|
+
return this._constants;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Generate a new ID with strong typing.
|
|
40
|
+
*/
|
|
41
|
+
generateId() {
|
|
42
|
+
const bytes = this._idProvider.generate();
|
|
43
|
+
return this._idProvider.fromBytes(bytes);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Validate an ID with strong typing.
|
|
47
|
+
*/
|
|
48
|
+
validateId(id) {
|
|
49
|
+
const bytes = this._idProvider.toBytes(id);
|
|
50
|
+
return this._idProvider.validate(bytes);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Serialize an ID to string with strong typing.
|
|
54
|
+
*/
|
|
55
|
+
serializeId(id) {
|
|
56
|
+
return this._idProvider.idToString(id);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Deserialize a string to ID with strong typing.
|
|
60
|
+
*/
|
|
61
|
+
deserializeId(str) {
|
|
62
|
+
return this._idProvider.idFromString(str);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.TypedConfiguration = TypedConfiguration;
|
|
66
|
+
/**
|
|
67
|
+
* Configuration factory functions that infer and preserve ID provider types.
|
|
68
|
+
*/
|
|
69
|
+
/**
|
|
70
|
+
* Create a typed configuration with ObjectId provider.
|
|
71
|
+
* This is the default configuration with strong ObjectId typing.
|
|
72
|
+
*/
|
|
73
|
+
function createObjectIdConfiguration(overrides) {
|
|
74
|
+
const constants = (0, constants_1.createRuntimeConfiguration)(overrides);
|
|
75
|
+
return new TypedConfiguration(constants);
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create a typed configuration with GuidV4 provider.
|
|
79
|
+
*/
|
|
80
|
+
function createGuidV4Configuration(overrides) {
|
|
81
|
+
const constants = (0, constants_1.createRuntimeConfiguration)(overrides);
|
|
82
|
+
return new TypedConfiguration(constants);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Create a typed configuration with Uint8Array provider (generic bytes).
|
|
86
|
+
*/
|
|
87
|
+
function createUint8ArrayConfiguration(overrides) {
|
|
88
|
+
const constants = (0, constants_1.createRuntimeConfiguration)(overrides);
|
|
89
|
+
return new TypedConfiguration(constants);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Create a typed configuration with UUID string provider.
|
|
93
|
+
*/
|
|
94
|
+
function createUuidConfiguration(overrides) {
|
|
95
|
+
const constants = (0, constants_1.createRuntimeConfiguration)(overrides);
|
|
96
|
+
return new TypedConfiguration(constants);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Get a strongly-typed ID provider from the default configuration.
|
|
100
|
+
* This is the direct equivalent of `getRuntimeConfiguration().idProvider` with strong typing.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```typescript
|
|
104
|
+
* // BEFORE: Weak typing
|
|
105
|
+
* const Constants = getRuntimeConfiguration();
|
|
106
|
+
* const id = Constants.idProvider.generate(); // Returns Uint8Array, no strong typing
|
|
107
|
+
*
|
|
108
|
+
* // AFTER: Strong typing
|
|
109
|
+
* const idProvider = getTypedIdProvider<ObjectId>();
|
|
110
|
+
* const id = idProvider.generateTyped(); // Returns ObjectId - strongly typed!
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
function getTypedIdProvider(key) {
|
|
114
|
+
const constants = (0, constants_1.getRuntimeConfiguration)(key);
|
|
115
|
+
return constants.idProvider;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Enhanced ID provider wrapper that adds strongly-typed convenience methods
|
|
119
|
+
* while preserving all original functionality.
|
|
120
|
+
*/
|
|
121
|
+
class TypedIdProviderWrapper {
|
|
122
|
+
_provider;
|
|
123
|
+
constructor(provider) {
|
|
124
|
+
this._provider = provider;
|
|
125
|
+
}
|
|
126
|
+
// Delegate all original methods
|
|
127
|
+
get byteLength() {
|
|
128
|
+
return this._provider.byteLength;
|
|
129
|
+
}
|
|
130
|
+
get name() {
|
|
131
|
+
return this._provider.name;
|
|
132
|
+
}
|
|
133
|
+
generate() {
|
|
134
|
+
return this._provider.generate();
|
|
135
|
+
}
|
|
136
|
+
validate(id) {
|
|
137
|
+
return this._provider.validate(id);
|
|
138
|
+
}
|
|
139
|
+
serialize(id) {
|
|
140
|
+
return this._provider.serialize(id);
|
|
141
|
+
}
|
|
142
|
+
deserialize(str) {
|
|
143
|
+
return this._provider.deserialize(str);
|
|
144
|
+
}
|
|
145
|
+
toBytes(id) {
|
|
146
|
+
return this._provider.toBytes(id);
|
|
147
|
+
}
|
|
148
|
+
fromBytes(bytes) {
|
|
149
|
+
return this._provider.fromBytes(bytes);
|
|
150
|
+
}
|
|
151
|
+
equals(a, b) {
|
|
152
|
+
return this._provider.equals(a, b);
|
|
153
|
+
}
|
|
154
|
+
clone(id) {
|
|
155
|
+
return this._provider.clone(id);
|
|
156
|
+
}
|
|
157
|
+
idToString(id) {
|
|
158
|
+
return this._provider.idToString(id);
|
|
159
|
+
}
|
|
160
|
+
idFromString(str) {
|
|
161
|
+
return this._provider.idFromString(str);
|
|
162
|
+
}
|
|
163
|
+
// Add strongly-typed convenience methods
|
|
164
|
+
/**
|
|
165
|
+
* Generate a new ID with strong typing.
|
|
166
|
+
* This is equivalent to generate() + fromBytes() but returns the native type directly.
|
|
167
|
+
*/
|
|
168
|
+
generateTyped() {
|
|
169
|
+
const bytes = this.generate();
|
|
170
|
+
return this.fromBytes(bytes);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Validate an ID with strong typing.
|
|
174
|
+
* Accepts the native ID type and validates it.
|
|
175
|
+
*/
|
|
176
|
+
validateTyped(id) {
|
|
177
|
+
const bytes = this.toBytes(id);
|
|
178
|
+
return this.validate(bytes);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Serialize an ID to string with strong typing.
|
|
182
|
+
* Accepts the native ID type directly.
|
|
183
|
+
*/
|
|
184
|
+
serializeTyped(id) {
|
|
185
|
+
return this.idToString(id);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Deserialize a string to ID with strong typing.
|
|
189
|
+
* Returns the native ID type directly.
|
|
190
|
+
*/
|
|
191
|
+
deserializeTyped(str) {
|
|
192
|
+
return this.idFromString(str);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
exports.TypedIdProviderWrapper = TypedIdProviderWrapper;
|
|
196
|
+
/**
|
|
197
|
+
* Get a strongly-typed ID provider with enhanced convenience methods.
|
|
198
|
+
* This provides the most ergonomic API for strongly-typed ID operations.
|
|
199
|
+
*
|
|
200
|
+
* @example
|
|
201
|
+
* ```typescript
|
|
202
|
+
* // Direct replacement for Constants.idProvider with strong typing
|
|
203
|
+
* const idProvider = getEnhancedIdProvider<ObjectId>();
|
|
204
|
+
*
|
|
205
|
+
* // All original methods work the same
|
|
206
|
+
* const bytes = idProvider.generate(); // Uint8Array
|
|
207
|
+
* const isValid = idProvider.validate(bytes); // boolean
|
|
208
|
+
*
|
|
209
|
+
* // Plus new strongly-typed methods
|
|
210
|
+
* const objectId = idProvider.generateTyped(); // ObjectId - strongly typed!
|
|
211
|
+
* const valid = idProvider.validateTyped(objectId); // boolean
|
|
212
|
+
* const serialized = idProvider.serializeTyped(objectId); // string
|
|
213
|
+
* const deserialized = idProvider.deserializeTyped(serialized); // ObjectId
|
|
214
|
+
* ```
|
|
215
|
+
*/
|
|
216
|
+
function getEnhancedIdProvider(key) {
|
|
217
|
+
const provider = getTypedIdProvider(key);
|
|
218
|
+
return new TypedIdProviderWrapper(provider);
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Get a typed configuration from the registry.
|
|
222
|
+
* The type parameter must match the actual ID provider type.
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* ```typescript
|
|
226
|
+
* // For ObjectId configurations
|
|
227
|
+
* const config = getTypedConfiguration<ObjectId>();
|
|
228
|
+
* const id = config.generateId(); // Returns ObjectId
|
|
229
|
+
*
|
|
230
|
+
* // For GUID configurations
|
|
231
|
+
* const guidConfig = getTypedConfiguration<GuidV4>('my-guid-config');
|
|
232
|
+
* const guid = guidConfig.generateId(); // Returns GuidV4
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
function getTypedConfiguration(key) {
|
|
236
|
+
const constants = (0, constants_1.getRuntimeConfiguration)(key);
|
|
237
|
+
return new TypedConfiguration(constants);
|
|
238
|
+
}
|
|
239
|
+
/**
|
|
240
|
+
* Type-safe configuration builder that infers the ID provider type.
|
|
241
|
+
* This provides the strongest typing by inferring TID from the idProvider.
|
|
242
|
+
*/
|
|
243
|
+
function createTypedConfiguration(config) {
|
|
244
|
+
const constants = (0, constants_1.createRuntimeConfiguration)(config);
|
|
245
|
+
return new TypedConfiguration(constants);
|
|
246
|
+
}
|
|
247
|
+
//# sourceMappingURL=typed-configuration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-configuration.js","sourceRoot":"","sources":["../../../../packages/digitaldefiance-ecies-lib/src/typed-configuration.ts"],"names":[],"mappings":";;;AA+EA,kEAKC;AAKD,8DAKC;AAKD,sEAKC;AAKD,0DAKC;AAiBD,gDAKC;AA4GD,sDAKC;AAiBD,sDAKC;AAMD,4DASC;AA7RD,2CAIqB;AAMrB;;;;GAIG;AACH,MAAa,kBAAkB;IACZ,UAAU,CAAa;IACvB,WAAW,CAAmB;IAE/C,YAAY,SAAqB;QAC/B,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,WAAW,GAAG,SAAS,CAAC,UAA8B,CAAC;IAC9D,CAAC;IAED;;;OAGG;IACH,IAAI,UAAU;QACZ,OAAO,IAAI,CAAC,WAAW,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,IAAI,SAAS;QACX,OAAO,IAAI,CAAC,UAAU,CAAC;IACzB,CAAC;IAED;;OAEG;IACH,UAAU;QACR,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;QAC1C,OAAO,IAAI,CAAC,WAAW,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;IAC3C,CAAC;IAED;;OAEG;IACH,UAAU,CAAC,EAAO;QAChB,MAAM,KAAK,GAAG,IAAI,CAAC,WAAW,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QAC3C,OAAO,IAAI,CAAC,WAAW,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAC1C,CAAC;IAED;;OAEG;IACH,WAAW,CAAC,EAAO;QACjB,OAAO,IAAI,CAAC,WAAW,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACzC,CAAC;IA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