bitcore-node
Version:
A blockchain indexing node with extended capabilities using bitcore
122 lines (117 loc) • 3.53 kB
text/typescript
import { cpus, homedir } from 'os';
import { ConfigType } from './types/Config';
import { merge } from './utils';
import parseArgv from './utils/parseArgv';
let program = parseArgv([], ['config']);
function findConfig(): ConfigType | undefined {
let foundConfig;
const envConfigPath = process.env.BITCORE_CONFIG_PATH;
const argConfigPath = program.config;
const configFileName = 'bitcore.config.json';
let bitcoreConfigPaths = [
`${homedir()}/${configFileName}`,
`../../../../${configFileName}`,
`../../${configFileName}`
];
const overrideConfig = argConfigPath || envConfigPath;
if (overrideConfig) {
bitcoreConfigPaths.unshift(overrideConfig);
}
// No config specified. Search home, bitcore and cur directory
for (let path of bitcoreConfigPaths) {
if (!foundConfig) {
try {
const expanded = path[0] === '~' ? path.replace('~', homedir()) : path;
const bitcoreConfig = require(expanded) as { bitcoreNode: ConfigType };
foundConfig = bitcoreConfig.bitcoreNode;
} catch (e) {
foundConfig = undefined;
}
}
}
return foundConfig;
}
function setTrustedPeers(config: ConfigType): ConfigType {
for (let [chain, chainObj] of Object.entries(config)) {
for (let network of Object.keys(chainObj)) {
let env = process.env;
const envString = `TRUSTED_${chain.toUpperCase()}_${network.toUpperCase()}_PEER`;
if (env[envString]) {
let peers = config.chains[chain][network].trustedPeers || [];
peers.push({
host: env[envString] as string,
port: env[`${envString}_PORT`] as string
});
config.chains[chain][network].trustedPeers = peers;
}
}
}
return config;
}
const Config = function(): ConfigType {
let config: ConfigType = {
maxPoolSize: 50,
port: 3000,
dbUrl: process.env.DB_URL || '',
dbHost: process.env.DB_HOST || '127.0.0.1',
dbName: process.env.DB_NAME || 'bitcore',
dbPort: process.env.DB_PORT || '27017',
dbUser: process.env.DB_USER || '',
dbPass: process.env.DB_PASS || '',
numWorkers: cpus().length,
chains: {},
aliasMapping: {
chains: {},
networks: {}
},
services: {
api: {
rateLimiter: {
disabled: false,
whitelist: ['::ffff:127.0.0.1', '::1']
},
wallets: {
allowCreationBeforeCompleteSync: false,
allowUnauthenticatedCalls: false
}
},
event: {
onlyWalletEvents: false
},
p2p: {},
socket: {
bwsKeys: []
},
storage: {}
},
externalProviders: {
moralis: {
apiKey: 'string'
}
}
};
let foundConfig = findConfig();
config = merge(config, foundConfig);
if (!Object.keys(config.chains).length) {
Object.assign(config.chains, {
BTC: {
mainnet: {
chainSource: 'p2p',
trustedPeers: [{ host: '127.0.0.1', port: 8333 }],
rpc: {
host: '127.0.0.1',
port: 8332,
username: 'bitcoin',
password: 'bitcoin'
}
}
}
});
}
if ((config as any).modules) {
throw new Error('The config modules has moved! You can remove the `modules` array from your config to use the defaults, or if you need to use custom modules then you can specify the paths in the specific chain-network config objects with `modulePath`');
}
config = setTrustedPeers(config);
return config;
};
export default Config();