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?
Add a comment
|
1 Answer
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:
- The path to your
keystore
folder - 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.
-
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... Commented Mar 29, 2020 at 6:03