2019-05-07 19:49:22 +00:00
|
|
|
import { __ } from 'embark-i18n';
|
2018-05-17 17:48:39 +00:00
|
|
|
const bip39 = require("bip39");
|
|
|
|
const hdkey = require('ethereumjs-wallet/hdkey');
|
2018-08-24 19:30:44 +00:00
|
|
|
const ethereumjsWallet = require('ethereumjs-wallet');
|
2019-04-30 13:26:59 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
import {getHexBalanceFromString} from './web3Utils';
|
|
|
|
const {utils} = require('web3');
|
2018-05-17 17:48:39 +00:00
|
|
|
|
2018-09-21 18:04:04 +00:00
|
|
|
const path = require('path');
|
|
|
|
|
2018-05-17 17:48:39 +00:00
|
|
|
class AccountParser {
|
2019-04-30 13:26:59 +00:00
|
|
|
static parseAccountsConfig(accountsConfig, web3, dappPath, logger, nodeAccounts) {
|
2018-05-17 17:48:39 +00:00
|
|
|
let accounts = [];
|
|
|
|
if (accountsConfig && accountsConfig.length) {
|
|
|
|
accountsConfig.forEach(accountConfig => {
|
2019-04-30 13:26:59 +00:00
|
|
|
let account = AccountParser.getAccount(accountConfig, web3, dappPath, logger, nodeAccounts);
|
2018-05-17 17:48:39 +00:00
|
|
|
if (!account) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Array.isArray(account)) {
|
|
|
|
accounts = accounts.concat(account);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
accounts.push(account);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
2018-12-17 18:29:06 +00:00
|
|
|
/*eslint complexity: ["error", 30]*/
|
2019-04-30 13:26:59 +00:00
|
|
|
static getAccount(accountConfig, web3, dappPath, logger = console, nodeAccounts) {
|
2018-12-17 18:29:06 +00:00
|
|
|
const returnAddress = web3 === false;
|
2018-06-07 14:53:20 +00:00
|
|
|
let hexBalance = null;
|
2018-12-17 18:29:06 +00:00
|
|
|
if (accountConfig.balance && web3) {
|
2018-07-16 16:48:32 +00:00
|
|
|
hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
|
2018-06-07 14:53:20 +00:00
|
|
|
}
|
2018-08-03 19:56:19 +00:00
|
|
|
|
|
|
|
if (accountConfig.privateKey === 'random') {
|
2018-12-17 18:29:06 +00:00
|
|
|
if (!web3) {
|
|
|
|
logger.warn('Cannot use random in this context');
|
|
|
|
return null;
|
|
|
|
}
|
2018-08-03 19:56:19 +00:00
|
|
|
let randomAccount = web3.eth.accounts.create();
|
|
|
|
accountConfig.privateKey = randomAccount.privateKey;
|
|
|
|
}
|
|
|
|
|
2018-10-04 19:18:00 +00:00
|
|
|
if (accountConfig.nodeAccounts) {
|
2018-12-17 18:29:06 +00:00
|
|
|
if (!nodeAccounts && !returnAddress) {
|
2018-10-04 19:18:00 +00:00
|
|
|
logger.warn('Cannot use nodeAccounts in this context');
|
|
|
|
return null;
|
|
|
|
}
|
2018-12-17 18:29:06 +00:00
|
|
|
if (!nodeAccounts || !nodeAccounts.length) {
|
2018-10-04 19:18:00 +00:00
|
|
|
return null;
|
|
|
|
}
|
2018-11-21 21:57:58 +00:00
|
|
|
|
|
|
|
return nodeAccounts.map(account => {
|
|
|
|
return (typeof account === 'string') ? { address: account } : account;
|
2018-10-04 19:18:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:48:39 +00:00
|
|
|
if (accountConfig.privateKey) {
|
|
|
|
if (!accountConfig.privateKey.startsWith('0x')) {
|
|
|
|
accountConfig.privateKey = '0x' + accountConfig.privateKey;
|
|
|
|
}
|
2018-12-17 18:29:06 +00:00
|
|
|
if (!utils.isHexStrict(accountConfig.privateKey)) {
|
2018-05-18 17:27:01 +00:00
|
|
|
logger.warn(`Private key ending with ${accountConfig.privateKey.substr(accountConfig.privateKey.length - 5)} is not a HEX string`);
|
|
|
|
return null;
|
|
|
|
}
|
2019-02-14 18:57:11 +00:00
|
|
|
|
2018-12-17 18:29:06 +00:00
|
|
|
if (returnAddress) {
|
2019-05-07 21:28:11 +00:00
|
|
|
const key = Buffer.from(accountConfig.privateKey.substr(2), 'hex');
|
2019-02-14 18:57:11 +00:00
|
|
|
return ethereumjsWallet.fromPrivateKey(key).getChecksumAddressString();
|
2018-12-17 18:29:06 +00:00
|
|
|
}
|
2019-05-07 21:28:11 +00:00
|
|
|
return Object.assign(web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey), {hexBalance});
|
2019-02-22 20:31:29 +00:00
|
|
|
} else if (Object.hasOwnProperty('privateKey')) {
|
|
|
|
logger.error(__('accounts error: privateKey field is specified but its value is undefined'));
|
2018-05-17 17:48:39 +00:00
|
|
|
}
|
2018-08-24 19:30:44 +00:00
|
|
|
|
2018-05-17 17:48:39 +00:00
|
|
|
if (accountConfig.privateKeyFile) {
|
2019-04-30 13:26:59 +00:00
|
|
|
let privateKeyFile = path.resolve(dappPath, accountConfig.privateKeyFile);
|
2018-09-21 18:04:04 +00:00
|
|
|
let fileContent = fs.readFileSync(privateKeyFile).toString();
|
2018-08-24 19:30:44 +00:00
|
|
|
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);
|
|
|
|
|
2018-12-17 18:29:06 +00:00
|
|
|
if (returnAddress) {
|
|
|
|
return wallet.getChecksumAddressString();
|
|
|
|
}
|
2018-08-24 19:30:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 17:48:39 +00:00
|
|
|
fileContent = fileContent.trim().split(/[,;]/);
|
2018-05-18 17:27:01 +00:00
|
|
|
return fileContent.map((key, index) => {
|
2018-05-17 17:48:39 +00:00
|
|
|
if (!key.startsWith('0x')) {
|
|
|
|
key = '0x' + key;
|
|
|
|
}
|
2018-12-17 18:29:06 +00:00
|
|
|
if (!utils.isHexStrict(key)) {
|
2018-05-18 17:27:01 +00:00
|
|
|
logger.warn(`Private key is not a HEX string in file ${accountConfig.privateKeyFile} at index ${index}`);
|
|
|
|
return null;
|
|
|
|
}
|
2019-02-14 18:57:11 +00:00
|
|
|
|
2018-12-17 18:29:06 +00:00
|
|
|
if (returnAddress) {
|
2019-05-07 21:28:11 +00:00
|
|
|
key = Buffer.from(key.substr(2), 'hex');
|
2018-12-17 18:29:06 +00:00
|
|
|
return ethereumjsWallet.fromPrivateKey(key).getChecksumAddressString();
|
|
|
|
}
|
2018-06-07 14:53:20 +00:00
|
|
|
return Object.assign(web3.eth.accounts.privateKeyToAccount(key), {hexBalance});
|
2018-05-17 17:48:39 +00:00
|
|
|
});
|
2019-02-22 20:31:29 +00:00
|
|
|
} else if (Object.hasOwnProperty('privateKeyFile')) {
|
|
|
|
logger.error(__('accounts error: privateKeyFile field is specified but its value is undefined'));
|
2018-05-17 17:48:39 +00:00
|
|
|
}
|
2018-08-24 19:30:44 +00:00
|
|
|
|
2018-05-17 17:48:39 +00:00
|
|
|
if (accountConfig.mnemonic) {
|
|
|
|
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(accountConfig.mnemonic.trim()));
|
|
|
|
|
|
|
|
const addressIndex = accountConfig.addressIndex || 0;
|
|
|
|
const numAddresses = accountConfig.numAddresses || 1;
|
|
|
|
const wallet_hdpath = accountConfig.hdpath || "m/44'/60'/0'/0/";
|
|
|
|
|
|
|
|
const accounts = [];
|
|
|
|
for (let i = addressIndex; i < addressIndex + numAddresses; i++) {
|
|
|
|
const wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet();
|
2018-12-17 18:29:06 +00:00
|
|
|
if (returnAddress) {
|
|
|
|
accounts.push(wallet.getAddressString());
|
|
|
|
} else {
|
|
|
|
accounts.push(Object.assign(web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex')), {hexBalance}));
|
|
|
|
}
|
2018-05-17 17:48:39 +00:00
|
|
|
}
|
|
|
|
return accounts;
|
2019-02-22 20:31:29 +00:00
|
|
|
} else if (Object.hasOwnProperty('mnemonic')) {
|
|
|
|
logger.error(__('accounts error: mnemonic field is specified but its value is undefined'));
|
2018-05-17 17:48:39 +00:00
|
|
|
}
|
2019-02-22 20:31:29 +00:00
|
|
|
|
2018-12-04 15:54:05 +00:00
|
|
|
if (accountConfig.secretKey) {
|
|
|
|
// Ignore simulator configs
|
|
|
|
return null;
|
|
|
|
}
|
2018-12-03 16:29:46 +00:00
|
|
|
logger.warn(__('Unsupported account configuration: %s' ,JSON.stringify(accountConfig)));
|
|
|
|
logger.warn(__('Check the docs at %s', 'https://embark.status.im/docs/contracts_deployment.html#Using-accounts-in-a-wallet'.underline));
|
2018-05-17 17:48:39 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = AccountParser;
|