2

I made several accounts with some commands personal.newAccount() and the accounts created were push to the list. What I want to do is, get private key with public key that I've got with getAccount() function because of security reason.

I don't want to show my owner address and public key so I want to get address from list using the function.

web3.eth.getAccounts(); 
//["0x407d73d8a49eeb85d32cf465507dd71d507100c1"] 

Then, I want to get the private key with that address like this way below.

var publicKey = web3.eth.getAccounts();
var privateKey = extractPrivateKey(pulicKey); 

Is there any way to do like this using web3? Is there some way to access the keystore on javascript file? I have to use sendTransaction() function in javascript code but it needs the privateKey to sign. Now I'm storing the privateKey as static and I think is quite dangerous. I'm figuring out to hide my owner public and private key in the code.

TylerH
  • 20,799
  • 66
  • 75
  • 101
rachel_hong
  • 471
  • 2
  • 6
  • 15

1 Answers1

0

First of all this line of code var publicKey = web3.eth.getAccounts(); doesn't return you the public key, it returns you an array of addresses.

You could use the npm package keythereum. It extracts the private Key from your Keystore-File by passing the specific address. First you use the importFromFile function to import it and then use the recover function to extract the private key.

It would look like this:

var keystorePath = "/yourPathToTheKeystoreFile";

async function importPrivateKey(address) {
let methodName = ">>> [importPrivateKey]: ";
  try{

      var keyObject = await keythereum.importFromFile(address, keystorePath);

      var privateKey = await keythereum.recover(password, keyObject);

      return privateKey;


    }catch(e) {
      console.log(e);
      throw e;
    }
}

Now you can just call this function directly in any other function and use the extracted private key directly as a new parameter for the sendTransactionfunction without storing the private Key in any variable.

MiDa
  • 225
  • 3
  • 14