embark-area-51/lib/core/provider.js

101 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-05-10 18:52:30 +00:00
const ProviderEngine = require('web3-provider-engine');
const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js');
2018-05-10 18:52:51 +00:00
const HookedWalletSubprovider = require('web3-provider-engine/subproviders/hooked-wallet.js');
const bip39 = require("bip39");
const hdkey = require('ethereumjs-wallet/hdkey');
const fs = require('./fs');
2018-05-10 18:52:30 +00:00
class Provider {
2018-05-10 18:52:51 +00:00
constructor(options) {
const self = this;
this.web3 = options.web3;
this.accountsConfig = options.accountsConfig;
this.logger = options.logger;
2018-05-10 18:52:30 +00:00
this.engine = new ProviderEngine();
// this.web3 = new Web3(engine);
this.engine.addProvider(new RpcSubprovider({
2018-05-10 18:52:51 +00:00
rpcUrl: options.web3Endpoint
2018-05-10 18:52:30 +00:00
}));
2018-05-10 18:52:51 +00:00
if (this.accountsConfig && this.accountsConfig.length) {
this.accounts = [];
this.accountsConfig.forEach(accountConfig => {
const account = this.getAccount(accountConfig);
if (!account) {
return;
}
if (Array.isArray(account)) {
this.accounts = this.accounts.concat(account);
return;
}
this.accounts.push(this.getAccount(accountConfig));
});
/*this.engine.addProvider(new HookedWalletSubprovider({
getAccounts: function (cb) {
cb(null, self.accounts);
},
approveTransaction: function (cb) {
},
signTransaction: function (cb) {
}
}));*/
}
2018-05-10 18:52:30 +00:00
this.engine.on('block', function (block) {
console.log('================================');
console.log('BLOCK CHANGED:', '#' + block.number.toString('hex'), '0x' + block.hash.toString('hex'));
console.log('================================');
});
// network connectivity error
this.engine.on('error', function (err) {
// report connectivity errors
console.error(err.stack);
});
this.engine.start();
}
2018-05-10 18:52:51 +00:00
getAccount(accountConfig) {
if (accountConfig.privateKey) {
return this.web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey);
}
if (accountConfig.privateKeyFile) {
let fileContent = fs.readFileSync(fs.dappPath(accountConfig.privateKeyFile)).toString();
return this.web3.eth.accounts.privateKeyToAccount(fileContent.trim());
}
if (accountConfig.mnemonic) {
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(accountConfig.mnemonic.trim()));
const addressIndex = accountConfig.addressIndex || 0;
const numAddresses = accountConfig.numAddresses || 1;
const wallet_hdpath = "m/44'/60'/0'/0/";
const accounts = [];
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()));
}
return accounts;
}
this.logger.warn('Unsupported account configuration: ' + JSON.stringify(accountConfig));
this.logger.warn('Try using one of those: ' +
'{ "privateKey": "your-private-key", "privateKeyFile": "path/to/file/containing/key", "mnemonic": "12 word mnemonic" }');
}
2018-05-10 18:52:30 +00:00
sendAsync() {
this.engine.sendAsync.apply(this.engine, arguments);
}
send() {
return this.engine.send.apply(this.engine, arguments);
}
}
module.exports = Provider;