From 9d4a3fd2282a319ad5c33639d0e34e8df1e89a57 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 24 Aug 2018 15:30:44 -0400 Subject: [PATCH] if there is a password field in privateKeyFile object, decode keystore --- lib/utils/accountParser.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/utils/accountParser.js b/lib/utils/accountParser.js index 3ad1dac9..ee329a9c 100644 --- a/lib/utils/accountParser.js +++ b/lib/utils/accountParser.js @@ -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()));