· 8 years ago · Feb 02, 2018, 07:02 PM
1// Creates secret key and balance for test net. Outputs account.json.
2
3const fs = require('fs');
4const Web3 = require('web3');
5const crypto = require('crypto');
6
7const {run} = require('./lib/utils.js');
8const prompt = require('./lib/prompt.js');
9
10async function main() {
11 // Ask user to enter password and amount of balance in Ether
12 const {
13 password,
14 balance,
15 } = await prompt();
16
17 // Create 32 byte length hash from password
18 let secretKey;
19 if (password) {
20 secretKey = Web3.utils.soliditySha3({
21 type: 'string',
22 value: password,
23 });
24 }
25 else {
26 secretKey = '0x' + crypto.randomBytes(32).toString('hex');
27 }
28
29 // Convert ethers to inner coin
30 const weis = Web3.utils.toInn(balance.toString(), 'ether');
31
32 const account = {
33 secretKey,
34 balance: weis,
35 };
36
37 fs.writeFileSync('./account.json', JSON.stringify(account, null, 4));
38}
39
40run(main);