add default account if set in config

This commit is contained in:
Jonathan Rainville 2018-10-04 15:18:00 -04:00 committed by Pascal Precht
parent 0fb0fa0648
commit eaafa9bed2
No known key found for this signature in database
GPG Key ID: 0EE28D8D6FD85D7D
2 changed files with 51 additions and 30 deletions

View File

@ -28,17 +28,27 @@ class Provider {
self.web3.setProvider(self.provider); self.web3.setProvider(self.provider);
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger); self.web3.eth.getAccounts((err, accounts) => {
if (err) {
self.logger.warn('Error while getting the node\'s accounts.', err.message || err);
}
self.accounts = AccountParser.parseAccountsConfig(self.accountsConfig, self.web3, self.logger, accounts);
self.addresses = []; self.addresses = [];
if (!self.accounts.length) { if (!self.accounts.length) {
return callback(); return callback();
} }
self.accounts.forEach(account => { self.accounts.forEach(account => {
self.addresses.push(account.address); self.addresses.push(account.address);
if (account.privateKey) {
self.web3.eth.accounts.wallet.add(account); self.web3.eth.accounts.wallet.add(account);
}
}); });
self.web3.eth.defaultAccount = self.addresses[0]; self.web3.eth.defaultAccount = self.addresses[0];
console.dir(self.addresses);
const realSend = self.provider.send.bind(self.provider); const realSend = self.provider.send.bind(self.provider);
self.provider.send = function (payload, cb) { self.provider.send = function (payload, cb) {
if (payload.method === 'eth_accounts') { if (payload.method === 'eth_accounts') {
@ -46,7 +56,7 @@ class Provider {
if (err) { if (err) {
return cb(err); return cb(err);
} }
result.result = result.result.concat(self.addresses); result.result = self.addresses; // Send our addresses
cb(null, result); cb(null, result);
}); });
} }
@ -54,6 +64,7 @@ class Provider {
}; };
callback(); callback();
});
} }
stop() { stop() {

View File

@ -7,11 +7,11 @@ const {getHexBalanceFromString} = require('../utils/utils');
const path = require('path'); const path = require('path');
class AccountParser { class AccountParser {
static parseAccountsConfig(accountsConfig, web3, logger) { static parseAccountsConfig(accountsConfig, web3, logger, nodeAccounts) {
let accounts = []; let accounts = [];
if (accountsConfig && accountsConfig.length) { if (accountsConfig && accountsConfig.length) {
accountsConfig.forEach(accountConfig => { accountsConfig.forEach(accountConfig => {
const account = AccountParser.getAccount(accountConfig, web3, logger); const account = AccountParser.getAccount(accountConfig, web3, logger, nodeAccounts);
if (!account) { if (!account) {
return; return;
} }
@ -25,10 +25,7 @@ class AccountParser {
return accounts; return accounts;
} }
static getAccount(accountConfig, web3, logger) { static getAccount(accountConfig, web3, logger = console, nodeAccounts) {
if (!logger) {
logger = console;
}
let hexBalance = null; let hexBalance = null;
if (accountConfig.balance) { if (accountConfig.balance) {
hexBalance = getHexBalanceFromString(accountConfig.balance, web3); hexBalance = getHexBalanceFromString(accountConfig.balance, web3);
@ -39,6 +36,19 @@ class AccountParser {
accountConfig.privateKey = randomAccount.privateKey; 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) {
if (!accountConfig.privateKey.startsWith('0x')) { if (!accountConfig.privateKey.startsWith('0x')) {
accountConfig.privateKey = '0x' + accountConfig.privateKey; accountConfig.privateKey = '0x' + accountConfig.privateKey;