trufflesuite / ganache-cli-archive

Fast Ethereum RPC client for testing and development. See https://github.com/trufflesuite/ganache for current development.
https://www.trufflesuite.com/ganache
MIT License
3.36k stars 696 forks source link

wrong return value for web3.eth.getAccounts #54

Closed JMLX42 closed 8 years ago

JMLX42 commented 8 years ago

Here is my code:

var web3 = new Web3();
web3.setProvider(TestRPC.provider());

web3.eth.getAccounts((err, acc) => {
    console.log(acc);
});

Here is what I get:

[ '0x871f46e97b6419ce887073a9ccb99b964d438bdd',
  '0x728ef897367608fd7ce8b9dbc66eb840d9cbc63c',
  '0x4f7aef45c0a6c311e15a16a766ac95673978886f',
  '0x92dce6468f51c35266c70b176c6771ac648eb305',
  '0xaf4381f1861c78d399dcdc3c0f36d6fff22086af',
  '0x11d9e50c259ffe93e9fd6f95967599387c4c1ee1',
  '0x55ef102990a7c9fe26d9756da1de372e91e3aaf8',
  '0xe06e79a7c975d1d94ad32ada38855ca701d9dd8a',
  '0x99a17f3c5e4ce7a684d1aa4a805b720ccde276b9',
  '0x513fd961555c0687c81a8465a9ea0a855a408457' ]

Here is what I have in the testrpc logs:

EthereumJS TestRPC v2.0.0

Available Accounts
==================
(0) 0x4e4134b36b3e7f8fca80a2fbfe6d54549e5f0f93

Private Keys
==================
(0) 81aa38abb2b5d5aa3bbc0c852423cca1118dc90ef1a83c18fc8010fb276ec378

But if I use the classic HTTP Provider, the account list is correct:

var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("http://127.0.0.1:8545"));

web3.eth.getAccounts((err, acc) => {
    console.log(acc);
});
[ '0x4e4134b36b3e7f8fca80a2fbfe6d54549e5f0f93' ]

But then nothing gets mined... Any idea what's wrong?

Thanks,

tcoulter commented 8 years ago

You're not passing the same options to TestRPC.provider() as you are to the command line. Check out the readme. On Apr 3, 2016 11:32 AM, "Jean-Marc Le Roux" notifications@github.com wrote:

Here is my code:

var web3 = new Web3();web3.setProvider(TestRPC.provider()); web3.eth.getAccounts((err, acc) => { console.log(acc); });

Here is what I get:

[ '0x871f46e97b6419ce887073a9ccb99b964d438bdd', '0x728ef897367608fd7ce8b9dbc66eb840d9cbc63c', '0x4f7aef45c0a6c311e15a16a766ac95673978886f', '0x92dce6468f51c35266c70b176c6771ac648eb305', '0xaf4381f1861c78d399dcdc3c0f36d6fff22086af', '0x11d9e50c259ffe93e9fd6f95967599387c4c1ee1', '0x55ef102990a7c9fe26d9756da1de372e91e3aaf8', '0xe06e79a7c975d1d94ad32ada38855ca701d9dd8a', '0x99a17f3c5e4ce7a684d1aa4a805b720ccde276b9', '0x513fd961555c0687c81a8465a9ea0a855a408457' ]

Here is what I have in the testrpc logs:

EthereumJS TestRPC v2.0.0

Available Accounts

(0) 0x4e4134b36b3e7f8fca80a2fbfe6d54549e5f0f93

Private Keys

(0) 81aa38abb2b5d5aa3bbc0c852423cca1118dc90ef1a83c18fc8010fb276ec378

But if I use the classic HTTP Provider, the account list is correct:

var web3 = new Web3();web3.setProvider(new web3.providers.HttpProvider("http://127.0.0.1:8545")); web3.eth.getAccounts((err, acc) => { console.log(acc); });

[ '0x4e4134b36b3e7f8fca80a2fbfe6d54549e5f0f93' ]

Any idea what's wrong?

Thanks,

— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/ethereumjs/testrpc/issues/54

JMLX42 commented 8 years ago

I don't understand. You mean that I shoud either use the TestRPC.provider OR run the testrpc binary, but not both ? I thought TestRPC.provider was a specific provider to connect to the testrpc binary server.

tcoulter commented 8 years ago

Yes. When you use TestRPC.provider(), you're using the TestRPC as a library and creating a whole new instance of the TestRPC in memory.

So the correct usage is to use the HttpProvider + the TestRPC server together, or only use the TestRPC as a provider on its own. On Apr 3, 2016 11:36 AM, "Jean-Marc Le Roux" notifications@github.com wrote:

I don't understand. You mean that I shoud either use the TestRPC.provider OR run the testrpc binary, but not both ?

— You are receiving this because you commented. Reply to this email directly or view it on GitHub https://github.com/ethereumjs/testrpc/issues/54#issuecomment-205026391

danielyaa5 commented 7 years ago

Im also having trouble understanding. I don't understand the difference between new web3.providers.HttpProvider("http://127.0.0.1:8545") and passing TestRPC.provider()

10247bit commented 6 years ago

I think i am facing similar problem, I have used a infura address instead of localhost"const web3 = new Web3(new Web3.providers.HttpProvider("https://kovan.infura.io/TOKEN_ID")); and when i am executing the web3.eth.getAccounts((err, acc) => { console.log(acc); }); it is returning an empty array, but my metamask window have 2 accounts

benjamincburns commented 6 years ago

@IntenseRave to understand what's going on there, see the JSONRPC wiki page for eth_accounts.

When you run web3.eth.getAccounts with an HTTP provider pointed at infura, you're sending the eth_accounts RPC request over to Infura. Infura is then responding with the (empty) list of accounts it owns. "Owns" in this context means "holds the private key." Since Infura is a public client, it doesn't hold anybody's private keys, meaning it can't sign any transactions for you.

Instead, your choices are to write your Dapp so that it holds the private key, signs transactions, and sends them via eth_sendRawTransaction (don't do this), or to rely on wallet providers like MetaMask to hold your keys and sign transactions for you/your users (do this). For more info on that, see this FAQ.

benjamincburns commented 6 years ago

Also just a tip for the future - you're far more likely to get a quick/informative response to questions like this on our gitter channel than you are here. There are ≥3000 people there to answer things. Here there's just me, and sometimes I can take over a week to respond.

10247bit commented 6 years ago

@benjamincburns thank you for clearing the confusion.