-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
executable file
·89 lines (72 loc) · 2.67 KB
/
index.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
#!/usr/bin/env node
'use strict';
const fs = require('fs');
const path = require('path');
const utils = require('ethereumjs-util');
let template = require('./template.json');
const CONFIG_FILENAME = 'quorum-config.json';
const OUTPUT = 'quorum-genesis.json';
function padIndex(number, prefix) {
if(prefix) {
return utils.addHexPrefix(utils.setLengthLeft([number], 32, false).toString('hex'));
}
return utils.setLengthLeft([number], 32, false).toString('hex');
}
function padAddress(address) {
return "000000000000000000000000" + utils.stripHexPrefix(address);
}
function storageKey(index, address) {
let paddedAddress = padAddress(address);
let paddedIndex = padIndex(index);
let result = utils.sha3(new Buffer(paddedAddress+paddedIndex, 'hex')).toString('hex');
return utils.addHexPrefix(result)
}
function mapAddresses(index, addresses) {
let value = utils.intToHex(1);
for(let i=0; i<addresses.length; i++) {
let key = storageKey(index, addresses[i]);
template['alloc']['0x0000000000000000000000000000000000000020'].storage[key] = value;
}
}
function buildStorage(input) {
template['alloc']['0x0000000000000000000000000000000000000020'].storage[padIndex(1,true)] = utils.intToHex(input.threshold);
template['alloc']['0x0000000000000000000000000000000000000020'].storage[padIndex(2,true)] = utils.intToHex(input.voters.length);
mapAddresses(3, input.voters);
template['alloc']['0x0000000000000000000000000000000000000020'].storage[padIndex(4,true)] = utils.intToHex(input.makers.length);
mapAddresses(5,input.makers);
}
function fundAddresses(input) {
let all = input.voters.concat(input.makers);
for(let i=0; i<all.length; i++) {
template['alloc'][utils.addHexPrefix(all[i])] = { balance: "1000000000000000000000000000"};
}
}
function loadConfig() {
let fn = path.join(process.cwd(),CONFIG_FILENAME);
if(!fs.existsSync(fn)) {
console.log(` > Missing config file '${CONFIG_FILENAME}' in the current directory`);
process.exit(1);
}
let contents = fs.readFileSync(fn);
let json = JSON.parse(contents);
if(!json.threshold || json.threshold < 1) {
console.log(" > Voting threshold missing or less than 1" );
process.exit(1);
}
if(!json.voters || json.voters.length < json.threshold) {
console.log(" > Voter addresses missing or less than the threshold" );
process.exit(1);
}
if(!json.makers || json.makers.length < 1) {
console.log(" > BlockMaker addresses missing or less than 1" );
process.exit(1);
}
return json;
}
function main() {
let input = loadConfig();
buildStorage(input);
fundAddresses(input)
fs.writeFileSync(path.join(process.cwd(),OUTPUT), JSON.stringify(template, null, 2));
}
main();