<commit_before>const ethJSUtil = require('ethereumjs-util')
const BN = ethJSUtil.BN
const Web3 = require('web3')

const Accounts = function (executionContext) {
  this.web3 = new Web3()
  this.executionContext = executionContext
  // TODO: make it random and/or use remix-libs
  this.accountsList = [this.web3.eth.accounts.create(['abcd']), this.web3.eth.accounts.create(['ef12']), this.web3.eth.accounts.create(['ef34'])]
  this.accounts = {}
  this.accountsKeys = {}

  this.executionContext.init({get: () => { return true }})
}

Accounts.prototype.init = async function () {
  let setBalance = (account) => {
    return new Promise((resolve, reject) => {
      this.accountsKeys[ethJSUtil.toChecksumAddress(account.address)] = account.privateKey
      this.accounts[ethJSUtil.toChecksumAddress(account.address)] = { privateKey: Buffer.from(account.privateKey.replace('0x', ''), 'hex'), nonce: 0 }

      this.executionContext.vm().stateManager.getAccount(Buffer.from(account.address.replace('0x', ''), 'hex'), (err, account) => {
        if (err) {
          throw new Error(err)
        }
        const balance = '0x56BC75E2D63100000'
        account.balance = balance || '0xf00000000000000001'
        resolve()
      })
    })
  }

  for (let _account of this.accountsList) {
    await setBalance(_account)
  }
}

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('<KEY>', '0x56BC75E2D63100000')
  this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000')
  this._addAccount('<KEY>', '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 }
}

Accounts.prototype.newAccount = (_passwordPromptCb, 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),
    eth_getBalance: this.eth_getBalance.bind(this),
    eth_sign: this.eth_sign.bind(this)
  }
}

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

Accounts.prototype.eth_getBalance = function (payload, cb) {
  let address = payload.params[0]
  address = ethJSUtil.stripHexPrefix(address)

  this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, account) => {
    if (err) {
      return cb(err)
    }
    cb(null, new BN(account.balance).toString(10))
  })
}
<commit_msg>fix account list; fix newAccount function<commit_after>const ethJSUtil = require('ethereumjs-util')
const { BN, privateToAddress, isValidPrivate } = require('ethereumjs-util')
const Web3 = require('web3')
const crypto = require('crypto')

const Accounts = function (executionContext) {
  this.web3 = new Web3()
  this.executionContext = executionContext
  // TODO: make it random and/or use remix-libs
  this.accountsList = [this.web3.eth.accounts.create(['abcd']), this.web3.eth.accounts.create(['ef12']), this.web3.eth.accounts.create(['ef34'])]
  this.accounts = {}
  this.accountsKeys = {}

  this.executionContext.init({get: () => { return true }})
}

Accounts.prototype.init = async function () {
  let setBalance = (account) => {
    return new Promise((resolve, reject) => {
      this.accountsKeys[ethJSUtil.toChecksumAddress(account.address)] = account.privateKey
      this.accounts[ethJSUtil.toChecksumAddress(account.address)] = { privateKey: Buffer.from(account.privateKey.replace('0x', ''), 'hex'), nonce: 0 }

      this.executionContext.vm().stateManager.getAccount(Buffer.from(account.address.replace('0x', ''), 'hex'), (err, account) => {
        if (err) {
          throw new Error(err)
        }
        const balance = '0x56BC75E2D63100000'
        account.balance = balance || '0xf00000000000000001'
        resolve()
      })
    })
  }

  for (let _account of this.accountsList) {
    await setBalance(_account)
  }
}

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('<KEY>', '0x56BC75E2D63100000')
  this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000')
  this._addAccount('<KEY>', '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.accounts[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
  // this.accountsList[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
}

Accounts.prototype.newAccount = function (cb) {
  console.dir("newAccount")
  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),
    eth_getBalance: this.eth_getBalance.bind(this),
    eth_sign: this.eth_sign.bind(this)
  }
}

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

Accounts.prototype.eth_getBalance = function (payload, cb) {
  let address = payload.params[0]
  address = ethJSUtil.stripHexPrefix(address)

  this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, account) => {
    if (err) {
      return cb(err)
    }
    cb(null, new BN(account.balance).toString(10))
  })
}

{'fraction_non_alphanumeric': 0.1403623949579832, 'fraction_numerical': 0.07352941176470588, 'mean_word_length': 4.650593471810089, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2964480', 'n_tokens_mistral': 2930, 'n_tokens_neox': 2634, 'n_words': 426}