1
1
import { buf as crc32Buffer } from 'crc-32'
2
- import { chains as chainParams } from './chains'
2
+ import { _getInitializedChains } from './chains'
3
3
import { hardforks as HARDFORK_CHANGES } from './hardforks'
4
4
import { EIPs } from './eips'
5
5
import { Chain } from './types'
@@ -9,7 +9,8 @@ import { Chain } from './types'
9
9
*/
10
10
export interface CommonOpts {
11
11
/**
12
- * String ('mainnet') or Number (1) chain
12
+ * Chain name ('mainnet') or id (1), either from a chain directly supported
13
+ * or a custom chain passed in via `customChains`
13
14
*/
14
15
chain : string | number | object
15
16
/**
@@ -31,6 +32,18 @@ export interface CommonOpts {
31
32
* - [EIP-2537](https://eips.ethereum.org/EIPS/eip-2537) - BLS12-381 precompiles
32
33
*/
33
34
eips ?: number [ ]
35
+ /**
36
+ * Initialize (in addition to the supported chains) with the selected
37
+ * custom chains
38
+ *
39
+ * Usage (directly with the respective chain intialization via the `chain` option):
40
+ *
41
+ * ```javascript
42
+ * import myCustomChain1 from '[PATH_TO_MY_CHAINS]/myCustomChain1.json'
43
+ * const common = new Common({ chain: 'myCustomChain1', customChains: [ myCustomChain1 ]})
44
+ * ```
45
+ */
46
+ customChains ?: Chain [ ]
34
47
}
35
48
36
49
interface hardforkOptions {
@@ -44,12 +57,13 @@ interface hardforkOptions {
44
57
* Common class to access chain and hardfork parameters
45
58
*/
46
59
export default class Common {
47
- readonly DEFAULT_HARDFORK : string = 'istanbul'
60
+ readonly DEFAULT_HARDFORK : string
48
61
49
62
private _chainParams : Chain
50
63
private _hardfork : string
51
64
private _supportedHardforks : Array < string > = [ ]
52
65
private _eips : number [ ] = [ ]
66
+ private _customChains : Chain [ ]
53
67
54
68
/**
55
69
* Creates a Common object for a custom chain, based on a standard one. It uses all the [[Chain]]
@@ -79,17 +93,19 @@ export default class Common {
79
93
} )
80
94
}
81
95
82
- private static _getChainParams ( chain : string | number ) : Chain {
96
+ private static _getChainParams ( chain : string | number , customChains ?: Chain [ ] ) : Chain {
97
+ const initializedChains : any = _getInitializedChains ( customChains )
83
98
if ( typeof chain === 'number' ) {
84
- if ( chainParams [ 'names' ] [ chain ] ) {
85
- return chainParams [ chainParams [ 'names' ] [ chain ] ]
99
+ if ( initializedChains [ 'names' ] [ chain ] ) {
100
+ const name : string = initializedChains [ 'names' ] [ chain ]
101
+ return initializedChains [ name ]
86
102
}
87
103
88
104
throw new Error ( `Chain with ID ${ chain } not supported` )
89
105
}
90
106
91
- if ( chainParams [ chain ] ) {
92
- return chainParams [ chain ]
107
+ if ( initializedChains [ chain ] ) {
108
+ return initializedChains [ chain ]
93
109
}
94
110
95
111
throw new Error ( `Chain with name ${ chain } not supported` )
@@ -99,7 +115,9 @@ export default class Common {
99
115
* @constructor
100
116
*/
101
117
constructor ( opts : CommonOpts ) {
118
+ this . _customChains = opts . customChains ?? [ ]
102
119
this . _chainParams = this . setChain ( opts . chain )
120
+ this . DEFAULT_HARDFORK = this . _chainParams . defaultHardfork ?? 'istanbul'
103
121
this . _hardfork = this . DEFAULT_HARDFORK
104
122
if ( opts . supportedHardforks ) {
105
123
this . _supportedHardforks = opts . supportedHardforks
@@ -120,8 +138,13 @@ export default class Common {
120
138
*/
121
139
setChain ( chain : string | number | object ) : any {
122
140
if ( typeof chain === 'number' || typeof chain === 'string' ) {
123
- this . _chainParams = Common . _getChainParams ( chain )
141
+ this . _chainParams = Common . _getChainParams ( chain , this . _customChains )
124
142
} else if ( typeof chain === 'object' ) {
143
+ if ( this . _customChains . length > 0 ) {
144
+ throw new Error (
145
+ 'Chain must be a string or number when initialized with customChains passed in'
146
+ )
147
+ }
125
148
const required = [ 'networkId' , 'genesis' , 'hardforks' , 'bootstrapNodes' ]
126
149
for ( const param of required ) {
127
150
if ( ( < any > chain ) [ param ] === undefined ) {
@@ -613,7 +636,7 @@ export default class Common {
613
636
* @returns chain name (lower case)
614
637
*/
615
638
chainName ( ) : string {
616
- return chainParams [ 'names' ] [ this . chainId ( ) ] || ( < any > this . _chainParams ) [ 'name' ]
639
+ return ( < any > this . _chainParams ) [ 'name' ]
617
640
}
618
641
619
642
/**
0 commit comments