From 38e3c95aabb1ee5827ee0b0e2e612f3c402762b1 Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 3 Jan 2020 16:34:31 -0500 Subject: [PATCH] create classes for each type of provider; move get accounts details to each provider --- src/blockchain/blockchain.js | 97 ++++++++++++---------------- src/blockchain/providers/injected.js | 13 ++++ src/blockchain/providers/node.js | 17 +++++ src/blockchain/providers/vm.js | 18 ++++++ 4 files changed, 90 insertions(+), 55 deletions(-) create mode 100644 src/blockchain/providers/injected.js create mode 100644 src/blockchain/providers/node.js create mode 100644 src/blockchain/providers/vm.js diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index 4fc188cff1..187c30cb80 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -17,6 +17,10 @@ const { EventEmitter } = require('events') const { resultToRemixTx } = require('./txResultHelper') +const VMProvider = require('./providers/vm.js') +const InjectedProvider = require('./providers/injected.js') +const NodeProvider = require('./providers/node.js') + class Blockchain { // NOTE: the config object will need to be refactored out in remix-lib @@ -36,11 +40,11 @@ class Blockchain { return this.executionContext.getProvider() === 'web3' ? this.config.get('settings/personal-mode') : false } }, this.executionContext) - this.accounts = {} this.executionContext.event.register('contextChanged', this.resetEnvironment.bind(this)) this.networkcallid = 0 this.setupEvents() + this.setupProviders() } setupEvents () { @@ -57,6 +61,34 @@ class Blockchain { }) } + setupProviders () { + this.providers = {} + this.providers.vm = new VMProvider(this.executionContext) + this.providers.injected = new InjectedProvider(this.executionContext) + this.providers.web3 = new NodeProvider(this.executionContext, this.config) + } + + getCurrentProvider () { + const provider = this.executionContext.getProvider() + return this.providers[provider] + } + + /** Return the list of accounts */ + // note: the dual promise/callback is kept for now as it was before + getAccounts (cb) { + return new Promise((resolve, reject) => { + this.getCurrentProvider().getAccounts((error, accounts) => { + if (cb) { + return cb(error, accounts) + } + if (error) { + reject(error) + } + resolve(accounts) + }) + }) + } + async deployContract (selectedContract, args, contractMetadata, compilerContracts, callbacks, confirmationCb) { const { continueCb, promptCb, statusCb, finalCb } = callbacks @@ -208,7 +240,7 @@ class Blockchain { if (isVM) { const personalMsg = ethJSUtil.hashPersonalMessage(Buffer.from(message)) - const privKey = this.accounts[account].privateKey + const privKey = this.providers.vm.accounts[account].privateKey try { const rsv = ethJSUtil.ecsign(personalMsg, privKey) const signedData = ethJSUtil.toRpcSig(rsv.v, rsv.r, rsv.s) @@ -293,8 +325,8 @@ class Blockchain { // NOTE: the config is only needed because exectuionContext.init does // if config.get('settings/always-use-vm'), we can simplify this later - resetAndInit (config, transactionContext) { - this.resetAPI(transactionContext) + resetAndInit (config, transactionContextAPI) { + this.transactionContextAPI = transactionContextAPI this.executionContext.init(config) this.executionContext.stopListenOnLastBlock() this.executionContext.listenOnLastBlock() @@ -318,7 +350,7 @@ class Blockchain { } resetEnvironment () { - this.accounts = {} + this.providers.vm.accounts = {} if (this.executionContext.isVM()) { this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000') this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000') @@ -327,7 +359,7 @@ class Blockchain { this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000') } // TODO: most params here can be refactored away in txRunner - this.txRunner = new TxRunner(this.accounts, { + this.txRunner = new TxRunner(this.providers.vm.accounts, { // TODO: only used to check value of doNotShowTransactionConfirmationAgain property config: this.config, // TODO: to refactor, TxRunner already has access to executionContext @@ -346,10 +378,6 @@ class Blockchain { }) } - resetAPI (transactionContextAPI) { - this.transactionContextAPI = transactionContextAPI - } - /** * Create a VM Account * @param {{privateKey: string, balance: string}} newAccount The new account to create @@ -387,7 +415,7 @@ class Blockchain { throw new Error('_addAccount() cannot be called in non-VM mode') } - if (this.accounts) { + if (this.providers.vm.accounts) { privateKey = Buffer.from(privateKey, 'hex') const address = privateToAddress(privateKey) @@ -401,51 +429,10 @@ class Blockchain { }) }) - this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } + this.providers.vm.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } } } - /** Return the list of accounts */ - getAccounts (cb) { - return new Promise((resolve, reject) => { - const provider = this.executionContext.getProvider() - switch (provider) { - case 'vm': { - if (!this.accounts) { - if (cb) cb('No accounts?') - return reject('No accounts?') - } - if (cb) cb(null, Object.keys(this.accounts)) - resolve(Object.keys(this.accounts)) - } - break - case 'web3': { - if (this.config.get('settings/personal-mode')) { - return this.executionContext.web3().personal.getListAccounts((error, accounts) => { - if (cb) cb(error, accounts) - if (error) return reject(error) - resolve(accounts) - }) - } else { - this.executionContext.web3().eth.getAccounts((error, accounts) => { - if (cb) cb(error, accounts) - if (error) return reject(error) - resolve(accounts) - }) - } - } - break - case 'injected': { - this.executionContext.web3().eth.getAccounts((error, accounts) => { - if (cb) cb(error, accounts) - if (error) return reject(error) - resolve(accounts) - }) - } - } - }) - } - /** Get the balance of an address, and convert wei to ether */ getBalanceInEther (address, cb) { address = stripHexPrefix(address) @@ -458,7 +445,7 @@ class Blockchain { cb(null, Web3.utils.fromWei(res.toString(10), 'ether')) }) } - if (!this.accounts) { + if (!this.providers.vm.accounts) { return cb('No accounts?') } @@ -560,7 +547,7 @@ class Blockchain { if (err) return next(err) if (!address) return next('No accounts available') - if (self.executionContext.isVM() && !self.accounts[address]) { + if (self.executionContext.isVM() && !self.providers.vm.accounts[address]) { return next('Invalid account selected') } next(null, address, value, gasLimit) diff --git a/src/blockchain/providers/injected.js b/src/blockchain/providers/injected.js new file mode 100644 index 0000000000..7206e993a3 --- /dev/null +++ b/src/blockchain/providers/injected.js @@ -0,0 +1,13 @@ + +class InjectedProvider { + + constructor (executionContext) { + this.executionContext = executionContext + } + + getAccounts (cb) { + return this.executionContext.web3().eth.getAccounts(cb) + } +} + +module.exports = InjectedProvider diff --git a/src/blockchain/providers/node.js b/src/blockchain/providers/node.js new file mode 100644 index 0000000000..47315a026d --- /dev/null +++ b/src/blockchain/providers/node.js @@ -0,0 +1,17 @@ + +class NodeProvider { + + constructor (executionContext, config) { + this.executionContext = executionContext + this.config = config + } + + getAccounts (cb) { + if (this.config.get('settings/personal-mode')) { + return this.executionContext.web3().personal.getListAccounts(cb) + } + return this.executionContext.web3().eth.getAccounts(cb) + } +} + +module.exports = NodeProvider diff --git a/src/blockchain/providers/vm.js b/src/blockchain/providers/vm.js new file mode 100644 index 0000000000..0936645b5e --- /dev/null +++ b/src/blockchain/providers/vm.js @@ -0,0 +1,18 @@ + +class VMProvider { + + constructor (executionContext) { + this.executionContext = executionContext + this.accounts = {} + } + + getAccounts (cb) { + if (!this.accounts) { + cb('No accounts?') + return cb('No accounts?') + } + return cb(null, Object.keys(this.accounts)) + } +} + +module.exports = VMProvider