0

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. – goodvibration Aug 6 at 5:54
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.

| improve this answer | |

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.