Method To Generate same Keystore content of geth from nodejs

Posted By : Md Imroz Alam | 10-Apr-2017

In geth, If any account create then a keystore file generate, we can generate same keystore  form nodejs side using ethereumjs-wallet npm.

Step 1:
first we have to generate random private key of an  account


var accountPassword="123456";
var key=Wallet.generate(accountPassword);
var privateKey=key._privKey;
var wallet=Wallet.fromPrivateKey(privateKey);
var keyStoreData=wallet.toV3String(accountPassword);

above line of code, generate keystore content as when any account create by geth and create respective keystore for an account

Step 2:

Using the above privateKey , we create account on geth instance , that will generate same account address


privateKey=privateKey.toString('hex');
var sendData = {
    "jsonrpc": "2.0",
    "method": "personal_importRawKey",
    "params": [privateKey, accountPassword],
    "id": new Date().getTime()
};
var bodyJson = JSON.stringify(sendData);
var options = {
      url: "http://localhost:8002", // Our local geth instance url
      method: "POST",
      headers: {
            'Content-type': 'application/json',
      },
      body: bodyJson
};

var request = require('request');


request(options, function(error, response, body) {
  var body = JSON.parse(body);
  if (body.error) {
    console.log("error: ",body.error);
  } else {
    console.log("accountAddres: ", body.result);
    keyStoreFilename(body.result);
  }
})

Step 3:
Save file name of keyStore

function keyStoreFilename(accountAddress){

  var curTime = new Date();

  var utcTime = curTime.toISOString();

  var dateStr = utcTime.split("T");

  var date = dateStr[0];

  var hour = curTime.getUTCHours();

  var minute = curTime.getUTCMinutes();

  var second = curTime.getUTCSeconds();

  var millisecond = curTime.getUTCMilliseconds();

  var fileName = "UTC--" + date + "T" + hour + "-" + minute + "-" + second + "." + millisecond + "Z" + "--" + accountAddress.substring(2);

}

I hope this will be helpful
Thanks

About Author

Author Image
Md Imroz Alam

Md. Imroz Alam is a bright Web App Developer, he has good knowledge of Java, J2SE, Jsp, Servlet, jdbc. His hobbies are watching movie, playing carom.

Request for Proposal

Name is required

Comment is required

Sending message..