intercept get accounts

This commit is contained in:
Jonathan Rainville 2018-05-11 11:41:54 -04:00
parent b7908a3797
commit 00bc3050ba
1 changed files with 42 additions and 20 deletions

View File

@ -1,18 +1,16 @@
const ProviderEngine = require('web3-provider-engine'); const ProviderEngine = require('web3-provider-engine');
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js'); const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js');
const HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js');
const bip39 = require("bip39"); const bip39 = require("bip39");
const hdkey = require('ethereumjs-wallet/hdkey'); const hdkey = require('ethereumjs-wallet/hdkey');
const fs = require('./fs'); const fs = require('./fs');
class Provider { class Provider {
constructor(options) { constructor(options) {
const self = this; const self = this;
this.web3 = options.web3; this.web3 = options.web3;
this.accountsConfig = options.accountsConfig; this.accountsConfig = options.accountsConfig;
this.logger = options.logger; this.logger = options.logger;
this.engine = new ProviderEngine(); this.engine = new ProviderEngine();
// this.web3 = new Web3(engine);
this.engine.addProvider(new RpcSubprovider({ this.engine.addProvider(new RpcSubprovider({
rpcUrl: options.web3Endpoint rpcUrl: options.web3Endpoint
@ -20,6 +18,7 @@ class Provider {
if (this.accountsConfig && this.accountsConfig.length) { if (this.accountsConfig && this.accountsConfig.length) {
this.accounts = []; this.accounts = [];
this.addresses = [];
this.accountsConfig.forEach(accountConfig => { this.accountsConfig.forEach(accountConfig => {
const account = this.getAccount(accountConfig); const account = this.getAccount(accountConfig);
if (!account) { if (!account) {
@ -27,25 +26,25 @@ class Provider {
} }
if (Array.isArray(account)) { if (Array.isArray(account)) {
this.accounts = this.accounts.concat(account); this.accounts = this.accounts.concat(account);
account.forEach(acc => {
this.addresses.push(acc.address);
});
return; return;
} }
this.accounts.push(this.getAccount(accountConfig)); this.accounts.push(account);
this.addresses.push(account.address);
}); });
/*this.engine.addProvider(new HookedWalletSubprovider({ if (this.accounts.length) {
getAccounts: function (cb) { this.asyncMethods = {
cb(null, self.accounts); eth_accounts: self.eth_accounts.bind(this)
}, };
approveTransaction: function (cb) { this.syncMethods = {
eth_accounts: self.eth_accounts_sync.bind(this)
}, };
signTransaction: function (cb) { }
}
}));*/
} }
this.engine.on('block', function (block) { this.engine.on('block', function (block) {
console.log('================================'); console.log('================================');
console.log('BLOCK CHANGED:', '#' + block.number.toString('hex'), '0x' + block.hash.toString('hex')); 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 addressIndex = accountConfig.addressIndex || 0;
const numAddresses = accountConfig.numAddresses || 1; 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 = []; 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(); const wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet();
accounts.push(this.web3.eth.accounts.privateKeyToAccount(wallet.getPrivateKey())); 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" }'); '{ "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); 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); return this.engine.send.apply(this.engine, arguments);
} }
} }