@@ -6,67 +6,53 @@ import { dirSync } from "tmp";
66
77import { CryptoConfigPaths , Wallet } from "../contracts" ;
88import { Generator } from "./generator" ;
9+ import { Types } from "@arkecosystem/core-kernel" ;
910
1011export class CryptoGenerator extends Generator {
12+ /**
13+ * @private
14+ * @type {string }
15+ * @memberof CoreGenerator
16+ */
17+ private destination ! : string ;
18+
19+ /**
20+ * @returns {CoreConfigPaths }
21+ * @memberof CoreGenerator
22+ */
1123 public generate ( ) : CryptoConfigPaths {
12- const cryptoConfigDest : string = resolve ( __dirname , `${ dirSync ( ) . name } /${ this . options . crypto . network } ` ) ;
24+ this . destination = resolve ( __dirname , `${ dirSync ( ) . name } /${ this . options . crypto . flags . network } ` ) ;
1325
14- if ( existsSync ( cryptoConfigDest ) ) {
15- throw new Error ( `${ cryptoConfigDest } already exists.` ) ;
26+ if ( existsSync ( this . destination ) ) {
27+ throw new Error ( `${ this . destination } already exists.` ) ;
1628 }
1729
18- ensureDirSync ( cryptoConfigDest ) ;
30+ ensureDirSync ( this . destination ) ;
1931
20- const delegates : any [ ] = this . generateCoreDelegates (
21- this . options . crypto . delegates ,
22- this . options . crypto . pubKeyHash ,
23- ) ;
24-
25- const genesisBlock = this . generateGenesisBlock (
26- this . createWallet ( this . options . crypto . pubKeyHash ) ,
27- delegates ,
28- this . options . crypto . pubKeyHash ,
29- this . options . crypto . premine ,
30- this . options . crypto . distribute ,
31- ) ;
32+ const genesisBlock =
33+ this . options . crypto . genesisBlock ??
34+ this . generateGenesisBlock (
35+ this . createWallet ( this . options . crypto . flags . pubKeyHash ) ,
36+ this . generateCoreDelegates ( this . options . crypto . flags . delegates , this . options . crypto . flags . pubKeyHash ) ,
37+ this . options . crypto . flags . pubKeyHash ,
38+ this . options . crypto . flags . premine ,
39+ this . options . crypto . flags . distribute ,
40+ ) ;
3241
33- writeJSONSync (
34- resolve ( cryptoConfigDest , "network.json" ) ,
35- this . generateNetwork (
36- this . options . crypto . network ,
37- this . options . crypto . pubKeyHash ,
38- genesisBlock . payloadHash ,
39- this . options . crypto . wif ,
40- this . options . crypto . token ,
41- this . options . crypto . symbol ,
42- this . options . crypto . explorer ,
43- ) ,
44- { spaces : 4 } ,
45- ) ;
42+ this . writeExceptions ( ) ;
4643
47- writeJSONSync (
48- resolve ( cryptoConfigDest , "milestones.json" ) ,
49- this . generateMilestones (
50- this . options . crypto . delegates ,
51- this . options . crypto . blocktime ,
52- this . options . crypto . maxTxPerBlock ,
53- this . options . crypto . maxBlockPayload ,
54- this . options . crypto . rewardHeight ,
55- this . options . crypto . rewardAmount ,
56- ) ,
57- { spaces : 4 } ,
58- ) ;
44+ this . writeGenesisBlock ( genesisBlock ) ;
5945
60- writeJSONSync ( resolve ( cryptoConfigDest , " genesisBlock.json" ) , genesisBlock , { spaces : 4 } ) ;
46+ this . writeMilestones ( genesisBlock ) ;
6147
62- writeJSONSync ( resolve ( cryptoConfigDest , "exceptions.json" ) , { } ) ;
48+ this . writeNetwork ( genesisBlock . payloadHash ) ;
6349
6450 return {
65- root : cryptoConfigDest ,
66- exceptions : resolve ( cryptoConfigDest , "exceptions.json" ) ,
67- genesisBlock : resolve ( cryptoConfigDest , "genesisBlock.json" ) ,
68- milestones : resolve ( cryptoConfigDest , "milestones.json" ) ,
69- network : resolve ( cryptoConfigDest , "network.json" ) ,
51+ root : this . destination ,
52+ exceptions : resolve ( this . destination , "exceptions.json" ) ,
53+ genesisBlock : resolve ( this . destination , "genesisBlock.json" ) ,
54+ milestones : resolve ( this . destination , "milestones.json" ) ,
55+ network : resolve ( this . destination , "network.json" ) ,
7056 } ;
7157 }
7258
@@ -355,4 +341,86 @@ export class CryptoGenerator extends Generator {
355341
356342 return byteBuffer . toBuffer ( ) ;
357343 }
344+
345+ /**
346+ * @private
347+ * @memberof CryptoGenerator
348+ */
349+ private writeExceptions ( ) : void {
350+ const filePath : string = resolve ( this . destination , "exceptions.json" ) ;
351+
352+ if ( this . options . crypto . exceptions ) {
353+ writeJSONSync ( filePath , this . options . crypto . exceptions , { spaces : 4 } ) ;
354+ } else {
355+ writeJSONSync ( resolve ( this . destination , "exceptions.json" ) , { } ) ;
356+ }
357+ }
358+
359+ /**
360+ * @private
361+ * @param {Types.JsonObject } genesisBlock
362+ * @memberof CryptoGenerator
363+ */
364+ private writeGenesisBlock ( genesisBlock : Types . JsonObject ) : void {
365+ const filePath : string = resolve ( this . destination , "genesisBlock.json" ) ;
366+
367+ if ( this . options . crypto . genesisBlock ) {
368+ writeJSONSync ( filePath , this . options . crypto . genesisBlock , { spaces : 4 } ) ;
369+ } else {
370+ writeJSONSync ( filePath , genesisBlock , { spaces : 4 } ) ;
371+ }
372+ }
373+
374+ /**
375+ * @private
376+ * @param {Types.JsonObject } genesisBlock
377+ * @memberof CryptoGenerator
378+ */
379+ private writeMilestones ( genesisBlock : Types . JsonObject ) : void {
380+ const filePath : string = resolve ( this . destination , "milestones.json" ) ;
381+
382+ if ( this . options . crypto . milestones ) {
383+ writeJSONSync ( filePath , this . options . crypto . milestones , { spaces : 4 } ) ;
384+ } else {
385+ writeJSONSync (
386+ resolve ( this . destination , "milestones.json" ) ,
387+ this . generateMilestones (
388+ this . options . crypto . flags . delegates ,
389+ this . options . crypto . flags . blocktime ,
390+ this . options . crypto . flags . maxTxPerBlock ,
391+ this . options . crypto . flags . maxBlockPayload ,
392+ this . options . crypto . flags . rewardHeight ,
393+ this . options . crypto . flags . rewardAmount ,
394+ ) ,
395+ { spaces : 4 } ,
396+ ) ;
397+ }
398+ }
399+
400+ /**
401+ * @private
402+ * @param {string } payloadHash
403+ * @memberof CryptoGenerator
404+ */
405+ private writeNetwork ( payloadHash : string ) : void {
406+ const filePath : string = resolve ( this . destination , "network.json" ) ;
407+
408+ if ( this . options . crypto . network ) {
409+ writeJSONSync ( filePath , this . options . crypto . network , { spaces : 4 } ) ;
410+ } else {
411+ writeJSONSync (
412+ filePath ,
413+ this . generateNetwork (
414+ this . options . crypto . flags . network ,
415+ this . options . crypto . flags . pubKeyHash ,
416+ payloadHash ,
417+ this . options . crypto . flags . wif ,
418+ this . options . crypto . flags . token ,
419+ this . options . crypto . flags . symbol ,
420+ this . options . crypto . flags . explorer ,
421+ ) ,
422+ { spaces : 4 } ,
423+ ) ;
424+ }
425+ }
358426}
0 commit comments