diff --git a/lib/contracts/accountParser.js b/lib/contracts/accountParser.js new file mode 100644 index 000000000..139f3c7cf --- /dev/null +++ b/lib/contracts/accountParser.js @@ -0,0 +1,66 @@ +const bip39 = require("bip39"); +const hdkey = require('ethereumjs-wallet/hdkey'); +const fs = require('../core/fs'); + + +class AccountParser { + static parseAccountsConfig(accountsConfig, web3, logger) { + let accounts = []; + if (accountsConfig && accountsConfig.length) { + accountsConfig.forEach(accountConfig => { + const account = AccountParser.getAccount(accountConfig, web3, logger); + if (!account) { + return; + } + if (Array.isArray(account)) { + accounts = accounts.concat(account); + return; + } + accounts.push(account); + }); + } + return accounts; + } + + static getAccount(accountConfig, web3, logger) { + if (!logger) { + logger = console; + } + if (accountConfig.privateKey) { + if (!accountConfig.privateKey.startsWith('0x')) { + accountConfig.privateKey = '0x' + accountConfig.privateKey; + } + return web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey); + } + if (accountConfig.privateKeyFile) { + let fileContent = fs.readFileSync(fs.dappPath(accountConfig.privateKeyFile)).toString(); + fileContent = fileContent.trim().split(/[,;]/); + return fileContent.map(key => { + if (!key.startsWith('0x')) { + key = '0x' + key; + } + return web3.eth.accounts.privateKeyToAccount(key); + }); + } + 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 = accountConfig.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(web3.eth.accounts.privateKeyToAccount('0x' + wallet.getPrivateKey().toString('hex'))); + } + return accounts; + } + logger.warn('Unsupported account configuration: ' + JSON.stringify(accountConfig)); + logger.warn('Try using one of those: ' + + '{ "privateKey": "your-private-key", "privateKeyFile": "path/to/file/containing/key", "mnemonic": "12 word mnemonic" }'); + return null; + } +} + +module.exports = AccountParser; diff --git a/lib/contracts/provider.js b/lib/contracts/provider.js index a5225f0ed..4f89b0e24 100644 --- a/lib/contracts/provider.js +++ b/lib/contracts/provider.js @@ -1,8 +1,6 @@ const ProviderEngine = require('web3-provider-engine'); const RpcSubprovider = require('web3-provider-engine/subproviders/rpc.js'); -const bip39 = require("bip39"); -const hdkey = require('ethereumjs-wallet/hdkey'); -const fs = require('../core/fs'); +const AccountParser = require('./accountParser'); class Provider { constructor(options) { @@ -18,35 +16,18 @@ class Provider { rpcUrl: options.web3Endpoint })); - if (this.accountsConfig && this.accountsConfig.length) { - this.accounts = []; - this.addresses = []; - this.accountsConfig.forEach(accountConfig => { - const account = this.getAccount(accountConfig); - if (!account) { - return; - } - if (Array.isArray(account)) { - this.accounts = this.accounts.concat(account); - account.forEach(acc => { - this.addresses.push(acc.address); - }); - return; - } - this.accounts.push(account); + this.accounts = AccountParser.parseAccountsConfig(this.accountsConfig, this.web3, this.logger); + this.addresses = []; + if (this.accounts.length) { + this.accounts.forEach(account => { this.addresses.push(account.address); + this.web3.eth.accounts.wallet.add(account); }); - - if (this.accounts.length) { - this.accounts.forEach(account => { - this.web3.eth.accounts.wallet.add(account); - }); - 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.'); - } + 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.'); } } @@ -58,43 +39,6 @@ class Provider { this.engine.start(); } - getAccount(accountConfig) { - if (accountConfig.privateKey) { - if (!accountConfig.privateKey.startsWith('0x')) { - accountConfig.privateKey = '0x' + accountConfig.privateKey; - } - return this.web3.eth.accounts.privateKeyToAccount(accountConfig.privateKey); - } - if (accountConfig.privateKeyFile) { - let fileContent = fs.readFileSync(fs.dappPath(accountConfig.privateKeyFile)).toString(); - fileContent = fileContent.trim().split(/[,;]/); - return fileContent.map(key => { - if (!key.startsWith('0x')) { - key = '0x' + key; - } - return this.web3.eth.accounts.privateKeyToAccount(key); - }); - } - 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 = accountConfig.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('0x' + wallet.getPrivateKey().toString('hex'))); - } - 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" }'); - return null; - } - eth_accounts(payload, cb) { return cb(null, this.addresses); }