// lib/config-loader.cjs - Configuration Loader
require('bare-node-runtime/global')
const fs = require('bare-fs')
const path = require('bare-path')
/**
* Load configuration from file
* Looks for config/default.json relative to project root
* @param {string} configPath - Optional path to config file
* @returns {Object} - Configuration object
*/
function loadConfig(configPath) {
// Default path: config/default.json in project root
const defaultPath = path.join(__dirname, '..', 'config', 'default.json')
const filePath = configPath || defaultPath
try {
if (!fs.existsSync(filePath)) {
console.warn(`⚠️ Config file not found: ${filePath}`)
console.warn('Using default configuration')
return getDefaultConfig()
}
const content = fs.readFileSync(filePath, 'utf8')
const config = JSON.parse(content)
// Convert flat config to format expected by wdk-integration.cjs
return normalizeConfig(config)
} catch (err) {
console.error(`Failed to load config from ${filePath}:`, err.message)
console.warn('Falling back to default configuration')
return getDefaultConfig()
}
}
/**
* Normalize config to expected format
* Converts config/default.json format to internal format used by wdk-integration
* @param {Object} config - Raw config from file
* @returns {Object} - Normalized config
*/
function normalizeConfig(config) {
const normalized = {
storagePath: config.storagePath || '~/.hyper-wallet/storage',
network: config.network || 'testnet',
protocols: config.protocols || ['wdk'],
// Convert network flag to boolean flags expected by wdk-integration.cjs
testnet: config.network === 'testnet',
testnet4: config.network === 'testnet4',
mainnet: config.network === 'mainnet',
// WDK-specific config
wdk: config.wdk || {}
}
return normalized
}
/**
* Get default configuration (fallback)
* Used when config file is missing or invalid
* @returns {Object} - Default configuration
*/
function getDefaultConfig() {
return {
storagePath: '~/.hyper-wallet/storage',
network: 'testnet',
protocols: ['wdk'],
testnet: true,
testnet4: false,
mainnet: false,
wdk: {
chains: ['ethereum', 'bitcoin'],
networks: {
ethereum: {
name: 'sepolia',
provider: 'https://ethereum-sepolia-rpc.publicnode.com',
chainId: 11155111
},
bitcoin: {
network: 'testnet',
client: {
type: 'electrum',
clientConfig: {
host: 'electrum.blockstream.info',
port: 60001
}
}
}
}
}
}
}
module.exports = {
loadConfig,
normalizeConfig,
getDefaultConfig
}