[Ethereum] How to decrypt private key form keystore file, without using third party applications?

private-key

I would like to write a piece of code that takes input as UTC key store file and password, and returns decrypted Private Key. I am aware of options like "MyEtherWallet" but I would like to do using my code. Any help/resources that I can use is appreciated.

Best Answer

You can use the Javascript library ethereumjs-wallet which allows to load a wallet from a private key, seed phrase or UTC JSON file (V1 or V3)

I have generated a UTC JSON file on MyEtherWallet and the sample code below shows how to decrypt it to sign transactions:

const Wallet = require('ethereumjs-wallet'),
      fs = require('fs');


const utcFile = "./UTC--2018-04-29T10-08-25.072Z--1f7c98090febf46155496a370002a10af7eb6766"
const password = "password123"


const myWallet = Wallet.fromV3(fs.readFileSync(utcFile).toString(), password, true);

console.log("Private Key: " + myWallet.getPrivateKey().toString('hex')) 
console.log("Address: " + myWallet.getAddress().toString('hex')) 

Output

$ node index.js 
Private Key: 8ecb2c8c4fcf3986e5d3c249bac183b206f41a7a2d9a81e203ef6219be87421b
Address: 1f7c98090febf46155496a370002a10af7eb6766

I pushed some of the code here

Related Topic