[Ethereum] How to get private key from account address and password


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 "" 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

Best Answer

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);
Related Topic