11import { chains as chainParams } from './chains'
22import { hardforks as hardforkChanges } from './hardforks'
3+ import { Chain } from './types'
34
45interface hardforkOptions {
56 /** optional, only allow supported HFs (default: false) */
@@ -14,7 +15,51 @@ interface hardforkOptions {
1415export default class Common {
1516 private _hardfork : string | null
1617 private _supportedHardforks : Array < string >
17- private _chainParams : object
18+ private _chainParams : Chain
19+
20+ /**
21+ * Creates a Common object for a custom chain, based on a standard one. It uses all the [[Chain]]
22+ * params from [[baseChain]] except the ones overridden in [[customChainParams]].
23+ *
24+ * @param baseChain The name (`mainnet`) or id (`1`) of a standard chain used to base the custom
25+ * chain params on.
26+ * @param customChainParams The custom parameters of the chain.
27+ * @param hardfork String identifier ('byzantium') for hardfork (optional)
28+ * @param supportedHardforks Limit parameter returns to the given hardforks (optional)
29+ */
30+ static forCustomChain (
31+ baseChain : string | number ,
32+ customChainParams : Partial < Chain > ,
33+ hardfork ?: string | null ,
34+ supportedHardforks ?: Array < string > ,
35+ ) : Common {
36+ const standardChainParams = Common . _getChainParams ( baseChain )
37+
38+ return new Common (
39+ {
40+ ...standardChainParams ,
41+ ...customChainParams ,
42+ } ,
43+ hardfork ,
44+ supportedHardforks ,
45+ )
46+ }
47+
48+ private static _getChainParams ( chain : string | number ) : Chain {
49+ if ( typeof chain === 'number' ) {
50+ if ( chainParams [ 'names' ] [ chain ] ) {
51+ return chainParams [ chainParams [ 'names' ] [ chain ] ]
52+ }
53+
54+ throw new Error ( `Chain with ID ${ chain } not supported` )
55+ }
56+
57+ if ( chainParams [ chain ] ) {
58+ return chainParams [ chain ]
59+ }
60+
61+ throw new Error ( `Chain with name ${ chain } not supported` )
62+ }
1863
1964 /**
2065 * @constructor
@@ -42,26 +87,16 @@ export default class Common {
4287 * @returns The dictionary with parameters set as chain
4388 */
4489 setChain ( chain : string | number | object ) : any {
45- if ( typeof chain === 'number' ) {
46- if ( chainParams [ 'names' ] [ chain ] ) {
47- this . _chainParams = chainParams [ chainParams [ 'names' ] [ chain ] ]
48- } else {
49- throw new Error ( `Chain with ID ${ chain } not supported` )
50- }
51- } else if ( typeof chain === 'string' ) {
52- if ( chainParams [ chain ] ) {
53- this . _chainParams = chainParams [ chain ]
54- } else {
55- throw new Error ( `Chain with name ${ chain } not supported` )
56- }
90+ if ( typeof chain === 'number' || typeof chain === 'string' ) {
91+ this . _chainParams = Common . _getChainParams ( chain )
5792 } else if ( typeof chain === 'object' ) {
5893 const required = [ 'networkId' , 'genesis' , 'hardforks' , 'bootstrapNodes' ]
5994 for ( const param of required ) {
6095 if ( ( < any > chain ) [ param ] === undefined ) {
6196 throw new Error ( `Missing required chain parameter: ${ param } ` )
6297 }
6398 }
64- this . _chainParams = chain
99+ this . _chainParams = chain as Chain
65100 } else {
66101 throw new Error ( 'Wrong input format' )
67102 }
0 commit comments