From 32648fa737be75ed7363ffd4df4b8c755456dcae Mon Sep 17 00:00:00 2001 From: Iuri Matias Date: Fri, 31 Jan 2020 16:28:02 -0500 Subject: [PATCH] use remix-simulator accounts and addAccount --- src/blockchain/blockchain.js | 6 ++-- src/blockchain/providers/vm.js | 52 +++++++++++++++++++++------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/blockchain/blockchain.js b/src/blockchain/blockchain.js index dc4e2829c4..a71bb7201e 100644 --- a/src/blockchain/blockchain.js +++ b/src/blockchain/blockchain.js @@ -321,7 +321,8 @@ class Blockchain { resetEnvironment () { this.getCurrentProvider().resetEnvironment() // TODO: most params here can be refactored away in txRunner - this.txRunner = new TxRunner(this.providers.vm.accounts, { + // this.txRunner = new TxRunner(this.providers.vm.accounts, { + this.txRunner = new TxRunner(this.providers.vm.RemixSimulatorProvider.Accounts.accounts, { // TODO: only used to check value of doNotShowTransactionConfirmationAgain property config: this.config, // TODO: to refactor, TxRunner already has access to executionContext @@ -430,7 +431,8 @@ class Blockchain { if (err) return next(err) if (!address) return next('No accounts available') - if (self.executionContext.isVM() && !self.providers.vm.accounts[address]) { + // if (self.executionContext.isVM() && !self.providers.vm.accounts[address]) { + if (self.executionContext.isVM() && !self.providers.vm.RemixSimulatorProvider.Accounts.accounts[address]) { return next('Invalid account selected') } next(null, address, value, gasLimit) diff --git a/src/blockchain/providers/vm.js b/src/blockchain/providers/vm.js index 01ae4f10c8..758f0ed5eb 100644 --- a/src/blockchain/providers/vm.js +++ b/src/blockchain/providers/vm.js @@ -2,20 +2,32 @@ const Web3 = require('web3') const { BN, privateToAddress, toChecksumAddress, isValidPrivate, stripHexPrefix } = require('ethereumjs-util') const crypto = require('crypto') const ethJSUtil = require('ethereumjs-util') +const RemixSimulator = require('remix-simulator') class VMProvider { constructor (executionContext) { this.executionContext = executionContext + this.RemixSimulatorProvider = new RemixSimulator.Provider({executionContext: this.executionContext}) + this.RemixSimulatorProvider.init() + this.web3 = new Web3(this.RemixSimulatorProvider) this.accounts = {} } getAccounts (cb) { - if (!this.accounts) { - cb('No accounts?') - return cb('No accounts?') - } - return cb(null, Object.keys(this.accounts)) + this.web3.eth.getAccounts((err, accounts) => { + console.dir(err) + console.dir(accounts) + if (err) { + return cb('No accounts?') + } + return cb(null, accounts) + }) + // if (!this.accounts) { + // cb('No accounts?') + // return cb('No accounts?') + // } + // return cb(null, Object.keys(this.accounts)) } resetEnvironment () { @@ -29,20 +41,21 @@ class VMProvider { /** Add an account to the list of account (only for Javascript VM) */ _addAccount (privateKey, balance) { - privateKey = Buffer.from(privateKey, 'hex') - const address = privateToAddress(privateKey) - + this.RemixSimulatorProvider.Accounts._addAccount(privateKey, balance) + // privateKey = Buffer.from(privateKey, 'hex') + // const address = privateToAddress(privateKey) +// // FIXME: we don't care about the callback, but we should still make this proper - let stateManager = this.executionContext.vm().stateManager - stateManager.getAccount(address, (error, account) => { - if (error) return console.log(error) - account.balance = balance || '0xf00000000000000001' - stateManager.putAccount(address, account, (error) => { - if (error) console.log(error) - }) - }) - - this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } + // let stateManager = this.executionContext.vm().stateManager + // stateManager.getAccount(address, (error, account) => { + // if (error) return console.log(error) + // account.balance = balance || '0xf00000000000000001' + // stateManager.putAccount(address, account, (error) => { + // if (error) console.log(error) + // }) + // }) +// + // this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } } createVMAccount (newAccount) { @@ -78,7 +91,8 @@ class VMProvider { signMessage (message, account, _passphrase, cb) { const personalMsg = ethJSUtil.hashPersonalMessage(Buffer.from(message)) - const privKey = this.accounts[account].privateKey + // const privKey = this.providers.vm.accounts[account].privateKey + const privKey = this.RemixSimulatorProvider.Accounts.accounts[account].privateKey try { const rsv = ethJSUtil.ecsign(personalMsg, privKey) const signedData = ethJSUtil.toRpcSig(rsv.v, rsv.r, rsv.s)