@autonomaai/security-utils 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.
@@ -0,0 +1,300 @@
1
+ /**
2
+ * Secure Credential Manager for autonoma
3
+ *
4
+ * Provides encryption/decryption of sensitive environment variables
5
+ * and secure credential storage mechanisms.
6
+ */
7
+ import { createCipheriv, createDecipheriv, randomBytes, createHash } from 'crypto';
8
+ import { readFileSync, writeFileSync, existsSync } from 'fs';
9
+ import { join } from 'path';
10
+ export class CredentialManager {
11
+ encryptionKey;
12
+ credentialsFile;
13
+ environment;
14
+ credentials = new Map();
15
+ constructor(config = {}) {
16
+ this.environment = config.environment || process.env.NODE_ENV || 'development';
17
+ this.credentialsFile = config.credentialsFile || join(process.cwd(), '.credentials.enc');
18
+ // Generate or use provided encryption key
19
+ this.encryptionKey = config.encryptionKey ||
20
+ process.env.ENCRYPTION_KEY ||
21
+ this.generateEncryptionKey();
22
+ // Validate encryption key length
23
+ if (this.encryptionKey.length < 32) {
24
+ throw new Error('Encryption key must be at least 32 characters long');
25
+ }
26
+ this.loadCredentials();
27
+ }
28
+ /**
29
+ * Generate a secure encryption key
30
+ */
31
+ generateEncryptionKey() {
32
+ return randomBytes(32).toString('hex');
33
+ }
34
+ /**
35
+ * Encrypt a value using AES-256-GCM (authenticated encryption)
36
+ */
37
+ encrypt(text) {
38
+ try {
39
+ const algorithm = 'aes-256-gcm';
40
+ const key = createHash('sha256').update(this.encryptionKey).digest();
41
+ const iv = randomBytes(12); // 96-bit IV for GCM
42
+ const cipher = createCipheriv(algorithm, key, iv);
43
+ let encrypted = cipher.update(text, 'utf8', 'hex');
44
+ encrypted += cipher.final('hex');
45
+ const authTag = cipher.getAuthTag().toString('hex');
46
+ // Format: iv:authTag:encrypted
47
+ return iv.toString('hex') + ':' + authTag + ':' + encrypted;
48
+ }
49
+ catch (error) {
50
+ throw new Error(`Encryption failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
51
+ }
52
+ }
53
+ /**
54
+ * Decrypt a value using AES-256-GCM (authenticated encryption)
55
+ */
56
+ decrypt(encryptedText) {
57
+ try {
58
+ const algorithm = 'aes-256-gcm';
59
+ const key = createHash('sha256').update(this.encryptionKey).digest();
60
+ const parts = encryptedText.split(':');
61
+ if (parts.length !== 3) {
62
+ throw new Error('Invalid encrypted text format (expected iv:authTag:encrypted)');
63
+ }
64
+ const [ivHex, authTagHex, encrypted] = parts;
65
+ if (!ivHex || !authTagHex || !encrypted) {
66
+ throw new Error('Missing encrypted data components');
67
+ }
68
+ const iv = Buffer.from(ivHex, 'hex');
69
+ const authTag = Buffer.from(authTagHex, 'hex');
70
+ const decipher = createDecipheriv(algorithm, key, iv);
71
+ decipher.setAuthTag(authTag);
72
+ let decrypted = decipher.update(encrypted, 'hex', 'utf8');
73
+ decrypted += decipher.final('utf8');
74
+ return decrypted;
75
+ }
76
+ catch (error) {
77
+ throw new Error(`Decryption failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
78
+ }
79
+ }
80
+ /**
81
+ * Load credentials from encrypted file
82
+ */
83
+ loadCredentials() {
84
+ if (!existsSync(this.credentialsFile)) {
85
+ return; // No credentials file exists yet
86
+ }
87
+ try {
88
+ const encryptedData = readFileSync(this.credentialsFile, 'utf8');
89
+ const decryptedData = this.decrypt(encryptedData);
90
+ const credentialData = JSON.parse(decryptedData);
91
+ for (const cred of credentialData) {
92
+ this.credentials.set(cred.name, {
93
+ ...cred,
94
+ lastUpdated: new Date(cred.lastUpdated)
95
+ });
96
+ }
97
+ }
98
+ catch (error) {
99
+ console.warn(`Failed to load credentials: ${error instanceof Error ? error.message : 'Unknown error'}`);
100
+ }
101
+ }
102
+ /**
103
+ * Save credentials to encrypted file
104
+ */
105
+ saveCredentials() {
106
+ try {
107
+ const credentialArray = Array.from(this.credentials.values());
108
+ const jsonData = JSON.stringify(credentialArray, null, 2);
109
+ const encryptedData = this.encrypt(jsonData);
110
+ writeFileSync(this.credentialsFile, encryptedData, 'utf8');
111
+ }
112
+ catch (error) {
113
+ throw new Error(`Failed to save credentials: ${error instanceof Error ? error.message : 'Unknown error'}`);
114
+ }
115
+ }
116
+ /**
117
+ * Set a credential (encrypts sensitive values)
118
+ */
119
+ setCredential(name, value, encrypt = true) {
120
+ const credential = {
121
+ name,
122
+ value: encrypt ? this.encrypt(value) : value,
123
+ encrypted: encrypt,
124
+ lastUpdated: new Date(),
125
+ environment: this.environment
126
+ };
127
+ this.credentials.set(name, credential);
128
+ this.saveCredentials();
129
+ }
130
+ /**
131
+ * Get a credential (decrypts if needed)
132
+ */
133
+ getCredential(name) {
134
+ const credential = this.credentials.get(name);
135
+ if (!credential) {
136
+ // Fallback to environment variable
137
+ return process.env[name];
138
+ }
139
+ try {
140
+ return credential.encrypted ? this.decrypt(credential.value) : credential.value;
141
+ }
142
+ catch (error) {
143
+ console.warn(`Failed to decrypt credential ${name}: ${error instanceof Error ? error.message : 'Unknown error'}`);
144
+ return process.env[name]; // Fallback to env var
145
+ }
146
+ }
147
+ /**
148
+ * Get all credential names (for listing purposes)
149
+ */
150
+ getCredentialNames() {
151
+ return Array.from(this.credentials.keys());
152
+ }
153
+ /**
154
+ * Remove a credential
155
+ */
156
+ removeCredential(name) {
157
+ const deleted = this.credentials.delete(name);
158
+ if (deleted) {
159
+ this.saveCredentials();
160
+ }
161
+ return deleted;
162
+ }
163
+ /**
164
+ * Migrate plain-text environment variables to encrypted credentials
165
+ */
166
+ migrateEnvironmentVariables(sensitiveVars) {
167
+ for (const varName of sensitiveVars) {
168
+ const value = process.env[varName];
169
+ if (value && !this.credentials.has(varName)) {
170
+ this.setCredential(varName, value, true);
171
+ // Use structured logging format
172
+ const logEntry = {
173
+ timestamp: new Date().toISOString(),
174
+ level: 'INFO',
175
+ component: 'CredentialManager',
176
+ message: 'Credential migrated to encrypted storage',
177
+ variable: varName
178
+ };
179
+ if (process.env.NODE_ENV === 'production') {
180
+ console.log(JSON.stringify(logEntry));
181
+ }
182
+ else {
183
+ console.log(`[${logEntry.timestamp}] ${logEntry.level} [${logEntry.component}] Migrated ${varName} to encrypted storage`);
184
+ }
185
+ }
186
+ }
187
+ }
188
+ /**
189
+ * Validate that required credentials are present
190
+ */
191
+ validateRequiredCredentials(requiredVars) {
192
+ const missing = [];
193
+ for (const varName of requiredVars) {
194
+ const value = this.getCredential(varName);
195
+ if (!value || value.trim() === '') {
196
+ missing.push(varName);
197
+ }
198
+ }
199
+ return {
200
+ valid: missing.length === 0,
201
+ missing
202
+ };
203
+ }
204
+ /**
205
+ * Get secure environment configuration
206
+ */
207
+ getSecureEnvConfig() {
208
+ const config = {};
209
+ // Add non-sensitive environment variables
210
+ for (const [key, value] of Object.entries(process.env)) {
211
+ if (value && !this.credentials.has(key)) {
212
+ config[key] = value;
213
+ }
214
+ }
215
+ // Add decrypted credentials
216
+ for (const name of this.credentials.keys()) {
217
+ const value = this.getCredential(name);
218
+ if (value) {
219
+ config[name] = value;
220
+ }
221
+ }
222
+ return config;
223
+ }
224
+ /**
225
+ * Health check for credential system
226
+ */
227
+ healthCheck() {
228
+ const details = [];
229
+ let status = 'healthy';
230
+ // Check encryption key
231
+ if (this.encryptionKey.length < 32) {
232
+ details.push('Encryption key is too short');
233
+ status = 'error';
234
+ }
235
+ // Check credentials file access
236
+ try {
237
+ if (existsSync(this.credentialsFile)) {
238
+ readFileSync(this.credentialsFile, 'utf8');
239
+ details.push('Credentials file accessible');
240
+ }
241
+ else {
242
+ details.push('No credentials file found (will be created on first write)');
243
+ }
244
+ }
245
+ catch (error) {
246
+ details.push(`Credentials file access error: ${error instanceof Error ? error.message : 'Unknown error'}`);
247
+ status = 'error';
248
+ }
249
+ // Test encryption/decryption
250
+ try {
251
+ const testValue = 'test_encryption';
252
+ const encrypted = this.encrypt(testValue);
253
+ const decrypted = this.decrypt(encrypted);
254
+ if (decrypted === testValue) {
255
+ details.push('Encryption/decryption working');
256
+ }
257
+ else {
258
+ details.push('Encryption/decryption test failed');
259
+ status = 'error';
260
+ }
261
+ }
262
+ catch (error) {
263
+ details.push(`Encryption test failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
264
+ status = 'error';
265
+ }
266
+ return { status, details };
267
+ }
268
+ }
269
+ // Default instance for convenience
270
+ export const credentialManager = new CredentialManager();
271
+ // Helper function to get secure environment variables
272
+ export function getSecureEnv(name, defaultValue) {
273
+ return credentialManager.getCredential(name) || defaultValue || '';
274
+ }
275
+ // Helper function to migrate sensitive environment variables
276
+ export function migrateSensitiveEnvVars() {
277
+ const sensitiveVars = [
278
+ 'POSTGRES_PASSWORD',
279
+ 'REDIS_PASSWORD',
280
+ 'HUMMINGBOT_PASSWORD',
281
+ 'HUMMINGBOT_API_KEY',
282
+ 'API_SECRET_KEY',
283
+ 'JWT_SECRET',
284
+ 'ENCRYPTION_KEY',
285
+ 'BINANCE_API_KEY',
286
+ 'BINANCE_SECRET_KEY',
287
+ 'KUCOIN_API_KEY',
288
+ 'KUCOIN_SECRET_KEY',
289
+ 'KUCOIN_PASSPHRASE',
290
+ 'COINBASE_API_KEY',
291
+ 'COINBASE_SECRET',
292
+ 'COINBASE_PASSPHRASE',
293
+ 'HYPERLIQUID_PRIVATE_KEY',
294
+ 'SUPABASE_SERVICE_ROLE_KEY',
295
+ 'OPENAI_API_KEY',
296
+ 'MORALIS_API_KEY'
297
+ ];
298
+ credentialManager.migrateEnvironmentVariables(sensitiveVars);
299
+ }
300
+ //# sourceMappingURL=credential-manager.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"credential-manager.js","sourceRoot":"","sources":["../src/credential-manager.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,cAAc,EAAE,gBAAgB,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACnF,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,IAAI,CAAC;AAC7D,OAAO,EAAE,IAAI,EAAE,MAAM,MAAM,CAAC;AAgB5B,MAAM,OAAO,iBAAiB;IACp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,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,EAAE,CAAC,CAAC;YACpG,MAAM,GAAG,OAAO,CAAC;SAClB;QAED,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;IAC7B,CAAC;CACF;AAED,mCAAmC;AACnC,MAAM,CAAC,MAAM,iBAAiB,GAAG,IAAI,iBAAiB,EAAE,CAAC;AAEzD,sDAAsD;AACtD,MAAM,UAAU,YAAY,CAAC,IAAY,EAAE,YAAqB;IAC9D,OAAO,iBAAiB,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,YAAY,IAAI,EAAE,CAAC;AACrE,CAAC;AAED,6DAA6D;AAC7D,MAAM,UAAU,uBAAuB;IACrC,MAAM,aAAa,GAAG;QACpB,mBAAmB;QACnB,gBAAgB;QAChB,qBAAqB;QACrB,oBAAoB;QACpB,gBAAgB;QAChB,YAAY;QACZ,gBAAgB;QAChB,iBAAiB;QACjB,oBAAoB;QACpB,gBAAgB;QAChB,mBAAmB;QACnB,mBAAmB;QACnB,kBAAkB;QAClB,iBAAiB;QACjB,qBAAqB;QACrB,yBAAyB;QACzB,2BAA2B;QAC3B,gBAAgB;QAChB,iBAAiB;KAClB,CAAC;IAEF,iBAAiB,CAAC,2BAA2B,CAAC,aAAa,CAAC,CAAC;AAC/D,CAAC"}
@@ -0,0 +1,15 @@
1
+ /**
2
+ * Security Utilities for autonoma
3
+ *
4
+ * Provides credential management, secret storage, and security utilities
5
+ * for the autonoma trading platform.
6
+ */
7
+ export { CredentialManager, type CredentialConfig, type SecureCredential } from './credential-manager.js';
8
+ export { SecretManager, RuntimeSecretInjector, type SecretConfig, type SecretMetadata, type RotationPolicy } from './secret-manager.js';
9
+ import { SecretManager, RuntimeSecretInjector } from './secret-manager.js';
10
+ export declare function initializeSecureEnvironment(encryptionKey?: string): {
11
+ secretManager: SecretManager;
12
+ injector: RuntimeSecretInjector;
13
+ };
14
+ export declare function generateSecureDefaults(): Record<string, string>;
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,iBAAiB,EAAE,KAAK,gBAAgB,EAAE,KAAK,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC1G,OAAO,EACL,aAAa,EACb,qBAAqB,EACrB,KAAK,YAAY,EACjB,KAAK,cAAc,EACnB,KAAK,cAAc,EACpB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAG3E,wBAAgB,2BAA2B,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG;IACnE,aAAa,EAAE,aAAa,CAAC;IAC7B,QAAQ,EAAE,qBAAqB,CAAC;CACjC,CAKA;AAED,wBAAgB,sBAAsB,IAAI,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAU/D"}
package/dist/index.js ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Security Utilities for autonoma
3
+ *
4
+ * Provides credential management, secret storage, and security utilities
5
+ * for the autonoma trading platform.
6
+ */
7
+ export { CredentialManager } from './credential-manager.js';
8
+ export { SecretManager, RuntimeSecretInjector } from './secret-manager.js';
9
+ import { SecretManager, RuntimeSecretInjector } from './secret-manager.js';
10
+ // Utility functions for environment setup
11
+ export function initializeSecureEnvironment(encryptionKey) {
12
+ const secretManager = new SecretManager(encryptionKey);
13
+ const injector = new RuntimeSecretInjector(secretManager);
14
+ return { secretManager, injector };
15
+ }
16
+ export function generateSecureDefaults() {
17
+ const secretManager = new SecretManager();
18
+ return {
19
+ ENCRYPTION_KEY: secretManager.generateSecretValue(32),
20
+ JWT_SECRET: secretManager.generateSecretValue(64),
21
+ API_SECRET_KEY: secretManager.generateSecretValue(32),
22
+ POSTGRES_PASSWORD: secretManager.generateSecretValue(24),
23
+ REDIS_PASSWORD: secretManager.generateSecretValue(24)
24
+ };
25
+ }
26
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,iBAAiB,EAAgD,MAAM,yBAAyB,CAAC;AAC1G,OAAO,EACL,aAAa,EACb,qBAAqB,EAItB,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAE3E,0CAA0C;AAC1C,MAAM,UAAU,2BAA2B,CAAC,aAAsB;IAIhE,MAAM,aAAa,GAAG,IAAI,aAAa,CAAC,aAAa,CAAC,CAAC;IACvD,MAAM,QAAQ,GAAG,IAAI,qBAAqB,CAAC,aAAa,CAAC,CAAC;IAE1D,OAAO,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC;AACrC,CAAC;AAED,MAAM,UAAU,sBAAsB;IACpC,MAAM,aAAa,GAAG,IAAI,aAAa,EAAE,CAAC;IAE1C,OAAO;QACL,cAAc,EAAE,aAAa,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACrD,UAAU,EAAE,aAAa,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACjD,cAAc,EAAE,aAAa,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACrD,iBAAiB,EAAE,aAAa,CAAC,mBAAmB,CAAC,EAAE,CAAC;QACxD,cAAc,EAAE,aAAa,CAAC,mBAAmB,CAAC,EAAE,CAAC;KACtD,CAAC;AACJ,CAAC"}
@@ -0,0 +1,114 @@
1
+ /**
2
+ * Secret Management System for autonoma
3
+ *
4
+ * Provides secure secret storage, runtime injection, and rotation capabilities
5
+ * for sensitive configuration values like API keys and database credentials.
6
+ */
7
+ export interface SecretConfig {
8
+ name: string;
9
+ value: string;
10
+ description?: string;
11
+ rotationPolicy?: RotationPolicy;
12
+ tags?: string[];
13
+ createdAt: Date;
14
+ updatedAt: Date;
15
+ expiresAt?: Date;
16
+ }
17
+ export interface RotationPolicy {
18
+ enabled: boolean;
19
+ intervalDays: number;
20
+ autoRotate: boolean;
21
+ notifyBeforeExpiry: number;
22
+ }
23
+ export interface SecretMetadata {
24
+ name: string;
25
+ description?: string;
26
+ tags?: string[];
27
+ createdAt: Date;
28
+ updatedAt: Date;
29
+ expiresAt?: Date;
30
+ rotationPolicy?: RotationPolicy;
31
+ }
32
+ export declare class SecretManager {
33
+ private readonly credentialManager;
34
+ private readonly secrets;
35
+ private readonly secretsFile;
36
+ constructor(encryptionKey?: string, secretsFile?: string);
37
+ /**
38
+ * Store a secret with optional rotation policy
39
+ */
40
+ setSecret(name: string, value: string, options?: {
41
+ description?: string;
42
+ rotationPolicy?: RotationPolicy;
43
+ tags?: string[];
44
+ expiresAt?: Date;
45
+ }): Promise<void>;
46
+ /**
47
+ * Retrieve a secret value
48
+ */
49
+ getSecret(name: string): Promise<string | null>;
50
+ /**
51
+ * Get secret metadata without exposing the value
52
+ */
53
+ getSecretMetadata(name: string): SecretMetadata | null;
54
+ /**
55
+ * List all secret names and metadata
56
+ */
57
+ listSecrets(): SecretMetadata[];
58
+ /**
59
+ * Rotate a secret value
60
+ */
61
+ rotateSecret(name: string, newValue: string): Promise<void>;
62
+ /**
63
+ * Delete a secret
64
+ */
65
+ deleteSecret(name: string): Promise<boolean>;
66
+ /**
67
+ * Get secrets that need rotation
68
+ */
69
+ getSecretsNeedingRotation(): SecretMetadata[];
70
+ /**
71
+ * Inject secrets into environment variables
72
+ */
73
+ injectSecretsIntoEnv(): Promise<void>;
74
+ /**
75
+ * Generate secure random secret value
76
+ */
77
+ generateSecretValue(length?: number): string;
78
+ /**
79
+ * Validate secret strength
80
+ */
81
+ validateSecretStrength(value: string): {
82
+ isStrong: boolean;
83
+ score: number;
84
+ feedback: string[];
85
+ };
86
+ /**
87
+ * Load secrets from encrypted file
88
+ */
89
+ private loadSecrets;
90
+ /**
91
+ * Save secrets to encrypted file
92
+ */
93
+ private saveSecrets;
94
+ }
95
+ /**
96
+ * Runtime secret injection utility
97
+ */
98
+ export declare class RuntimeSecretInjector {
99
+ private readonly secretManager;
100
+ constructor(secretManager: SecretManager);
101
+ /**
102
+ * Replace placeholders in configuration with actual secret values
103
+ */
104
+ injectSecrets(config: Record<string, any>): Promise<Record<string, any>>;
105
+ /**
106
+ * Recursively find and replace secret placeholders
107
+ */
108
+ private recursivelyInjectSecrets;
109
+ /**
110
+ * Create environment variable mapping from secrets
111
+ */
112
+ createEnvMapping(): Promise<Record<string, string>>;
113
+ }
114
+ //# sourceMappingURL=secret-manager.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secret-manager.d.ts","sourceRoot":"","sources":["../src/secret-manager.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAOH,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,cAAc,CAAC,EAAE,cAAc,CAAC;IAChC,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,CAAC,EAAE,IAAI,CAAC;CAClB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,OAAO,CAAC;IACpB,kBAAkB,EAAE,MAAM,CAAC;CAC5B;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,CAAC,EAAE,IAAI,CAAC;IACjB,cAAc,CAAC,EAAE,cAAc,CAAC;CACjC;AAED,qBAAa,aAAa;IACxB,OAAO,CAAC,QA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