I am running ganache-cli through a node application:
const ganache = require('ganache-core');
const ethers = require('ethers');
const provider = new ethers.providers.Web3Provider(
ganache.provider({
total_accounts: 5,
account_keys_path: './accounts.json',
gasPrice: 20000000000,
gasLimit: 20000000000,
default_balance_ether: 100
})
);
This runs the ganache-cli and output acocunt details in accounts.json
. The file looks like this:
{
"addresses":{
"0x73f5b3f74db1b37927696c280c04d544f4e9ff64":{
"secretKey":{
"type":"Buffer",
"data":[88, 17, .....]
},
"publicKey":{
"type":"Buffer",
"data":[13, 52, .....]
},
"address":"0x73f5b3f74db1b37927696c280c04d544f4e9ff64",
"account":{
"nonce":"0x",
"balance":"0x056bc75e2d63100000",
"stateRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"codeHash":"0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
}
}
}
}
I can see the account address, but how can I decode/get the private key from this information?
You cannot get the private key from accounts directly, but there's a few workarounds to do this with ganache-cli.
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.
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.
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.