-
Notifications
You must be signed in to change notification settings - Fork 22
/
e2eCeremonyTest.js
182 lines (141 loc) · 5.73 KB
/
e2eCeremonyTest.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const fs = require('fs');
const utils = require("./utils/utils");
const Constants = require("./utils/constants");
const constants = Constants.constants;
const dir = require('node-dir');
const path = require('path');
const Web3 = require('web3');
const EthereumTx = require('ethereumjs-tx');
const webdriver = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome');
require("chromedriver");
const metaMaskWallet = require('./MetaMaskWallet.js');
const MetaMaskWallet = metaMaskWallet.MetaMaskWallet;
const meta = require('./pages/MetaMask.js');
const buttonSubmit = require('./pages/MetaMask.js');
const ceremony = require('./pages/Ceremony.js');
const timeout = ms => new Promise(res => setTimeout(res, ms))
const ceremonyURL = 'http://localhost:3000'
let args = process.argv.slice(2);
let validator_num = args[0];
let files = dir.files(constants.initialKeysFolder, {sync:true});
files = files.filter((file) => {
let isNotGitKeep = path.basename(file) !== path.basename(`${constants.initialKeysFolder}.gitkeep`);
let isNotDsStore = path.basename(file) !== path.basename(`${constants.initialKeysFolder}.DS_Store`);
return isNotGitKeep && isNotDsStore;
})
let initialKeyPath = files[0];
let initialKeyContent = fs.readFileSync(initialKeyPath, "utf8");
let initialKey
let initialKeyPrivateKeyHex
try {
let initialKeyObj = JSON.parse(initialKeyContent);
initialKey = initialKeyObj.address;
initialKeyPrivateKeyHex = initialKeyObj.privateKey;
} catch(e) {
console.log(e.message);
}
main()
async function main() {
let options = new chrome.Options();
options.addExtensions('./MetaMask_v3.14.1.crx');
options.addArguments('start-maximized');
options.addArguments('disable-popup-blocking');
let driver = new webdriver.Builder()
.withCapabilities(options.toCapabilities())
.build();
let wallet = MetaMaskWallet.createMetaMaskWallet(initialKeyPath);
let metaMask = new meta.MetaMask(driver, wallet);
let ceremonyPage = await new ceremony.Ceremony(driver,ceremonyURL);
metaMask.open();
metaMask.activate();
metaMask.switchToAnotherPage();
ceremonyPage.open();
driver.sleep(4000);
ceremonyPage.clickButtonGenerateKeys();
driver.sleep(2000);
metaMask.switchToAnotherPage();
driver.sleep(3000);
metaMask.refresh();
driver.sleep(2000);
let el = await metaMask.isElementPresent(buttonSubmit.buttonSubmit)
if (el) {
confirmTx(el)
} else {
console.log("Something went wrong. Let's try once more...")
driver.sleep(2000);
let el = await metaMask.isElementPresent(buttonSubmit.buttonSubmit)
confirmTx(el)
}
async function confirmTx(el) {
metaMask.submitTransaction();
ceremonyPage.switchToAnotherPage();
driver.sleep(7000);
let votingKey = await getKeys(ceremonyPage);
console.log("Productions keys are saved")
await transferETHToVotingKey(initialKey, votingKey);
console.log("ETH was transfered from initial key to voting key");
driver.sleep(5000);
let handles = await driver.getAllWindowHandles();
for (let i = 0; i < handles.length; i++) {
driver.switchTo().window(handles[i]);
driver.close();
}
}
}
async function getKeys(ceremonyPage) {
console.log("Productions keys saving...")
let miningKey = await ceremonyPage.getMiningKey();
let payoutKey = await ceremonyPage.getPayoutKey();
let votingKey = await ceremonyPage.getVotingKey();
fs.writeFileSync(`${constants.miningKeysFolder}/${miningKey.address}.key`, JSON.stringify(miningKey));
fs.writeFileSync(`${constants.payoutKeysFolder}/${payoutKey.address}.key`, JSON.stringify(payoutKey));
fs.writeFileSync(`${constants.votingKeysFolder}/${votingKey.address}.key`, JSON.stringify(votingKey));
let validator_path = `${constants.nodeFolder}/parity_validator_${validator_num}`;
if (!fs.existsSync(validator_path)) {
fs.mkdirSync(validator_path);
}
if (!fs.existsSync(`${validator_path}/keys`)) {
fs.mkdirSync(`${validator_path}/keys`);
}
if (!fs.existsSync(`${validator_path}/keys/Sokol`)) {
fs.mkdirSync(`${validator_path}/keys/Sokol`);
}
fs.writeFileSync(`${validator_path}/keys/Sokol/${miningKey.address}`, JSON.stringify(miningKey.keyObject));
fs.writeFileSync(`${validator_path}/node.pwd`, miningKey.password);
fs.unlinkSync(initialKeyPath);
await fs.readdir(constants.scriptsMocOutputFolder, (err, files) => {
if (err) throw err;
for (const file of files) {
if (file != '.gitkeep' && file != '.DS_Store') {
fs.unlinkSync(path.join(constants.scriptsMocOutputFolder, file));
}
}
});
return votingKey.address;
}
async function transferETHToVotingKey(initialKey, votingKey) {
console.log("initialKey:", initialKey);
console.log("votingKey:", votingKey);
let web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
var nonce = await web3.eth.getTransactionCount(initialKey);
var nonceHex = web3.utils.toHex(nonce);
var BN = web3.utils.BN;
var ethToSend = web3.utils.toWei(new BN(90), "milliether");
const rawTx = {
nonce: nonceHex,
gasPrice: 21000,
gasLimit: 100000,
to: votingKey,
value: ethToSend,
data: '0x0',
chainId: web3.utils.toHex(web3.version.network)
};
const initialKeyPrivateKeyBuf = Buffer.from(initialKeyPrivateKeyHex, 'hex')
var tx = new EthereumTx(rawTx);
tx.sign(initialKeyPrivateKeyBuf);
var serializedTx = tx.serialize();
let receipt;
try { receipt = await web3.eth.sendSignedTransaction("0x" + serializedTx.toString('hex')) }
catch (err) { return console.log(err.message); }
}