mirror of https://github.com/embarklabs/embark.git
add default account if set in config
This commit is contained in:
parent
0fb0fa0648
commit
eaafa9bed2
|
@ -28,32 +28,43 @@ class Provider {
|
|||
|
||||
self.web3.setProvider(self.provider);
|
||||
|
||||
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger);
|
||||
self.addresses = [];
|
||||
|
||||
if (!self.accounts.length) {
|
||||
return callback();
|
||||
}
|
||||
self.accounts.forEach(account => {
|
||||
self.addresses.push(account.address);
|
||||
self.web3.eth.accounts.wallet.add(account);
|
||||
});
|
||||
self.web3.eth.defaultAccount = self.addresses[0];
|
||||
const realSend = self.provider.send.bind(self.provider);
|
||||
self.provider.send = function (payload, cb) {
|
||||
if (payload.method === 'eth_accounts') {
|
||||
return realSend(payload, function (err, result) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
result.result = result.result.concat(self.addresses);
|
||||
cb(null, result);
|
||||
});
|
||||
self.web3.eth.getAccounts((err, accounts) => {
|
||||
if (err) {
|
||||
self.logger.warn('Error while getting the node\'s accounts.', err.message || err);
|
||||
}
|
||||
realSend(payload, cb);
|
||||
};
|
||||
|
||||
callback();
|
||||
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger, accounts);
|
||||
self.addresses = [];
|
||||
|
||||
if (!self.accounts.length) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
self.accounts.forEach(account => {
|
||||
self.addresses.push(account.address);
|
||||
if (account.privateKey) {
|
||||
self.web3.eth.accounts.wallet.add(account);
|
||||
}
|
||||
});
|
||||
self.web3.eth.defaultAccount = self.addresses[0];
|
||||
console.dir(self.addresses);
|
||||
|
||||
const realSend = self.provider.send.bind(self.provider);
|
||||
self.provider.send = function (payload, cb) {
|
||||
if (payload.method === 'eth_accounts') {
|
||||
return realSend(payload, function (err, result) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
result.result = self.addresses; // Send our addresses
|
||||
cb(null, result);
|
||||
});
|
||||
}
|
||||
realSend(payload, cb);
|
||||
};
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
stop() {
|
||||
|
|
|
@ -7,11 +7,11 @@ const {getHexBalanceFromString} = require('../utils/utils');
|
|||
const path = require('path');
|
||||
|
||||
class AccountParser {
|
||||
static parseAccountsConfig(accountsConfig, web3, logger) {
|
||||
static parseAccountsConfig(accountsConfig, web3, logger, nodeAccounts) {
|
||||
let accounts = [];
|
||||
if (accountsConfig && accountsConfig.length) {
|
||||
accountsConfig.forEach(accountConfig => {
|
||||
const account = AccountParser.getAccount(accountConfig, web3, logger);
|
||||
const account = AccountParser.getAccount(accountConfig, web3, logger, nodeAccounts);
|
||||
if (!account) {
|
||||
return;
|
||||
}
|
||||
|
@ -25,10 +25,7 @@ class AccountParser {
|
|||
return accounts;
|
||||
}
|
||||
|
||||
static getAccount(accountConfig, web3, logger) {
|
||||
if (!logger) {
|
||||
logger = console;
|
||||
}
|
||||
static getAccount(accountConfig, web3, logger = console, nodeAccounts) {
|
||||
let hexBalance = null;
|
||||
if (accountConfig.balance) {
|
||||
hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
|
||||
|
@ -39,6 +36,19 @@ class AccountParser {
|
|||
accountConfig.privateKey = randomAccount.privateKey;
|
||||
}
|
||||
|
||||
if (accountConfig.nodeAccounts) {
|
||||
if (!nodeAccounts) {
|
||||
logger.warn('Cannot use nodeAccounts in this context');
|
||||
return null;
|
||||
}
|
||||
if (!nodeAccounts.length) {
|
||||
return null;
|
||||
}
|
||||
return nodeAccounts.map(address => {
|
||||
return {address};
|
||||
});
|
||||
}
|
||||
|
||||
if (accountConfig.privateKey) {
|
||||
if (!accountConfig.privateKey.startsWith('0x')) {
|
||||
accountConfig.privateKey = '0x' + accountConfig.privateKey;
|
||||
|
|
Loading…
Reference in New Issue