import * as fs from 'fs'; import * as path from 'path'; import * as os from 'os'; import yaml from 'js-yaml'; export interface Config { privateKey?: string; network?: string; networks?: { [network: string]: { schemaUID?: string; rpcUrl?: string; easContractAddress?: string; schemaRegistryAddress?: string; }; }; } /** * Load config from multiple sources (priority order): * 1. Custom config path (--config flag) * 2. ./.atnotary.yaml (current directory) * 3. ~/.atnotary.yaml (home directory) * 4. .env file (fallback) */ export function loadConfig(customPath?: string): Config { let config: Config = {}; // Try custom path first if (customPath) { if (fs.existsSync(customPath)) { config = loadYamlConfig(customPath); return config; } else { throw new Error(`Config file not found: ${customPath}`); } } // Try current directory const localConfig = path.join(process.cwd(), '.atnotary.yaml'); if (fs.existsSync(localConfig)) { config = loadYamlConfig(localConfig); return config; } // Try home directory const homeConfig = path.join(os.homedir(), '.atnotary.yaml'); if (fs.existsSync(homeConfig)) { config = loadYamlConfig(homeConfig); return config; } return config; } function loadYamlConfig(filePath: string): Config { try { const fileContents = fs.readFileSync(filePath, 'utf8'); const config = yaml.load(fileContents) as Config; return config; } catch (error: any) { throw new Error(`Failed to load config from ${filePath}: ${error.message}`); } } /** * Get schema UID for specific network */ export function getSchemaUID(config: Config, network: string): string | undefined { return config.networks?.[network]?.schemaUID; } /** * Create default config template */ export function createConfigTemplate(outputPath: string): void { const template = `# atnotary configuration # Private key for signing transactions (required for notarization) privateKey: "0x..." # Default network (sepolia, base-sepolia, base) network: sepolia # Network-specific configuration networks: sepolia: schemaUID: "0x..." # Optional: custom RPC URL # rpcUrl: "https://..." base-sepolia: schemaUID: "0x..." # rpcUrl: "https://sepolia.base.org" base: schemaUID: "0x..." # rpcUrl: "https://mainnet.base.org" `; fs.writeFileSync(outputPath, template, 'utf8'); }