From d0e80226d3b81f2f6d42a26bdc3fcd20fe2ecede Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 27 Dec 2019 14:56:18 -0500 Subject: [PATCH] move blockchain specific logic from settings to blockchain model --- src/app/tabs/runTab/model/blockchain.js | 99 ++++++++++++++++++- src/app/tabs/runTab/model/settings.js | 125 ++++-------------------- src/app/tabs/runTab/settings.js | 36 +++---- src/app/udapp/run-tab.js | 6 +- 4 files changed, 137 insertions(+), 129 deletions(-) diff --git a/src/app/tabs/runTab/model/blockchain.js b/src/app/tabs/runTab/model/blockchain.js index 51e2735a16..1043856428 100644 --- a/src/app/tabs/runTab/model/blockchain.js +++ b/src/app/tabs/runTab/model/blockchain.js @@ -3,6 +3,8 @@ const txFormat = remixLib.execution.txFormat const txExecution = remixLib.execution.txExecution const typeConversion = remixLib.execution.typeConversion const EventManager = remixLib.EventManager +const ethJSUtil = require('ethereumjs-util') +const Personal = require('web3-eth-personal') const Web3 = require('web3') class Blockchain { @@ -11,12 +13,22 @@ class Blockchain { this.event = new EventManager() this.executionContext = executionContext this.udapp = udapp + + this.networkcallid = 0 this.setupEvents() } setupEvents () { - this.executionContext.event.register('contextChanged', () => { - this.event.trigger('contextChanged', []) + this.executionContext.event.register('contextChanged', (context, silent) => { + this.event.trigger('contextChanged', [context, silent]) + }) + + this.executionContext.event.register('addProvider', (network) => { + this.event.trigger('addProvider', [network]) + }) + + this.executionContext.event.register('removeProvider', (name) => { + this.event.trigger('removeProvider', [name]) }) this.udapp.event.register('initiatingTransaction', (timestamp, tx, payLoad) => { @@ -141,10 +153,93 @@ class Blockchain { return this.executionContext.isVM() ? txResult.result.createdAddress : txResult.result.contractAddress } + changeExecutionContext (context, confirmCb, infoCb, cb) { + return this.executionContext.executionContextChange(context, null, confirmCb, infoCb, cb) + } + + setProviderFromEndpoint (target, context, cb) { + return this.executionContext.setProviderFromEndpoint(target, context, cb) + } + + getProvider () { + return this.executionContext.getProvider() + } + + getAccountBalanceForAddress (address, cb) { + return this.udapp.getBalanceInEther(address, cb) + } + + updateNetwork (cb) { + this.networkcallid++ + ((callid) => { + this.executionContext.detectNetwork((err, { id, name } = {}) => { + if (this.networkcallid > callid) return + this.networkcallid++ + if (err) { + return cb(err) + } + cb(null, {id, name}) + }) + })(this.networkcallid) + } + + newAccount (passphraseCb, cb) { + return this.udapp.newAccount('', passphraseCb, cb) + } + getAccounts (cb) { return this.udapp.getAccounts(cb) } + isWeb3Provider () { + var isVM = this.executionContext.isVM() + var isInjected = this.executionContext.getProvider() === 'injected' + return (!isVM && !isInjected) + } + + isInjectedWeb3 () { + return this.executionContext.getProvider() === 'injected' + } + + signMessage (message, account, passphrase, cb) { + var isVM = this.executionContext.isVM() + var isInjected = this.executionContext.getProvider() === 'injected' + + if (isVM) { + const personalMsg = ethJSUtil.hashPersonalMessage(Buffer.from(message)) + var privKey = this.udapp.accounts[account].privateKey + try { + var rsv = ethJSUtil.ecsign(personalMsg, privKey) + var signedData = ethJSUtil.toRpcSig(rsv.v, rsv.r, rsv.s) + cb(null, '0x' + personalMsg.toString('hex'), signedData) + } catch (e) { + cb(e.message) + } + return + } + if (isInjected) { + const hashedMsg = Web3.utils.sha3(message) + try { + this.executionContext.web3().eth.sign(account, hashedMsg, (error, signedData) => { + cb(error.message, hashedMsg, signedData) + }) + } catch (e) { + cb(e.message) + } + return + } + + const hashedMsg = Web3.utils.sha3(message) + try { + var personal = new Personal(this.executionContext.web3().currentProvider) + personal.sign(hashedMsg, account, passphrase, (error, signedData) => { + cb(error.message, hashedMsg, signedData) + }) + } catch (e) { + cb(e.message) + } + } + } module.exports = Blockchain diff --git a/src/app/tabs/runTab/model/settings.js b/src/app/tabs/runTab/model/settings.js index e126e583f0..8965312d9c 100644 --- a/src/app/tabs/runTab/model/settings.js +++ b/src/app/tabs/runTab/model/settings.js @@ -1,122 +1,35 @@ -var ethJSUtil = require('ethereumjs-util') -var Personal = require('web3-eth-personal') +// var ethJSUtil = require('ethereumjs-util') +// var Personal = require('web3-eth-personal') var remixLib = require('remix-lib') -var Web3 = require('web3') -const addTooltip = require('../../../ui/tooltip') +// var Web3 = require('web3') +// const addTooltip = require('../../../ui/tooltip') var EventManager = remixLib.EventManager class Settings { - constructor (executionContext, udapp) { + constructor (blockchain, executionContext, udapp) { + this.blockchain = blockchain this.executionContext = executionContext this.udapp = udapp this.event = new EventManager() - this.udapp.event.register('transactionExecuted', (error, from, to, data, lookupOnly, txResult) => { - this.event.trigger('transactionExecuted', [error, from, to, data, lookupOnly, txResult]) - }) + // this.blockchain.event.register('transactionExecuted', (error, from, to, data, lookupOnly, txResult) => { + // this.event.trigger('transactionExecuted', [error, from, to, data, lookupOnly, txResult]) + // }) - this.executionContext.event.register('contextChanged', (context, silent) => { - this.event.trigger('contextChanged', [context, silent]) - }) + // this.blockchain.event.register('contextChanged', (context, silent) => { + // this.event.trigger('contextChanged', [context, silent]) + // }) - this.executionContext.event.register('addProvider', (network) => { - this.event.trigger('addProvider', [network]) - }) + // this.blockchain.event.register('addProvider', (network) => { + // this.event.trigger('addProvider', [network]) + // }) - this.executionContext.event.register('removeProvider', (name) => { - this.event.trigger('removeProvider', [name]) - }) + // this.blockchain.event.register('removeProvider', (name) => { + // this.event.trigger('removeProvider', [name]) + // }) - this.networkcallid = 0 - } - - changeExecutionContext (context, confirmCb, infoCb, cb) { - return this.executionContext.executionContextChange(context, null, confirmCb, infoCb, cb) - } - - setProviderFromEndpoint (target, context, cb) { - return this.executionContext.setProviderFromEndpoint(target, context, cb) - } - - getProvider () { - return this.executionContext.getProvider() - } - - getAccountBalanceForAddress (address, cb) { - return this.udapp.getBalanceInEther(address, cb) - } - - updateNetwork (cb) { - this.networkcallid++ - ((callid) => { - this.executionContext.detectNetwork((err, { id, name } = {}) => { - if (this.networkcallid > callid) return - this.networkcallid++ - if (err) { - return cb(err) - } - cb(null, {id, name}) - }) - })(this.networkcallid) - } - - newAccount (passphraseCb, cb) { - return this.udapp.newAccount('', passphraseCb, cb) - } - - getAccounts (cb) { - return this.udapp.getAccounts(cb) - } - - isWeb3Provider () { - var isVM = this.executionContext.isVM() - var isInjected = this.executionContext.getProvider() === 'injected' - return (!isVM && !isInjected) - } - - isInjectedWeb3 () { - return this.executionContext.getProvider() === 'injected' - } - - signMessage (message, account, passphrase, cb) { - var isVM = this.executionContext.isVM() - var isInjected = this.executionContext.getProvider() === 'injected' - - if (isVM) { - const personalMsg = ethJSUtil.hashPersonalMessage(Buffer.from(message)) - var privKey = this.udapp.accounts[account].privateKey - try { - var rsv = ethJSUtil.ecsign(personalMsg, privKey) - var signedData = ethJSUtil.toRpcSig(rsv.v, rsv.r, rsv.s) - cb(null, '0x' + personalMsg.toString('hex'), signedData) - } catch (e) { - cb(e.message) - } - return - } - if (isInjected) { - const hashedMsg = Web3.utils.sha3(message) - try { - addTooltip('Please check your provider to approve') - this.executionContext.web3().eth.sign(account, hashedMsg, (error, signedData) => { - cb(error.message, hashedMsg, signedData) - }) - } catch (e) { - cb(e.message) - } - return - } - - const hashedMsg = Web3.utils.sha3(message) - try { - var personal = new Personal(this.executionContext.web3().currentProvider) - personal.sign(hashedMsg, account, passphrase, (error, signedData) => { - cb(error.message, hashedMsg, signedData) - }) - } catch (e) { - cb(e.message) - } + // this.networkcallid = 0 } } diff --git a/src/app/tabs/runTab/settings.js b/src/app/tabs/runTab/settings.js index 76032c4650..09375020a3 100644 --- a/src/app/tabs/runTab/settings.js +++ b/src/app/tabs/runTab/settings.js @@ -11,12 +11,12 @@ const globalRegistry = require('../../../global/registry') class SettingsUI { - constructor (settings, networkModule) { - this.settings = settings + constructor (blockchain, networkModule) { + this.blockchain = blockchain this.event = new EventManager() this._components = {} - this.settings.event.register('transactionExecuted', (error, from, to, data, lookupOnly, txResult) => { + this.blockchain.event.register('transactionExecuted', (error, from, to, data, lookupOnly, txResult) => { if (error) return if (!lookupOnly) this.el.querySelector('#value').value = '0' this.updateAccountBalances() @@ -44,7 +44,7 @@ class SettingsUI { if (!this.el) return var accounts = $(this.el.querySelector('#txorigin')).children('option') accounts.each((index, account) => { - this.settings.getAccountBalanceForAddress(account.value, (err, balance) => { + this.blockchain.getAccountBalanceForAddress(account.value, (err, balance) => { if (err) return account.innerText = helper.shortenAddress(account.value, balance) }) @@ -139,7 +139,7 @@ class SettingsUI { var selectExEnv = environmentEl.querySelector('#selectExEnvOptions') this.setDropdown(selectExEnv) - this.settings.event.register('contextChanged', (context, silent) => { + this.blockchain.event.register('contextChanged', (context, silent) => { this.setFinalContext() }) @@ -156,7 +156,7 @@ class SettingsUI { setDropdown (selectExEnv) { this.selectExEnv = selectExEnv - this.settings.event.register('addProvider', (network) => { + this.blockchain.event.register('addProvider', (network) => { selectExEnv.appendChild(yo`