index.js 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. var __values = (this && this.__values) || function(o) {
  29. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  30. if (m) return m.call(o);
  31. if (o && typeof o.length === "number") return {
  32. next: function () {
  33. if (o && i >= o.length) o = void 0;
  34. return { value: o && o[i++], done: !o };
  35. }
  36. };
  37. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  38. };
  39. Object.defineProperty(exports, "__esModule", { value: true });
  40. exports.ConsensusAlgorithm = exports.ConsensusType = exports.Hardfork = exports.Chain = exports.CustomChain = void 0;
  41. var events_1 = require("events");
  42. var crc_32_1 = require("crc-32");
  43. var ethereumjs_util_1 = require("ethereumjs-util");
  44. var chains_1 = require("./chains");
  45. var hardforks_1 = require("./hardforks");
  46. var eips_1 = require("./eips");
  47. var CustomChain;
  48. (function (CustomChain) {
  49. /**
  50. * Polygon (Matic) Mainnet
  51. *
  52. * - [Documentation](https://docs.matic.network/docs/develop/network-details/network)
  53. */
  54. CustomChain["PolygonMainnet"] = "polygon-mainnet";
  55. /**
  56. * Polygon (Matic) Mumbai Testnet
  57. *
  58. * - [Documentation](https://docs.matic.network/docs/develop/network-details/network)
  59. */
  60. CustomChain["PolygonMumbai"] = "polygon-mumbai";
  61. /**
  62. * Arbitrum Rinkeby Testnet
  63. *
  64. * - [Documentation](https://developer.offchainlabs.com/docs/public_testnet)
  65. */
  66. CustomChain["ArbitrumRinkebyTestnet"] = "arbitrum-rinkeby-testnet";
  67. /**
  68. * xDai EVM sidechain with a native stable token
  69. *
  70. * - [Documentation](https://www.xdaichain.com/)
  71. */
  72. CustomChain["xDaiChain"] = "x-dai-chain";
  73. })(CustomChain = exports.CustomChain || (exports.CustomChain = {}));
  74. var Chain;
  75. (function (Chain) {
  76. Chain[Chain["Mainnet"] = 1] = "Mainnet";
  77. Chain[Chain["Ropsten"] = 3] = "Ropsten";
  78. Chain[Chain["Rinkeby"] = 4] = "Rinkeby";
  79. Chain[Chain["Kovan"] = 42] = "Kovan";
  80. Chain[Chain["Goerli"] = 5] = "Goerli";
  81. })(Chain = exports.Chain || (exports.Chain = {}));
  82. var Hardfork;
  83. (function (Hardfork) {
  84. Hardfork["Chainstart"] = "chainstart";
  85. Hardfork["Homestead"] = "homestead";
  86. Hardfork["Dao"] = "dao";
  87. Hardfork["TangerineWhistle"] = "tangerineWhistle";
  88. Hardfork["SpuriousDragon"] = "spuriousDragon";
  89. Hardfork["Byzantium"] = "byzantium";
  90. Hardfork["Constantinople"] = "constantinople";
  91. Hardfork["Petersburg"] = "petersburg";
  92. Hardfork["Istanbul"] = "istanbul";
  93. Hardfork["MuirGlacier"] = "muirGlacier";
  94. Hardfork["Berlin"] = "berlin";
  95. Hardfork["London"] = "london";
  96. Hardfork["Shanghai"] = "shanghai";
  97. Hardfork["Merge"] = "merge";
  98. })(Hardfork = exports.Hardfork || (exports.Hardfork = {}));
  99. var ConsensusType;
  100. (function (ConsensusType) {
  101. ConsensusType["ProofOfStake"] = "pos";
  102. ConsensusType["ProofOfWork"] = "pow";
  103. ConsensusType["ProofOfAuthority"] = "poa";
  104. })(ConsensusType = exports.ConsensusType || (exports.ConsensusType = {}));
  105. var ConsensusAlgorithm;
  106. (function (ConsensusAlgorithm) {
  107. ConsensusAlgorithm["Ethash"] = "ethash";
  108. ConsensusAlgorithm["Clique"] = "clique";
  109. ConsensusAlgorithm["Casper"] = "casper";
  110. })(ConsensusAlgorithm = exports.ConsensusAlgorithm || (exports.ConsensusAlgorithm = {}));
  111. /**
  112. * Common class to access chain and hardfork parameters and to provide
  113. * a unified and shared view on the network and hardfork state.
  114. *
  115. * Use the {@link Common.custom} static constructor for creating simple
  116. * custom chain {@link Common} objects (more complete custom chain setups
  117. * can be created via the main constructor and the {@link CommonOpts.customChains} parameter).
  118. */
  119. var Common = /** @class */ (function (_super) {
  120. __extends(Common, _super);
  121. /**
  122. *
  123. * @constructor
  124. */
  125. function Common(opts) {
  126. var e_1, _a;
  127. var _b, _c;
  128. var _this = _super.call(this) || this;
  129. _this._supportedHardforks = [];
  130. _this._eips = [];
  131. _this._customChains = (_b = opts.customChains) !== null && _b !== void 0 ? _b : [];
  132. _this._chainParams = _this.setChain(opts.chain);
  133. _this.DEFAULT_HARDFORK = (_c = _this._chainParams.defaultHardfork) !== null && _c !== void 0 ? _c : Hardfork.Istanbul;
  134. try {
  135. for (var _d = __values(_this._chainParams.hardforks), _e = _d.next(); !_e.done; _e = _d.next()) {
  136. var hf = _e.value;
  137. if (!hf.forkHash) {
  138. hf.forkHash = _this._calcForkHash(hf.name);
  139. }
  140. }
  141. }
  142. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  143. finally {
  144. try {
  145. if (_e && !_e.done && (_a = _d.return)) _a.call(_d);
  146. }
  147. finally { if (e_1) throw e_1.error; }
  148. }
  149. _this._hardfork = _this.DEFAULT_HARDFORK;
  150. if (opts.supportedHardforks) {
  151. _this._supportedHardforks = opts.supportedHardforks;
  152. }
  153. if (opts.hardfork) {
  154. _this.setHardfork(opts.hardfork);
  155. }
  156. if (opts.eips) {
  157. _this.setEIPs(opts.eips);
  158. }
  159. return _this;
  160. }
  161. /**
  162. * Creates a {@link Common} object for a custom chain, based on a standard one.
  163. *
  164. * It uses all the {@link Chain} parameters from the {@link baseChain} option except the ones overridden
  165. * in a provided {@link chainParamsOrName} dictionary. Some usage example:
  166. *
  167. * ```javascript
  168. * Common.custom({chainId: 123})
  169. * ```
  170. *
  171. * There are also selected supported custom chains which can be initialized by using one of the
  172. * {@link CustomChains} for {@link chainParamsOrName}, e.g.:
  173. *
  174. * ```javascript
  175. * Common.custom(CustomChains.MaticMumbai)
  176. * ```
  177. *
  178. * Note that these supported custom chains only provide some base parameters (usually the chain and
  179. * network ID and a name) and can only be used for selected use cases (e.g. sending a tx with
  180. * the `@ethereumjs/tx` library to a Layer-2 chain).
  181. *
  182. * @param chainParamsOrName Custom parameter dict (`name` will default to `custom-chain`) or string with name of a supported custom chain
  183. * @param opts Custom chain options to set the {@link CustomCommonOpts.baseChain}, selected {@link CustomCommonOpts.hardfork} and others
  184. */
  185. Common.custom = function (chainParamsOrName, opts) {
  186. var _a;
  187. if (opts === void 0) { opts = {}; }
  188. var baseChain = (_a = opts.baseChain) !== null && _a !== void 0 ? _a : 'mainnet';
  189. var standardChainParams = __assign({}, Common._getChainParams(baseChain));
  190. standardChainParams['name'] = 'custom-chain';
  191. if (typeof chainParamsOrName !== 'string') {
  192. return new Common(__assign({ chain: __assign(__assign({}, standardChainParams), chainParamsOrName) }, opts));
  193. }
  194. else {
  195. if (chainParamsOrName === CustomChain.PolygonMainnet) {
  196. return Common.custom({
  197. name: CustomChain.PolygonMainnet,
  198. chainId: 137,
  199. networkId: 137,
  200. });
  201. }
  202. if (chainParamsOrName === CustomChain.PolygonMumbai) {
  203. return Common.custom({
  204. name: CustomChain.PolygonMumbai,
  205. chainId: 80001,
  206. networkId: 80001,
  207. });
  208. }
  209. if (chainParamsOrName === CustomChain.ArbitrumRinkebyTestnet) {
  210. return Common.custom({
  211. name: CustomChain.ArbitrumRinkebyTestnet,
  212. chainId: 421611,
  213. networkId: 421611,
  214. });
  215. }
  216. if (chainParamsOrName === CustomChain.xDaiChain) {
  217. return Common.custom({
  218. name: CustomChain.xDaiChain,
  219. chainId: 100,
  220. networkId: 100,
  221. });
  222. }
  223. throw new Error("Custom chain " + chainParamsOrName + " not supported");
  224. }
  225. };
  226. /**
  227. * Creates a {@link Common} object for a custom chain, based on a standard one. It uses all the `Chain`
  228. * params from {@link baseChain} except the ones overridden in {@link customChainParams}.
  229. *
  230. * @deprecated Use {@link Common.custom} instead
  231. *
  232. * @param baseChain The name (`mainnet`) or id (`1`) of a standard chain used to base the custom
  233. * chain params on.
  234. * @param customChainParams The custom parameters of the chain.
  235. * @param hardfork String identifier ('byzantium') for hardfork (optional)
  236. * @param supportedHardforks Limit parameter returns to the given hardforks (optional)
  237. */
  238. Common.forCustomChain = function (baseChain, customChainParams, hardfork, supportedHardforks) {
  239. var standardChainParams = Common._getChainParams(baseChain);
  240. return new Common({
  241. chain: __assign(__assign({}, standardChainParams), customChainParams),
  242. hardfork: hardfork,
  243. supportedHardforks: supportedHardforks,
  244. });
  245. };
  246. /**
  247. * Static method to determine if a {@link chainId} is supported as a standard chain
  248. * @param chainId BN id (`1`) of a standard chain
  249. * @returns boolean
  250. */
  251. Common.isSupportedChainId = function (chainId) {
  252. var initializedChains = (0, chains_1._getInitializedChains)();
  253. return Boolean(initializedChains['names'][chainId.toString()]);
  254. };
  255. Common._getChainParams = function (chain, customChains) {
  256. var initializedChains = (0, chains_1._getInitializedChains)(customChains);
  257. if (typeof chain === 'number' || ethereumjs_util_1.BN.isBN(chain)) {
  258. chain = chain.toString();
  259. if (initializedChains['names'][chain]) {
  260. var name_1 = initializedChains['names'][chain];
  261. return initializedChains[name_1];
  262. }
  263. throw new Error("Chain with ID " + chain + " not supported");
  264. }
  265. if (initializedChains[chain]) {
  266. return initializedChains[chain];
  267. }
  268. throw new Error("Chain with name " + chain + " not supported");
  269. };
  270. /**
  271. * Sets the chain
  272. * @param chain String ('mainnet') or Number (1) chain
  273. * representation. Or, a Dictionary of chain parameters for a private network.
  274. * @returns The dictionary with parameters set as chain
  275. */
  276. Common.prototype.setChain = function (chain) {
  277. var e_2, _a;
  278. if (typeof chain === 'number' || typeof chain === 'string' || ethereumjs_util_1.BN.isBN(chain)) {
  279. // Filter out genesis states if passed in to customChains
  280. var plainCustomChains = void 0;
  281. if (this._customChains &&
  282. this._customChains.length > 0 &&
  283. Array.isArray(this._customChains[0])) {
  284. plainCustomChains = this._customChains.map(function (e) { return e[0]; });
  285. }
  286. else {
  287. plainCustomChains = this._customChains;
  288. }
  289. this._chainParams = Common._getChainParams(chain, plainCustomChains);
  290. }
  291. else if (typeof chain === 'object') {
  292. if (this._customChains.length > 0) {
  293. throw new Error('Chain must be a string, number, or BN when initialized with customChains passed in');
  294. }
  295. var required = ['networkId', 'genesis', 'hardforks', 'bootstrapNodes'];
  296. try {
  297. for (var required_1 = __values(required), required_1_1 = required_1.next(); !required_1_1.done; required_1_1 = required_1.next()) {
  298. var param = required_1_1.value;
  299. if (chain[param] === undefined) {
  300. throw new Error("Missing required chain parameter: " + param);
  301. }
  302. }
  303. }
  304. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  305. finally {
  306. try {
  307. if (required_1_1 && !required_1_1.done && (_a = required_1.return)) _a.call(required_1);
  308. }
  309. finally { if (e_2) throw e_2.error; }
  310. }
  311. this._chainParams = chain;
  312. }
  313. else {
  314. throw new Error('Wrong input format');
  315. }
  316. return this._chainParams;
  317. };
  318. /**
  319. * Sets the hardfork to get params for
  320. * @param hardfork String identifier (e.g. 'byzantium') or {@link Hardfork} enum
  321. */
  322. Common.prototype.setHardfork = function (hardfork) {
  323. var e_3, _a;
  324. if (!this._isSupportedHardfork(hardfork)) {
  325. throw new Error("Hardfork " + hardfork + " not set as supported in supportedHardforks");
  326. }
  327. var existing = false;
  328. try {
  329. for (var HARDFORK_CHANGES_1 = __values(hardforks_1.hardforks), HARDFORK_CHANGES_1_1 = HARDFORK_CHANGES_1.next(); !HARDFORK_CHANGES_1_1.done; HARDFORK_CHANGES_1_1 = HARDFORK_CHANGES_1.next()) {
  330. var hfChanges = HARDFORK_CHANGES_1_1.value;
  331. if (hfChanges[0] === hardfork) {
  332. if (this._hardfork !== hardfork) {
  333. this._hardfork = hardfork;
  334. this.emit('hardforkChanged', hardfork);
  335. }
  336. existing = true;
  337. }
  338. }
  339. }
  340. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  341. finally {
  342. try {
  343. if (HARDFORK_CHANGES_1_1 && !HARDFORK_CHANGES_1_1.done && (_a = HARDFORK_CHANGES_1.return)) _a.call(HARDFORK_CHANGES_1);
  344. }
  345. finally { if (e_3) throw e_3.error; }
  346. }
  347. if (!existing) {
  348. throw new Error("Hardfork with name " + hardfork + " not supported");
  349. }
  350. };
  351. /**
  352. * Returns the hardfork based on the block number or an optional
  353. * total difficulty (Merge HF) provided.
  354. *
  355. * An optional TD takes precedence in case the corresponding HF block
  356. * is set to `null` or otherwise needs to match (if not an error
  357. * will be thrown).
  358. *
  359. * @param blockNumber
  360. * @param td
  361. * @returns The name of the HF
  362. */
  363. Common.prototype.getHardforkByBlockNumber = function (blockNumber, td) {
  364. var e_4, _a;
  365. blockNumber = (0, ethereumjs_util_1.toType)(blockNumber, ethereumjs_util_1.TypeOutput.BN);
  366. td = td ? (0, ethereumjs_util_1.toType)(td, ethereumjs_util_1.TypeOutput.BN) : undefined;
  367. var hardfork = Hardfork.Chainstart;
  368. var minTdHF;
  369. var maxTdHF;
  370. var previousHF;
  371. try {
  372. for (var _b = __values(this.hardforks()), _c = _b.next(); !_c.done; _c = _b.next()) {
  373. var hf = _c.value;
  374. // Skip comparison for not applied HFs
  375. if (hf.block === null) {
  376. if (td && hf.td) {
  377. if (td.gten(hf.td)) {
  378. return hf.name;
  379. }
  380. }
  381. continue;
  382. }
  383. if (blockNumber.gte(new ethereumjs_util_1.BN(hf.block))) {
  384. hardfork = hf.name;
  385. }
  386. if (td && hf.td) {
  387. if (td.gten(hf.td)) {
  388. minTdHF = hf.name;
  389. }
  390. else {
  391. maxTdHF = previousHF;
  392. }
  393. }
  394. previousHF = hf.name;
  395. }
  396. }
  397. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  398. finally {
  399. try {
  400. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  401. }
  402. finally { if (e_4) throw e_4.error; }
  403. }
  404. if (td) {
  405. var msgAdd = "block number: " + blockNumber + " (-> " + hardfork + "), ";
  406. if (minTdHF) {
  407. if (!this.hardforkGteHardfork(hardfork, minTdHF)) {
  408. var msg = 'HF determined by block number is lower than the minimum total difficulty HF';
  409. msgAdd += "total difficulty: " + td + " (-> " + minTdHF + ")";
  410. throw new Error(msg + ": " + msgAdd);
  411. }
  412. }
  413. if (maxTdHF) {
  414. if (!this.hardforkGteHardfork(maxTdHF, hardfork)) {
  415. var msg = 'Maximum HF determined by total difficulty is lower than the block number HF';
  416. msgAdd += "total difficulty: " + td + " (-> " + maxTdHF + ")";
  417. throw new Error(msg + ": " + msgAdd);
  418. }
  419. }
  420. }
  421. return hardfork;
  422. };
  423. /**
  424. * Sets a new hardfork based on the block number or an optional
  425. * total difficulty (Merge HF) provided.
  426. *
  427. * An optional TD takes precedence in case the corresponding HF block
  428. * is set to `null` or otherwise needs to match (if not an error
  429. * will be thrown).
  430. *
  431. * @param blockNumber
  432. * @param td
  433. * @returns The name of the HF set
  434. */
  435. Common.prototype.setHardforkByBlockNumber = function (blockNumber, td) {
  436. var hardfork = this.getHardforkByBlockNumber(blockNumber, td);
  437. this.setHardfork(hardfork);
  438. return hardfork;
  439. };
  440. /**
  441. * Internal helper function to choose between hardfork set and hardfork provided as param
  442. * @param hardfork Hardfork given to function as a parameter
  443. * @returns Hardfork chosen to be used
  444. */
  445. Common.prototype._chooseHardfork = function (hardfork, onlySupported) {
  446. if (onlySupported === void 0) { onlySupported = true; }
  447. if (!hardfork) {
  448. hardfork = this._hardfork;
  449. }
  450. else if (onlySupported && !this._isSupportedHardfork(hardfork)) {
  451. throw new Error("Hardfork " + hardfork + " not set as supported in supportedHardforks");
  452. }
  453. return hardfork;
  454. };
  455. /**
  456. * Internal helper function, returns the params for the given hardfork for the chain set
  457. * @param hardfork Hardfork name
  458. * @returns Dictionary with hardfork params
  459. */
  460. Common.prototype._getHardfork = function (hardfork) {
  461. var e_5, _a;
  462. var hfs = this.hardforks();
  463. try {
  464. for (var hfs_1 = __values(hfs), hfs_1_1 = hfs_1.next(); !hfs_1_1.done; hfs_1_1 = hfs_1.next()) {
  465. var hf = hfs_1_1.value;
  466. if (hf['name'] === hardfork)
  467. return hf;
  468. }
  469. }
  470. catch (e_5_1) { e_5 = { error: e_5_1 }; }
  471. finally {
  472. try {
  473. if (hfs_1_1 && !hfs_1_1.done && (_a = hfs_1.return)) _a.call(hfs_1);
  474. }
  475. finally { if (e_5) throw e_5.error; }
  476. }
  477. throw new Error("Hardfork " + hardfork + " not defined for chain " + this.chainName());
  478. };
  479. /**
  480. * Internal helper function to check if a hardfork is set to be supported by the library
  481. * @param hardfork Hardfork name
  482. * @returns True if hardfork is supported
  483. */
  484. Common.prototype._isSupportedHardfork = function (hardfork) {
  485. var e_6, _a;
  486. if (this._supportedHardforks.length > 0) {
  487. try {
  488. for (var _b = __values(this._supportedHardforks), _c = _b.next(); !_c.done; _c = _b.next()) {
  489. var supportedHf = _c.value;
  490. if (hardfork === supportedHf)
  491. return true;
  492. }
  493. }
  494. catch (e_6_1) { e_6 = { error: e_6_1 }; }
  495. finally {
  496. try {
  497. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  498. }
  499. finally { if (e_6) throw e_6.error; }
  500. }
  501. }
  502. else {
  503. return true;
  504. }
  505. return false;
  506. };
  507. /**
  508. * Sets the active EIPs
  509. * @param eips
  510. */
  511. Common.prototype.setEIPs = function (eips) {
  512. var e_7, _a;
  513. var _this = this;
  514. if (eips === void 0) { eips = []; }
  515. var _loop_1 = function (eip) {
  516. if (!(eip in eips_1.EIPs)) {
  517. throw new Error(eip + " not supported");
  518. }
  519. var minHF = this_1.gteHardfork(eips_1.EIPs[eip]['minimumHardfork']);
  520. if (!minHF) {
  521. throw new Error(eip + " cannot be activated on hardfork " + this_1.hardfork() + ", minimumHardfork: " + minHF);
  522. }
  523. if (eips_1.EIPs[eip].requiredEIPs) {
  524. // eslint-disable-next-line prettier/prettier
  525. eips_1.EIPs[eip].requiredEIPs.forEach(function (elem) {
  526. if (!(eips.includes(elem) || _this.isActivatedEIP(elem))) {
  527. throw new Error(eip + " requires EIP " + elem + ", but is not included in the EIP list");
  528. }
  529. });
  530. }
  531. };
  532. var this_1 = this;
  533. try {
  534. for (var eips_2 = __values(eips), eips_2_1 = eips_2.next(); !eips_2_1.done; eips_2_1 = eips_2.next()) {
  535. var eip = eips_2_1.value;
  536. _loop_1(eip);
  537. }
  538. }
  539. catch (e_7_1) { e_7 = { error: e_7_1 }; }
  540. finally {
  541. try {
  542. if (eips_2_1 && !eips_2_1.done && (_a = eips_2.return)) _a.call(eips_2);
  543. }
  544. finally { if (e_7) throw e_7.error; }
  545. }
  546. this._eips = eips;
  547. };
  548. /**
  549. * Returns a parameter for the current chain setup
  550. *
  551. * If the parameter is present in an EIP, the EIP always takes precendence.
  552. * Otherwise the parameter if taken from the latest applied HF with
  553. * a change on the respective parameter.
  554. *
  555. * @param topic Parameter topic ('gasConfig', 'gasPrices', 'vm', 'pow')
  556. * @param name Parameter name (e.g. 'minGasLimit' for 'gasConfig' topic)
  557. * @returns The value requested or `null` if not found
  558. */
  559. Common.prototype.param = function (topic, name) {
  560. var e_8, _a;
  561. // TODO: consider the case that different active EIPs
  562. // can change the same parameter
  563. var value = null;
  564. try {
  565. for (var _b = __values(this._eips), _c = _b.next(); !_c.done; _c = _b.next()) {
  566. var eip = _c.value;
  567. value = this.paramByEIP(topic, name, eip);
  568. if (value !== null) {
  569. return value;
  570. }
  571. }
  572. }
  573. catch (e_8_1) { e_8 = { error: e_8_1 }; }
  574. finally {
  575. try {
  576. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  577. }
  578. finally { if (e_8) throw e_8.error; }
  579. }
  580. return this.paramByHardfork(topic, name, this._hardfork);
  581. };
  582. /**
  583. * Returns the parameter corresponding to a hardfork
  584. * @param topic Parameter topic ('gasConfig', 'gasPrices', 'vm', 'pow')
  585. * @param name Parameter name (e.g. 'minGasLimit' for 'gasConfig' topic)
  586. * @param hardfork Hardfork name
  587. * @returns The value requested or `null` if not found
  588. */
  589. Common.prototype.paramByHardfork = function (topic, name, hardfork) {
  590. var e_9, _a, e_10, _b;
  591. hardfork = this._chooseHardfork(hardfork);
  592. var value = null;
  593. try {
  594. for (var HARDFORK_CHANGES_2 = __values(hardforks_1.hardforks), HARDFORK_CHANGES_2_1 = HARDFORK_CHANGES_2.next(); !HARDFORK_CHANGES_2_1.done; HARDFORK_CHANGES_2_1 = HARDFORK_CHANGES_2.next()) {
  595. var hfChanges = HARDFORK_CHANGES_2_1.value;
  596. // EIP-referencing HF file (e.g. berlin.json)
  597. if ('eips' in hfChanges[1]) {
  598. var hfEIPs = hfChanges[1]['eips'];
  599. try {
  600. for (var hfEIPs_1 = (e_10 = void 0, __values(hfEIPs)), hfEIPs_1_1 = hfEIPs_1.next(); !hfEIPs_1_1.done; hfEIPs_1_1 = hfEIPs_1.next()) {
  601. var eip = hfEIPs_1_1.value;
  602. var valueEIP = this.paramByEIP(topic, name, eip);
  603. value = valueEIP !== null ? valueEIP : value;
  604. }
  605. }
  606. catch (e_10_1) { e_10 = { error: e_10_1 }; }
  607. finally {
  608. try {
  609. if (hfEIPs_1_1 && !hfEIPs_1_1.done && (_b = hfEIPs_1.return)) _b.call(hfEIPs_1);
  610. }
  611. finally { if (e_10) throw e_10.error; }
  612. }
  613. // Paramater-inlining HF file (e.g. istanbul.json)
  614. }
  615. else {
  616. if (!hfChanges[1][topic]) {
  617. throw new Error("Topic " + topic + " not defined");
  618. }
  619. if (hfChanges[1][topic][name] !== undefined) {
  620. value = hfChanges[1][topic][name].v;
  621. }
  622. }
  623. if (hfChanges[0] === hardfork)
  624. break;
  625. }
  626. }
  627. catch (e_9_1) { e_9 = { error: e_9_1 }; }
  628. finally {
  629. try {
  630. if (HARDFORK_CHANGES_2_1 && !HARDFORK_CHANGES_2_1.done && (_a = HARDFORK_CHANGES_2.return)) _a.call(HARDFORK_CHANGES_2);
  631. }
  632. finally { if (e_9) throw e_9.error; }
  633. }
  634. return value;
  635. };
  636. /**
  637. * Returns a parameter corresponding to an EIP
  638. * @param topic Parameter topic ('gasConfig', 'gasPrices', 'vm', 'pow')
  639. * @param name Parameter name (e.g. 'minGasLimit' for 'gasConfig' topic)
  640. * @param eip Number of the EIP
  641. * @returns The value requested or `null` if not found
  642. */
  643. Common.prototype.paramByEIP = function (topic, name, eip) {
  644. if (!(eip in eips_1.EIPs)) {
  645. throw new Error(eip + " not supported");
  646. }
  647. var eipParams = eips_1.EIPs[eip];
  648. if (!(topic in eipParams)) {
  649. throw new Error("Topic " + topic + " not defined");
  650. }
  651. if (eipParams[topic][name] === undefined) {
  652. return null;
  653. }
  654. var value = eipParams[topic][name].v;
  655. return value;
  656. };
  657. /**
  658. * Returns a parameter for the hardfork active on block number
  659. * @param topic Parameter topic
  660. * @param name Parameter name
  661. * @param blockNumber Block number
  662. */
  663. Common.prototype.paramByBlock = function (topic, name, blockNumber) {
  664. var activeHfs = this.activeHardforks(blockNumber);
  665. var hardfork = activeHfs[activeHfs.length - 1]['name'];
  666. return this.paramByHardfork(topic, name, hardfork);
  667. };
  668. /**
  669. * Checks if an EIP is activated by either being included in the EIPs
  670. * manually passed in with the {@link CommonOpts.eips} or in a
  671. * hardfork currently being active
  672. *
  673. * Note: this method only works for EIPs being supported
  674. * by the {@link CommonOpts.eips} constructor option
  675. * @param eip
  676. */
  677. Common.prototype.isActivatedEIP = function (eip) {
  678. var e_11, _a;
  679. if (this.eips().includes(eip)) {
  680. return true;
  681. }
  682. try {
  683. for (var HARDFORK_CHANGES_3 = __values(hardforks_1.hardforks), HARDFORK_CHANGES_3_1 = HARDFORK_CHANGES_3.next(); !HARDFORK_CHANGES_3_1.done; HARDFORK_CHANGES_3_1 = HARDFORK_CHANGES_3.next()) {
  684. var hfChanges = HARDFORK_CHANGES_3_1.value;
  685. var hf = hfChanges[1];
  686. if (this.gteHardfork(hf['name']) && 'eips' in hf) {
  687. if (hf['eips'].includes(eip)) {
  688. return true;
  689. }
  690. }
  691. }
  692. }
  693. catch (e_11_1) { e_11 = { error: e_11_1 }; }
  694. finally {
  695. try {
  696. if (HARDFORK_CHANGES_3_1 && !HARDFORK_CHANGES_3_1.done && (_a = HARDFORK_CHANGES_3.return)) _a.call(HARDFORK_CHANGES_3);
  697. }
  698. finally { if (e_11) throw e_11.error; }
  699. }
  700. return false;
  701. };
  702. /**
  703. * Checks if set or provided hardfork is active on block number
  704. * @param hardfork Hardfork name or null (for HF set)
  705. * @param blockNumber
  706. * @param opts Hardfork options (onlyActive unused)
  707. * @returns True if HF is active on block number
  708. */
  709. Common.prototype.hardforkIsActiveOnBlock = function (hardfork, blockNumber, opts) {
  710. var _a;
  711. if (opts === void 0) { opts = {}; }
  712. blockNumber = (0, ethereumjs_util_1.toType)(blockNumber, ethereumjs_util_1.TypeOutput.BN);
  713. var onlySupported = (_a = opts.onlySupported) !== null && _a !== void 0 ? _a : false;
  714. hardfork = this._chooseHardfork(hardfork, onlySupported);
  715. var hfBlock = this.hardforkBlockBN(hardfork);
  716. if (hfBlock && blockNumber.gte(hfBlock)) {
  717. return true;
  718. }
  719. return false;
  720. };
  721. /**
  722. * Alias to hardforkIsActiveOnBlock when hardfork is set
  723. * @param blockNumber
  724. * @param opts Hardfork options (onlyActive unused)
  725. * @returns True if HF is active on block number
  726. */
  727. Common.prototype.activeOnBlock = function (blockNumber, opts) {
  728. return this.hardforkIsActiveOnBlock(null, blockNumber, opts);
  729. };
  730. /**
  731. * Sequence based check if given or set HF1 is greater than or equal HF2
  732. * @param hardfork1 Hardfork name or null (if set)
  733. * @param hardfork2 Hardfork name
  734. * @param opts Hardfork options
  735. * @returns True if HF1 gte HF2
  736. */
  737. Common.prototype.hardforkGteHardfork = function (hardfork1, hardfork2, opts) {
  738. var e_12, _a;
  739. if (opts === void 0) { opts = {}; }
  740. var onlyActive = opts.onlyActive === undefined ? false : opts.onlyActive;
  741. hardfork1 = this._chooseHardfork(hardfork1, opts.onlySupported);
  742. var hardforks;
  743. if (onlyActive) {
  744. hardforks = this.activeHardforks(null, opts);
  745. }
  746. else {
  747. hardforks = this.hardforks();
  748. }
  749. var posHf1 = -1, posHf2 = -1;
  750. var index = 0;
  751. try {
  752. for (var hardforks_2 = __values(hardforks), hardforks_2_1 = hardforks_2.next(); !hardforks_2_1.done; hardforks_2_1 = hardforks_2.next()) {
  753. var hf = hardforks_2_1.value;
  754. if (hf['name'] === hardfork1)
  755. posHf1 = index;
  756. if (hf['name'] === hardfork2)
  757. posHf2 = index;
  758. index += 1;
  759. }
  760. }
  761. catch (e_12_1) { e_12 = { error: e_12_1 }; }
  762. finally {
  763. try {
  764. if (hardforks_2_1 && !hardforks_2_1.done && (_a = hardforks_2.return)) _a.call(hardforks_2);
  765. }
  766. finally { if (e_12) throw e_12.error; }
  767. }
  768. return posHf1 >= posHf2 && posHf2 !== -1;
  769. };
  770. /**
  771. * Alias to hardforkGteHardfork when hardfork is set
  772. * @param hardfork Hardfork name
  773. * @param opts Hardfork options
  774. * @returns True if hardfork set is greater than hardfork provided
  775. */
  776. Common.prototype.gteHardfork = function (hardfork, opts) {
  777. return this.hardforkGteHardfork(null, hardfork, opts);
  778. };
  779. /**
  780. * Checks if given or set hardfork is active on the chain
  781. * @param hardfork Hardfork name, optional if HF set
  782. * @param opts Hardfork options (onlyActive unused)
  783. * @returns True if hardfork is active on the chain
  784. */
  785. Common.prototype.hardforkIsActiveOnChain = function (hardfork, opts) {
  786. var e_13, _a;
  787. var _b;
  788. if (opts === void 0) { opts = {}; }
  789. var onlySupported = (_b = opts.onlySupported) !== null && _b !== void 0 ? _b : false;
  790. hardfork = this._chooseHardfork(hardfork, onlySupported);
  791. try {
  792. for (var _c = __values(this.hardforks()), _d = _c.next(); !_d.done; _d = _c.next()) {
  793. var hf = _d.value;
  794. if (hf['name'] === hardfork && hf['block'] !== null)
  795. return true;
  796. }
  797. }
  798. catch (e_13_1) { e_13 = { error: e_13_1 }; }
  799. finally {
  800. try {
  801. if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
  802. }
  803. finally { if (e_13) throw e_13.error; }
  804. }
  805. return false;
  806. };
  807. /**
  808. * Returns the active hardfork switches for the current chain
  809. * @param blockNumber up to block if provided, otherwise for the whole chain
  810. * @param opts Hardfork options (onlyActive unused)
  811. * @return Array with hardfork arrays
  812. */
  813. Common.prototype.activeHardforks = function (blockNumber, opts) {
  814. var e_14, _a;
  815. if (opts === void 0) { opts = {}; }
  816. var activeHardforks = [];
  817. var hfs = this.hardforks();
  818. try {
  819. for (var hfs_2 = __values(hfs), hfs_2_1 = hfs_2.next(); !hfs_2_1.done; hfs_2_1 = hfs_2.next()) {
  820. var hf = hfs_2_1.value;
  821. if (hf['block'] === null)
  822. continue;
  823. if (blockNumber !== undefined && blockNumber !== null && blockNumber < hf['block'])
  824. break;
  825. if (opts.onlySupported && !this._isSupportedHardfork(hf['name']))
  826. continue;
  827. activeHardforks.push(hf);
  828. }
  829. }
  830. catch (e_14_1) { e_14 = { error: e_14_1 }; }
  831. finally {
  832. try {
  833. if (hfs_2_1 && !hfs_2_1.done && (_a = hfs_2.return)) _a.call(hfs_2);
  834. }
  835. finally { if (e_14) throw e_14.error; }
  836. }
  837. return activeHardforks;
  838. };
  839. /**
  840. * Returns the latest active hardfork name for chain or block or throws if unavailable
  841. * @param blockNumber up to block if provided, otherwise for the whole chain
  842. * @param opts Hardfork options (onlyActive unused)
  843. * @return Hardfork name
  844. */
  845. Common.prototype.activeHardfork = function (blockNumber, opts) {
  846. if (opts === void 0) { opts = {}; }
  847. var activeHardforks = this.activeHardforks(blockNumber, opts);
  848. if (activeHardforks.length > 0) {
  849. return activeHardforks[activeHardforks.length - 1]['name'];
  850. }
  851. else {
  852. throw new Error("No (supported) active hardfork found");
  853. }
  854. };
  855. /**
  856. * Returns the hardfork change block for hardfork provided or set
  857. * @param hardfork Hardfork name, optional if HF set
  858. * @returns Block number or null if unscheduled
  859. * @deprecated Please use {@link Common.hardforkBlockBN} for large number support
  860. */
  861. Common.prototype.hardforkBlock = function (hardfork) {
  862. var block = this.hardforkBlockBN(hardfork);
  863. return block ? (0, ethereumjs_util_1.toType)(block, ethereumjs_util_1.TypeOutput.Number) : null;
  864. };
  865. /**
  866. * Returns the hardfork change block for hardfork provided or set
  867. * @param hardfork Hardfork name, optional if HF set
  868. * @returns Block number or null if unscheduled
  869. */
  870. Common.prototype.hardforkBlockBN = function (hardfork) {
  871. hardfork = this._chooseHardfork(hardfork, false);
  872. var block = this._getHardfork(hardfork)['block'];
  873. if (block === undefined || block === null) {
  874. return null;
  875. }
  876. return new ethereumjs_util_1.BN(block);
  877. };
  878. /**
  879. * Returns the hardfork change total difficulty (Merge HF) for hardfork provided or set
  880. * @param hardfork Hardfork name, optional if HF set
  881. * @returns Total difficulty or null if no set
  882. */
  883. Common.prototype.hardforkTD = function (hardfork) {
  884. hardfork = this._chooseHardfork(hardfork, false);
  885. var td = this._getHardfork(hardfork)['td'];
  886. if (td === undefined || td === null) {
  887. return null;
  888. }
  889. return new ethereumjs_util_1.BN(td);
  890. };
  891. /**
  892. * True if block number provided is the hardfork (given or set) change block
  893. * @param blockNumber Number of the block to check
  894. * @param hardfork Hardfork name, optional if HF set
  895. * @returns True if blockNumber is HF block
  896. */
  897. Common.prototype.isHardforkBlock = function (blockNumber, hardfork) {
  898. blockNumber = (0, ethereumjs_util_1.toType)(blockNumber, ethereumjs_util_1.TypeOutput.BN);
  899. hardfork = this._chooseHardfork(hardfork, false);
  900. var block = this.hardforkBlockBN(hardfork);
  901. return block ? block.eq(blockNumber) : false;
  902. };
  903. /**
  904. * Returns the change block for the next hardfork after the hardfork provided or set
  905. * @param hardfork Hardfork name, optional if HF set
  906. * @returns Block number or null if not available
  907. * @deprecated Please use {@link Common.nextHardforkBlockBN} for large number support
  908. */
  909. Common.prototype.nextHardforkBlock = function (hardfork) {
  910. var block = this.nextHardforkBlockBN(hardfork);
  911. return block === null ? null : (0, ethereumjs_util_1.toType)(block, ethereumjs_util_1.TypeOutput.Number);
  912. };
  913. /**
  914. * Returns the change block for the next hardfork after the hardfork provided or set
  915. * @param hardfork Hardfork name, optional if HF set
  916. * @returns Block number or null if not available
  917. */
  918. Common.prototype.nextHardforkBlockBN = function (hardfork) {
  919. hardfork = this._chooseHardfork(hardfork, false);
  920. var hfBlock = this.hardforkBlockBN(hardfork);
  921. if (hfBlock === null) {
  922. return null;
  923. }
  924. // Next fork block number or null if none available
  925. // Logic: if accumulator is still null and on the first occurrence of
  926. // a block greater than the current hfBlock set the accumulator,
  927. // pass on the accumulator as the final result from this time on
  928. var nextHfBlock = this.hardforks().reduce(function (acc, hf) {
  929. var block = new ethereumjs_util_1.BN(hf.block);
  930. return block.gt(hfBlock) && acc === null ? block : acc;
  931. }, null);
  932. return nextHfBlock;
  933. };
  934. /**
  935. * True if block number provided is the hardfork change block following the hardfork given or set
  936. * @param blockNumber Number of the block to check
  937. * @param hardfork Hardfork name, optional if HF set
  938. * @returns True if blockNumber is HF block
  939. */
  940. Common.prototype.isNextHardforkBlock = function (blockNumber, hardfork) {
  941. blockNumber = (0, ethereumjs_util_1.toType)(blockNumber, ethereumjs_util_1.TypeOutput.BN);
  942. hardfork = this._chooseHardfork(hardfork, false);
  943. var nextHardforkBlock = this.nextHardforkBlockBN(hardfork);
  944. return nextHardforkBlock === null ? false : nextHardforkBlock.eq(blockNumber);
  945. };
  946. /**
  947. * Internal helper function to calculate a fork hash
  948. * @param hardfork Hardfork name
  949. * @returns Fork hash as hex string
  950. */
  951. Common.prototype._calcForkHash = function (hardfork) {
  952. var e_15, _a;
  953. var genesis = Buffer.from(this.genesis().hash.substr(2), 'hex');
  954. var hfBuffer = Buffer.alloc(0);
  955. var prevBlock = 0;
  956. try {
  957. for (var _b = __values(this.hardforks()), _c = _b.next(); !_c.done; _c = _b.next()) {
  958. var hf = _c.value;
  959. var block = hf.block;
  960. // Skip for chainstart (0), not applied HFs (null) and
  961. // when already applied on same block number HFs
  962. if (block !== 0 && block !== null && block !== prevBlock) {
  963. var hfBlockBuffer = Buffer.from(block.toString(16).padStart(16, '0'), 'hex');
  964. hfBuffer = Buffer.concat([hfBuffer, hfBlockBuffer]);
  965. }
  966. if (hf.name === hardfork)
  967. break;
  968. if (block !== null) {
  969. prevBlock = block;
  970. }
  971. }
  972. }
  973. catch (e_15_1) { e_15 = { error: e_15_1 }; }
  974. finally {
  975. try {
  976. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  977. }
  978. finally { if (e_15) throw e_15.error; }
  979. }
  980. var inputBuffer = Buffer.concat([genesis, hfBuffer]);
  981. // CRC32 delivers result as signed (negative) 32-bit integer,
  982. // convert to hex string
  983. var forkhash = (0, ethereumjs_util_1.intToBuffer)((0, crc_32_1.buf)(inputBuffer) >>> 0).toString('hex');
  984. return "0x" + forkhash;
  985. };
  986. /**
  987. * Returns an eth/64 compliant fork hash (EIP-2124)
  988. * @param hardfork Hardfork name, optional if HF set
  989. */
  990. Common.prototype.forkHash = function (hardfork) {
  991. hardfork = this._chooseHardfork(hardfork, false);
  992. var data = this._getHardfork(hardfork);
  993. if (data['block'] === null) {
  994. var msg = 'No fork hash calculation possible for non-applied or future hardfork';
  995. throw new Error(msg);
  996. }
  997. if (data['forkHash'] !== undefined) {
  998. return data['forkHash'];
  999. }
  1000. return this._calcForkHash(hardfork);
  1001. };
  1002. /**
  1003. *
  1004. * @param forkHash Fork hash as a hex string
  1005. * @returns Array with hardfork data (name, block, forkHash)
  1006. */
  1007. Common.prototype.hardforkForForkHash = function (forkHash) {
  1008. var resArray = this.hardforks().filter(function (hf) {
  1009. return hf.forkHash === forkHash;
  1010. });
  1011. return resArray.length >= 1 ? resArray[resArray.length - 1] : null;
  1012. };
  1013. /**
  1014. * Returns the Genesis parameters of the current chain
  1015. * @returns Genesis dictionary
  1016. */
  1017. Common.prototype.genesis = function () {
  1018. return this._chainParams['genesis'];
  1019. };
  1020. /**
  1021. * Returns the Genesis state of the current chain,
  1022. * both account addresses and values are provided
  1023. * as hex-prefixed strings
  1024. *
  1025. * @returns {Array} Genesis state
  1026. */
  1027. Common.prototype.genesisState = function () {
  1028. var e_16, _a;
  1029. // Use require statements here in favor of import statements
  1030. // to load json files on demand
  1031. // (high memory usage by large mainnet.json genesis state file)
  1032. switch (this.chainName()) {
  1033. case 'mainnet':
  1034. return require('./genesisStates/mainnet.json');
  1035. case 'ropsten':
  1036. return require('./genesisStates/ropsten.json');
  1037. case 'rinkeby':
  1038. return require('./genesisStates/rinkeby.json');
  1039. case 'kovan':
  1040. return require('./genesisStates/kovan.json');
  1041. case 'goerli':
  1042. return require('./genesisStates/goerli.json');
  1043. }
  1044. // Custom chains with genesis state provided
  1045. if (this._customChains &&
  1046. this._customChains.length > 0 &&
  1047. Array.isArray(this._customChains[0])) {
  1048. try {
  1049. for (var _b = __values(this._customChains), _c = _b.next(); !_c.done; _c = _b.next()) {
  1050. var chainArrayWithGenesis = _c.value;
  1051. if (chainArrayWithGenesis[0].name === this.chainName()) {
  1052. return chainArrayWithGenesis[1];
  1053. }
  1054. }
  1055. }
  1056. catch (e_16_1) { e_16 = { error: e_16_1 }; }
  1057. finally {
  1058. try {
  1059. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  1060. }
  1061. finally { if (e_16) throw e_16.error; }
  1062. }
  1063. }
  1064. return {};
  1065. };
  1066. /**
  1067. * Returns the hardforks for current chain
  1068. * @returns {Array} Array with arrays of hardforks
  1069. */
  1070. Common.prototype.hardforks = function () {
  1071. return this._chainParams['hardforks'];
  1072. };
  1073. /**
  1074. * Returns bootstrap nodes for the current chain
  1075. * @returns {Dictionary} Dict with bootstrap nodes
  1076. */
  1077. Common.prototype.bootstrapNodes = function () {
  1078. return this._chainParams['bootstrapNodes'];
  1079. };
  1080. /**
  1081. * Returns DNS networks for the current chain
  1082. * @returns {String[]} Array of DNS ENR urls
  1083. */
  1084. Common.prototype.dnsNetworks = function () {
  1085. return this._chainParams['dnsNetworks'];
  1086. };
  1087. /**
  1088. * Returns the hardfork set
  1089. * @returns Hardfork name
  1090. */
  1091. Common.prototype.hardfork = function () {
  1092. return this._hardfork;
  1093. };
  1094. /**
  1095. * Returns the Id of current chain
  1096. * @returns chain Id
  1097. * @deprecated Please use {@link Common.chainIdBN} for large number support
  1098. */
  1099. Common.prototype.chainId = function () {
  1100. return (0, ethereumjs_util_1.toType)(this.chainIdBN(), ethereumjs_util_1.TypeOutput.Number);
  1101. };
  1102. /**
  1103. * Returns the Id of current chain
  1104. * @returns chain Id
  1105. */
  1106. Common.prototype.chainIdBN = function () {
  1107. return new ethereumjs_util_1.BN(this._chainParams['chainId']);
  1108. };
  1109. /**
  1110. * Returns the name of current chain
  1111. * @returns chain name (lower case)
  1112. */
  1113. Common.prototype.chainName = function () {
  1114. return this._chainParams['name'];
  1115. };
  1116. /**
  1117. * Returns the Id of current network
  1118. * @returns network Id
  1119. * @deprecated Please use {@link Common.networkIdBN} for large number support
  1120. */
  1121. Common.prototype.networkId = function () {
  1122. return (0, ethereumjs_util_1.toType)(this.networkIdBN(), ethereumjs_util_1.TypeOutput.Number);
  1123. };
  1124. /**
  1125. * Returns the Id of current network
  1126. * @returns network Id
  1127. */
  1128. Common.prototype.networkIdBN = function () {
  1129. return new ethereumjs_util_1.BN(this._chainParams['networkId']);
  1130. };
  1131. /**
  1132. * Returns the active EIPs
  1133. * @returns List of EIPs
  1134. */
  1135. Common.prototype.eips = function () {
  1136. return this._eips;
  1137. };
  1138. /**
  1139. * Returns the consensus type of the network
  1140. * Possible values: "pow"|"poa"|"pos"
  1141. *
  1142. * Note: This value can update along a hardfork.
  1143. */
  1144. Common.prototype.consensusType = function () {
  1145. var e_17, _a;
  1146. var hardfork = this.hardfork();
  1147. var value;
  1148. try {
  1149. for (var HARDFORK_CHANGES_4 = __values(hardforks_1.hardforks), HARDFORK_CHANGES_4_1 = HARDFORK_CHANGES_4.next(); !HARDFORK_CHANGES_4_1.done; HARDFORK_CHANGES_4_1 = HARDFORK_CHANGES_4.next()) {
  1150. var hfChanges = HARDFORK_CHANGES_4_1.value;
  1151. if ('consensus' in hfChanges[1]) {
  1152. value = hfChanges[1]['consensus']['type'];
  1153. }
  1154. if (hfChanges[0] === hardfork)
  1155. break;
  1156. }
  1157. }
  1158. catch (e_17_1) { e_17 = { error: e_17_1 }; }
  1159. finally {
  1160. try {
  1161. if (HARDFORK_CHANGES_4_1 && !HARDFORK_CHANGES_4_1.done && (_a = HARDFORK_CHANGES_4.return)) _a.call(HARDFORK_CHANGES_4);
  1162. }
  1163. finally { if (e_17) throw e_17.error; }
  1164. }
  1165. if (value) {
  1166. return value;
  1167. }
  1168. return this._chainParams['consensus']['type'];
  1169. };
  1170. /**
  1171. * Returns the concrete consensus implementation
  1172. * algorithm or protocol for the network
  1173. * e.g. "ethash" for "pow" consensus type,
  1174. * "clique" for "poa" consensus type or
  1175. * "casper" for "pos" consensus type.
  1176. *
  1177. * Note: This value can update along a hardfork.
  1178. */
  1179. Common.prototype.consensusAlgorithm = function () {
  1180. var e_18, _a;
  1181. var hardfork = this.hardfork();
  1182. var value;
  1183. try {
  1184. for (var HARDFORK_CHANGES_5 = __values(hardforks_1.hardforks), HARDFORK_CHANGES_5_1 = HARDFORK_CHANGES_5.next(); !HARDFORK_CHANGES_5_1.done; HARDFORK_CHANGES_5_1 = HARDFORK_CHANGES_5.next()) {
  1185. var hfChanges = HARDFORK_CHANGES_5_1.value;
  1186. if ('consensus' in hfChanges[1]) {
  1187. value = hfChanges[1]['consensus']['algorithm'];
  1188. }
  1189. if (hfChanges[0] === hardfork)
  1190. break;
  1191. }
  1192. }
  1193. catch (e_18_1) { e_18 = { error: e_18_1 }; }
  1194. finally {
  1195. try {
  1196. if (HARDFORK_CHANGES_5_1 && !HARDFORK_CHANGES_5_1.done && (_a = HARDFORK_CHANGES_5.return)) _a.call(HARDFORK_CHANGES_5);
  1197. }
  1198. finally { if (e_18) throw e_18.error; }
  1199. }
  1200. if (value) {
  1201. return value;
  1202. }
  1203. return this._chainParams['consensus']['algorithm'];
  1204. };
  1205. /**
  1206. * Returns a dictionary with consensus configuration
  1207. * parameters based on the consensus algorithm
  1208. *
  1209. * Expected returns (parameters must be present in
  1210. * the respective chain json files):
  1211. *
  1212. * ethash: -
  1213. * clique: period, epoch
  1214. * aura: -
  1215. * casper: -
  1216. *
  1217. * Note: This value can update along a hardfork.
  1218. */
  1219. Common.prototype.consensusConfig = function () {
  1220. var e_19, _a;
  1221. var hardfork = this.hardfork();
  1222. var value;
  1223. try {
  1224. for (var HARDFORK_CHANGES_6 = __values(hardforks_1.hardforks), HARDFORK_CHANGES_6_1 = HARDFORK_CHANGES_6.next(); !HARDFORK_CHANGES_6_1.done; HARDFORK_CHANGES_6_1 = HARDFORK_CHANGES_6.next()) {
  1225. var hfChanges = HARDFORK_CHANGES_6_1.value;
  1226. if ('consensus' in hfChanges[1]) {
  1227. // The config parameter is named after the respective consensus algorithm
  1228. value = hfChanges[1]['consensus'][hfChanges[1]['consensus']['algorithm']];
  1229. }
  1230. if (hfChanges[0] === hardfork)
  1231. break;
  1232. }
  1233. }
  1234. catch (e_19_1) { e_19 = { error: e_19_1 }; }
  1235. finally {
  1236. try {
  1237. if (HARDFORK_CHANGES_6_1 && !HARDFORK_CHANGES_6_1.done && (_a = HARDFORK_CHANGES_6.return)) _a.call(HARDFORK_CHANGES_6);
  1238. }
  1239. finally { if (e_19) throw e_19.error; }
  1240. }
  1241. if (value) {
  1242. return value;
  1243. }
  1244. var consensusAlgorithm = this.consensusAlgorithm();
  1245. return this._chainParams['consensus'][consensusAlgorithm];
  1246. };
  1247. /**
  1248. * Returns a deep copy of this {@link Common} instance.
  1249. */
  1250. Common.prototype.copy = function () {
  1251. return Object.assign(Object.create(Object.getPrototypeOf(this)), this);
  1252. };
  1253. return Common;
  1254. }(events_1.EventEmitter));
  1255. exports.default = Common;
  1256. //# sourceMappingURL=index.js.map