Skip to content

Commit

Permalink
Allow for auto registration of modules in bitcore.config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby authored and Ben Clabby committed Jul 9, 2021
1 parent d0470bd commit b287022
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
36 changes: 35 additions & 1 deletion packages/bitcore-node/src/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _ from 'lodash';

import { ChainStateProvider } from '../providers/chain-state';
import { Libs } from '../providers/libs';
import { Api } from '../services/api';
Expand Down Expand Up @@ -42,10 +44,42 @@ export class BaseModule implements IService {

class ModuleManager extends BaseModule {
internalServices = new Array<IService>();

// Chain names -> module paths map
KNOWN_MODULE_PATHS = {
'BTC': './bitcoin',
'ETH': './ethereum',
'BCH': './bitcoin-cash',
'DOGE': './dogecoin',
'LTC': './litecoin',
'XRP': './ripple'
};

loadConfigured() {
for (const modulePath of Config.get().modules) {
let { modules, chains } = Config.get();
modules = modules || [];

const registerModuleClass = (modulePath) => {
const moduleClass = require(modulePath).default || (require(modulePath) as Class<BaseModule>);
this.internalServices.push(new moduleClass(this.bitcoreServices));
};

// Register all configured modules (in case users want to add custom modules)
if (modules.length > 0)
for (const modulePath of modules)
registerModuleClass(modulePath);

// Auto register known modules from config.chains
for (const chain in chains) {
const modulePath = this.KNOWN_MODULE_PATHS[chain];
if (!modulePath)
throw new Error(
`Auto module registration failed for chain "${chain}". ` +
'Is the chain name / module path inside of KNOWN_MODULE_PATHS?'
);

// Do not register detected modulePath if it is in config.modules as well
if (!_.includes(modules, modulePath)) registerModuleClass(modulePath);
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions packages/bitcore-node/src/modules/modules.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Modules
Modules are loaded before services are started. This allows code to hook into services and register classes, event handlers, etc that alter the behaviors of services.

## Known Modules
The modules in this table will automatically register with `bitcore-node` if your `bitcore.config.json` contains a valid configuration for their respective chains.

| Chain | Module | Module Path (Relative to ModuleManager) |
| -------------- | -------------- | -------------- |
| BTC | bitcoin | ./bitcoin |
| ETH | ethereum | ./ethereum |
| BCH | bitcoin-cash | ./bitcoin-cash |
| LTC | litecoin | ./litecoin |
| DOGE | dogecoin | ./dogecoin |
| XRP | ripple | ./ripple |

If there is a custom or third-party module you'd like to use, follow the example below.

## Example - Syncing BCH
Let's say we have a node_module, named `bitcore-node-bch` with the following code

Expand Down

0 comments on commit b287022

Please sign in to comment.