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())); diff --git a/templates/boilerplate/config/contracts.js b/templates/boilerplate/config/contracts.js index 20c9ccf1..68265c7e 100644 --- a/templates/boilerplate/config/contracts.js +++ b/templates/boilerplate/config/contracts.js @@ -14,7 +14,8 @@ module.exports = { // Balances are in Wei, but you can specify the unit with its name }, { - privateKeyFile: "path/to/file" // You can put more than one key, separated by , or ; + privateKeyFile: "path/to/file", // Either a keystore or a list of keys, separated by , or ; + password: "passwordForTheKeystore" // Needed to decrypt the keystore file }, { mnemonic: "12 word mnemonic", diff --git a/templates/demo/config/contracts.js b/templates/demo/config/contracts.js index 2ca24def..37d689f9 100644 --- a/templates/demo/config/contracts.js +++ b/templates/demo/config/contracts.js @@ -14,7 +14,8 @@ module.exports = { // Balances are in Wei, but you can specify the unit with its name }, { - privateKeyFile: "path/to/file" // You can put more than one key, separated by , or ; + privateKeyFile: "path/to/file", // Either a keystore or a list of keys, separated by , or ; + password: "passwordForTheKeystore" // Needed to decrypt the keystore file }, { mnemonic: "12 word mnemonic", diff --git a/templates/simple/contracts.js b/templates/simple/contracts.js index 20c9ccf1..68265c7e 100644 --- a/templates/simple/contracts.js +++ b/templates/simple/contracts.js @@ -14,7 +14,8 @@ module.exports = { // Balances are in Wei, but you can specify the unit with its name }, { - privateKeyFile: "path/to/file" // You can put more than one key, separated by , or ; + privateKeyFile: "path/to/file", // Either a keystore or a list of keys, separated by , or ; + password: "passwordForTheKeystore" // Needed to decrypt the keystore file }, { mnemonic: "12 word mnemonic",