move blockchain specific logic from settings to blockchain model

pull/1/head
Iuri Matias 5 years ago
parent 7fdbf7c8a6
commit d0e80226d3
  1. 99
      src/app/tabs/runTab/model/blockchain.js
  2. 125
      src/app/tabs/runTab/model/settings.js
  3. 36
      src/app/tabs/runTab/settings.js
  4. 6
      src/app/udapp/run-tab.js

@ -3,6 +3,8 @@ const txFormat = remixLib.execution.txFormat
const txExecution = remixLib.execution.txExecution const txExecution = remixLib.execution.txExecution
const typeConversion = remixLib.execution.typeConversion const typeConversion = remixLib.execution.typeConversion
const EventManager = remixLib.EventManager const EventManager = remixLib.EventManager
const ethJSUtil = require('ethereumjs-util')
const Personal = require('web3-eth-personal')
const Web3 = require('web3') const Web3 = require('web3')
class Blockchain { class Blockchain {
@ -11,12 +13,22 @@ class Blockchain {
this.event = new EventManager() this.event = new EventManager()
this.executionContext = executionContext this.executionContext = executionContext
this.udapp = udapp this.udapp = udapp
this.networkcallid = 0
this.setupEvents() this.setupEvents()
} }
setupEvents () { setupEvents () {
this.executionContext.event.register('contextChanged', () => { this.executionContext.event.register('contextChanged', (context, silent) => {
this.event.trigger('contextChanged', []) 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) => { this.udapp.event.register('initiatingTransaction', (timestamp, tx, payLoad) => {
@ -141,10 +153,93 @@ class Blockchain {
return this.executionContext.isVM() ? txResult.result.createdAddress : txResult.result.contractAddress 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) { getAccounts (cb) {
return this.udapp.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 module.exports = Blockchain

@ -1,122 +1,35 @@
var ethJSUtil = require('ethereumjs-util') // var ethJSUtil = require('ethereumjs-util')
var Personal = require('web3-eth-personal') // var Personal = require('web3-eth-personal')
var remixLib = require('remix-lib') var remixLib = require('remix-lib')
var Web3 = require('web3') // var Web3 = require('web3')
const addTooltip = require('../../../ui/tooltip') // const addTooltip = require('../../../ui/tooltip')
var EventManager = remixLib.EventManager var EventManager = remixLib.EventManager
class Settings { class Settings {
constructor (executionContext, udapp) { constructor (blockchain, executionContext, udapp) {
this.blockchain = blockchain
this.executionContext = executionContext this.executionContext = executionContext
this.udapp = udapp this.udapp = udapp
this.event = new EventManager() this.event = new EventManager()
this.udapp.event.register('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.event.trigger('transactionExecuted', [error, from, to, data, lookupOnly, txResult])
}) // })
this.executionContext.event.register('contextChanged', (context, silent) => { // this.blockchain.event.register('contextChanged', (context, silent) => {
this.event.trigger('contextChanged', [context, silent]) // this.event.trigger('contextChanged', [context, silent])
}) // })
this.executionContext.event.register('addProvider', (network) => { // this.blockchain.event.register('addProvider', (network) => {
this.event.trigger('addProvider', [network]) // this.event.trigger('addProvider', [network])
}) // })
this.executionContext.event.register('removeProvider', (name) => { // this.blockchain.event.register('removeProvider', (name) => {
this.event.trigger('removeProvider', [name]) // this.event.trigger('removeProvider', [name])
}) // })
this.networkcallid = 0 // 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)
}
} }
} }

@ -11,12 +11,12 @@ const globalRegistry = require('../../../global/registry')
class SettingsUI { class SettingsUI {
constructor (settings, networkModule) { constructor (blockchain, networkModule) {
this.settings = settings this.blockchain = blockchain
this.event = new EventManager() this.event = new EventManager()
this._components = {} 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 (error) return
if (!lookupOnly) this.el.querySelector('#value').value = '0' if (!lookupOnly) this.el.querySelector('#value').value = '0'
this.updateAccountBalances() this.updateAccountBalances()
@ -44,7 +44,7 @@ class SettingsUI {
if (!this.el) return if (!this.el) return
var accounts = $(this.el.querySelector('#txorigin')).children('option') var accounts = $(this.el.querySelector('#txorigin')).children('option')
accounts.each((index, account) => { accounts.each((index, account) => {
this.settings.getAccountBalanceForAddress(account.value, (err, balance) => { this.blockchain.getAccountBalanceForAddress(account.value, (err, balance) => {
if (err) return if (err) return
account.innerText = helper.shortenAddress(account.value, balance) account.innerText = helper.shortenAddress(account.value, balance)
}) })
@ -139,7 +139,7 @@ class SettingsUI {
var selectExEnv = environmentEl.querySelector('#selectExEnvOptions') var selectExEnv = environmentEl.querySelector('#selectExEnvOptions')
this.setDropdown(selectExEnv) this.setDropdown(selectExEnv)
this.settings.event.register('contextChanged', (context, silent) => { this.blockchain.event.register('contextChanged', (context, silent) => {
this.setFinalContext() this.setFinalContext()
}) })
@ -156,7 +156,7 @@ class SettingsUI {
setDropdown (selectExEnv) { setDropdown (selectExEnv) {
this.selectExEnv = selectExEnv this.selectExEnv = selectExEnv
this.settings.event.register('addProvider', (network) => { this.blockchain.event.register('addProvider', (network) => {
selectExEnv.appendChild(yo`<option selectExEnv.appendChild(yo`<option
title="Manually added environment: ${network.url}" title="Manually added environment: ${network.url}"
value="${network.name}" value="${network.name}"
@ -167,7 +167,7 @@ class SettingsUI {
addTooltip(`${network.name} [${network.url}] added`) addTooltip(`${network.name} [${network.url}] added`)
}) })
this.settings.event.register('removeProvider', (name) => { this.blockchain.event.register('removeProvider', (name) => {
var env = selectExEnv.querySelector(`option[value="${name}"]`) var env = selectExEnv.querySelector(`option[value="${name}"]`)
if (env) { if (env) {
selectExEnv.removeChild(env) selectExEnv.removeChild(env)
@ -177,9 +177,9 @@ class SettingsUI {
selectExEnv.addEventListener('change', (event) => { selectExEnv.addEventListener('change', (event) => {
let context = selectExEnv.options[selectExEnv.selectedIndex].value let context = selectExEnv.options[selectExEnv.selectedIndex].value
this.settings.changeExecutionContext(context, () => { this.blockchain.changeExecutionContext(context, () => {
modalDialogCustom.prompt('External node request', this.web3ProviderDialogBody(), 'http://localhost:8545', (target) => { modalDialogCustom.prompt('External node request', this.web3ProviderDialogBody(), 'http://localhost:8545', (target) => {
this.settings.setProviderFromEndpoint(target, context, (alertMsg) => { this.blockchain.setProviderFromEndpoint(target, context, (alertMsg) => {
if (alertMsg) addTooltip(alertMsg) if (alertMsg) addTooltip(alertMsg)
this.setFinalContext() this.setFinalContext()
}) })
@ -189,7 +189,7 @@ class SettingsUI {
}, this.setFinalContext.bind(this)) }, this.setFinalContext.bind(this))
}) })
selectExEnv.value = this.settings.getProvider() selectExEnv.value = this.blockchain.getProvider()
} }
web3ProviderDialogBody () { web3ProviderDialogBody () {
@ -208,7 +208,7 @@ class SettingsUI {
setFinalContext () { setFinalContext () {
// set the final context. Cause it is possible that this is not the one we've originaly selected // set the final context. Cause it is possible that this is not the one we've originaly selected
this.selectExEnv.value = this.settings.getProvider() this.selectExEnv.value = this.blockchain.getProvider()
this.event.trigger('clearInstance', []) this.event.trigger('clearInstance', [])
this.updateNetwork() this.updateNetwork()
this.updatePlusButton() this.updatePlusButton()
@ -250,7 +250,7 @@ class SettingsUI {
} }
newAccount () { newAccount () {
this.settings.newAccount( this.blockchain.newAccount(
(cb) => { (cb) => {
modalDialogCustom.promptPassphraseCreation((error, passphrase) => { modalDialogCustom.promptPassphraseCreation((error, passphrase) => {
if (error) { if (error) {
@ -269,14 +269,14 @@ class SettingsUI {
} }
signMessage () { signMessage () {
this.settings.getAccounts((err, accounts) => { this.blockchain.getAccounts((err, accounts) => {
if (err) { if (err) {
return addTooltip(`Cannot get account list: ${err}`) return addTooltip(`Cannot get account list: ${err}`)
} }
var signMessageDialog = { 'title': 'Sign a message', 'text': 'Enter a message to sign', 'inputvalue': 'Message to sign' } var signMessageDialog = { 'title': 'Sign a message', 'text': 'Enter a message to sign', 'inputvalue': 'Message to sign' }
var $txOrigin = this.el.querySelector('#txorigin') var $txOrigin = this.el.querySelector('#txorigin')
if (!$txOrigin.selectedOptions[0] && (this.settings.isInjectedWeb3() || this.settings.isWeb3Provider())) { if (!$txOrigin.selectedOptions[0] && (this.blockchain.isInjectedWeb3() || this.blockchain.isWeb3Provider())) {
return addTooltip(`Account list is empty, please make sure the current provider is properly connected to remix`) return addTooltip(`Account list is empty, please make sure the current provider is properly connected to remix`)
} }
@ -284,7 +284,7 @@ class SettingsUI {
var promptCb = (passphrase) => { var promptCb = (passphrase) => {
const modal = modalDialogCustom.promptMulti(signMessageDialog, (message) => { const modal = modalDialogCustom.promptMulti(signMessageDialog, (message) => {
this.settings.signMessage(message, account, passphrase, (err, msgHash, signedData) => { this.blockchain.signMessage(message, account, passphrase, (err, msgHash, signedData) => {
if (err) { if (err) {
return addTooltip(err) return addTooltip(err)
} }
@ -301,7 +301,7 @@ class SettingsUI {
}, false) }, false)
} }
if (this.settings.isWeb3Provider()) { if (this.blockchain.isWeb3Provider()) {
return modalDialogCustom.promptPassphrase( return modalDialogCustom.promptPassphrase(
'Passphrase to sign a message', 'Passphrase to sign a message',
'Enter your passphrase for this account to sign the message', 'Enter your passphrase for this account to sign the message',
@ -315,7 +315,7 @@ class SettingsUI {
} }
updateNetwork () { updateNetwork () {
this.settings.updateNetwork((err, {id, name} = {}) => { this.blockchain.updateNetwork((err, {id, name} = {}) => {
if (err) { if (err) {
this.netUI.innerHTML = 'can\'t detect network ' this.netUI.innerHTML = 'can\'t detect network '
return return
@ -331,7 +331,7 @@ class SettingsUI {
this.accountListCallId++ this.accountListCallId++
var callid = this.accountListCallId var callid = this.accountListCallId
var txOrigin = this.el.querySelector('#txorigin') var txOrigin = this.el.querySelector('#txorigin')
this.settings.getAccounts((err, accounts) => { this.blockchain.getAccounts((err, accounts) => {
if (this.accountListCallId > callid) return if (this.accountListCallId > callid) return
this.accountListCallId++ this.accountListCallId++
if (err) { addTooltip(`Cannot get account list: ${err}`) } if (err) { addTooltip(`Cannot get account list: ${err}`) }

@ -9,7 +9,7 @@ const EventManager = require('../../lib/events')
const Card = require('../ui/card') const Card = require('../ui/card')
const css = require('../tabs/styles/run-tab-styles') const css = require('../tabs/styles/run-tab-styles')
const Settings = require('../tabs/runTab/model/settings.js') // const Settings = require('../tabs/runTab/model/settings.js')
const SettingsUI = require('../tabs/runTab/settings.js') const SettingsUI = require('../tabs/runTab/settings.js')
const Recorder = require('../tabs/runTab/model/recorder.js') const Recorder = require('../tabs/runTab/model/recorder.js')
const RecorderUI = require('../tabs/runTab/recorder.js') const RecorderUI = require('../tabs/runTab/recorder.js')
@ -124,8 +124,8 @@ export class RunTab extends LibraryPlugin {
} }
renderSettings (udapp) { renderSettings (udapp) {
var settings = new Settings(this.executionContext, udapp) // var settings = new Settings(this.blockchain, this.executionContext, udapp)
this.settingsUI = new SettingsUI(settings, this.networkModule) this.settingsUI = new SettingsUI(this.blockchain, this.networkModule)
this.settingsUI.event.register('clearInstance', () => { this.settingsUI.event.register('clearInstance', () => {
this.event.trigger('clearInstance', []) this.event.trigger('clearInstance', [])

Loading…
Cancel
Save