0

I've lunched Geth for the first time and created some accounts on it. While creating accounts, I entered a password for each, and a key file is generated which contains encrypted private key belonging to each address. On the other hand, sending transactions requires signing data with the private key.
My question is how can I retrieve private key of each account in Geth while it is encrypted?

1

You can npm install keythereum, and then run the following NodeJS script:

const fs = require("fs");
const keythereum = require("keythereum");

const KEYSTORE = process.argv[2];
const PASSWORD = process.argv[3];

for (const fileName of fs.readdirSync(KEYSTORE)) {
    const keyObject  = JSON.parse(fs.readFileSync(KEYSTORE + "/" + fileName, "utf8"));
    const publicAddr = keyObject.address;
    const privateKey = keythereum.recover(PASSWORD, keyObject).toString("hex");
    console.log(`0x${publicAddr}: 0x${privateKey}`);
}

You should pass the following input arguments to this script:

  1. The path to your keystore folder
  2. Your password

You can also hard-code them in the script, but that would be less secured if you want to keep this file on your machine (let alone under version control) for future usage and maintenance.

| improve this answer | |
  • Thank you @goodvibration. As my project is using PHP, I hope I can change your code based on it. But the code you provided can show me the way... – Mohammad Saberi Mar 29 at 6:03
  • @MohammadSaberi: You're welcome. – goodvibration Mar 29 at 6:06

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.