mirror of
https://github.com/embarklabs/embark.git
synced 2025-02-23 10:58:28 +00:00
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 bip39 = require("bip39");
|
||||||
const hdkey = require('ethereumjs-wallet/hdkey');
|
const hdkey = require('ethereumjs-wallet/hdkey');
|
||||||
|
const ethereumjsWallet = require('ethereumjs-wallet');
|
||||||
const fs = require('../core/fs');
|
const fs = require('../core/fs');
|
||||||
const {getHexBalanceFromString} = require('../utils/utils');
|
const {getHexBalanceFromString} = require('../utils/utils');
|
||||||
|
|
||||||
@ -29,7 +30,6 @@ class AccountParser {
|
|||||||
let hexBalance = null;
|
let hexBalance = null;
|
||||||
if (accountConfig.balance) {
|
if (accountConfig.balance) {
|
||||||
hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
|
hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
|
||||||
//hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (accountConfig.privateKey === 'random') {
|
if (accountConfig.privateKey === 'random') {
|
||||||
@ -47,8 +47,26 @@ class AccountParser {
|
|||||||
}
|
}
|
||||||
return Object.assign(web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey), {hexBalance});
|
return Object.assign(web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey), {hexBalance});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (accountConfig.privateKeyFile) {
|
if (accountConfig.privateKeyFile) {
|
||||||
let fileContent = fs.readFileSync(fs.dappPath(accountConfig.privateKeyFile)).toString();
|
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(/[,;]/);
|
fileContent = fileContent.trim().split(/[,;]/);
|
||||||
return fileContent.map((key, index) => {
|
return fileContent.map((key, index) => {
|
||||||
if (!key.startsWith('0x')) {
|
if (!key.startsWith('0x')) {
|
||||||
@ -61,6 +79,7 @@ class AccountParser {
|
|||||||
return Object.assign(web3.eth.accounts.privateKeyToAccount(key), {hexBalance});
|
return Object.assign(web3.eth.accounts.privateKeyToAccount(key), {hexBalance});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (accountConfig.mnemonic) {
|
if (accountConfig.mnemonic) {
|
||||||
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(accountConfig.mnemonic.trim()));
|
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
|
// 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",
|
mnemonic: "12 word mnemonic",
|
||||||
|
@ -14,7 +14,8 @@ module.exports = {
|
|||||||
// Balances are in Wei, but you can specify the unit with its name
|
// 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",
|
mnemonic: "12 word mnemonic",
|
||||||
|
@ -14,7 +14,8 @@ module.exports = {
|
|||||||
// Balances are in Wei, but you can specify the unit with its name
|
// 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",
|
mnemonic: "12 word mnemonic",
|
||||||
|
Loading…
x
Reference in New Issue
Block a user