Merge pull request #747 from embark-framework/features/password-protected-keyfiles
Unlock account using keystore file with password
This commit is contained in:
commit
f0a6b913c4
|
@ -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()));
|
||||
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue