36

What I have
1. I am able to create new account with personal.newAccount() method of web3 api.
2. I am working on my private blockchain.
3. geth node start with following command:-
geth --datadir ~/privateBlockChain1 --networkid 1300 --rpcport 8002 --port 30301 --rpcapi="db,eth,net,web3,personal,admin,txpool,debug" --rpc --maxpeers 0 --nodiscover --targetgaslimit 110000000000 --dev --rpccorsdomain "*" --rpcaddr "127.0.0.1" console
4. I am using web3 api and ethereumjs-wallet npm api.
5. Keystore file is successfully store in private blockchain directory.

What I want
1. I want to get private key of respective account.
2. actually, i want to get all data of keystore file of respective account from my nodejs side and will be able to save in my regular database.

Case 1:
1. when i create account with personal.newAccount() method with password, how to get private key, if i got private key, then i will be use that key to get all keystore data by following code:---

   var Wallet = require('ethereumjs-wallet');  
    var Key=Buffer.from('account_privatekey','hex');  
    var wallet = Wallet.fromPrivateKey(Key);  
    var json=wallet.toV3String(user_define_password);  // it will return keystore object data. 

but i am unable to get actual private key from account address and password.
` Case 2:
1. suppose i create private key by following code:-

     var Wallet = require('ethereumjs-wallet');  
        var privateKey=Wallet.generate(password)._privKey;  
        var wallet = Wallet.fromPrivateKey(privateKey);  
        var json=wallet.toV3String(password); // it will get keystore data      `
  2. account creation on my private blockchain this way   
           `  personal.importRawKey(key,password)  
          but error showing like , importRawKey not a function `

How to fix these

2
  • Isn't it the case that the whole point of a private key is that you cannot get it from the account address and password? The private key is on your hard drive somewhere, but the whole point of the security of public/private keys is that you cannot get it--even if you spent an near-infinite amount of computing resources--from the public key. Commented Mar 7, 2017 at 15:06
  • you should add --rpcapi "personal"
    – user22021
    Commented Nov 1, 2017 at 11:53

11 Answers 11

24

I used this code to recover the private key from account address and password. firstly, install Keythereum:

npm install keythereum

Keythereum is a JavaScript tool to generate, import and export Ethereum keys. read the documentation: GitHub Link

and this is my code:

var keythereum = require("keythereum");
var datadir = "/home/super/.ethereum/rinkeby";
var address= "0xc8096d713000002c77e4eb62f0000ead5f105a7e";
const password = "mypass";

var keyObject = keythereum.importFromFile(address, datadir);
var privateKey = keythereum.recover(password, keyObject);
console.log(privateKey.toString('hex'));
12

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

How to get private key from account address and password?

One of the founding notions of public/private key cryptography is that what you are describing should be impossible. No cryptographic system in existence would be trusted if you could reverse engineer the private key from the public address.

So not only is it impossible, but it should be impossible.


You state:

when i create account with personal.newAccount() method with password, how to get private key, if i got private key, then i will be use that key to get all keystore data by following code:---

Are you ask for the directory location of your keystore? If so this may answer your question : Getting private keys of geth accounts

Also see : https://ethereum.gitbooks.io/frontier-guide/content/backup_restore.html

Everything geth persists gets written inside its data directory (except for the PoW Ethash DAG, see note below). The default data directory locations are platform specific:

  • Mac: ~/Library/Ethereum
  • Linux: ~/.ethereum
  • Windows: %APPDATA%/Ethereum

Accounts are stored in the keystore subdirectory. The contents of this directories should be transportable between nodes, platforms, implementations (C++, Go, Python).

2
  • 14
    The password is used to decrypt the keystore, and thus, the private key. So not only is it not impossible, but it HAS to be possible otherwise the wallet won't be able to spend funds. The OP has clearly stated that the account was created on the same node where he is entering the password.
    – Jus12
    Commented Jul 16, 2017 at 18:42
  • 3
    this is not the answer to the question, downvote
    – Nulik
    Commented May 10, 2018 at 21:46
