9

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

0

5 Answers 5

4

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'));
1
  • In case of Node.js, keythereum-node can be used as a faster alternative to keythereum.
    – rustyx
    May 15, 2018 at 14:48
4

2021-12

FYI for Googlers, you can inspect the UTC keystore file with the ethkey cli tool that comes with geth with the --private flag to show the private key

ethkey inspect --private test_chain/keystore/UTC--<file name

output

Address:       0x...
Public key:    ...
Private key:   ...
1

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)
1

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

web3 account extract --keyfile ~/Downloads/keystore-file --password password
1
  • web3 CLI installation: curl -LSs https://raw.githubusercontent.com/gochain/web3/master/install.sh | sh
    – JuliSmz
    Jun 16, 2021 at 0:05
0

Assuming you have been activated personal rpc of your geth, to do this programatically without hardcoding the keystore file directory path in python, do the following:

from web3 import Web3
import eth_keys
from eth_account import account

w3 = Web3(Web3.HTTPProvider('http://127.0.0.1'))
address = '0x...'
password = 'password'

wallets_list = w3.geth.personal.list_wallets()
keyfile_path = (wallets_list[list(i['accounts'][0]['address'] for i in wallets_list).index(address)]['url']).replace("keystore://", "").replace("\\", "/")
keyfile = open(keyfile_path)
keyfile_contents = keyfile.read()
keyfile.close()
private_key = eth_keys.keys.PrivateKey(account.Account.decrypt(keyfile_contents, password))
public_key = private_key.public_key

private_key_str = str(private_key)
public_key_str = str(public_key)
4
  • Hi Rolij! Copying and pasting the same answer to multiple questions isn't very helpful if the questions are similar you could mark them as duplicated or you could add a link to your answer as a comment.
    – Ismael
    Jan 27 at 14:08
  • Thank you @Ismael for the heads up. I did see the same question almost everywhere and did not see anyone marking them as duplicate(despite the huge number of views), so thought that they are ok. And also I saw all answers with hardcode of the keystore file and thought that my answer might be more useful, so decided to post it everywhere. despite all of that, I will check if I have the privilege to mark them as duplicate and will delete my answers. Thank you again for the guidance. Jan 27 at 16:08
  • All is good, you don't have to do anything, in the future just don't do that. The system is just trying to detect bots, I'm making sure you are a human, so everything is fine.
    – Ismael
    Jan 27 at 19:52
  • Thank you @Ismael for the guidence. I will remember that. Feb 4 at 20:00

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