2

I create accounts by geth account new with a password, then I get a UTC file which is a json file saved in folder keystore under data directory. Now I am attempt to get private key from this json file.

The reason I want to do this is because I am using metamask un ubuntu and there is an issue that does not allow me to import json file directly into metamask under ubuntu.

Anyone knows how to get private key through UTC json file with password?

Thank you

3

https://www.myetherwallet.com is a popular option. But I'd recommend downloading the offline version of the site for increased security.

If you're up for writing a little code, keythereum is a good option.

Something like this:

var keyObject = keythereum.importFromFile(address, datadir);
var privateKey = keythereum.recover(password, keyObject);
console.log(privateKey.toString('hex'));
  • In case of Node.js, keythereum-node can be used as a faster alternative to keythereum. – rustyx May 15 '18 at 14:48
0

Private key is often required to import your account in a different wallet. Even Metamask needs a private key to import the account. This is how I was able to extract the private key from the node where I created my account. Ethereum keys in a Linux node will be encrypted and stored in the following location.

~/.ethereum/keystore/ (mainnet)
~/.ethereum/rinkeby/keystore/ (rinkeby testnet)

If you have the public address of the account and the password used to lock the account, you should be able to extract the private key. I used web3 python package to extract the private key. Install this using pip.

pip install web3

Execute the following code

>>> from web3.auto import w3
>>> with open("~/.ethereum/rinkeby/keystore/UTC--2018-06-
    10T05-43-22.134895238Z--9e63c0d223d9232a4f3076947ad7cff353cc1a28") 
     as keyfile:
...     encrypted_key = keyfile.read()
...     private_key = w3.eth.account.decrypt(encrypted_key, 
                                             'password')

UTC--2018-06-10T05-43-22.134895238Z--9e63c0d223d9232a4f3076947ad7cff353cc1a28 is the file containing stored key. This will return a private key in byte format.

You can get the private key in hex format as follows.

import binascii
binascii.b2a_hex(private_key)
0

You can use the web3 CLI tool to do this easily:

web3 account extract --keyfile ~/Downloads/keystore-file --password password

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.