@ethereumjs/common
Version:
Resources common to all Ethereum implementations
49 lines • 1.73 kB
JavaScript
import { Common, parseGethGenesis } from "./index.js";
/**
* Creates a {@link Common} object for a custom chain, based on a standard one.
*
* It uses all the {@link Chain} parameters from the {@link baseChain} option except the ones overridden
* in a provided {@link chainParamsOrName} dictionary. Some usage example:
*
* ```javascript
* import { createCustomCommon, Mainnet } from '@ethereumjs/common'
*
* createCustomCommon({chainId: 123}, Mainnet)
* ```
*
* @param partialConfig Custom parameter dict
* @param baseChain `ChainConfig` chain configuration taken as a base chain, e.g. `Mainnet` (exported at root level)
* @param opts Custom chain options to set various {@link BaseOpts}
*/
export function createCustomCommon(partialConfig, baseChain, opts = {}) {
return new Common({
chain: {
...baseChain,
...partialConfig,
},
...opts,
});
}
/**
* Static method to load and set common from a geth genesis object
* @param genesisJSON GethGenesis object
* @returns a new {@link Common} object
*/
export function createCommonFromGethGenesis(genesisJSON, { chain, eips, genesisHash, hardfork, params, customCrypto }) {
const genesisParams = parseGethGenesis(genesisJSON, chain);
const common = new Common({
chain: {
...genesisParams,
name: genesisParams.name ?? 'Custom chain',
}, // Typecasting because of `string` -> `PrefixedHexString` mismatches
eips,
params,
hardfork: hardfork ?? genesisParams.hardfork,
customCrypto,
});
if (genesisHash !== undefined) {
common.setForkHashes(genesisHash);
}
return common;
}
//# sourceMappingURL=constructors.js.map