This repository was archived by the owner on Dec 16, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathgenerateInitialKey.js
More file actions
131 lines (108 loc) · 3.87 KB
/
Copy pathgenerateInitialKey.js
File metadata and controls
131 lines (108 loc) · 3.87 KB
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
var fs = require('fs');
var keythereum = require("keythereum");
var Web3 = require('web3');
var generatePassword = require('password-generator');
generateAddress(generateAddressCallback);
function generateAddress(cb) {
var params = { keyBytes: 32, ivBytes: 16 };
var dk = keythereum.create(params);
keythereum.create(params, function (dk) {
var options = {};
var password = generatePassword(20, false);
keythereum.dump(password, dk.privateKey, dk.salt, dk.iv, options, function (keyObject) {
//keythereum.exportToFile(keyObject);
cb(keyObject, password);
});
});
}
function generateAddressCallback(keyObject, password) {
var initialKey = "0x" + keyObject.address
var filename = "./initialKeysDemo/" + keyObject.address + ".json";
var content = JSON.stringify(keyObject);
fs.writeFileSync(filename, content)
console.log("Initial key " + initialKey + " is generated to " + filename);
console.log("Password for initial key:", password);
attachToContract(initialKey, addInitialKey);
}
function attachToContract(initialKey, cb) {
configureWeb3(function(web3, config, defaultAccount) {
var contractABI = config.Ethereum.contracts.Oracles.abi;
var contractAddress = config.Ethereum[config.environment].contractAddress;
var contractInstance = new web3.eth.Contract(contractABI, contractAddress);
cb(contractInstance, web3, initialKey);
});
}
function getConfig() {
var config = JSON.parse(fs.readFileSync('./config.json', 'utf8'));
return config;
}
function configureWeb3(cb) {
var config = getConfig();
var web3;
if (typeof web3 !== 'undefined') web3 = new Web3(web3.currentProvider);
else web3 = new Web3(new Web3.providers.HttpProvider(config.Ethereum[config.environment].rpc));
if (!web3) return finishScript(err);
web3.eth.net.isListening().then(function(isListening) {
if (!isListening) {
var err = {code: 500, title: "Error", message: "check RPC"};
return finishScript(err);
}
web3.eth.defaultAccount = config.Ethereum[config.environment].account;
cb(web3, config, web3.eth.defaultAccount);
}, function(err) {
return finishScript(err);
});
}
function addInitialKey(contract, web3, initialKey) {
addInitialKeyTX(web3, contract, initialKey, function(err, txHash) {
if (err) return finishScript(err);
console.log("Wait tx " + txHash + " to add initial key to be mined...");
getTxCallBack(web3, txHash, function() {
console.log("Initial key " + initialKey + " is added to contract");
sendEtherToInitialKeyTX(web3, initialKey, finishScript);
});
});
}
function getTxCallBack(web3, txHash, cb) {
web3.eth.getTransaction(txHash, function(err, txDetails) {
if (err) return finishScript(err);
if (!txDetails.blockNumber) {
setTimeout(function() {
getTxCallBack(web3, txHash, cb);
}, 2000)
} else {
cb();
}
});
};
function addInitialKeyTX(web3, contract, initialKey, cb) {
contract.methods.addInitialKey(initialKey).estimateGas()
.then(function(estimatedGas) {
console.log("Estimated gas to add initial key:", estimatedGas)
var opts = {from: web3.eth.defaultAccount, gasLimit: estimatedGas}
contract.methods.addInitialKey(initialKey)
.send(opts, function(err, txHash) {
cb(err, txHash);
});
})
}
function sendEtherToInitialKeyTX(web3, initialKey, cb) {
var BN = web3.utils.BN;
var ethToSend = web3.utils.toWei(new BN(100), "milliether");
console.log("WEI to send to initial key: " + ethToSend)
var opts = {from: web3.eth.defaultAccount, to: initialKey, value: ethToSend};
web3.eth.sendTransaction(opts, function(err, result) {
cb(err, web3, result);
});
}
function finishScript(err, web3, txHash) {
if (err) {
console.log("Something went wrong with generating initial key");
console.log(err.message);
return;
}
console.log("Wait tx to send Eth to initial key to be mined...");
getTxCallBack(web3, txHash, function() {
console.log("0.1 Eth sent to initial key");
});
}