refactor(@embark/blockchain): consistently merge config accounts and node accounts

This commit is contained in:
Michael Bradley, Jr 2019-07-19 19:19:59 -05:00 committed by Michael Bradley
parent c2094dbc49
commit 0c12097c17
2 changed files with 29 additions and 10 deletions

View File

@ -96,7 +96,8 @@ class Provider {
self.web3.eth.accounts.wallet.add(account); self.web3.eth.accounts.wallet.add(account);
} }
}); });
self.addresses = [...new Set(self.addresses)]; // Remove duplicates // Normalize addresses and remove duplicates
self.addresses = [...new Set(self.addresses.map(ethUtil.toChecksumAddress))];
if (self.accounts.length) { if (self.accounts.length) {
self.web3.eth.defaultAccount = self.addresses[0]; self.web3.eth.defaultAccount = self.addresses[0];
@ -132,13 +133,18 @@ class Provider {
}, 1); }, 1);
self.provider.send = function(payload, cb) { self.provider.send = function(payload, cb) {
if (payload.method === 'eth_accounts') { if (payload.method === 'eth_accounts' || payload.method === 'personal_listAccounts') {
return realSend(payload, function(err, result) { return realSend(payload, function(err, result) {
if (err) { if (err) {
return cb(err); return cb(err);
} }
if (self.accounts.length) { if (self.accounts.length) {
result.result = self.addresses; result.result = [
...new Set([
...self.addresses,
...result.result.map(ethUtil.toChecksumAddress)
])
];
} }
cb(null, result); cb(null, result);
}); });

View File

@ -16,17 +16,26 @@ const modifyResponse = require('node-http-proxy-json');
const Transaction = require('ethereumjs-tx'); const Transaction = require('ethereumjs-tx');
const ethUtil = require('ethereumjs-util'); const ethUtil = require('ethereumjs-util');
import { pingEndpoint } from './utils'; import { pingEndpoint } from './utils';
import Web3 from 'web3';
const METHODS_TO_MODIFY = {accounts: 'eth_accounts'}; const METHODS_TO_MODIFY = {
accounts: 'eth_accounts',
personalAccounts: 'personal_listAccounts'
};
const REQUEST_TIMEOUT = 5000; const REQUEST_TIMEOUT = 5000;
const modifyPayload = (toModifyPayloads, body, accounts) => { const modifyPayload = (toModifyPayloads, body, accounts) => {
switch (toModifyPayloads[body.id]) { if (toModifyPayloads[body.id] === METHODS_TO_MODIFY.accounts ||
case METHODS_TO_MODIFY.accounts: toModifyPayloads[body.id] === METHODS_TO_MODIFY.personalAccounts) {
delete toModifyPayloads[body.id]; delete toModifyPayloads[body.id];
body.result = Array.isArray(body.result) && body.result.concat(accounts); if (Array.isArray(body.result)) {
break; body.result = [
default: ...new Set([
...accounts,
...body.result.map(ethUtil.toChecksumAddress)
])
];
}
} }
return body; return body;
}; };
@ -226,6 +235,10 @@ export class Proxy {
}); });
}()); }());
const web3 = new Web3(`${ws ? 'ws' : 'http'}://${canonicalHost(host)}:${port}`);
accounts = (await web3.eth.getAccounts() || []).concat(accounts || []);
accounts = [...new Set(accounts.map(ethUtil.toChecksumAddress))];
let proxy = httpProxy.createProxyServer({ let proxy = httpProxy.createProxyServer({
ssl: certOptions, ssl: certOptions,
target: { target: {