5

I wrote this Go program to export the private key in Hex format:

package main

import (
    "fmt"
    "io/ioutil"
    "github.com/ethereum/go-ethereum/accounts/keystore"
    "github.com/ethereum/go-ethereum/crypto"
)

func main() {
    inPath:=".ethereum/keystore/UTC--2018-11-05T16-07-58.071516100Z--21f2bda7bb26e113687ca8827fac1966a68e8b4c"
    outPath:="key.hex"
    password:="my-wallet-password"
    keyjson,e:=ioutil.ReadFile(inPath); if e != nil { panic(e) }
    key,e:=keystore.DecryptKey(keyjson,password); if e != nil { panic(e) }
    e=crypto.SaveECDSA(outPath,key.PrivateKey); if e!=nil { panic(e) }
    fmt.Println("Key saved to:",outPath)
}

I build it like this:

go build showkey.go

I run it like this:

./showkey 

It produces an output file that looks like this (i changed some of the numbers):

$ cat key.hex 
e33b57632115a6015cc7721fdda48357b468b73a17e33e3a250c4de266efb1c0

I am able to import the resulting private key into MetaMask.

2
  • How do i get UTC--2018-11-05T16-07-58.071516100Z--21f2bda7bb26e113687ca8827fac1966a68e8b4c from given address?
    – TomSawyer
    Commented Jun 14, 2019 at 8:32
  • The UTC--... thing is the filename of the ethereum wallet.
    – likebike
    Commented Sep 6, 2019 at 12:19
3

You cannot get the private key from the address and password. But you can get the private key from the keyfile and the password, which should work in your case, as I understand the question. For more information on how to get the private key from the keyfile see here.

1

The answer to your question is here:

https://github.com/ethereum/go-ethereum/blob/master/accounts/keystore/keystore_passphrase_test.go

Change the filename , password add a line to print the private key to the console and run

go build ./keystore_passphrase_test.go
1

But because I needed to do this for a long time and I did not get any results, I wrote this example I have provided a sample code and repository

package main

import (
    "fmt"
    "github.com/ethereum/go-ethereum/accounts/keystore"
    "github.com/ethereum/go-ethereum/common/hexutil"
    "github.com/ethereum/go-ethereum/crypto"
    "io/ioutil"
)

func main() {
    keys, err := ioutil.ReadFile("test.json")
    if err != nil {
        panic(err)
    }
    password := "****** Personal wallet password here ******"
    key, err := keystore.DecryptKey(keys, password)

    privateKeyBytes := crypto.FromECDSA(key.PrivateKey)

    if err != nil {
        panic(err)
    }
    fmt.Printf("\naddress :\t %x", key.Address)
    fmt.Printf("\n*** Key : \t %x", hexutil.Encode(privateKeyBytes)[2:])
}

https://github.com/irmaster/EthereumKEY

0

You can try to brute-force the private key and check if it matches the public key in mind. https://en.wikipedia.org/wiki/Brute-force_attack. It will take you some time because it's designed to be mathematically impossible. Have a look at https://en.wikipedia.org/wiki/Public-key_cryptography

1
  • It's designed to be infeasible (based on our current understanding of mathematics and physics), not mathematically impossible. I guess this comment also applies to the other answer provided.
    – lungj
    Commented Oct 24, 2017 at 16:41
0

I found this because I'm trying to export a private key - which I can fully use to send transfers inside geth - so that I can load it into an android app. For that, I noticed that you can download MyEtherWallet from GitHub and run it locally on your machine. You can then safely (as it's all local to your PC) import the geth keystore JSON into that offline MEW and use MEW to decrypt the wallet and show you the private key.

https://github.com/kvhnuke/etherwallet/releases/tag/v3.21.11

0

If you prefer a commandline tool written in golang:

ethereal account keys --address=ADDRESS --passphrase=PASS

https://github.com/wealdtech/ethereal

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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