(filePath: string)
| 38 | * @throws KeyStoreLoadError When JSON is invalid, schema validation fails, or other IO/parse errors occur. |
| 39 | */ |
| 40 | export function loadKeystoreFile(filePath: string): KeyStore { |
| 41 | try { |
| 42 | const content = readFileSync(filePath, 'utf-8'); |
| 43 | |
| 44 | // Parse JSON and validate with Zod schema (following Aztec patterns) |
| 45 | return keystoreSchema.parse(JSON.parse(content)); |
| 46 | } catch (error) { |
| 47 | if (error instanceof SyntaxError) { |
| 48 | throw new KeyStoreLoadError('Invalid JSON format', filePath, error); |
| 49 | } |
| 50 | if (error && typeof error === 'object' && 'issues' in error) { |
| 51 | const issues = (error as any).issues ?? []; |
| 52 | const message = |
| 53 | issues |
| 54 | .map((e: any) => { |
| 55 | const path = Array.isArray(e.path) ? e.path.join('.') : String(e.path ?? 'root'); |
| 56 | return `${e.message} (${path})`; |
| 57 | }) |
| 58 | .join('. ') || 'Schema validation error'; |
| 59 | throw new KeyStoreLoadError(`Schema validation failed: ${message}`, filePath, error as unknown as Error); |
| 60 | } |
| 61 | throw new KeyStoreLoadError(`Unexpected error: ${String(error)}`, filePath, error as Error); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Loads keystore files from a directory (only .json files). |
no test coverage detected