web3.eth.getAccounts() returns list of accounts.

Is there a way around to get the private key of one of the account from the list returned from the above code?

  • If there was a way, then it would make the entire Ethereum cryptography useless. Aug 6, 2020 at 5:54

2 Answers 2


You cannot get the private key from web3.eth.getAccounts() directly, but there's a few workarounds to do this with ganache:

  1. Specify a mnemonic phrase with the -m option, e.g. ganache-cli -m "stereo consider quality wild fat farm symptom bundle laundry side one lemon", this will derive private keys from the mnemonic phrase (with the derivation path m/44'/60'/0'/0/n.

  2. Use the --account_keys_path option to save all private keys to a file, e.g. ganache-cli --account_keys_path keys.json. This will result in a JSON file with all addresses, private keys and public keys.

  3. Use the --account option to manually specify a private key and balance, e.g. ganache-cli --account "0x31c354f57fc542eba2c56699286723e94f7bd02a4891a0a7f68566c2a2df6795,1000000000000000000". This will assign 1 ETH (= 1000000000000000000 Wei) to the address corresponding to this private key. You can use the --account option multiple times, with different private keys.

  • How could you do the same things on js file? const ganache = require("ganache-core"); const mnemonicPhrase = 'dfafas' then
    – Anonymous
    Nov 18, 2021 at 23:41

According to the Ganache CLI documentation, during the setup of your ganache instance you can specify a path to save the accounts and private keys to.

 ganache.server({ account_keys_path: "src/accounts.json" });
  return await new Promise<Web3>((resolve) => {
    server.listen(8545, () => {
      console.log("Ganache running at http://localhost:8545");

The account_keys_path argument will specify the relative path of the file you'd like the accounts and private keys to be written. This can be done when using ganache.provider aswell.

The documentation also mentions that you can pass an 'accounts' object itself, where you specify the accounts and private keys manually, but I have not tested this approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.