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,32 +28,43 @@ 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) => {
self.addresses = []; if (err) {
self.logger.warn('Error while getting the node\'s accounts.', err.message || err);
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);
});
} }
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() { 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;