forked from poanetwork/poa-test-setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepareMoCNode.js
127 lines (100 loc) · 4.48 KB
/
prepareMoCNode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const Constants = require("./utils/constants");
const constants = Constants.constants;
const fs = require('fs');
const keythereum = require("keythereum");
const generatePassword = require('password-generator');
const Web3 = require('web3');
const utils = require("./utils/utils");
const toml = require('toml');
const tomlJS = require('toml-js');
main()
async function main() {
const consensusObj = JSON.parse(fs.readFileSync(constants.pathToConsensusContract));
const consensusBytecode = consensusObj.bytecode;
const mocPassword = generatePassword(20, false)
const keyObject = await generateAddress(mocPassword)
const moc = `0x${keyObject.address}`;
const keyStoreFileName = `${constants.mocKeysFolder}${keyObject.address}.json`;
const privateKeyFileName = `${constants.mocKeysFolder}moc.key`;
const passwordFileName = `${constants.mocKeysFolder}moc.pwd`;
const mocKeyStore = JSON.stringify(keyObject);
const privateKey = keythereum.recover(mocPassword, keyObject).toString('hex');
let spec
try {
spec = await utils.getSpec('sokol');
} catch (e) {
return console.log(e.message)
}
utils.clearFolder(constants.mocKeysFolder);
let POAKeysFolder = `${constants.masterNodeKeysFolder}${spec.name}`;
if (!fs.existsSync(POAKeysFolder)){
fs.mkdirSync(POAKeysFolder);
}
utils.clearFolder(POAKeysFolder);
try { await utils.saveToFile(keyStoreFileName, mocKeyStore) }
catch (err) { return console.log(err.message); }
console.log(`MoC keystore file ${moc} is generated to ${keyStoreFileName}`);
const key = {
address: moc,
privateKey: privateKey
}
try { await utils.saveToFile(privateKeyFileName, JSON.stringify(key, null, 2)) }
catch (err) { return console.log(err.message); }
console.log(`MoC private key are generated to ${privateKeyFileName}`);
try { await utils.saveToFile(passwordFileName, mocPassword) }
catch (err) { return console.log(err.message); }
console.log(`MoC password is generated to ${passwordFileName}`);
utils.clearFolder(constants.masterNodeKeysFolder);
const masterNodeKeyStoreFolder = `${constants.masterNodeKeysFolder}${spec.name}`;
fs.existsSync(masterNodeKeyStoreFolder) || fs.mkdirSync(masterNodeKeyStoreFolder);
const masterNodeParentKeyStorePath = `${masterNodeKeyStoreFolder}/moc_${moc}.json`;
try { await utils.saveToFile(masterNodeParentKeyStorePath, mocKeyStore) }
catch (err) { return console.log(err.message); }
console.log(`Keystore file is copied to ${masterNodeParentKeyStorePath}`);
web3 = new Web3(new Web3.providers.HttpProvider('https://sokol.poa.network'));
const mocABIEncoded = web3.eth.abi.encodeParameters(['address', `address[]`], [moc, []]).substr(2);
const newConsensusBytecode = consensusBytecode + mocABIEncoded;
spec.accounts[moc] = {
balance: '252460800000000000000000000'
};
spec.accounts[constants.poaNetworkConsensusContractAddress] = {
balance: '1',
constructor: newConsensusBytecode
};
spec.engine.authorityRound.params.validators.multi = {
"0": {
"safeContract": constants.poaNetworkConsensusContractAddress
}
};
utils.clearFolder(constants.specFolder);
try { await utils.saveToFile(`${constants.specFolder}spec.json`, JSON.stringify(spec, null, 2)) }
catch (err) { return console.log(err.message); }
console.log(`spec.json is generated to ${constants.specFolder}`);
const masterNodeExampleTomlPath = `${constants.nodeFolder}node-master.toml`;
const masterNodeTomlContent = fs.readFileSync(masterNodeExampleTomlPath, "utf8");
const masterNodeToml = toml.parse(masterNodeTomlContent);
masterNodeToml.account.unlock = [moc];
masterNodeToml.mining.engine_signer = moc;
const newToml = tomlJS.dump(masterNodeToml);
utils.removeFolderRecursive(`${constants.masterNodeFolder}cache`);
utils.removeFolderRecursive(`${constants.masterNodeFolder}chains`);
utils.removeFolderRecursive(`${constants.masterNodeFolder}dapps`);
utils.removeFolderRecursive(`${constants.masterNodeFolder}network`);
const mocTomlPath = `${constants.masterNodeFolder}moc.toml`;
try { await utils.saveToFile(`${mocTomlPath}`, newToml)}
catch (err) { return console.log(err.message); }
console.log("MoC node is prepared");
}
//generates initial key keystore file
function generateAddress(password) {
return new Promise((resolve, reject) => {
let params = { keyBytes: 32, ivBytes: 16 };
let dk = keythereum.create(params);
keythereum.create(params, function (dk) {
let options = {};
keythereum.dump(password, dk.privateKey, dk.salt, dk.iv, options, function (keyObject) {
resolve(keyObject);
});
});
})
}