Skip to content
This repository has been archived by the owner on Mar 3, 2021. It is now read-only.

add resetAccounts, new Account, fix getAccounts on remix-simulator #1423

Merged
merged 7 commits into from
Feb 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions remix-simulator/src/methods/accounts.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ethJSUtil = require('ethereumjs-util')
const BN = ethJSUtil.BN
const { BN, privateToAddress, isValidPrivate } = require('ethereumjs-util')
const Web3 = require('web3')
const crypto = require('crypto')

const Accounts = function (executionContext) {
this.web3 = new Web3()
Expand Down Expand Up @@ -35,6 +36,44 @@ Accounts.prototype.init = async function () {
}
}

Accounts.prototype.resetAccounts = function () {
// TODO: setting this to {} breaks the app currently, unclear why still
// this.accounts = {}
// this.accountsKeys = {}
this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000')
this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000')
this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000')
this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000')
this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000')
}

Accounts.prototype._addAccount = function (privateKey, balance) {
privateKey = Buffer.from(privateKey, 'hex')
const address = ethJSUtil.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[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
this.accountsKeys[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = '0x' + privateKey.toString('hex')
}

Accounts.prototype.newAccount = function (cb) {
let privateKey
do {
privateKey = crypto.randomBytes(32)
} while (!isValidPrivate(privateKey))
this._addAccount(privateKey, '0x56BC75E2D63100000')
return cb(null, '0x' + privateToAddress(privateKey).toString('hex'))
}

Accounts.prototype.methods = function () {
return {
eth_accounts: this.eth_accounts.bind(this),
Expand All @@ -43,8 +82,8 @@ Accounts.prototype.methods = function () {
}
}

Accounts.prototype.eth_accounts = function (payload, cb) {
return cb(null, this.accountsList.map((x) => ethJSUtil.toChecksumAddress(x.address)))
Accounts.prototype.eth_accounts = function (_payload, cb) {
return cb(null, Object.keys(this.accounts))
}

Accounts.prototype.eth_getBalance = function (payload, cb) {
Expand Down