From 00bc3050bafdb2121cba421efd722f969e8654ed Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Fri, 11 May 2018 11:41:54 -0400 Subject: [PATCH] intercept get accounts --- lib/core/provider.js | 62 ++++++++++++++++++++++++++++++-------------- 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/lib/core/provider.js b/lib/core/provider.js index 7857e332b..c8bcc9f20 100644 --- a/lib/core/provider.js +++ b/lib/core/provider.js @@ -1,18 +1,16 @@ const ProviderEngine = require('web3-provider-engine'); const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js'); -const HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js'); const bip39 = require("bip39"); const hdkey = require('ethereumjs-wallet/hdkey'); const fs = require('./fs'); class Provider { constructor(options) { - const self = this; + const self = this; this.web3 = options.web3; this.accountsConfig = options.accountsConfig; this.logger = options.logger; this.engine = new ProviderEngine(); - // this.web3 = new Web3(engine); this.engine.addProvider(new RpcSubprovider({ rpcUrl: options.web3Endpoint @@ -20,6 +18,7 @@ class Provider { if (this.accountsConfig && this.accountsConfig.length) { this.accounts = []; + this.addresses = []; this.accountsConfig.forEach(accountConfig => { const account = this.getAccount(accountConfig); if (!account) { @@ -27,25 +26,25 @@ class Provider { } if (Array.isArray(account)) { this.accounts = this.accounts.concat(account); + account.forEach(acc => { + this.addresses.push(acc.address); + }); return; } - this.accounts.push(this.getAccount(accountConfig)); + this.accounts.push(account); + this.addresses.push(account.address); }); - /*this.engine.addProvider(new HookedWalletSubprovider({ - getAccounts: function (cb) { - cb(null, self.accounts); - }, - approveTransaction: function (cb) { - - }, - signTransaction: function (cb) { - - } - }));*/ + if (this.accounts.length) { + this.asyncMethods = { + eth_accounts: self.eth_accounts.bind(this) + }; + this.syncMethods = { + eth_accounts: self.eth_accounts_sync.bind(this) + }; + } } - this.engine.on('block', function (block) { console.log('================================'); console.log('BLOCK CHANGED:', '#' + block.number.toString('hex'), '0x' + block.hash.toString('hex')); @@ -73,10 +72,10 @@ class Provider { const addressIndex = accountConfig.addressIndex || 0; const numAddresses = accountConfig.numAddresses || 1; - const wallet_hdpath = "m/44'/60'/0'/0/"; + const wallet_hdpath = "m/44'/60'/0'/0/"; // TODO check if this can change const accounts = []; - for (let i = addressIndex; i < addressIndex + numAddresses; i++){ + for (let i = addressIndex; i < addressIndex + numAddresses; i++) { const wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet(); accounts.push(this.web3.eth.accounts.privateKeyToAccount(wallet.getPrivateKey())); } @@ -88,11 +87,34 @@ class Provider { '{ "privateKey": "your-private-key", "privateKeyFile": "path/to/file/containing/key", "mnemonic": "12 word mnemonic" }'); } - sendAsync() { + eth_accounts(payload, cb) { + return cb(null, this.addresses); + } + + eth_accounts_sync() { + return this.addresses; + } + + sendAsync(payload, callback) { + // this.engine.sendAsync.apply(this.engine, arguments); + let method = this.asyncMethods[payload.method]; + if (method) { + return method.call(method, payload, (err, result) => { + if (err) { + return callback(err); + } + let response = {'id': payload.id, 'jsonrpc': '2.0', 'result': result}; + callback(null, response); + }); + } this.engine.sendAsync.apply(this.engine, arguments); } - send() { + send(payload) { + let method = this.syncMethods[payload.method]; + if (method) { + return method.call(method, payload); // TODO check if that makes sense + } return this.engine.send.apply(this.engine, arguments); } }