2018-05-17 17:48:39 +00:00
|
|
|
const bip39 = require("bip39");
|
|
|
|
const hdkey = require('ethereumjs-wallet/hdkey');
|
|
|
|
const fs = require('../core/fs');
|
2018-07-16 16:48:32 +00:00
|
|
|
const {getHexBalanceFromString} = require('../utils/utils');
|
2018-05-17 17:48:39 +00:00
|
|
|
|
|
|
|
class AccountParser {
|
|
|
|
static parseAccountsConfig(accountsConfig, web3, logger) {
|
|
|
|
let accounts = [];
|
|
|
|
if (accountsConfig && accountsConfig.length) {
|
|
|
|
accountsConfig.forEach(accountConfig => {
|
|
|
|
const account = AccountParser.getAccount(accountConfig, web3, logger);
|
|
|
|
if (!account) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Array.isArray(account)) {
|
|
|
|
accounts = accounts.concat(account);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
accounts.push(account);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static getAccount(accountConfig, web3, logger) {
|
|
|
|
if (!logger) {
|
|
|
|
logger = console;
|
|
|
|
}
|
2018-06-07 14:53:20 +00:00
|
|
|
let hexBalance = null;
|
|
|
|
if (accountConfig.balance) {
|
2018-07-16 16:48:32 +00:00
|
|
|
hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
|
|
|
|
//hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
|
2018-06-07 14:53:20 +00:00
|
|
|
}
|
2018-08-03 19:56:19 +00:00
|
|
|
|
|
|
|
if (accountConfig.privateKey === 'random') {
|
|
|
|
let randomAccount = web3.eth.accounts.create();
|
|
|
|
accountConfig.privateKey = randomAccount.privateKey;
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:48:39 +00:00
|
|
|
if (accountConfig.privateKey) {
|
|
|
|
if (!accountConfig.privateKey.startsWith('0x')) {
|
|
|
|
accountConfig.privateKey = '0x' + accountConfig.privateKey;
|
|
|
|
}
|
2018-05-18 17:27:01 +00:00
|
|
|
if (!web3.utils.isHexStrict(accountConfig.privateKey)) {
|
|
|
|
logger.warn(`Private key ending with ${accountConfig.privateKey.substr(accountConfig.privateKey.length - 5)} is not a HEX string`);
|
|
|
|
return null;
|
|
|
|
}
|
2018-06-07 14:53:20 +00:00
|
|
|
return Object.assign(web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey), {hexBalance});
|
2018-05-17 17:48:39 +00:00
|
|
|
}
|
|
|
|
if (accountConfig.privateKeyFile) {
|
|
|
|
let fileContent = fs.readFileSync(fs.dappPath(accountConfig.privateKeyFile)).toString();
|
|
|
|
fileContent = fileContent.trim().split(/[,;]/);
|
2018-05-18 17:27:01 +00:00
|
|
|
return fileContent.map((key, index) => {
|
2018-05-17 17:48:39 +00:00
|
|
|
if (!key.startsWith('0x')) {
|
|
|
|
key = '0x' + key;
|
|
|
|
}
|
2018-05-18 17:27:01 +00:00
|
|
|
if (!web3.utils.isHexStrict(key)) {
|
|
|
|
logger.warn(`Private key is not a HEX string in file ${accountConfig.privateKeyFile} at index ${index}`);
|
|
|
|
return null;
|
|
|
|
}
|
2018-06-07 14:53:20 +00:00
|
|
|
return Object.assign(web3.eth.accounts.privateKeyToAccount(key), {hexBalance});
|
2018-05-17 17:48:39 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (accountConfig.mnemonic) {
|
|
|
|
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(accountConfig.mnemonic.trim()));
|
|
|
|
|
|
|
|
const addressIndex = accountConfig.addressIndex || 0;
|
|
|
|
const numAddresses = accountConfig.numAddresses || 1;
|
|
|
|
const wallet_hdpath = accountConfig.hdpath || "m/44'/60'/0'/0/";
|
|
|
|
|
|
|
|
const accounts = [];
|
|
|
|
for (let i = addressIndex; i < addressIndex + numAddresses; i++) {
|
|
|
|
const wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet();
|
2018-06-07 14:53:20 +00:00
|
|
|
accounts.push(Object.assign(web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex')), {hexBalance}));
|
2018-05-17 17:48:39 +00:00
|
|
|
}
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
logger.warn('Unsupported account configuration: ' + JSON.stringify(accountConfig));
|
|
|
|
logger.warn('Try using one of those: ' +
|
|
|
|
'{ "privateKey": "your-private-key", "privateKeyFile": "path/to/file/containing/key", "mnemonic": "12 word mnemonic" }');
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AccountParser;
|