Skip to content

Commit

Permalink
refactor(@embark/blockchain): consistently merge config accounts and …
Browse files Browse the repository at this point in the history
…node accounts
  • Loading branch information
michaelsbradleyjr committed Jul 23, 2019
1 parent 1491028 commit 6e7dbb0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 10 deletions.
12 changes: 9 additions & 3 deletions packages/embark-blockchain-connector/src/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ class Provider {
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) {
self.web3.eth.defaultAccount = self.addresses[0];
Expand Down Expand Up @@ -132,13 +133,18 @@ class Provider {
}, 1);

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) {
if (err) {
return cb(err);
}
if (self.accounts.length) {
result.result = self.addresses;
result.result = [
...new Set([
...self.addresses,
...result.result.map(ethUtil.toChecksumAddress)
])
];
}
cb(null, result);
});
Expand Down
27 changes: 20 additions & 7 deletions packages/embark-blockchain-process/src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,26 @@ const modifyResponse = require('node-http-proxy-json');
const Transaction = require('ethereumjs-tx');
const ethUtil = require('ethereumjs-util');
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 modifyPayload = (toModifyPayloads, body, accounts) => {
switch (toModifyPayloads[body.id]) {
case METHODS_TO_MODIFY.accounts:
delete toModifyPayloads[body.id];
body.result = Array.isArray(body.result) && body.result.concat(accounts);
break;
default:
if (toModifyPayloads[body.id] === METHODS_TO_MODIFY.accounts ||
toModifyPayloads[body.id] === METHODS_TO_MODIFY.personalAccounts) {
delete toModifyPayloads[body.id];
if (Array.isArray(body.result)) {
body.result = [
...new Set([
...accounts,
...body.result.map(ethUtil.toChecksumAddress)
])
];
}
}
return body;
};
Expand Down Expand Up @@ -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({
ssl: certOptions,
target: {
Expand Down

0 comments on commit 6e7dbb0

Please sign in to comment.