Merge pull request #1160 from ethereum/improve_remix_sim
add more methods to remix-sim; fix accountspull/7/head
commit
c7d84a0c41
@ -1,2 +1,84 @@ |
||||
# `remix-simulator` |
||||
|
||||
Implemented: |
||||
|
||||
* [X] web3_clientVersion |
||||
* [X] web3_sha3 |
||||
* [X] net_version |
||||
* [X] net_listening |
||||
* [X] net_peerCount |
||||
* [X] eth_protocolVersion |
||||
* [X] eth_syncing |
||||
* [X] eth_coinbase |
||||
* [X] eth_mining |
||||
* [X] eth_hashrate |
||||
* [~] eth_gasPrice |
||||
* [~] eth_accounts |
||||
* [X] eth_blockNumber |
||||
* [X] eth_getBalance |
||||
* [_] eth_getStorageAt |
||||
* [~] eth_getTransactionCount |
||||
* [_] eth_getBlockTransactionCountByHash |
||||
* [_] eth_getBlockTransactionCountByNumber |
||||
* [_] eth_getUncleCountByBlockHash |
||||
* [_] eth_getUncleCountByBlockNumber |
||||
* [X] eth_getCode |
||||
* [~] eth_sign |
||||
* [X] eth_sendTransaction |
||||
* [_] eth_sendRawTransaction |
||||
* [X] eth_call |
||||
* [~] eth_estimateGas |
||||
* [~] eth_getBlockByHash |
||||
* [~] eth_getBlockByNumber |
||||
* [~] eth_getTransactionByHash |
||||
* [_] eth_getTransactionByBlockHashAndIndex |
||||
* [_] eth_getTransactionByBlockNumberAndIndex |
||||
* [~] eth_getTransactionReceipt |
||||
* [_] eth_getUncleByBlockHashAndIndex |
||||
* [_] eth_getUncleByBlockNumberAndIndex |
||||
* [X] eth_getCompilers (DEPRECATED) |
||||
* [_] eth_compileSolidity (DEPRECATED) |
||||
* [_] eth_compileLLL (DEPRECATED) |
||||
* [_] eth_compileSerpent (DEPRECATED) |
||||
* [_] eth_newFilter |
||||
* [_] eth_newBlockFilter |
||||
* [_] eth_newPendingTransactionFilter |
||||
* [_] eth_uninstallFilter |
||||
* [_] eth_getFilterChanges |
||||
* [_] eth_getFilterLogs |
||||
* [_] eth_getLogs |
||||
* [_] eth_getWork |
||||
* [_] eth_submitWork |
||||
* [_] eth_submitHashrate |
||||
* [_] eth_getProof |
||||
* [_] db_putString |
||||
* [_] db_getString |
||||
* [_] db_putHex |
||||
* [_] db_getHex |
||||
* [X] shh_version |
||||
* [_] shh_post |
||||
* [_] shh_newIdentity |
||||
* [_] shh_hasIdentity |
||||
* [_] shh_newGroup |
||||
* [_] shh_addToGroup |
||||
* [_] shh_newFilter |
||||
* [_] shh_uninstallFilter |
||||
* [_] shh_getFilterChanges |
||||
* [_] shh_getMessages |
||||
* [_] bzz_hive (stub) |
||||
* [_] bzz_info (stub) |
||||
* [_] debug_traceTransaction |
||||
* [_] eth_subscribe |
||||
* [_] eth_unsubscribe |
||||
* [_] miner_start |
||||
* [_] miner_stop |
||||
* [_] personal_listAccounts |
||||
* [_] personal_lockAccount |
||||
* [_] personal_newAccount |
||||
* [_] personal_importRawKey |
||||
* [_] personal_unlockAccount |
||||
* [_] personal_sendTransaction |
||||
* [_] rpc_modules |
||||
* [_] web3_clientVersion |
||||
* [_] web3_sha3 |
||||
|
||||
|
@ -1,27 +1,66 @@ |
||||
var RemixLib = require('remix-lib') |
||||
var executionContext = RemixLib.execution.executionContext |
||||
var ethJSUtil = require('ethereumjs-util') |
||||
var BN = ethJSUtil.BN |
||||
var Web3 = require('web3') |
||||
|
||||
var Accounts = function () { |
||||
this.web3 = new Web3() |
||||
// TODO: make it random and/or use remix-libs
|
||||
this.accounts = [this.web3.eth.accounts.create(['abcd']), this.web3.eth.accounts.create(['ef12']), this.web3.eth.accounts.create(['ef34'])] |
||||
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.accounts[this.accounts[0].address.toLowerCase()] = this.accounts[0] |
||||
this.accounts[this.accounts[1].address.toLowerCase()] = this.accounts[1] |
||||
this.accounts[this.accounts[2].address.toLowerCase()] = this.accounts[2] |
||||
executionContext.init({get: () => { return true }}) |
||||
|
||||
this.accounts[this.accounts[0].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[0].address.toLowerCase()].privateKey.slice(2), 'hex') |
||||
this.accounts[this.accounts[1].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[1].address.toLowerCase()].privateKey.slice(2), 'hex') |
||||
this.accounts[this.accounts[2].address.toLowerCase()].privateKey = Buffer.from(this.accounts[this.accounts[2].address.toLowerCase()].privateKey.slice(2), 'hex') |
||||
for (let _account of this.accountsList) { |
||||
this.accountsKeys[_account.address.toLowerCase()] = _account.privateKey |
||||
this.accounts[_account.address.toLowerCase()] = { privateKey: Buffer.from(_account.privateKey.replace('0x', ''), 'hex'), nonce: 0 } |
||||
|
||||
executionContext.vm().stateManager.getAccount(Buffer.from(_account.address.toLowerCase().replace('0x', ''), 'hex'), (err, account) => { |
||||
if (err) { |
||||
throw new Error(err) |
||||
} |
||||
var balance = '0x56BC75E2D63100000' |
||||
account.balance = balance || '0xf00000000000000001' |
||||
}) |
||||
} |
||||
} |
||||
|
||||
Accounts.prototype.methods = function () { |
||||
return { |
||||
eth_accounts: this.eth_accounts.bind(this) |
||||
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.accounts.map((x) => x.address)) |
||||
return cb(null, this.accountsList.map((x) => x.address)) |
||||
} |
||||
|
||||
Accounts.prototype.eth_getBalance = function (payload, cb) { |
||||
let address = payload.params[0] |
||||
address = ethJSUtil.stripHexPrefix(address) |
||||
|
||||
executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, account) => { |
||||
if (err) { |
||||
return cb(err) |
||||
} |
||||
cb(null, new BN(account.balance).toString(10)) |
||||
}) |
||||
} |
||||
|
||||
Accounts.prototype.eth_sign = function (payload, cb) { |
||||
let address = payload.params[0] |
||||
let message = payload.params[1] |
||||
|
||||
let privateKey = this.accountsKeys[address] |
||||
let account = Web3.eth.accounts.privateKeyToAccount(privateKey) |
||||
|
||||
let data = account.sign(message) |
||||
|
||||
cb(null, data.signature) |
||||
} |
||||
|
||||
module.exports = Accounts |
||||
|
Loading…
Reference in new issue