embark/lib/contracts/provider.js

122 lines
3.9 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 bip39 = require("bip39");
const hdkey = require('ethereumjs-wallet/hdkey');
2018-05-15 16:45:38 +00:00
const fs = require('../core/fs');
2018-05-10 18:52:30 +00:00
class Provider {
2018-05-10 18:52:51 +00:00
constructor(options) {
2018-05-11 15:41:54 +00:00
const self = this;
2018-05-10 18:52:51 +00:00
this.web3 = options.web3;
this.accountsConfig = options.accountsConfig;
this.logger = options.logger;
this.isDev = options.isDev;
2018-05-10 18:52:30 +00:00
this.engine = new ProviderEngine();
2018-05-14 16:08:03 +00:00
this.asyncMethods = {};
2018-05-10 18:52:30 +00:00
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 = [];
2018-05-11 15:41:54 +00:00
this.addresses = [];
2018-05-10 18:52:51 +00:00
this.accountsConfig.forEach(accountConfig => {
const account = this.getAccount(accountConfig);
if (!account) {
return;
}
if (Array.isArray(account)) {
this.accounts = this.accounts.concat(account);
2018-05-11 15:41:54 +00:00
account.forEach(acc => {
this.addresses.push(acc.address);
});
2018-05-10 18:52:51 +00:00
return;
}
2018-05-11 15:41:54 +00:00
this.accounts.push(account);
this.addresses.push(account.address);
2018-05-10 18:52:51 +00:00
});
2018-05-11 15:41:54 +00:00
if (this.accounts.length) {
2018-05-14 16:08:03 +00:00
this.accounts.forEach(account => {
this.web3.eth.accounts.wallet.add(account);
});
2018-05-11 15:41:54 +00:00
this.asyncMethods = {
eth_accounts: self.eth_accounts.bind(this)
};
if (this.isDev) {
this.logger.warn('You are using your own account in the develop environment. It might not be funded.');
}
2018-05-11 15:41:54 +00:00
}
2018-05-10 18:52:51 +00:00
}
2018-05-10 18:52:30 +00:00
// network connectivity error
2018-05-14 16:08:03 +00:00
this.engine.on('error', (err) => {
2018-05-10 18:52:30 +00:00
// report connectivity errors
2018-05-14 16:08:03 +00:00
this.logger.error(err.stack);
2018-05-10 18:52:30 +00:00
});
this.engine.start();
}
2018-05-10 18:52:51 +00:00
getAccount(accountConfig) {
if (accountConfig.privateKey) {
2018-05-14 20:38:15 +00:00
if (!accountConfig.privateKey.startsWith('0x')) {
accountConfig.privateKey = '0x' + accountConfig.privateKey;
}
2018-05-10 18:52:51 +00:00
return this.web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey);
}
if (accountConfig.privateKeyFile) {
let fileContent = fs.readFileSync(fs.dappPath(accountConfig.privateKeyFile)).toString();
2018-05-14 17:30:13 +00:00
fileContent = fileContent.trim().split(/[,;]/);
return fileContent.map(key => {
2018-05-14 20:38:15 +00:00
if (!key.startsWith('0x')) {
key = '0x' + key;
}
2018-05-14 17:30:13 +00:00
return this.web3.eth.accounts.privateKeyToAccount(key);
});
2018-05-10 18:52:51 +00:00
}
if (accountConfig.mnemonic) {
const hdwallet = hdkey.fromMasterSeed(bip39.mnemonicToSeed(accountConfig.mnemonic.trim()));
const addressIndex = accountConfig.addressIndex || 0;
const numAddresses = accountConfig.numAddresses || 1;
2018-05-14 16:08:03 +00:00
const wallet_hdpath = accountConfig.hdpath || "m/44'/60'/0'/0/";
2018-05-10 18:52:51 +00:00
const accounts = [];
2018-05-11 15:41:54 +00:00
for (let i = addressIndex; i < addressIndex + numAddresses; i++) {
2018-05-10 18:52:51 +00:00
const wallet = hdwallet.derivePath(wallet_hdpath + i).getWallet();
2018-05-15 15:33:19 +00:00
accounts.push(this.web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex')));
2018-05-10 18:52:51 +00:00
}
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-14 18:04:26 +00:00
return null;
2018-05-10 18:52:51 +00:00
}
2018-05-11 15:41:54 +00:00
eth_accounts(payload, cb) {
return cb(null, this.addresses);
}
sendAsync(payload, callback) {
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);
});
}
2018-05-10 18:52:30 +00:00
this.engine.sendAsync.apply(this.engine, arguments);
}
send() {
2018-05-10 18:52:30 +00:00
return this.engine.send.apply(this.engine, arguments);
}
}
module.exports = Provider;