if there is a password field in privateKeyFile object, decode keystore

This commit is contained in:
Jonathan Rainville 2018-08-24 15:30:44 -04:00
parent 864b41a2c2
commit 9d4a3fd228
1 changed files with 20 additions and 1 deletions

View File

@ -1,5 +1,6 @@
const bip39 = require("bip39");
const hdkey = require('ethereumjs-wallet/hdkey');
const ethereumjsWallet = require('ethereumjs-wallet');
const fs = require('../core/fs');
const {getHexBalanceFromString} = require('../utils/utils');
@ -29,7 +30,6 @@ class AccountParser {
let hexBalance = null;
if (accountConfig.balance) {
hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
//hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
}
if (accountConfig.privateKey === 'random') {
@ -47,8 +47,26 @@ class AccountParser {
}
return Object.assign(web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey), {hexBalance});
}
if (accountConfig.privateKeyFile) {
let fileContent = fs.readFileSync(fs.dappPath(accountConfig.privateKeyFile)).toString();
if (accountConfig.password) {
try {
fileContent = JSON.parse(fileContent);
if (!ethereumjsWallet['fromV' + fileContent.version]) {
logger.error(`Key file ${accountConfig.privateKeyFile} is not a valid keystore file`);
return null;
}
const wallet = ethereumjsWallet['fromV' + fileContent.version](fileContent, accountConfig.password);
return Object.assign(web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex')), {hexBalance});
} catch (e) {
logger.error('Private key file is not a keystore JSON file but a password was provided');
logger.error(e.message || e);
return null;
}
}
fileContent = fileContent.trim().split(/[,;]/);
return fileContent.map((key, index) => {
if (!key.startsWith('0x')) {
@ -61,6 +79,7 @@ class AccountParser {
return Object.assign(web3.eth.accounts.privateKeyToAccount(key), {hexBalance});
});
}
if (accountConfig.mnemonic) {
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(accountConfig.mnemonic.trim()));